lib: add pre condition asserts to check current thread has no error
[babeltrace.git] / src / lib / trace-ir / stream.c
CommitLineData
273b65be 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be 4 *
273b65be
JG
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/STREAM"
c2d9d9cf 25#include "lib/logging.h"
19abc2c6 26
578e048b 27#include "lib/assert-pre.h"
3fadfbc0
MJ
28#include <babeltrace2/trace-ir/stream-const.h>
29#include <babeltrace2/trace-ir/stream.h>
3fadfbc0 30#include <babeltrace2/trace-ir/stream-class.h>
3fadfbc0 31#include <babeltrace2/trace-ir/trace.h>
578e048b
MJ
32#include "compat/compiler.h"
33#include "common/align.h"
34#include "common/assert.h"
35#include "lib/property.h"
3dca2276 36#include <inttypes.h>
c4f23e30 37#include <stdbool.h>
3dca2276 38#include <unistd.h>
12c8a1a3 39
578e048b
MJ
40#include "packet.h"
41#include "stream-class.h"
42#include "stream.h"
43#include "trace.h"
c6962c96 44#include "lib/value.h"
d24d5663 45#include "lib/func-status.h"
578e048b 46
bdb288b3
PP
47#define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \
48 BT_ASSERT_PRE_DEV_HOT((_stream), "Stream", ": %!+s", (_stream))
263a7df5 49
c9af50d1 50static
44c440bc 51void destroy_stream(struct bt_object *obj)
c9af50d1 52{
3dca2276 53 struct bt_stream *stream = (void *) obj;
c9af50d1 54
44c440bc 55 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
c6962c96 56 BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes);
44c440bc
PP
57
58 if (stream->name.str) {
59 g_string_free(stream->name.str, TRUE);
238b7404
PP
60 stream->name.str = NULL;
61 stream->name.value = NULL;
44c440bc
PP
62 }
63
862ca4ed
PP
64 BT_LOGD_STR("Putting stream's class.");
65 bt_object_put_ref(stream->class);
312c056a 66 bt_object_pool_finalize(&stream->packet_pool);
3dca2276 67 g_free(stream);
c9af50d1
JG
68}
69
44c440bc
PP
70static
71void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
273b65be 72{
44c440bc
PP
73 bt_packet_destroy(packet);
74}
19abc2c6 75
44c440bc
PP
76static inline
77bool stream_id_is_unique(struct bt_trace *trace,
78 struct bt_stream_class *stream_class, uint64_t id)
79{
80 uint64_t i;
81 bool is_unique = true;
273b65be 82
44c440bc
PP
83 for (i = 0; i < trace->streams->len; i++) {
84 struct bt_stream *stream = trace->streams->pdata[i];
273b65be 85
44c440bc
PP
86 if (stream->class != stream_class) {
87 continue;
8bfa3f9c 88 }
1c1d572f 89
44c440bc
PP
90 if (stream->id == id) {
91 is_unique = false;
92 goto end;
98edd02c 93 }
273b65be
JG
94 }
95
3dca2276 96end:
44c440bc 97 return is_unique;
312c056a
PP
98}
99
273b65be 100static
44c440bc 101struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
862ca4ed 102 struct bt_trace *trace, uint64_t id)
273b65be 103{
3dca2276 104 int ret;
44c440bc 105 struct bt_stream *stream;
44c440bc
PP
106
107 BT_ASSERT(stream_class);
862ca4ed
PP
108 BT_ASSERT(trace);
109 BT_ASSERT_PRE(trace->class ==
110 bt_stream_class_borrow_trace_class_inline(stream_class),
111 "Trace's class is different from stream class's parent trace class: "
112 "%![sc-]+S, %![trace-]+t", stream_class, trace);
44c440bc
PP
113 BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id),
114 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
44c440bc
PP
115 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
116 trace, id);
3dca2276
PP
117 stream = g_new0(struct bt_stream, 1);
118 if (!stream) {
870631a2 119 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream.");
3dca2276 120 goto error;
12c8a1a3 121 }
b71d7298 122
44c440bc 123 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
c6962c96
PP
124 stream->user_attributes = bt_value_map_create();
125 if (!stream->user_attributes) {
126 BT_LIB_LOGE_APPEND_CAUSE(
127 "Failed to create a map value object.");
128 goto error;
129 }
130
44c440bc
PP
131 stream->name.str = g_string_new(NULL);
132 if (!stream->name.str) {
870631a2 133 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
3dca2276 134 goto error;
b71d7298 135 }
41ac640a 136
44c440bc 137 stream->id = id;
312c056a
PP
138 ret = bt_object_pool_initialize(&stream->packet_pool,
139 (bt_object_pool_new_object_func) bt_packet_new,
140 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
141 stream);
142 if (ret) {
870631a2
PP
143 BT_LIB_LOGE_APPEND_CAUSE(
144 "Failed to initialize packet pool: ret=%d", ret);
312c056a
PP
145 goto error;
146 }
147
44c440bc 148 stream->class = stream_class;
6871026b 149 bt_object_get_ref_no_null_check(stream_class);
862ca4ed
PP
150
151 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
44c440bc 152 bt_trace_add_stream(trace, stream);
862ca4ed 153
44c440bc
PP
154 bt_stream_class_freeze(stream_class);
155 BT_LIB_LOGD("Created stream object: %!+s", stream);
3dca2276 156 goto end;
3230ee6b 157
3dca2276 158error:
65300d60 159 BT_OBJECT_PUT_REF_AND_RESET(stream);
3dca2276
PP
160
161end:
162 return stream;
273b65be
JG
163}
164
862ca4ed
PP
165struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
166 struct bt_trace *trace)
273b65be 167{
44c440bc
PP
168 uint64_t id;
169
17f3083a 170 BT_ASSERT_PRE_NO_ERROR();
44c440bc 171 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
862ca4ed 172 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
173 BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id,
174 "Stream class does not automatically assigns stream IDs: "
175 "%![sc-]+S", stream_class);
862ca4ed
PP
176 id = bt_trace_get_automatic_stream_id(trace, stream_class);
177 return create_stream_with_id(stream_class, trace, id);
44c440bc 178}
d7b1ea66 179
40f4ba76 180struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
862ca4ed 181 struct bt_trace *trace, uint64_t id)
44c440bc 182{
17f3083a 183 BT_ASSERT_PRE_NO_ERROR();
862ca4ed
PP
184 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
185 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
186 BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id,
187 "Stream class automatically assigns stream IDs: "
188 "%![sc-]+S", stream_class);
862ca4ed 189 return create_stream_with_id(stream_class, trace, id);
273b65be 190}
b71d7298 191
094ff7c0 192struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
af9296f3 193{
bdb288b3 194 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream");
44c440bc 195 return stream->class;
af9296f3
JG
196}
197
40f4ba76
PP
198const struct bt_stream_class *bt_stream_borrow_class_const(
199 const struct bt_stream *stream)
e5be10ef 200{
40f4ba76 201 return bt_stream_borrow_class((void *) stream);
e5be10ef
PP
202}
203
862ca4ed
PP
204struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream)
205{
bdb288b3 206 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream");
862ca4ed
PP
207 return bt_stream_borrow_trace_inline(stream);
208}
209
210const struct bt_trace *bt_stream_borrow_trace_const(
211 const struct bt_stream *stream)
212{
213 return bt_stream_borrow_trace((void *) stream);
214}
215
40f4ba76 216const char *bt_stream_get_name(const struct bt_stream *stream)
b71d7298 217{
bdb288b3 218 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream");
44c440bc 219 return stream->name.value;
b71d7298 220}
98a4cbef 221
d24d5663 222enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream,
3dcff82b 223 const char *name)
98a4cbef 224{
17f3083a 225 BT_ASSERT_PRE_NO_ERROR();
28ee7eed 226 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
44c440bc 227 BT_ASSERT_PRE_NON_NULL(name, "Name");
bdb288b3 228 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
44c440bc
PP
229 g_string_assign(stream->name.str, name);
230 stream->name.value = stream->name.str->str;
3f7d4d90 231 BT_LIB_LOGD("Set stream's name: %!+s", stream);
d24d5663 232 return BT_FUNC_STATUS_OK;
44c440bc 233}
cb6f1f7d 234
40f4ba76 235uint64_t bt_stream_get_id(const struct bt_stream *stream)
44c440bc 236{
bdb288b3 237 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream class");
44c440bc
PP
238 return stream->id;
239}
cb6f1f7d 240
44c440bc 241BT_HIDDEN
40f4ba76 242void _bt_stream_freeze(const struct bt_stream *stream)
44c440bc 243{
44c440bc 244 BT_ASSERT(stream);
c6962c96
PP
245 BT_LIB_LOGD("Freezing stream's user attributes: %!+v",
246 stream->user_attributes);
247 bt_value_freeze(stream->user_attributes);
44c440bc 248 BT_LIB_LOGD("Freezing stream: %!+s", stream);
40f4ba76 249 ((struct bt_stream *) stream)->frozen = true;
98a4cbef 250}
c5b9b441 251
c6962c96
PP
252const struct bt_value *bt_stream_borrow_user_attributes_const(
253 const struct bt_stream *stream)
254{
255 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream");
256 return stream->user_attributes;
257}
258
259struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream)
260{
261 return (void *) bt_stream_borrow_user_attributes_const((void *) stream);
262}
263
264void bt_stream_set_user_attributes(struct bt_stream *stream,
265 const struct bt_value *user_attributes)
266{
267 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
268 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
269 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
270 "User attributes object is not a map value object.");
271 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
6871026b 272 bt_object_put_ref_no_null_check(stream->user_attributes);
c6962c96 273 stream->user_attributes = (void *) user_attributes;
6871026b 274 bt_object_get_ref_no_null_check(stream->user_attributes);
c6962c96
PP
275}
276
c5b9b441
PP
277void bt_stream_get_ref(const struct bt_stream *stream)
278{
279 bt_object_get_ref(stream);
280}
281
282void bt_stream_put_ref(const struct bt_stream *stream)
283{
284 bt_object_put_ref(stream);
285}
This page took 0.117109 seconds and 4 git commands to generate.