2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #define BT_LOG_TAG "NOTIF-EVENT"
26 #include <babeltrace/lib-logging-internal.h>
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>
47 static inline bool event_class_has_trace(struct bt_event_class
*event_class
)
49 struct bt_stream_class
*stream_class
;
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
;
57 struct bt_notification
*bt_notification_event_new(
58 struct bt_graph
*graph
)
60 struct bt_notification_event
*notification
= NULL
;
62 notification
= g_new0(struct bt_notification_event
, 1);
64 BT_LOGE_STR("Failed to allocate one event notification.");
68 bt_notification_init(¬ification
->parent
, BT_NOTIFICATION_TYPE_EVENT
,
69 (bt_object_release_func
) bt_notification_event_recycle
, graph
);
73 BT_OBJECT_PUT_REF_AND_RESET(notification
);
76 return (void *) notification
;
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
)
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
;
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",
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
);
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.
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).
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.
118 notification
= (void *) bt_notification_create_from_pool(
119 ¬if_iter
->graph
->event_notif_pool
, notif_iter
->graph
);
120 if (unlikely(!notification
)) {
121 /* bt_notification_create_from_pool() logs errors */
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
);
134 BT_ASSERT(!notification
);
135 bt_event_destroy(event
);
138 return (void *) notification
;
142 void bt_notification_event_destroy(struct bt_notification
*notif
)
144 struct bt_notification_event
*event_notif
= (void *) notif
;
146 BT_LIB_LOGD("Destroying event notification: %!+n", notif
);
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
;
158 void bt_notification_event_recycle(struct bt_notification
*notif
)
160 struct bt_notification_event
*event_notif
= (void *) notif
;
161 struct bt_graph
*graph
;
163 BT_ASSERT(event_notif
);
165 if (unlikely(!notif
->graph
)) {
166 bt_notification_event_destroy(notif
);
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
;
178 bt_object_pool_recycle_object(&graph
->event_notif_pool
, notif
);
182 struct bt_event
*borrow_event(struct bt_notification
*notification
)
184 struct bt_notification_event
*event_notification
;
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
;
193 struct bt_event
*bt_notification_event_borrow_event(
194 struct bt_notification
*notification
)
196 return borrow_event(notification
);
199 const struct bt_event
*bt_notification_event_borrow_event_const(
200 const struct bt_notification
*notification
)
202 return borrow_event((void *) notification
);