lib: add pre condition asserts to check current thread has no error
[babeltrace.git] / src / lib / graph / message / stream.c
CommitLineData
b09a5592
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
b03487ab 24#define BT_LOG_TAG "LIB/MSG-STREAM"
1633ef46 25#include "lib/logging.h"
b09a5592 26
57952005
MJ
27#include "lib/assert-pre.h"
28#include "compat/compiler.h"
71c5da58 29#include <babeltrace2/trace-ir/clock-snapshot-const.h>
57952005 30#include "lib/trace-ir/stream.h"
71c5da58 31#include <babeltrace2/trace-ir/stream-class.h>
57952005 32#include "lib/trace-ir/stream-class.h"
71c5da58
MJ
33#include <babeltrace2/graph/message-stream-beginning.h>
34#include <babeltrace2/graph/message-stream-end.h>
35#include <babeltrace2/graph/message-stream-beginning-const.h>
36#include <babeltrace2/graph/message-stream-end-const.h>
57952005 37#include "common/assert.h"
b09a5592
PP
38#include <inttypes.h>
39
57952005
MJ
40#include "stream.h"
41
b09a5592 42static
a1f053a9 43void destroy_stream_message(struct bt_object *obj)
b09a5592 44{
a1f053a9 45 struct bt_message_stream *message = (void *) obj;
b09a5592 46
a1f053a9 47 BT_LIB_LOGD("Destroying stream message: %!+n", message);
9fceecf0
PP
48
49 if (message->default_cs) {
50 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
51 message->default_cs);
52 bt_clock_snapshot_destroy(message->default_cs);
53 message->default_cs = NULL;
54 }
55
b09a5592
PP
56 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
57 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
b09a5592
PP
58 g_free(message);
59}
60
a1f053a9
PP
61static inline
62struct bt_message *create_stream_message(
b09a5592 63 struct bt_self_message_iterator *self_msg_iter,
a1f053a9 64 struct bt_stream *stream, enum bt_message_type type)
b09a5592 65{
a1f053a9 66 struct bt_message_stream *message;
b09a5592
PP
67 struct bt_stream_class *stream_class;
68
69 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
70 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
71 stream_class = bt_stream_borrow_class(stream);
72 BT_ASSERT(stream_class);
a1f053a9
PP
73 BT_LIB_LOGD("Creating stream message object: "
74 "type=%s, %![stream-]+s, %![sc-]+S",
75 bt_message_type_string(type), stream, stream_class);
76 message = g_new0(struct bt_message_stream, 1);
b09a5592 77 if (!message) {
a8f90e5d
PP
78 BT_LIB_LOGE_APPEND_CAUSE(
79 "Failed to allocate one stream message.");
b09a5592
PP
80 goto error;
81 }
82
a1f053a9
PP
83 bt_message_init(&message->parent, type,
84 destroy_stream_message, NULL);
b09a5592 85 message->stream = stream;
864aa43f 86 bt_object_get_ref_no_null_check(message->stream);
b7cbc799
SM
87
88 if (stream_class->default_clock_class) {
89 message->default_cs = bt_clock_snapshot_create(
90 stream_class->default_clock_class);
91 if (!message->default_cs) {
92 goto error;
93 }
94 }
95
a1f053a9 96 BT_LIB_LOGD("Created stream message object: "
b09a5592
PP
97 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
98 stream, stream_class);
99
b7cbc799 100 goto end;
a1f053a9 101
b09a5592 102error:
b7cbc799
SM
103 if (message) {
104 g_free(message);
105 message = NULL;
106 }
107
108end:
109 return &message->parent;
b09a5592
PP
110}
111
a1f053a9
PP
112struct bt_message *bt_message_stream_beginning_create(
113 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 114 const struct bt_stream *stream)
b09a5592 115{
7c7324d3
SM
116 BT_ASSERT_PRE_DEV_NO_ERROR();
117
6a6975d2 118 return create_stream_message(self_msg_iter, (void *) stream,
a1f053a9 119 BT_MESSAGE_TYPE_STREAM_BEGINNING);
b09a5592
PP
120}
121
a1f053a9
PP
122struct bt_message *bt_message_stream_end_create(
123 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 124 const struct bt_stream *stream)
b09a5592 125{
7c7324d3
SM
126 BT_ASSERT_PRE_DEV_NO_ERROR();
127
6a6975d2 128 return create_stream_message(self_msg_iter, (void *) stream,
a1f053a9 129 BT_MESSAGE_TYPE_STREAM_END);
b09a5592
PP
130}
131
a1f053a9
PP
132static inline
133struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
b09a5592 134{
a1f053a9 135 struct bt_message_stream *stream_msg;
b09a5592 136
ec4a3354 137 BT_ASSERT_DBG(message);
a1f053a9
PP
138 stream_msg = (void *) message;
139 return stream_msg->stream;
b09a5592
PP
140}
141
a1f053a9
PP
142struct bt_stream *bt_message_stream_beginning_borrow_stream(
143 struct bt_message *message)
b09a5592 144{
fa6cfec3
PP
145 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
146 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
147 BT_MESSAGE_TYPE_STREAM_BEGINNING);
a1f053a9 148 return borrow_stream_message_stream(message);
b09a5592
PP
149}
150
a1f053a9 151struct bt_stream *bt_message_stream_end_borrow_stream(
b09a5592
PP
152 struct bt_message *message)
153{
fa6cfec3
PP
154 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
155 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
156 BT_MESSAGE_TYPE_STREAM_END);
a1f053a9 157 return borrow_stream_message_stream(message);
b09a5592
PP
158}
159
160const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
161 const struct bt_message *message)
162{
163 return bt_message_stream_beginning_borrow_stream(
164 (void *) message);
165}
a1f053a9
PP
166
167const struct bt_stream *bt_message_stream_end_borrow_stream_const(
168 const struct bt_message *message)
169{
170 return bt_message_stream_end_borrow_stream(
171 (void *) message);
172}
b7cbc799
SM
173
174static
175void bt_message_stream_set_default_clock_snapshot(
176 struct bt_message *msg, uint64_t raw_value)
177{
178 struct bt_message_stream *stream_msg = (void *) msg;
179 struct bt_stream_class *sc;
180
181 BT_ASSERT(msg);
fa6cfec3 182 BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg);
b7cbc799
SM
183 sc = stream_msg->stream->class;
184 BT_ASSERT(sc);
185 BT_ASSERT_PRE(sc->default_clock_class,
186 "Message's stream's class has no default clock class: "
187 "%![msg-]+n, %![sc-]+S", msg, sc);
188 BT_ASSERT(stream_msg->default_cs);
189 bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value);
190 stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN;
191 BT_LIB_LOGD("Set stream message's default clock snapshot: "
192 "%![msg-]+n, value=%" PRIu64, msg, raw_value);
193}
194
195void bt_message_stream_beginning_set_default_clock_snapshot(
196 struct bt_message *message, uint64_t raw_value)
197{
198 BT_ASSERT_PRE_NON_NULL(message, "Message");
199 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
200
201 bt_message_stream_set_default_clock_snapshot(message, raw_value);
202}
203
204void bt_message_stream_end_set_default_clock_snapshot(
205 struct bt_message *message, uint64_t raw_value)
206{
207 BT_ASSERT_PRE_NON_NULL(message, "Message");
208 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
209
210 return bt_message_stream_set_default_clock_snapshot(message, raw_value);
211}
212
213static enum bt_message_stream_clock_snapshot_state
214bt_message_stream_borrow_default_clock_snapshot_const(
215 const bt_message *msg, const bt_clock_snapshot **snapshot)
216{
217 struct bt_message_stream *stream_msg = (void *) msg;
218 struct bt_stream_class *sc;
219
ec4a3354 220 BT_ASSERT_DBG(msg);
b7cbc799 221 sc = stream_msg->stream->class;
ec4a3354 222 BT_ASSERT_DBG(sc);
fa6cfec3 223 BT_ASSERT_PRE_DEV(sc->default_clock_class,
b7cbc799
SM
224 "Message's stream's class has no default clock class: "
225 "%![msg-]+n, %![sc-]+S", msg, sc);
ec4a3354 226 BT_ASSERT_DBG(stream_msg->default_cs);
b7cbc799
SM
227
228 *snapshot = stream_msg->default_cs;
229
230 return stream_msg->default_cs_state;
231}
232
233enum bt_message_stream_clock_snapshot_state
234bt_message_stream_beginning_borrow_default_clock_snapshot_const(
235 const bt_message *message, const bt_clock_snapshot **snapshot)
236{
fa6cfec3
PP
237 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
238 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
239 BT_MESSAGE_TYPE_STREAM_BEGINNING);
b7cbc799
SM
240 return bt_message_stream_borrow_default_clock_snapshot_const(
241 message, snapshot);
242}
243
244enum bt_message_stream_clock_snapshot_state
245bt_message_stream_end_borrow_default_clock_snapshot_const(
246 const bt_message *message, const bt_clock_snapshot **snapshot)
247{
fa6cfec3
PP
248 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
249 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
b7cbc799
SM
250 return bt_message_stream_borrow_default_clock_snapshot_const(
251 message, snapshot);
252}
253
254static inline
255const struct bt_clock_class *
256borrow_stream_message_stream_class_default_clock_class(
257 const struct bt_message *msg)
258{
259 struct bt_message_stream *stream_msg = (void *) msg;
260
ec4a3354 261 BT_ASSERT_DBG(msg);
b7cbc799
SM
262 return stream_msg->stream->class->default_clock_class;
263}
264
265const struct bt_clock_class *
266bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
267 const struct bt_message *msg)
268{
fa6cfec3
PP
269 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
270 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg,
b7cbc799
SM
271 BT_MESSAGE_TYPE_STREAM_BEGINNING);
272 return borrow_stream_message_stream_class_default_clock_class(msg);
273}
274
275const struct bt_clock_class *
276bt_message_stream_end_borrow_stream_class_default_clock_class_const(
277 const struct bt_message *msg)
278{
fa6cfec3
PP
279 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
280 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
b7cbc799
SM
281 return borrow_stream_message_stream_class_default_clock_class(msg);
282}
This page took 0.065714 seconds and 4 git commands to generate.