lib: make plugin API const-correct
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
47e5a032 2 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
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
5af447e5
PP
24#define BT_LOG_TAG "NOTIF-ITER"
25#include <babeltrace/lib-logging-internal.h>
26
3d9990ac 27#include <babeltrace/compiler-internal.h>
65300d60 28#include <babeltrace/object.h>
56e18c4c 29#include <babeltrace/trace-ir/fields.h>
40f4ba76 30#include <babeltrace/trace-ir/event-const.h>
56e18c4c 31#include <babeltrace/trace-ir/event-internal.h>
40f4ba76 32#include <babeltrace/trace-ir/packet-const.h>
56e18c4c
PP
33#include <babeltrace/trace-ir/packet-internal.h>
34#include <babeltrace/trace-ir/stream-internal.h>
73d5c1ad 35#include <babeltrace/graph/connection.h>
bd14d768 36#include <babeltrace/graph/connection-internal.h>
b2e0c907 37#include <babeltrace/graph/component.h>
e5be10ef 38#include <babeltrace/graph/component-internal.h>
b2e0c907
PP
39#include <babeltrace/graph/component-source-internal.h>
40#include <babeltrace/graph/component-class-internal.h>
8ed535b5
PP
41#include <babeltrace/graph/component-class-sink-colander-internal.h>
42#include <babeltrace/graph/component-sink.h>
fa054faf 43#include <babeltrace/graph/notification.h>
b2e0c907
PP
44#include <babeltrace/graph/notification-iterator.h>
45#include <babeltrace/graph/notification-iterator-internal.h>
d94d92ac
PP
46#include <babeltrace/graph/self-component-port-input-notification-iterator.h>
47#include <babeltrace/graph/port-output-notification-iterator.h>
e7fa96c3 48#include <babeltrace/graph/notification-internal.h>
3230ee6b
PP
49#include <babeltrace/graph/notification-event.h>
50#include <babeltrace/graph/notification-event-internal.h>
51#include <babeltrace/graph/notification-packet.h>
52#include <babeltrace/graph/notification-packet-internal.h>
53#include <babeltrace/graph/notification-stream.h>
54#include <babeltrace/graph/notification-stream-internal.h>
55#include <babeltrace/graph/port.h>
a2d06fd5 56#include <babeltrace/graph/private-graph.h>
8ed535b5 57#include <babeltrace/graph/graph-internal.h>
c55a9f58 58#include <babeltrace/types.h>
f6ccaed9 59#include <babeltrace/assert-internal.h>
f42867e2 60#include <babeltrace/assert-pre-internal.h>
fa054faf 61#include <stdint.h>
2ec84d26 62#include <inttypes.h>
0fbb9a9f 63#include <stdlib.h>
3230ee6b 64
d4393e08
PP
65/*
66 * TODO: Use graph's state (number of active iterators, etc.) and
67 * possibly system specifications to make a better guess than this.
68 */
69#define NOTIF_BATCH_SIZE 15
70
3230ee6b 71struct stream_state {
40f4ba76
PP
72 const struct bt_stream *stream; /* owned by this */
73 const struct bt_packet *cur_packet; /* owned by this */
f42867e2 74 uint64_t expected_notif_seq_num;
c55a9f58 75 bt_bool is_ended;
3230ee6b
PP
76};
77
26e21a82 78BT_ASSERT_PRE_FUNC
3230ee6b
PP
79static
80void destroy_stream_state(struct stream_state *stream_state)
81{
82 if (!stream_state) {
83 return;
84 }
85
5af447e5
PP
86 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
87 BT_LOGV_STR("Putting stream state's current packet.");
d94d92ac 88 BT_OBJECT_PUT_REF_AND_RESET(stream_state->cur_packet);
5af447e5 89 BT_LOGV_STR("Putting stream state's stream.");
d94d92ac 90 BT_OBJECT_PUT_REF_AND_RESET(stream_state->stream);
3230ee6b
PP
91 g_free(stream_state);
92}
93
26e21a82 94BT_ASSERT_PRE_FUNC
3230ee6b 95static
40f4ba76 96struct stream_state *create_stream_state(const struct bt_stream *stream)
3230ee6b
PP
97{
98 struct stream_state *stream_state = g_new0(struct stream_state, 1);
99
100 if (!stream_state) {
5af447e5 101 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
102 goto end;
103 }
104
105 /*
f42867e2 106 * We keep a reference to the stream until we know it's ended.
3230ee6b 107 */
398454ed
PP
108 stream_state->stream = stream;
109 bt_object_get_no_null_check(stream_state->stream);
d94d92ac 110 BT_LIB_LOGV("Created stream state: %![stream-]+s, "
5af447e5 111 "stream-state-addr=%p",
d94d92ac 112 stream, stream_state);
3230ee6b
PP
113
114end:
115 return stream_state;
116}
47e5a032 117
8ed535b5
PP
118static
119void destroy_base_notification_iterator(struct bt_object *obj)
120{
d4393e08
PP
121 struct bt_notification_iterator *iterator = (void *) obj;
122
123 BT_ASSERT(iterator);
124
125 if (iterator->notifs) {
126 g_ptr_array_free(iterator->notifs, TRUE);
d94d92ac 127 iterator->notifs = NULL;
d4393e08
PP
128 }
129
130 g_free(iterator);
8ed535b5
PP
131}
132
47e5a032 133static
d94d92ac 134void bt_self_component_port_input_notification_iterator_destroy(struct bt_object *obj)
47e5a032 135{
d94d92ac 136 struct bt_self_component_port_input_notification_iterator *iterator;
8738a040 137
f6ccaed9 138 BT_ASSERT(obj);
d3eb6e8f 139
bd14d768
PP
140 /*
141 * The notification iterator's reference count is 0 if we're
142 * here. Increment it to avoid a double-destroy (possibly
143 * infinitely recursive). This could happen for example if the
d94d92ac
PP
144 * notification iterator's finalization function does
145 * bt_object_get_ref() (or anything that causes
146 * bt_object_get_ref() to be called) on itself (ref. count goes
147 * from 0 to 1), and then bt_object_put_ref(): the reference
148 * count would go from 1 to 0 again and this function would be
149 * called again.
bd14d768 150 */
3fea54f6 151 obj->ref_count++;
07245ac2 152 iterator = (void *) obj;
d94d92ac
PP
153 BT_LIB_LOGD("Destroying self component input port notification iterator object: "
154 "%!+i", iterator);
155 bt_self_component_port_input_notification_iterator_finalize(iterator);
d3eb6e8f 156
3230ee6b
PP
157 if (iterator->stream_states) {
158 /*
159 * Remove our destroy listener from each stream which
160 * has a state in this iterator. Otherwise the destroy
161 * listener would be called with an invalid/other
162 * notification iterator object.
163 */
3230ee6b 164 g_hash_table_destroy(iterator->stream_states);
d94d92ac 165 iterator->stream_states = NULL;
3230ee6b
PP
166 }
167
bd14d768
PP
168 if (iterator->connection) {
169 /*
170 * Remove ourself from the originating connection so
171 * that it does not try to finalize a dangling pointer
172 * later.
173 */
174 bt_connection_remove_iterator(iterator->connection, iterator);
d94d92ac 175 iterator->connection = NULL;
bd14d768
PP
176 }
177
8ed535b5 178 destroy_base_notification_iterator(obj);
47e5a032
JG
179}
180
bd14d768 181BT_HIDDEN
d94d92ac
PP
182void bt_self_component_port_input_notification_iterator_finalize(
183 struct bt_self_component_port_input_notification_iterator *iterator)
bd14d768 184{
d94d92ac
PP
185 typedef void (*method_t)(void *);
186
bd14d768 187 struct bt_component_class *comp_class = NULL;
d94d92ac 188 method_t method = NULL;
bd14d768 189
f6ccaed9 190 BT_ASSERT(iterator);
bd14d768
PP
191
192 switch (iterator->state) {
d94d92ac 193 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
088d4023 194 /* Skip user finalization if user initialization failed */
d94d92ac
PP
195 BT_LIB_LOGD("Not finalizing non-initialized notification iterator: "
196 "%!+i", iterator);
088d4023 197 return;
d94d92ac
PP
198 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
199 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
bd14d768 200 /* Already finalized */
d94d92ac
PP
201 BT_LIB_LOGD("Not finalizing notification iterator: already finalized: "
202 "%!+i", iterator);
bd14d768
PP
203 return;
204 default:
205 break;
206 }
207
d94d92ac 208 BT_LIB_LOGD("Finalizing notification iterator: %!+i", iterator);
5af447e5 209
d94d92ac
PP
210 if (iterator->state == BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ENDED) {
211 BT_LIB_LOGD("Updating notification iterator's state: "
212 "new-state=BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
213 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
df14f8af 214 } else {
d94d92ac
PP
215 BT_LIB_LOGD("Updating notification iterator's state: "
216 "new-state=BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED");
217 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED;
df14f8af
MD
218 }
219
f6ccaed9 220 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
221 comp_class = iterator->upstream_component->class;
222
223 /* Call user-defined destroy method */
224 switch (comp_class->type) {
225 case BT_COMPONENT_CLASS_TYPE_SOURCE:
226 {
d94d92ac
PP
227 struct bt_component_class_source *src_comp_cls =
228 (void *) comp_class;
bd14d768 229
d94d92ac 230 method = (method_t) src_comp_cls->methods.notif_iter_finalize;
bd14d768
PP
231 break;
232 }
233 case BT_COMPONENT_CLASS_TYPE_FILTER:
234 {
d94d92ac
PP
235 struct bt_component_class_filter *flt_comp_cls =
236 (void *) comp_class;
bd14d768 237
d94d92ac 238 method = (method_t) flt_comp_cls->methods.notif_iter_finalize;
bd14d768
PP
239 break;
240 }
241 default:
242 /* Unreachable */
0fbb9a9f 243 abort();
bd14d768
PP
244 }
245
d94d92ac
PP
246 if (method) {
247 BT_LIB_LOGD("Calling user's finalization method: %!+i",
5af447e5 248 iterator);
d94d92ac 249 method(iterator);
bd14d768
PP
250 }
251
bd14d768
PP
252 iterator->upstream_component = NULL;
253 iterator->upstream_port = NULL;
d94d92ac 254 BT_LIB_LOGD("Finalized notification iterator: %!+i", iterator);
bd14d768
PP
255}
256
257BT_HIDDEN
d94d92ac
PP
258void bt_self_component_port_input_notification_iterator_set_connection(
259 struct bt_self_component_port_input_notification_iterator *iterator,
bd14d768
PP
260 struct bt_connection *connection)
261{
f6ccaed9 262 BT_ASSERT(iterator);
bd14d768 263 iterator->connection = connection;
d94d92ac
PP
264 BT_LIB_LOGV("Set notification iterator's connection: "
265 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
266}
267
90157d89 268static
d4393e08 269int init_notification_iterator(struct bt_notification_iterator *iterator,
90157d89
PP
270 enum bt_notification_iterator_type type,
271 bt_object_release_func destroy)
272{
d4393e08
PP
273 int ret = 0;
274
3fea54f6 275 bt_object_init_shared(&iterator->base, destroy);
90157d89 276 iterator->type = type;
d4393e08
PP
277 iterator->notifs = g_ptr_array_new();
278 if (!iterator->notifs) {
279 BT_LOGE_STR("Failed to allocate a GPtrArray.");
280 ret = -1;
281 goto end;
282 }
283
284 g_ptr_array_set_size(iterator->notifs, NOTIF_BATCH_SIZE);
285
286end:
287 return ret;
90157d89
PP
288}
289
d94d92ac
PP
290static
291struct bt_self_component_port_input_notification_iterator *
292bt_self_component_port_input_notification_iterator_create_initial(
3230ee6b 293 struct bt_component *upstream_comp,
d94d92ac 294 struct bt_port *upstream_port)
47e5a032 295{
d4393e08 296 int ret;
d94d92ac 297 struct bt_self_component_port_input_notification_iterator *iterator = NULL;
47e5a032 298
f6ccaed9
PP
299 BT_ASSERT(upstream_comp);
300 BT_ASSERT(upstream_port);
f6ccaed9 301 BT_ASSERT(bt_port_is_connected(upstream_port));
d94d92ac
PP
302 BT_LIB_LOGD("Creating initial notification iterator on self component input port: "
303 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
304 BT_ASSERT(bt_component_get_class_type(upstream_comp) ==
305 BT_COMPONENT_CLASS_TYPE_SOURCE ||
306 bt_component_get_class_type(upstream_comp) ==
307 BT_COMPONENT_CLASS_TYPE_FILTER);
308 iterator = g_new0(
309 struct bt_self_component_port_input_notification_iterator, 1);
47e5a032 310 if (!iterator) {
d94d92ac
PP
311 BT_LOGE_STR("Failed to allocate one self component input port "
312 "notification iterator.");
73d5c1ad 313 goto end;
47e5a032
JG
314 }
315
d4393e08 316 ret = init_notification_iterator((void *) iterator,
d94d92ac
PP
317 BT_NOTIFICATION_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
318 bt_self_component_port_input_notification_iterator_destroy);
d4393e08
PP
319 if (ret) {
320 /* init_notification_iterator() logs errors */
d94d92ac 321 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d4393e08
PP
322 goto end;
323 }
3230ee6b
PP
324
325 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
326 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
327 if (!iterator->stream_states) {
5af447e5 328 BT_LOGE_STR("Failed to allocate a GHashTable.");
d94d92ac 329 BT_OBJECT_PUT_REF_AND_RESET(iterator);
73d5c1ad 330 goto end;
3230ee6b
PP
331 }
332
bd14d768
PP
333 iterator->upstream_component = upstream_comp;
334 iterator->upstream_port = upstream_port;
d94d92ac 335 iterator->connection = iterator->upstream_port->connection;
5c563278 336 iterator->graph = bt_component_borrow_graph(upstream_comp);
d94d92ac
PP
337 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
338 BT_LIB_LOGD("Created initial notification iterator on self component input port: "
339 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
340 upstream_port, upstream_comp, iterator);
3230ee6b 341
47e5a032 342end:
d94d92ac 343 return iterator;
47e5a032
JG
344}
345
d94d92ac
PP
346struct bt_self_component_port_input_notification_iterator *
347bt_self_component_port_input_notification_iterator_create(
348 struct bt_self_component_port_input *self_port)
ea8d3e58 349{
d94d92ac
PP
350 typedef enum bt_self_notification_iterator_status (*init_method_t)(
351 void *, void *, void *);
352
353 init_method_t init_method = NULL;
354 struct bt_self_component_port_input_notification_iterator *iterator =
355 NULL;
356 struct bt_port *port = (void *) self_port;
357 struct bt_port *upstream_port;
358 struct bt_component *comp;
359 struct bt_component *upstream_comp;
360 struct bt_component_class *upstream_comp_cls;
361
362 BT_ASSERT_PRE_NON_NULL(port, "Port");
363 comp = bt_port_borrow_component(port);
364 BT_ASSERT_PRE(bt_port_is_connected(port),
365 "Port is not connected: %![port-]+p", port);
366 BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p",
367 port);
368 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp),
369 "Port's component's graph is canceled: "
370 "%![port-]+p, %![comp-]+c", port, comp);
371 BT_ASSERT(port->connection);
372 upstream_port = port->connection->upstream_port;
373 BT_ASSERT(upstream_port);
374 upstream_comp = bt_port_borrow_component(upstream_port);
375 BT_ASSERT(upstream_comp);
376 upstream_comp_cls = upstream_comp->class;
377 BT_ASSERT(upstream_comp->class->type ==
378 BT_COMPONENT_CLASS_TYPE_SOURCE ||
379 upstream_comp->class->type ==
380 BT_COMPONENT_CLASS_TYPE_FILTER);
381 iterator = bt_self_component_port_input_notification_iterator_create_initial(
382 upstream_comp, upstream_port);
383 if (!iterator) {
384 BT_LOGW_STR("Cannot create self component input port "
385 "notification iterator.");
386 goto end;
387 }
890882ef 388
d94d92ac
PP
389 switch (upstream_comp_cls->type) {
390 case BT_COMPONENT_CLASS_TYPE_SOURCE:
391 {
392 struct bt_component_class_source *src_comp_cls =
393 (void *) upstream_comp_cls;
394
395 init_method =
396 (init_method_t) src_comp_cls->methods.notif_iter_init;
397 break;
398 }
399 case BT_COMPONENT_CLASS_TYPE_FILTER:
400 {
401 struct bt_component_class_filter *flt_comp_cls =
402 (void *) upstream_comp_cls;
403
404 init_method =
405 (init_method_t) flt_comp_cls->methods.notif_iter_init;
406 break;
407 }
408 default:
409 /* Unreachable */
410 abort();
411 }
412
413 if (init_method) {
414 int iter_status;
415
416 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
417 iter_status = init_method(iterator, upstream_comp,
418 upstream_port);
419 BT_LOGD("User method returned: status=%s",
420 bt_notification_iterator_status_string(iter_status));
421 if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
422 BT_LOGW_STR("Initialization method failed.");
423 goto end;
424 }
425 }
426
427 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE;
428 g_ptr_array_add(port->connection->iterators, iterator);
429 BT_LIB_LOGD("Created notification iterator on self component input port: "
430 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
431 upstream_port, upstream_comp, iterator);
432
433end:
434 return iterator;
ea8d3e58
JG
435}
436
d94d92ac
PP
437void *bt_self_notification_iterator_get_data(
438 struct bt_self_notification_iterator *self_iterator)
ea8d3e58 439{
d94d92ac
PP
440 struct bt_self_component_port_input_notification_iterator *iterator =
441 (void *) self_iterator;
ea8d3e58 442
f42867e2 443 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
d94d92ac 444 return iterator->user_data;
8738a040 445}
413bc2c4 446
d94d92ac
PP
447void bt_self_notification_iterator_set_data(
448 struct bt_self_notification_iterator *self_iterator, void *data)
5c563278 449{
d94d92ac
PP
450 struct bt_self_component_port_input_notification_iterator *iterator =
451 (void *) self_iterator;
5c563278
PP
452
453 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
d94d92ac
PP
454 iterator->user_data = data;
455 BT_LIB_LOGV("Set notification iterator's user data: "
456 "%!+i, user-data-addr=%p", iterator, data);
5c563278
PP
457}
458
f42867e2
PP
459BT_ASSERT_PRE_FUNC
460static inline
461void bt_notification_borrow_packet_stream(struct bt_notification *notif,
40f4ba76
PP
462 const struct bt_stream **stream,
463 const struct bt_packet **packet)
fa054faf 464{
f42867e2 465 BT_ASSERT(notif);
fa054faf 466
f42867e2 467 switch (notif->type) {
fa054faf 468 case BT_NOTIFICATION_TYPE_EVENT:
40f4ba76 469 *packet = bt_event_borrow_packet_const(
f42867e2 470 bt_notification_event_borrow_event(notif));
40f4ba76 471 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf
PP
472 break;
473 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
f42867e2 474 *stream = bt_notification_stream_begin_borrow_stream(notif);
fa054faf
PP
475 break;
476 case BT_NOTIFICATION_TYPE_STREAM_END:
f42867e2 477 *stream = bt_notification_stream_end_borrow_stream(notif);
fa054faf
PP
478 break;
479 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
f42867e2 480 *packet = bt_notification_packet_begin_borrow_packet(notif);
40f4ba76 481 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf
PP
482 break;
483 case BT_NOTIFICATION_TYPE_PACKET_END:
f42867e2 484 *packet = bt_notification_packet_end_borrow_packet(notif);
40f4ba76 485 *stream = bt_packet_borrow_stream_const(*packet);
2ec84d26 486 break;
fa054faf 487 default:
f42867e2 488 break;
fa054faf 489 }
fa054faf
PP
490}
491
f42867e2
PP
492BT_ASSERT_PRE_FUNC
493static inline
494bool validate_notification(
d94d92ac 495 struct bt_self_component_port_input_notification_iterator *iterator,
f42867e2 496 struct bt_notification *notif)
3230ee6b 497{
f42867e2 498 bool is_valid = true;
3230ee6b 499 struct stream_state *stream_state;
40f4ba76
PP
500 const struct bt_stream *stream = NULL;
501 const struct bt_packet *packet = NULL;
f42867e2
PP
502
503 BT_ASSERT(notif);
504 bt_notification_borrow_packet_stream(notif, &stream, &packet);
505
506 if (!stream) {
507 /* we don't care about notifications not attached to streams */
508 goto end;
509 }
3230ee6b 510
f42867e2
PP
511 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
512 if (!stream_state) {
3230ee6b 513 /*
f42867e2
PP
514 * No stream state for this stream: this notification
515 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
516 * and its sequence number must be 0.
3230ee6b 517 */
f42867e2
PP
518 if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
519 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
520 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
521 "notification prior to this notification: "
522 "%![stream-]+s", stream);
523 is_valid = false;
3230ee6b
PP
524 goto end;
525 }
526
f42867e2
PP
527 if (notif->seq_num == -1ULL) {
528 notif->seq_num = 0;
3230ee6b
PP
529 }
530
f42867e2
PP
531 if (notif->seq_num != 0) {
532 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
533 "number for this notification iterator: "
534 "this is the first notification for this "
535 "stream, expecting sequence number 0: "
536 "seq-num=%" PRIu64 ", %![stream-]+s",
537 notif->seq_num, stream);
538 is_valid = false;
3230ee6b 539 goto end;
3230ee6b 540 }
3230ee6b 541
f42867e2
PP
542 stream_state = create_stream_state(stream);
543 if (!stream_state) {
544 abort();
545 }
fa054faf 546
40f4ba76
PP
547 g_hash_table_insert(iterator->stream_states,
548 (void *) stream, stream_state);
f42867e2
PP
549 stream_state->expected_notif_seq_num++;
550 goto end;
fa054faf
PP
551 }
552
f42867e2
PP
553 if (stream_state->is_ended) {
554 /*
555 * There's a new notification which has a reference to a
556 * stream which, from this iterator's point of view, is
557 * ended ("end of stream" notification was returned).
558 * This is bad: the API guarantees that it can never
559 * happen.
560 */
561 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
562 stream);
563 is_valid = false;
fa054faf
PP
564 goto end;
565 }
566
f42867e2
PP
567 if (notif->seq_num == -1ULL) {
568 notif->seq_num = stream_state->expected_notif_seq_num;
3230ee6b
PP
569 }
570
f42867e2
PP
571 if (notif->seq_num != -1ULL &&
572 notif->seq_num != stream_state->expected_notif_seq_num) {
573 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
574 "seq-num=%" PRIu64 ", "
575 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
576 notif->seq_num, stream_state->expected_notif_seq_num,
577 stream);
578 is_valid = false;
fa054faf
PP
579 goto end;
580 }
581
f42867e2
PP
582 switch (notif->type) {
583 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
584 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
585 "notification at this point: notif-seq-num=%" PRIu64 ", "
586 "%![stream-]+s", notif->seq_num, stream);
587 is_valid = false;
588 goto end;
589 case BT_NOTIFICATION_TYPE_STREAM_END:
590 if (stream_state->cur_packet) {
591 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
592 "notification: missing a "
593 "BT_NOTIFICATION_TYPE_PACKET_END notification "
594 "prior to this notification: "
595 "notif-seq-num=%" PRIu64 ", "
596 "%![stream-]+s", notif->seq_num, stream);
597 is_valid = false;
598 goto end;
599 }
600 stream_state->expected_notif_seq_num++;
601 stream_state->is_ended = true;
602 goto end;
603 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
604 if (stream_state->cur_packet) {
605 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
606 "notification at this point: missing a "
607 "BT_NOTIFICATION_TYPE_PACKET_END notification "
608 "prior to this notification: "
609 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
610 "%![packet-]+a", notif->seq_num, stream,
611 packet);
612 is_valid = false;
613 goto end;
614 }
615 stream_state->expected_notif_seq_num++;
398454ed
PP
616 stream_state->cur_packet = packet;
617 bt_object_get_no_null_check(stream_state->cur_packet);
f42867e2
PP
618 goto end;
619 case BT_NOTIFICATION_TYPE_PACKET_END:
620 if (!stream_state->cur_packet) {
621 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
622 "notification at this point: missing a "
623 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
624 "prior to this notification: "
625 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
626 "%![packet-]+a", notif->seq_num, stream,
627 packet);
628 is_valid = false;
629 goto end;
630 }
631 stream_state->expected_notif_seq_num++;
65300d60 632 BT_OBJECT_PUT_REF_AND_RESET(stream_state->cur_packet);
f42867e2
PP
633 goto end;
634 case BT_NOTIFICATION_TYPE_EVENT:
635 if (packet != stream_state->cur_packet) {
636 BT_ASSERT_PRE_MSG("Unexpected packet for "
637 "BT_NOTIFICATION_TYPE_EVENT notification: "
638 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
639 "%![notif-packet-]+a, %![expected-packet-]+a",
640 notif->seq_num, stream,
641 stream_state->cur_packet, packet);
642 is_valid = false;
643 goto end;
644 }
645 stream_state->expected_notif_seq_num++;
646 goto end;
647 default:
648 break;
3230ee6b
PP
649 }
650
3230ee6b 651end:
f42867e2 652 return is_valid;
3230ee6b
PP
653}
654
d4393e08
PP
655BT_ASSERT_PRE_FUNC
656static inline
657bool validate_notifications(
d94d92ac 658 struct bt_self_component_port_input_notification_iterator *iterator,
d4393e08
PP
659 uint64_t count)
660{
661 bool ret = true;
662 bt_notification_array notifs = (void *) iterator->base.notifs->pdata;
663 uint64_t i;
664
665 for (i = 0; i < count; i++) {
666 ret = validate_notification(iterator, notifs[i]);
667 if (!ret) {
668 break;
669 }
670 }
671
672 return ret;
673}
674
f42867e2
PP
675BT_ASSERT_PRE_FUNC
676static inline bool priv_conn_notif_iter_can_end(
d94d92ac 677 struct bt_self_component_port_input_notification_iterator *iterator)
3230ee6b 678{
f42867e2
PP
679 GHashTableIter iter;
680 gpointer stream_key, state_value;
681 bool ret = true;
3230ee6b 682
f42867e2
PP
683 /*
684 * Verify that this iterator received a
685 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
686 * which has a state.
687 */
3230ee6b 688
f42867e2 689 g_hash_table_iter_init(&iter, iterator->stream_states);
3230ee6b 690
f42867e2
PP
691 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
692 struct stream_state *stream_state = (void *) state_value;
3230ee6b 693
f42867e2
PP
694 BT_ASSERT(stream_state);
695 BT_ASSERT(stream_key);
fa054faf 696
f42867e2
PP
697 if (!stream_state->is_ended) {
698 BT_ASSERT_PRE_MSG("Ending notification iterator, "
699 "but stream is not ended: "
700 "%![stream-]s", stream_key);
701 ret = false;
702 goto end;
703 }
3230ee6b
PP
704 }
705
3230ee6b 706end:
3230ee6b
PP
707 return ret;
708}
709
f42867e2 710enum bt_notification_iterator_status
d94d92ac
PP
711bt_self_component_port_input_notification_iterator_next(
712 struct bt_self_component_port_input_notification_iterator *iterator,
713 bt_notification_array *notifs, uint64_t *user_count)
3230ee6b 714{
d94d92ac
PP
715 typedef enum bt_self_notification_iterator_status (*method_t)(
716 void *, bt_notification_array, uint64_t, uint64_t *);
717
718 method_t method = NULL;
719 struct bt_component_class *comp_cls;
720 int status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
721
722 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
723 BT_ASSERT_PRE_NON_NULL(notifs, "Notification array (output)");
724 BT_ASSERT_PRE_NON_NULL(user_count, "Notification count (output)");
f42867e2 725 BT_ASSERT_PRE(iterator->state ==
d94d92ac 726 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE,
f42867e2
PP
727 "Notification iterator's \"next\" called, but "
728 "iterator is in the wrong state: %!+i", iterator);
729 BT_ASSERT(iterator->upstream_component);
730 BT_ASSERT(iterator->upstream_component->class);
d94d92ac
PP
731 BT_LIB_LOGD("Getting next self component input port "
732 "notification iterator's notifications: %!+i", iterator);
733 comp_cls = iterator->upstream_component->class;
d3eb6e8f 734
3230ee6b 735 /* Pick the appropriate "next" method */
d94d92ac 736 switch (comp_cls->type) {
d3eb6e8f
PP
737 case BT_COMPONENT_CLASS_TYPE_SOURCE:
738 {
d94d92ac
PP
739 struct bt_component_class_source *src_comp_cls =
740 (void *) comp_cls;
d3eb6e8f 741
d94d92ac 742 method = (method_t) src_comp_cls->methods.notif_iter_next;
d3eb6e8f
PP
743 break;
744 }
745 case BT_COMPONENT_CLASS_TYPE_FILTER:
746 {
d94d92ac
PP
747 struct bt_component_class_filter *flt_comp_cls =
748 (void *) comp_cls;
d3eb6e8f 749
d94d92ac 750 method = (method_t) flt_comp_cls->methods.notif_iter_next;
d3eb6e8f
PP
751 break;
752 }
753 default:
0fbb9a9f 754 abort();
d3eb6e8f
PP
755 }
756
3230ee6b 757 /*
d94d92ac 758 * Call the user's "next" method to get the next notifications
fa054faf 759 * and status.
3230ee6b 760 */
d94d92ac 761 BT_ASSERT(method);
f42867e2 762 BT_LOGD_STR("Calling user's \"next\" method.");
d94d92ac
PP
763 status = method(iterator,
764 (void *) iterator->base.notifs->pdata,
d4393e08 765 NOTIF_BATCH_SIZE, user_count);
f42867e2 766 BT_LOGD("User method returned: status=%s",
d4393e08
PP
767 bt_notification_iterator_status_string(status));
768 if (status < 0) {
f42867e2 769 BT_LOGW_STR("User method failed.");
f42867e2
PP
770 goto end;
771 }
3230ee6b 772
d94d92ac
PP
773 if (iterator->state == BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
774 iterator->state == BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
f42867e2
PP
775 /*
776 * The user's "next" method, somehow, cancelled its own
777 * notification iterator. This can happen, for example,
778 * when the user's method removes the port on which
779 * there's the connection from which the iterator was
780 * created. In this case, said connection is ended, and
781 * all its notification iterators are finalized.
782 *
65300d60 783 * Only bt_object_put_ref() the returned notification if
d94d92ac
PP
784 * the status is BT_NOTIFICATION_ITERATOR_STATUS_OK
785 * because otherwise this field could be garbage.
f42867e2 786 */
d4393e08
PP
787 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
788 uint64_t i;
789 bt_notification_array notifs =
d94d92ac 790 (void *) iterator->base.notifs->pdata;
d4393e08
PP
791
792 for (i = 0; i < *user_count; i++) {
65300d60 793 bt_object_put_ref(notifs[i]);
d4393e08 794 }
3230ee6b
PP
795 }
796
f42867e2
PP
797 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
798 goto end;
799 }
8cf27cc5 800
d4393e08
PP
801 switch (status) {
802 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
803 BT_ASSERT_PRE(validate_notifications(iterator, *user_count),
804 "Notifications are invalid at this point: "
805 "%![notif-iter-]+i, count=%" PRIu64,
806 iterator, *user_count);
d94d92ac 807 *notifs = (void *) iterator->base.notifs->pdata;
d4393e08
PP
808 break;
809 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
d4393e08 810 goto end;
f42867e2
PP
811 case BT_NOTIFICATION_ITERATOR_STATUS_END:
812 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
813 "Notification iterator cannot end at this point: "
814 "%!+i", iterator);
815 BT_ASSERT(iterator->state ==
d94d92ac
PP
816 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE);
817 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ENDED;
f42867e2
PP
818 BT_LOGD("Set new status: status=%s",
819 bt_notification_iterator_status_string(status));
820 goto end;
f42867e2
PP
821 default:
822 /* Unknown non-error status */
823 abort();
41a2b7ae
PP
824 }
825
826end:
3230ee6b
PP
827 return status;
828}
829
830enum bt_notification_iterator_status
d94d92ac
PP
831bt_port_output_notification_iterator_next(
832 struct bt_port_output_notification_iterator *iterator,
d4393e08
PP
833 bt_notification_array *notifs_to_user,
834 uint64_t *count_to_user)
3230ee6b
PP
835{
836 enum bt_notification_iterator_status status;
07245ac2 837 enum bt_graph_status graph_status;
3230ee6b 838
f42867e2 839 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
d94d92ac
PP
840 BT_ASSERT_PRE_NON_NULL(notifs_to_user, "Notification array (output)");
841 BT_ASSERT_PRE_NON_NULL(count_to_user, "Notification count (output)");
842 BT_LIB_LOGD("Getting next output port notification iterator's notifications: "
07245ac2 843 "%!+i", iterator);
d4393e08 844
d94d92ac
PP
845 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
846 iterator->colander);
07245ac2
PP
847 switch (graph_status) {
848 case BT_GRAPH_STATUS_CANCELED:
07245ac2 849 case BT_GRAPH_STATUS_AGAIN:
07245ac2 850 case BT_GRAPH_STATUS_END:
07245ac2 851 case BT_GRAPH_STATUS_NOMEM:
d94d92ac 852 status = (int) graph_status;
07245ac2
PP
853 break;
854 case BT_GRAPH_STATUS_OK:
07245ac2 855 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
d4393e08
PP
856
857 /*
858 * On success, the colander sink moves the notifications
859 * to this iterator's array and sets this iterator's
860 * notification count: move them to the user.
861 */
d94d92ac
PP
862 *notifs_to_user = (void *) iterator->base.notifs->pdata;
863 *count_to_user = iterator->count;
90157d89 864 break;
90157d89 865 default:
07245ac2
PP
866 /* Other errors */
867 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
90157d89 868 }
3230ee6b 869
3230ee6b 870 return status;
53d45b87
JG
871}
872
d94d92ac
PP
873struct bt_component *bt_self_component_port_input_notification_iterator_borrow_component(
874 struct bt_self_component_port_input_notification_iterator *iterator)
875{
876 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
877 return iterator->upstream_component;
878}
879
880struct bt_self_component *bt_self_notification_iterator_borrow_component(
881 struct bt_self_notification_iterator *self_iterator)
413bc2c4 882{
d94d92ac
PP
883 struct bt_self_component_port_input_notification_iterator *iterator =
884 (void *) self_iterator;
90157d89 885
f42867e2 886 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
d94d92ac 887 return (void *) iterator->upstream_component;
413bc2c4
JG
888}
889
d94d92ac
PP
890struct bt_self_port_output *bt_self_notification_iterator_borrow_port(
891 struct bt_self_notification_iterator *self_iterator)
91457551 892{
d94d92ac
PP
893 struct bt_self_component_port_input_notification_iterator *iterator =
894 (void *) self_iterator;
895
896 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
897 return (void *) iterator->upstream_port;
91457551 898}
8ed535b5
PP
899
900static
d94d92ac 901void bt_port_output_notification_iterator_destroy(struct bt_object *obj)
8ed535b5 902{
d94d92ac 903 struct bt_port_output_notification_iterator *iterator = (void *) obj;
8ed535b5 904
d94d92ac 905 BT_LIB_LOGD("Destroying output port notification iterator object: %!+i",
8ed535b5
PP
906 iterator);
907 BT_LOGD_STR("Putting graph.");
d94d92ac 908 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
8ed535b5 909 BT_LOGD_STR("Putting colander sink component.");
d94d92ac 910 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
8ed535b5
PP
911 destroy_base_notification_iterator(obj);
912}
913
d94d92ac
PP
914struct bt_port_output_notification_iterator *
915bt_port_output_notification_iterator_create(
916 struct bt_private_graph *priv_graph,
5fd91d88 917 struct bt_port_output *output_port)
8ed535b5 918{
d94d92ac
PP
919 struct bt_port_output_notification_iterator *iterator = NULL;
920 struct bt_component_class_sink *colander_comp_cls = NULL;
8ed535b5 921 struct bt_component *output_port_comp = NULL;
d94d92ac
PP
922 struct bt_component_sink *colander_comp;
923 struct bt_graph *graph = (void *) priv_graph;
8ed535b5 924 enum bt_graph_status graph_status;
d94d92ac 925 struct bt_port_input *colander_in_port = NULL;
8ed535b5 926 struct bt_component_class_sink_colander_data colander_data;
d4393e08 927 int ret;
8ed535b5 928
d94d92ac 929 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
f42867e2 930 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
d94d92ac 931 output_port_comp = bt_port_borrow_component((void *) output_port);
f42867e2
PP
932 BT_ASSERT_PRE(output_port_comp,
933 "Output port has no component: %!+p", output_port);
d94d92ac
PP
934 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
935 (void *) graph,
936 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
937 graph, output_port);
8ed535b5
PP
938
939 /* Create notification iterator */
d94d92ac
PP
940 BT_LIB_LOGD("Creating notification iterator on output port: "
941 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
942 iterator = g_new0(struct bt_port_output_notification_iterator, 1);
8ed535b5
PP
943 if (!iterator) {
944 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
945 goto error;
946 }
947
d4393e08 948 ret = init_notification_iterator((void *) iterator,
d94d92ac
PP
949 BT_NOTIFICATION_ITERATOR_TYPE_PORT_OUTPUT,
950 bt_port_output_notification_iterator_destroy);
d4393e08
PP
951 if (ret) {
952 /* init_notification_iterator() logs errors */
65300d60 953 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d4393e08
PP
954 goto end;
955 }
8ed535b5
PP
956
957 /* Create colander component */
958 colander_comp_cls = bt_component_class_sink_colander_get();
959 if (!colander_comp_cls) {
960 BT_LOGW("Cannot get colander sink component class.");
961 goto error;
962 }
963
398454ed
PP
964 iterator->graph = graph;
965 bt_object_get_no_null_check(iterator->graph);
d4393e08
PP
966 colander_data.notifs = (void *) iterator->base.notifs->pdata;
967 colander_data.count_addr = &iterator->count;
5fd91d88
PP
968
969 /* Hope that nobody uses this very unique name */
d94d92ac
PP
970 graph_status =
971 bt_private_graph_add_sink_component_with_init_method_data(
5fd91d88
PP
972 (void *) graph, colander_comp_cls,
973 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
d94d92ac 974 NULL, &colander_data, &iterator->colander);
8ed535b5 975 if (graph_status != BT_GRAPH_STATUS_OK) {
d94d92ac
PP
976 BT_LIB_LOGW("Cannot add colander sink component to graph: "
977 "%1[graph-]+g, status=%s", graph,
8ed535b5
PP
978 bt_graph_status_string(graph_status));
979 goto error;
980 }
981
982 /*
983 * Connect provided output port to the colander component's
984 * input port.
985 */
d94d92ac 986 colander_in_port = bt_component_sink_borrow_input_port_by_index(
8ed535b5 987 iterator->colander, 0);
f6ccaed9 988 BT_ASSERT(colander_in_port);
d94d92ac 989 graph_status = bt_private_graph_connect_ports(priv_graph,
8ed535b5
PP
990 output_port, colander_in_port, NULL);
991 if (graph_status != BT_GRAPH_STATUS_OK) {
d94d92ac
PP
992 BT_LIB_LOGW("Cannot add colander sink component to graph: "
993 "%![graph-]+g, %![comp-]+c, status=%s", graph,
994 iterator->colander,
8ed535b5
PP
995 bt_graph_status_string(graph_status));
996 goto error;
997 }
998
999 /*
1000 * At this point everything went fine. Make the graph
1001 * nonconsumable forever so that only this notification iterator
1002 * can consume (thanks to bt_graph_consume_sink_no_check()).
1003 * This avoids leaking the notification created by the colander
07245ac2
PP
1004 * sink and moved to the notification iterator's notification
1005 * member.
8ed535b5 1006 */
d94d92ac 1007 bt_graph_set_can_consume(iterator->graph, false);
8ed535b5
PP
1008 goto end;
1009
1010error:
1011 if (iterator && iterator->graph && iterator->colander) {
1012 int ret;
1013
1014 /* Remove created colander component from graph if any */
1015 colander_comp = iterator->colander;
65300d60 1016 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
8ed535b5
PP
1017
1018 /*
1019 * At this point the colander component's reference
1020 * count is 0 because iterator->colander was the only
1021 * owner. We also know that it is not connected because
1022 * this is the last operation before this function
1023 * succeeds.
1024 *
1025 * Since we honor the preconditions here,
1026 * bt_graph_remove_unconnected_component() always
1027 * succeeds.
1028 */
1029 ret = bt_graph_remove_unconnected_component(iterator->graph,
d94d92ac 1030 (void *) colander_comp);
f6ccaed9 1031 BT_ASSERT(ret == 0);
8ed535b5
PP
1032 }
1033
65300d60 1034 BT_OBJECT_PUT_REF_AND_RESET(iterator);
8ed535b5
PP
1035
1036end:
65300d60 1037 bt_object_put_ref(colander_comp_cls);
8ed535b5
PP
1038 return (void *) iterator;
1039}
This page took 0.092854 seconds and 4 git commands to generate.