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