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