lib: graph: add "self" and some "private" APIs
[babeltrace.git] / lib / graph / notification / event.c
CommitLineData
d51202da 1/*
d51202da
JG
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
546a0c4a
PP
25#define BT_LOG_TAG "NOTIF-EVENT"
26#include <babeltrace/lib-logging-internal.h>
27
3d9990ac 28#include <babeltrace/compiler-internal.h>
c5a24b0a 29#include <babeltrace/object-internal.h>
108b91d0
PP
30#include <babeltrace/trace-ir/event.h>
31#include <babeltrace/trace-ir/event-internal.h>
32#include <babeltrace/trace-ir/event-class-internal.h>
33#include <babeltrace/trace-ir/stream-class-internal.h>
34#include <babeltrace/trace-ir/trace.h>
f7c3ac09 35#include <babeltrace/graph/graph-internal.h>
9e550e5f 36#include <babeltrace/graph/private-notification-event.h>
b2e0c907 37#include <babeltrace/graph/notification-event-internal.h>
c55a9f58 38#include <babeltrace/types.h>
8b45963b
PP
39#include <babeltrace/assert-internal.h>
40#include <babeltrace/assert-pre-internal.h>
9e550e5f 41#include <babeltrace/object.h>
a68c0f97 42#include <stdbool.h>
546a0c4a 43#include <inttypes.h>
d51202da 44
8b45963b 45BT_ASSERT_PRE_FUNC
a6918753 46static inline bool event_class_has_trace(struct bt_event_class *event_class)
a68c0f97 47{
839d52a5 48 struct bt_stream_class *stream_class;
a68c0f97 49
839d52a5 50 stream_class = bt_event_class_borrow_stream_class(event_class);
8b45963b 51 BT_ASSERT(stream_class);
839d52a5 52 return bt_stream_class_borrow_trace(stream_class) != NULL;
a68c0f97
PP
53}
54
f7c3ac09 55BT_HIDDEN
03e18d5d
PP
56struct bt_notification *bt_notification_event_new(
57 struct bt_graph *graph)
f7c3ac09
PP
58{
59 struct bt_notification_event *notification = NULL;
60
61 notification = g_new0(struct bt_notification_event, 1);
62 if (!notification) {
63 BT_LOGE_STR("Failed to allocate one event notification.");
64 goto error;
65 }
66
67 bt_notification_init(&notification->parent, BT_NOTIFICATION_TYPE_EVENT,
68 (bt_object_release_func) bt_notification_event_recycle, graph);
69 goto end;
70
71error:
8138bfe1 72 BT_OBJECT_PUT_REF_AND_RESET(notification);
f7c3ac09
PP
73
74end:
75 return (void *) notification;
76}
77
9e550e5f 78struct bt_private_notification *bt_private_notification_event_create(
834e9996 79 struct bt_self_notification_iterator *self_notif_iter,
9e550e5f
PP
80 struct bt_private_event_class *priv_event_class,
81 struct bt_private_packet *priv_packet)
d51202da 82{
834e9996
PP
83 struct bt_self_component_port_input_notification_iterator *notif_iter =
84 (void *) self_notif_iter;
9e550e5f
PP
85 struct bt_event_class *event_class = (void *) priv_event_class;
86 struct bt_packet *packet = (void *) priv_packet;
80f00602 87 struct bt_notification_event *notification = NULL;
c5a24b0a 88 struct bt_event *event;
546a0c4a 89
03e18d5d 90 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
a6918753
PP
91 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
92 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
a6918753
PP
93 BT_ASSERT_PRE(event_class_has_trace(event_class),
94 "Event class is not part of a trace: %!+E", event_class);
834e9996
PP
95 BT_LIB_LOGD("Creating event notification object: %![ec-]+E",
96 event_class);
c5a24b0a
PP
97 event = bt_event_create(event_class, packet);
98 if (unlikely(!event)) {
99 BT_LIB_LOGE("Cannot create event from event class: "
834e9996 100 "%![ec-]+E", event_class);
24626e8b
JG
101 goto error;
102 }
546a0c4a 103
c5a24b0a
PP
104 /*
105 * Create notification from pool _after_ we have everything
106 * (in this case, a valid event object) so that we never have an
107 * error condition with a non-NULL notification object.
108 * Otherwise:
109 *
110 * * We cannot recycle the notification on error because
111 * bt_notification_event_recycle() expects a complete
112 * notification (and the event or clock class priority map
113 * object could be unset).
114 *
115 * * We cannot destroy the notification because we would need
116 * to notify the graph (pool owner) so that it removes the
117 * notification from its notification array.
118 */
119 notification = (void *) bt_notification_create_from_pool(
834e9996 120 &notif_iter->graph->event_notif_pool, notif_iter->graph);
c5a24b0a
PP
121 if (unlikely(!notification)) {
122 /* bt_notification_create_from_pool() logs errors */
a6918753
PP
123 goto error;
124 }
125
c5a24b0a
PP
126 BT_ASSERT(!notification->event);
127 notification->event = event;
e5815ba2 128 bt_packet_set_is_frozen(packet, true);
7b33a0e0 129 bt_event_class_freeze(event_class);
834e9996
PP
130 BT_LIB_LOGD("Created event notification object: "
131 "%![notif-]+n, %![event-]+e", notification, event);
3fd45dc7 132 goto end;
546a0c4a 133
d51202da 134error:
c5a24b0a
PP
135 BT_ASSERT(!notification);
136 bt_event_destroy(event);
3fd45dc7
PP
137
138end:
f7c3ac09
PP
139 return (void *) notification;
140}
141
142BT_HIDDEN
143void bt_notification_event_destroy(struct bt_notification *notif)
144{
145 struct bt_notification_event *event_notif = (void *) notif;
146
834e9996 147 BT_LIB_LOGD("Destroying event notification: %!+n", notif);
f7c3ac09
PP
148
149 if (event_notif->event) {
834e9996 150 BT_LIB_LOGD("Recycling event: %!+e", event_notif->event);
f7c3ac09 151 bt_event_recycle(event_notif->event);
834e9996 152 event_notif->event = NULL;
f7c3ac09
PP
153 }
154
f7c3ac09
PP
155 g_free(notif);
156}
157
158BT_HIDDEN
159void bt_notification_event_recycle(struct bt_notification *notif)
160{
161 struct bt_notification_event *event_notif = (void *) notif;
162 struct bt_graph *graph;
163
164 BT_ASSERT(event_notif);
165
c5a24b0a 166 if (unlikely(!notif->graph)) {
f7c3ac09
PP
167 bt_notification_event_destroy(notif);
168 return;
169 }
170
834e9996
PP
171 BT_LIB_LOGD("Recycling event notification: %![notif-]+n, %![event-]+e",
172 notif, event_notif->event);
f7c3ac09 173 bt_notification_reset(notif);
c5a24b0a 174 BT_ASSERT(event_notif->event);
c5a24b0a
PP
175 bt_event_recycle(event_notif->event);
176 event_notif->event = NULL;
f7c3ac09
PP
177 graph = notif->graph;
178 notif->graph = NULL;
179 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
d51202da
JG
180}
181
5fe68922 182struct bt_event *bt_notification_event_borrow_event(
d51202da
JG
183 struct bt_notification *notification)
184{
185 struct bt_notification_event *event_notification;
186
8b45963b
PP
187 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
188 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
d51202da
JG
189 event_notification = container_of(notification,
190 struct bt_notification_event, parent);
5fe68922 191 return event_notification->event;
d51202da 192}
9e550e5f 193
96854e6a 194struct bt_private_event *bt_private_notification_event_borrow_event(
9e550e5f
PP
195 struct bt_private_notification *notification)
196{
197 return (void *) bt_notification_event_borrow_event(
198 (void *) notification);
199}
This page took 0.044031 seconds and 4 git commands to generate.