d466852cd26188be29708ee22eb23a336fe9bf8a
[babeltrace.git] / lib / graph / notification / event.c
1 /*
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
25 #define BT_LOG_TAG "NOTIF-EVENT"
26 #include <babeltrace/lib-logging-internal.h>
27
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/object-internal.h>
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>
35 #include <babeltrace/graph/graph-internal.h>
36 #include <babeltrace/graph/notification-event-const.h>
37 #include <babeltrace/graph/notification-event.h>
38 #include <babeltrace/graph/notification-event-internal.h>
39 #include <babeltrace/types.h>
40 #include <babeltrace/assert-internal.h>
41 #include <babeltrace/assert-pre-internal.h>
42 #include <babeltrace/object.h>
43 #include <stdbool.h>
44 #include <inttypes.h>
45
46 BT_ASSERT_PRE_FUNC
47 static inline bool event_class_has_trace(struct bt_event_class *event_class)
48 {
49 struct bt_stream_class *stream_class;
50
51 stream_class = bt_event_class_borrow_stream_class(event_class);
52 BT_ASSERT(stream_class);
53 return bt_stream_class_borrow_trace_class(stream_class) != NULL;
54 }
55
56 BT_HIDDEN
57 struct bt_notification *bt_notification_event_new(
58 struct bt_graph *graph)
59 {
60 struct bt_notification_event *notification = NULL;
61
62 notification = g_new0(struct bt_notification_event, 1);
63 if (!notification) {
64 BT_LOGE_STR("Failed to allocate one event notification.");
65 goto error;
66 }
67
68 bt_notification_init(&notification->parent, BT_NOTIFICATION_TYPE_EVENT,
69 (bt_object_release_func) bt_notification_event_recycle, graph);
70 goto end;
71
72 error:
73 BT_OBJECT_PUT_REF_AND_RESET(notification);
74
75 end:
76 return (void *) notification;
77 }
78
79 struct bt_notification *bt_notification_event_create(
80 struct bt_self_notification_iterator *self_notif_iter,
81 struct bt_event_class *event_class,
82 struct bt_packet *packet)
83 {
84 struct bt_self_component_port_input_notification_iterator *notif_iter =
85 (void *) self_notif_iter;
86 struct bt_notification_event *notification = NULL;
87 struct bt_event *event;
88
89 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
90 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
91 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
92 BT_ASSERT_PRE(event_class_has_trace(event_class),
93 "Event class is not part of a trace: %!+E", event_class);
94 BT_LIB_LOGD("Creating event notification object: %![ec-]+E",
95 event_class);
96 event = bt_event_create(event_class, packet);
97 if (unlikely(!event)) {
98 BT_LIB_LOGE("Cannot create event from event class: "
99 "%![ec-]+E", event_class);
100 goto error;
101 }
102
103 /*
104 * Create notification from pool _after_ we have everything
105 * (in this case, a valid event object) so that we never have an
106 * error condition with a non-NULL notification object.
107 * Otherwise:
108 *
109 * * We cannot recycle the notification on error because
110 * bt_notification_event_recycle() expects a complete
111 * notification (and the event or clock class priority map
112 * object could be unset).
113 *
114 * * We cannot destroy the notification because we would need
115 * to notify the graph (pool owner) so that it removes the
116 * notification from its notification array.
117 */
118 notification = (void *) bt_notification_create_from_pool(
119 &notif_iter->graph->event_notif_pool, notif_iter->graph);
120 if (unlikely(!notification)) {
121 /* bt_notification_create_from_pool() logs errors */
122 goto error;
123 }
124
125 BT_ASSERT(!notification->event);
126 notification->event = event;
127 bt_packet_set_is_frozen(packet, true);
128 bt_event_class_freeze(event_class);
129 BT_LIB_LOGD("Created event notification object: "
130 "%![notif-]+n, %![event-]+e", notification, event);
131 goto end;
132
133 error:
134 BT_ASSERT(!notification);
135 bt_event_destroy(event);
136
137 end:
138 return (void *) notification;
139 }
140
141 BT_HIDDEN
142 void bt_notification_event_destroy(struct bt_notification *notif)
143 {
144 struct bt_notification_event *event_notif = (void *) notif;
145
146 BT_LIB_LOGD("Destroying event notification: %!+n", notif);
147
148 if (event_notif->event) {
149 BT_LIB_LOGD("Recycling event: %!+e", event_notif->event);
150 bt_event_recycle(event_notif->event);
151 event_notif->event = NULL;
152 }
153
154 g_free(notif);
155 }
156
157 BT_HIDDEN
158 void bt_notification_event_recycle(struct bt_notification *notif)
159 {
160 struct bt_notification_event *event_notif = (void *) notif;
161 struct bt_graph *graph;
162
163 BT_ASSERT(event_notif);
164
165 if (unlikely(!notif->graph)) {
166 bt_notification_event_destroy(notif);
167 return;
168 }
169
170 BT_LIB_LOGD("Recycling event notification: %![notif-]+n, %![event-]+e",
171 notif, event_notif->event);
172 bt_notification_reset(notif);
173 BT_ASSERT(event_notif->event);
174 bt_event_recycle(event_notif->event);
175 event_notif->event = NULL;
176 graph = notif->graph;
177 notif->graph = NULL;
178 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
179 }
180
181 static inline
182 struct bt_event *borrow_event(struct bt_notification *notification)
183 {
184 struct bt_notification_event *event_notification;
185
186 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
187 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
188 event_notification = container_of(notification,
189 struct bt_notification_event, parent);
190 return event_notification->event;
191 }
192
193 struct bt_event *bt_notification_event_borrow_event(
194 struct bt_notification *notification)
195 {
196 return borrow_event(notification);
197 }
198
199 const struct bt_event *bt_notification_event_borrow_event_const(
200 const struct bt_notification *notification)
201 {
202 return borrow_event((void *) notification);
203 }
This page took 0.033886 seconds and 3 git commands to generate.