lib: make graph API const-correct
[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
546a0c4a
PP
25#define BT_LOG_TAG "NOTIF-EVENT"
26#include <babeltrace/lib-logging-internal.h>
27
3d9990ac 28#include <babeltrace/compiler-internal.h>
c5a24b0a 29#include <babeltrace/object-internal.h>
108b91d0
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>
f7c3ac09 35#include <babeltrace/graph/graph-internal.h>
7b53201c
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>
8b45963b
PP
40#include <babeltrace/assert-internal.h>
41#include <babeltrace/assert-pre-internal.h>
9e550e5f 42#include <babeltrace/object.h>
a68c0f97 43#include <stdbool.h>
546a0c4a 44#include <inttypes.h>
d51202da 45
8b45963b 46BT_ASSERT_PRE_FUNC
a6918753 47static inline bool event_class_has_trace(struct bt_event_class *event_class)
a68c0f97 48{
839d52a5 49 struct bt_stream_class *stream_class;
a68c0f97 50
839d52a5 51 stream_class = bt_event_class_borrow_stream_class(event_class);
8b45963b 52 BT_ASSERT(stream_class);
839d52a5 53 return bt_stream_class_borrow_trace(stream_class) != NULL;
a68c0f97
PP
54}
55
f7c3ac09 56BT_HIDDEN
03e18d5d
PP
57struct bt_notification *bt_notification_event_new(
58 struct bt_graph *graph)
f7c3ac09
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:
8138bfe1 73 BT_OBJECT_PUT_REF_AND_RESET(notification);
f7c3ac09
PP
74
75end:
76 return (void *) notification;
77}
78
7b53201c 79struct bt_notification *bt_notification_event_create(
834e9996 80 struct bt_self_notification_iterator *self_notif_iter,
78cf9df6
PP
81 struct bt_event_class *event_class,
82 struct bt_packet *packet)
d51202da 83{
834e9996
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;
c5a24b0a 87 struct bt_event *event;
546a0c4a 88
03e18d5d 89 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
a6918753
PP
90 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
91 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
a6918753
PP
92 BT_ASSERT_PRE(event_class_has_trace(event_class),
93 "Event class is not part of a trace: %!+E", event_class);
834e9996
PP
94 BT_LIB_LOGD("Creating event notification object: %![ec-]+E",
95 event_class);
c5a24b0a
PP
96 event = bt_event_create(event_class, packet);
97 if (unlikely(!event)) {
98 BT_LIB_LOGE("Cannot create event from event class: "
834e9996 99 "%![ec-]+E", event_class);
24626e8b
JG
100 goto error;
101 }
546a0c4a 102
c5a24b0a
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(
834e9996 119 &notif_iter->graph->event_notif_pool, notif_iter->graph);
c5a24b0a
PP
120 if (unlikely(!notification)) {
121 /* bt_notification_create_from_pool() logs errors */
a6918753
PP
122 goto error;
123 }
124
c5a24b0a
PP
125 BT_ASSERT(!notification->event);
126 notification->event = event;
e5815ba2 127 bt_packet_set_is_frozen(packet, true);
7b33a0e0 128 bt_event_class_freeze(event_class);
834e9996
PP
129 BT_LIB_LOGD("Created event notification object: "
130 "%![notif-]+n, %![event-]+e", notification, event);
3fd45dc7 131 goto end;
546a0c4a 132
d51202da 133error:
c5a24b0a
PP
134 BT_ASSERT(!notification);
135 bt_event_destroy(event);
3fd45dc7
PP
136
137end:
f7c3ac09
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
834e9996 146 BT_LIB_LOGD("Destroying event notification: %!+n", notif);
f7c3ac09
PP
147
148 if (event_notif->event) {
834e9996 149 BT_LIB_LOGD("Recycling event: %!+e", event_notif->event);
f7c3ac09 150 bt_event_recycle(event_notif->event);
834e9996 151 event_notif->event = NULL;
f7c3ac09
PP
152 }
153
f7c3ac09
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
c5a24b0a 165 if (unlikely(!notif->graph)) {
f7c3ac09
PP
166 bt_notification_event_destroy(notif);
167 return;
168 }
169
834e9996
PP
170 BT_LIB_LOGD("Recycling event notification: %![notif-]+n, %![event-]+e",
171 notif, event_notif->event);
f7c3ac09 172 bt_notification_reset(notif);
c5a24b0a 173 BT_ASSERT(event_notif->event);
c5a24b0a
PP
174 bt_event_recycle(event_notif->event);
175 event_notif->event = NULL;
f7c3ac09
PP
176 graph = notif->graph;
177 notif->graph = NULL;
178 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
d51202da
JG
179}
180
78cf9df6
PP
181static inline
182struct bt_event *borrow_event(struct bt_notification *notification)
d51202da
JG
183{
184 struct bt_notification_event *event_notification;
185
8b45963b
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);
5fe68922 190 return event_notification->event;
d51202da 191}
9e550e5f 192
7b53201c
PP
193struct bt_event *bt_notification_event_borrow_event(
194 struct bt_notification *notification)
9e550e5f 195{
7b53201c 196 return borrow_event(notification);
78cf9df6
PP
197}
198
7b53201c
PP
199const struct bt_event *bt_notification_event_borrow_event_const(
200 const struct bt_notification *notification)
78cf9df6 201{
7b53201c 202 return borrow_event((void *) notification);
9e550e5f 203}
This page took 0.044407 seconds and 4 git commands to generate.