lib: split trace API into trace class and trace 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
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>
0d72b8c3
PP
36#include <babeltrace/graph/notification-event-const.h>
37#include <babeltrace/graph/notification-event.h>
b2e0c907 38#include <babeltrace/graph/notification-event-internal.h>
c55a9f58 39#include <babeltrace/types.h>
f6ccaed9
PP
40#include <babeltrace/assert-internal.h>
41#include <babeltrace/assert-pre-internal.h>
e5be10ef 42#include <babeltrace/object.h>
a68c0f97 43#include <stdbool.h>
6072db50 44#include <inttypes.h>
d51202da 45
f6ccaed9 46BT_ASSERT_PRE_FUNC
312c056a 47static inline bool event_class_has_trace(struct bt_event_class *event_class)
a68c0f97 48{
50842bdc 49 struct bt_stream_class *stream_class;
a68c0f97 50
50842bdc 51 stream_class = bt_event_class_borrow_stream_class(event_class);
f6ccaed9 52 BT_ASSERT(stream_class);
862ca4ed 53 return bt_stream_class_borrow_trace_class(stream_class) != NULL;
a68c0f97
PP
54}
55
5c563278 56BT_HIDDEN
f0010051
PP
57struct bt_notification *bt_notification_event_new(
58 struct bt_graph *graph)
5c563278
PP
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
72error:
65300d60 73 BT_OBJECT_PUT_REF_AND_RESET(notification);
5c563278
PP
74
75end:
76 return (void *) notification;
77}
78
0d72b8c3 79struct bt_notification *bt_notification_event_create(
d94d92ac 80 struct bt_self_notification_iterator *self_notif_iter,
40f4ba76
PP
81 struct bt_event_class *event_class,
82 struct bt_packet *packet)
d51202da 83{
d94d92ac
PP
84 struct bt_self_component_port_input_notification_iterator *notif_iter =
85 (void *) self_notif_iter;
80f00602 86 struct bt_notification_event *notification = NULL;
6c677fb5 87 struct bt_event *event;
6072db50 88
f0010051 89 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
312c056a
PP
90 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
91 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
312c056a
PP
92 BT_ASSERT_PRE(event_class_has_trace(event_class),
93 "Event class is not part of a trace: %!+E", event_class);
d94d92ac
PP
94 BT_LIB_LOGD("Creating event notification object: %![ec-]+E",
95 event_class);
6c677fb5
PP
96 event = bt_event_create(event_class, packet);
97 if (unlikely(!event)) {
98 BT_LIB_LOGE("Cannot create event from event class: "
d94d92ac 99 "%![ec-]+E", event_class);
24626e8b
JG
100 goto error;
101 }
6072db50 102
6c677fb5
PP
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(
d94d92ac 119 &notif_iter->graph->event_notif_pool, notif_iter->graph);
6c677fb5
PP
120 if (unlikely(!notification)) {
121 /* bt_notification_create_from_pool() logs errors */
312c056a
PP
122 goto error;
123 }
124
6c677fb5
PP
125 BT_ASSERT(!notification->event);
126 notification->event = event;
ccf82993 127 bt_packet_set_is_frozen(packet, true);
44c440bc 128 bt_event_class_freeze(event_class);
d94d92ac
PP
129 BT_LIB_LOGD("Created event notification object: "
130 "%![notif-]+n, %![event-]+e", notification, event);
d4629a98 131 goto end;
6072db50 132
d51202da 133error:
6c677fb5
PP
134 BT_ASSERT(!notification);
135 bt_event_destroy(event);
d4629a98
PP
136
137end:
5c563278
PP
138 return (void *) notification;
139}
140
141BT_HIDDEN
142void bt_notification_event_destroy(struct bt_notification *notif)
143{
144 struct bt_notification_event *event_notif = (void *) notif;
145
d94d92ac 146 BT_LIB_LOGD("Destroying event notification: %!+n", notif);
5c563278
PP
147
148 if (event_notif->event) {
d94d92ac 149 BT_LIB_LOGD("Recycling event: %!+e", event_notif->event);
5c563278 150 bt_event_recycle(event_notif->event);
d94d92ac 151 event_notif->event = NULL;
5c563278
PP
152 }
153
5c563278
PP
154 g_free(notif);
155}
156
157BT_HIDDEN
158void 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
6c677fb5 165 if (unlikely(!notif->graph)) {
5c563278
PP
166 bt_notification_event_destroy(notif);
167 return;
168 }
169
d94d92ac
PP
170 BT_LIB_LOGD("Recycling event notification: %![notif-]+n, %![event-]+e",
171 notif, event_notif->event);
5c563278 172 bt_notification_reset(notif);
6c677fb5 173 BT_ASSERT(event_notif->event);
6c677fb5
PP
174 bt_event_recycle(event_notif->event);
175 event_notif->event = NULL;
5c563278
PP
176 graph = notif->graph;
177 notif->graph = NULL;
178 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
d51202da
JG
179}
180
40f4ba76
PP
181static inline
182struct bt_event *borrow_event(struct bt_notification *notification)
d51202da
JG
183{
184 struct bt_notification_event *event_notification;
185
f6ccaed9
PP
186 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
187 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
d51202da
JG
188 event_notification = container_of(notification,
189 struct bt_notification_event, parent);
094ff7c0 190 return event_notification->event;
d51202da 191}
e5be10ef 192
0d72b8c3
PP
193struct bt_event *bt_notification_event_borrow_event(
194 struct bt_notification *notification)
e5be10ef 195{
0d72b8c3 196 return borrow_event(notification);
40f4ba76
PP
197}
198
0d72b8c3
PP
199const struct bt_event *bt_notification_event_borrow_event_const(
200 const struct bt_notification *notification)
40f4ba76 201{
0d72b8c3 202 return borrow_event((void *) notification);
e5be10ef 203}
This page took 0.069362 seconds and 4 git commands to generate.