lib: add pre condition asserts to check current thread has no error
[babeltrace.git] / src / lib / graph / message / packet.c
CommitLineData
d6e69534
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-PACKET"
c2d9d9cf 25#include "lib/logging.h"
d6e69534 26
c4f23e30
FD
27#include <stdbool.h>
28
51375aa9
PP
29#include "lib/assert-pre.h"
30#include "lib/assert-post.h"
578e048b 31#include "compat/compiler.h"
3fadfbc0 32#include <babeltrace2/trace-ir/packet.h>
578e048b 33#include "lib/trace-ir/packet.h"
3fadfbc0
MJ
34#include <babeltrace2/trace-ir/stream-class.h>
35#include <babeltrace2/trace-ir/stream.h>
578e048b
MJ
36#include "lib/trace-ir/stream.h"
37#include "lib/trace-ir/stream-class.h"
38#include "lib/graph/graph.h"
3fadfbc0
MJ
39#include <babeltrace2/graph/message-packet-beginning-const.h>
40#include <babeltrace2/graph/message-packet-end-const.h>
41#include <babeltrace2/graph/message-packet-beginning.h>
42#include <babeltrace2/graph/message-packet-end.h>
578e048b 43#include "common/assert.h"
578e048b 44#include "lib/object.h"
d6e69534
PP
45#include <inttypes.h>
46
578e048b
MJ
47#include "packet.h"
48
5df26c89
PP
49static inline
50struct bt_message *new_packet_message(struct bt_graph *graph,
51 enum bt_message_type type, bt_object_release_func recycle_func)
d6e69534 52{
5df26c89 53 struct bt_message_packet *message;
d6e69534 54
5df26c89 55 message = g_new0(struct bt_message_packet, 1);
d6e69534 56 if (!message) {
870631a2
PP
57 BT_LIB_LOGE_APPEND_CAUSE(
58 "Failed to allocate one packet message.");
d6e69534
PP
59 goto error;
60 }
61
5df26c89 62 bt_message_init(&message->parent, type, recycle_func, graph);
d6e69534
PP
63 goto end;
64
65error:
66 BT_OBJECT_PUT_REF_AND_RESET(message);
67
68end:
69 return (void *) message;
70}
71
5df26c89
PP
72BT_HIDDEN
73struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
d6e69534 74{
5df26c89
PP
75 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
76 (bt_object_release_func) bt_message_packet_beginning_recycle);
77}
78
79BT_HIDDEN
80struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
81{
82 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
83 (bt_object_release_func) bt_message_packet_end_recycle);
84}
85
86static inline
a6d85d2f 87struct bt_message *create_packet_message(
5df26c89 88 struct bt_self_component_port_input_message_iterator *msg_iter,
a6d85d2f 89 struct bt_packet *packet, struct bt_object_pool *pool,
83a5656a 90 bool with_cs, uint64_t raw_value)
5df26c89
PP
91{
92 struct bt_message_packet *message = NULL;
d6e69534
PP
93 struct bt_stream *stream;
94 struct bt_stream_class *stream_class;
8cc5f12b 95 bool need_cs;
d6e69534 96
5df26c89 97 BT_ASSERT(msg_iter);
d6e69534
PP
98 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
99 stream = bt_packet_borrow_stream(packet);
100 BT_ASSERT(stream);
101 stream_class = bt_stream_borrow_class(stream);
102 BT_ASSERT(stream_class);
649934d2 103
26fc5aed
PP
104 /*
105 * It's not possible to create a packet from a stream of which
106 * the class indicates that packets are not supported.
107 */
108 BT_ASSERT(stream_class->supports_packets);
109
83a5656a 110 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
8cc5f12b 111 need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
649934d2 112 } else {
8cc5f12b 113 need_cs = stream_class->packets_have_end_default_clock_snapshot;
649934d2
PP
114 }
115
116 /*
117 * `packet_has_default_clock_snapshot` implies that the stream
118 * class has a default clock class (precondition).
119 */
8cc5f12b 120 BT_ASSERT_PRE(need_cs ? with_cs : true,
7fe92073 121 "Unexpected stream class configuration when creating "
8cc5f12b
PP
122 "a packet beginning or end message: "
123 "a default clock snapshot is needed, but none was provided: "
124 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
125 "cs-val=%" PRIu64,
126 stream, stream_class, with_cs, raw_value);
127 BT_ASSERT_PRE(!need_cs ? !with_cs : true,
128 "Unexpected stream class configuration when creating "
129 "a packet beginning or end message: "
130 "no default clock snapshot is needed, but one was provided: "
a6d85d2f
PP
131 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
132 "cs-val=%" PRIu64,
133 stream, stream_class, with_cs, raw_value);
5df26c89 134 BT_LIB_LOGD("Creating packet message object: "
d6e69534
PP
135 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
136 packet, stream, stream_class);
5df26c89 137 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
d6e69534
PP
138 if (!message) {
139 /* bt_message_create_from_pool() logs errors */
140 goto end;
141 }
142
a6d85d2f
PP
143 if (with_cs) {
144 BT_ASSERT(stream_class->default_clock_class);
145 message->default_cs = bt_clock_snapshot_create(
146 stream_class->default_clock_class);
147 if (!message->default_cs) {
6871026b 148 bt_object_put_ref_no_null_check(message);
a6d85d2f
PP
149 message = NULL;
150 goto end;
151 }
152
153 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
154 }
155
d6e69534
PP
156 BT_ASSERT(!message->packet);
157 message->packet = packet;
6871026b 158 bt_object_get_ref_no_null_check_no_parent_check(
d6e69534
PP
159 &message->packet->base);
160 bt_packet_set_is_frozen(packet, true);
5df26c89 161 BT_LIB_LOGD("Created packet message object: "
d6e69534
PP
162 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
163 message, packet, stream, stream_class);
164 goto end;
165
166end:
167 return (void *) message;
168}
169
5df26c89
PP
170struct bt_message *bt_message_packet_beginning_create(
171 struct bt_self_message_iterator *self_msg_iter,
58085ca4 172 const struct bt_packet *packet)
d6e69534 173{
5df26c89
PP
174 struct bt_self_component_port_input_message_iterator *msg_iter =
175 (void *) self_msg_iter;
d6e69534 176
17f3083a 177 BT_ASSERT_PRE_DEV_NO_ERROR();
5df26c89 178 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
a6d85d2f 179 return create_packet_message(msg_iter, (void *) packet,
83a5656a 180 &msg_iter->graph->packet_begin_msg_pool, false, 0);
a6d85d2f
PP
181}
182
183struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
184 struct bt_self_message_iterator *self_msg_iter,
185 const struct bt_packet *packet, uint64_t raw_value)
186{
187 struct bt_self_component_port_input_message_iterator *msg_iter =
188 (void *) self_msg_iter;
189
17f3083a 190 BT_ASSERT_PRE_DEV_NO_ERROR();
a6d85d2f
PP
191 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
192 return create_packet_message(msg_iter, (void *) packet,
83a5656a 193 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
d6e69534
PP
194}
195
5df26c89
PP
196struct bt_message *bt_message_packet_end_create(
197 struct bt_self_message_iterator *self_msg_iter,
58085ca4 198 const struct bt_packet *packet)
d6e69534 199{
5df26c89
PP
200 struct bt_self_component_port_input_message_iterator *msg_iter =
201 (void *) self_msg_iter;
d6e69534 202
17f3083a 203 BT_ASSERT_PRE_DEV_NO_ERROR();
5df26c89 204 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
a6d85d2f 205 return create_packet_message(msg_iter, (void *) packet,
83a5656a 206 &msg_iter->graph->packet_end_msg_pool, false, 0);
a6d85d2f
PP
207}
208
209struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
210 struct bt_self_message_iterator *self_msg_iter,
211 const struct bt_packet *packet, uint64_t raw_value)
212{
213 struct bt_self_component_port_input_message_iterator *msg_iter =
214 (void *) self_msg_iter;
215
17f3083a 216 BT_ASSERT_PRE_DEV_NO_ERROR();
a6d85d2f
PP
217 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
218 return create_packet_message(msg_iter, (void *) packet,
83a5656a 219 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
d6e69534
PP
220}
221
5df26c89
PP
222BT_HIDDEN
223void bt_message_packet_destroy(struct bt_message *msg)
d6e69534 224{
5df26c89 225 struct bt_message_packet *packet_msg = (void *) msg;
d6e69534 226
5df26c89
PP
227 BT_LIB_LOGD("Destroying packet message: %!+n", msg);
228 BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet);
229 BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet);
a6d85d2f
PP
230
231 if (packet_msg->default_cs) {
232 bt_clock_snapshot_recycle(packet_msg->default_cs);
233 packet_msg->default_cs = NULL;
234 }
235
5df26c89 236 g_free(msg);
d6e69534
PP
237}
238
5df26c89
PP
239static inline
240void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
d6e69534 241{
5df26c89
PP
242 struct bt_message_packet *packet_msg = (void *) msg;
243
244 BT_LIB_LOGD("Recycling packet message: %!+n", msg);
245 bt_message_reset(msg);
6871026b 246 bt_object_put_ref_no_null_check(&packet_msg->packet->base);
a6d85d2f
PP
247
248 if (packet_msg->default_cs) {
249 bt_clock_snapshot_recycle(packet_msg->default_cs);
250 packet_msg->default_cs = NULL;
251 }
252
5df26c89
PP
253 packet_msg->packet = NULL;
254 msg->graph = NULL;
255 bt_object_pool_recycle_object(pool, msg);
d6e69534
PP
256}
257
258BT_HIDDEN
5df26c89 259void bt_message_packet_beginning_recycle(struct bt_message *msg)
d6e69534 260{
5df26c89 261 BT_ASSERT(msg);
d6e69534 262
91d81473 263 if (G_UNLIKELY(!msg->graph)) {
5df26c89
PP
264 bt_message_packet_destroy(msg);
265 return;
d6e69534
PP
266 }
267
5df26c89 268 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
d6e69534
PP
269}
270
5df26c89
PP
271BT_HIDDEN
272void bt_message_packet_end_recycle(struct bt_message *msg)
d6e69534 273{
5df26c89 274 BT_ASSERT(msg);
d6e69534 275
91d81473 276 if (G_UNLIKELY(!msg->graph)) {
5df26c89
PP
277 bt_message_packet_destroy(msg);
278 return;
d6e69534
PP
279 }
280
5df26c89 281 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
d6e69534
PP
282}
283
5df26c89
PP
284struct bt_packet *bt_message_packet_beginning_borrow_packet(
285 struct bt_message *message)
d6e69534 286{
5df26c89 287 struct bt_message_packet *packet_msg = (void *) message;
d6e69534 288
bdb288b3
PP
289 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
290 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
5df26c89
PP
291 BT_MESSAGE_TYPE_PACKET_BEGINNING);
292 return packet_msg->packet;
d6e69534
PP
293}
294
5df26c89
PP
295const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
296 const struct bt_message *message)
d6e69534 297{
5df26c89
PP
298 return bt_message_packet_beginning_borrow_packet(
299 (void *) message);
d6e69534
PP
300}
301
302struct bt_packet *bt_message_packet_end_borrow_packet(
303 struct bt_message *message)
304{
5df26c89 305 struct bt_message_packet *packet_msg = (void *) message;
d6e69534 306
bdb288b3
PP
307 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
308 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
d6e69534 309 BT_MESSAGE_TYPE_PACKET_END);
5df26c89 310 return packet_msg->packet;
d6e69534
PP
311}
312
313const struct bt_packet *bt_message_packet_end_borrow_packet_const(
314 const struct bt_message *message)
315{
316 return bt_message_packet_end_borrow_packet(
317 (void *) message);
318}
a6d85d2f
PP
319
320static inline
0cbc2c33 321const struct bt_clock_snapshot *
a6d85d2f 322borrow_packet_message_default_clock_snapshot_const(
0cbc2c33 323 const struct bt_message *message)
a6d85d2f
PP
324{
325 struct bt_message_packet *packet_msg = (void *) message;
326
98b15851 327 BT_ASSERT_DBG(message);
bdb288b3
PP
328 BT_ASSERT_PRE_DEV(
329 packet_msg->packet->stream->class->default_clock_class,
a6d85d2f
PP
330 "Message's stream's class has no default clock class: "
331 "%![msg-]+n, %![sc-]+S",
332 message, packet_msg->packet->stream->class);
0cbc2c33 333 return packet_msg->default_cs;
a6d85d2f
PP
334}
335
0cbc2c33 336const struct bt_clock_snapshot *
a6d85d2f 337bt_message_packet_beginning_borrow_default_clock_snapshot_const(
0cbc2c33 338 const struct bt_message *msg)
a6d85d2f 339{
bdb288b3
PP
340 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
341 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
0cbc2c33 342 return borrow_packet_message_default_clock_snapshot_const(msg);
a6d85d2f
PP
343}
344
0cbc2c33 345const struct bt_clock_snapshot *
a6d85d2f 346bt_message_packet_end_borrow_default_clock_snapshot_const(
0cbc2c33 347 const struct bt_message *msg)
a6d85d2f 348{
bdb288b3
PP
349 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
350 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
0cbc2c33 351 return borrow_packet_message_default_clock_snapshot_const(msg);
a6d85d2f 352}
33931ab8
PP
353
354static inline
355const struct bt_clock_class *
356borrow_packet_message_stream_class_default_clock_class(
357 const struct bt_message *msg)
358{
359 struct bt_message_packet *packet_msg = (void *) msg;
360
98b15851 361 BT_ASSERT_DBG(msg);
33931ab8
PP
362 return packet_msg->packet->stream->class->default_clock_class;
363}
364
365const struct bt_clock_class *
366bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
367 const struct bt_message *msg)
368{
bdb288b3
PP
369 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
370 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
33931ab8
PP
371 return borrow_packet_message_stream_class_default_clock_class(msg);
372}
373
374const struct bt_clock_class *
375bt_message_packet_end_borrow_stream_class_default_clock_class_const(
376 const struct bt_message *msg)
377{
bdb288b3
PP
378 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
379 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
33931ab8
PP
380 return borrow_packet_message_stream_class_default_clock_class(msg);
381}
This page took 0.076088 seconds and 4 git commands to generate.