Add graph cancellation API
[babeltrace.git] / plugins / utils / muxer / muxer.c
CommitLineData
958f7d11
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#include <babeltrace/babeltrace-internal.h>
24#include <babeltrace/ctf-ir/clock-class.h>
25#include <babeltrace/ctf-ir/event.h>
26#include <babeltrace/graph/clock-class-priority-map.h>
27#include <babeltrace/graph/component-filter.h>
28#include <babeltrace/graph/component.h>
29#include <babeltrace/graph/notification-event.h>
30#include <babeltrace/graph/notification-inactivity.h>
31#include <babeltrace/graph/notification-iterator.h>
32#include <babeltrace/graph/notification.h>
33#include <babeltrace/graph/port.h>
34#include <babeltrace/graph/private-component-filter.h>
35#include <babeltrace/graph/private-component.h>
36#include <babeltrace/graph/private-component.h>
37#include <babeltrace/graph/private-connection.h>
38#include <babeltrace/graph/private-notification-iterator.h>
39#include <babeltrace/graph/private-port.h>
40#include <plugins-common.h>
41#include <glib.h>
c55a9f58 42#include <stdbool.h>
958f7d11
PP
43#include <assert.h>
44
65ee897d
PP
45#define IGNORE_ABSOLUTE_PARAM_NAME "ignore-absolute"
46
958f7d11
PP
47struct muxer_comp {
48 /* Array of struct bt_private_notification_iterator * (weak refs) */
49 GPtrArray *muxer_notif_iters;
50
51 /* Weak ref */
52 struct bt_private_component *priv_comp;
53 unsigned int next_port_num;
54 size_t available_input_ports;
55 bool error;
a09c6b95 56 bool initializing_muxer_notif_iter;
65ee897d 57 bool ignore_absolute;
958f7d11
PP
58};
59
60struct muxer_upstream_notif_iter {
ab11110e 61 /* Owned by this, NULL if ended */
958f7d11
PP
62 struct bt_notification_iterator *notif_iter;
63
64 /* Owned by this*/
65 struct bt_private_port *priv_port;
089717de
PP
66
67 /*
68 * This flag is true if the upstream notification iterator's
69 * current notification must be considered for the multiplexing
70 * operations. If the upstream iterator returns
71 * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN, then this object
72 * is considered invalid, because its current notification is
73 * still the previous one, but we already took it into account.
74 *
75 * The value of this flag is not important if notif_iter above
76 * is NULL (which means the upstream iterator is finished).
77 */
78 bool is_valid;
958f7d11
PP
79};
80
958f7d11 81struct muxer_notif_iter {
ab11110e
PP
82 /*
83 * Array of struct muxer_upstream_notif_iter * (owned by this).
84 *
85 * NOTE: This array is searched in linearly to find the youngest
86 * current notification. Keep this until benchmarks confirm that
87 * another data structure is faster than this for our typical
88 * use cases.
89 */
958f7d11
PP
90 GPtrArray *muxer_upstream_notif_iters;
91
ab11110e
PP
92 /*
93 * List of "recently" connected input ports (owned by this) to
94 * handle by this muxer notification iterator.
95 * muxer_port_connected() adds entries to this list, and the
96 * entries are removed when a notification iterator is created
97 * on the port's connection and put into
98 * muxer_upstream_notif_iters above by
99 * muxer_notif_iter_handle_newly_connected_ports().
100 */
101 GList *newly_connected_priv_ports;
958f7d11
PP
102
103 /* Next thing to return by the "next" method */
104 struct bt_notification_iterator_next_return next_next_return;
958f7d11
PP
105
106 /* Last time returned in a notification */
107 int64_t last_returned_ts_ns;
108};
109
ab11110e
PP
110static
111void destroy_muxer_upstream_notif_iter(
112 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
113{
114 if (!muxer_upstream_notif_iter) {
115 return;
116 }
117
118 bt_put(muxer_upstream_notif_iter->notif_iter);
119 bt_put(muxer_upstream_notif_iter->priv_port);
120 g_free(muxer_upstream_notif_iter);
121}
122
958f7d11
PP
123static
124struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
125 struct muxer_notif_iter *muxer_notif_iter,
126 struct bt_notification_iterator *notif_iter,
127 struct bt_private_port *priv_port)
128{
129 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
130 g_new0(struct muxer_upstream_notif_iter, 1);
131
132 if (!muxer_upstream_notif_iter) {
133 goto end;
134 }
135
136 muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter);
137 muxer_upstream_notif_iter->priv_port = bt_get(priv_port);
089717de 138 muxer_upstream_notif_iter->is_valid = false;
958f7d11
PP
139 g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
140 muxer_upstream_notif_iter);
141
142end:
143 return muxer_upstream_notif_iter;
144}
145
958f7d11
PP
146static
147int ensure_available_input_port(struct bt_private_component *priv_comp)
148{
149 struct muxer_comp *muxer_comp =
150 bt_private_component_get_user_data(priv_comp);
151 int ret = 0;
152 GString *port_name = NULL;
153 void *priv_port = NULL;
154
155 assert(muxer_comp);
156
157 if (muxer_comp->available_input_ports >= 1) {
158 goto end;
159 }
160
161 port_name = g_string_new("in");
162 if (!port_name) {
163 ret = -1;
164 goto end;
165 }
166
167 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
168 priv_port = bt_private_component_filter_add_input_private_port(
3e9b0023 169 priv_comp, port_name->str, NULL);
958f7d11
PP
170 if (!priv_port) {
171 ret = -1;
172 goto end;
173 }
174
175 muxer_comp->available_input_ports++;
176 muxer_comp->next_port_num++;
177
178end:
179 if (port_name) {
180 g_string_free(port_name, TRUE);
181 }
182
183 BT_PUT(priv_port);
184 return ret;
185}
186
958f7d11
PP
187static
188int create_output_port(struct bt_private_component *priv_comp)
189{
190 void *priv_port;
191 int ret = 0;
192
193 priv_port = bt_private_component_filter_add_output_private_port(
3e9b0023 194 priv_comp, "out", NULL);
958f7d11
PP
195 if (!priv_port) {
196 ret = -1;
197 }
198
199 bt_put(priv_port);
200 return ret;
201}
202
203static
204void destroy_muxer_comp(struct muxer_comp *muxer_comp)
205{
206 if (!muxer_comp) {
207 return;
208 }
209
210 if (muxer_comp->muxer_notif_iters) {
211 g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE);
212 }
213
214 g_free(muxer_comp);
215}
216
65ee897d
PP
217static
218struct bt_value *get_default_params(void)
219{
220 struct bt_value *params;
221 int ret;
222
223 params = bt_value_map_create();
224 if (!params) {
225 goto error;
226 }
227
228 ret = bt_value_map_insert_bool(params, IGNORE_ABSOLUTE_PARAM_NAME,
229 false);
230 if (ret) {
231 goto error;
232 }
233
234 goto end;
235
236error:
237 BT_PUT(params);
238
239end:
240 return params;
241}
242
243static
244int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params)
245{
246 struct bt_value *default_params = NULL;
247 struct bt_value *real_params = NULL;
248 struct bt_value *ignore_absolute = NULL;
249 int ret = 0;
c55a9f58 250 bt_bool bool_val;
65ee897d
PP
251
252 default_params = get_default_params();
253 if (!default_params) {
254 goto error;
255 }
256
257 real_params = bt_value_map_extend(default_params, params);
258 if (!real_params) {
259 goto error;
260 }
261
262 ignore_absolute = bt_value_map_get(real_params,
263 IGNORE_ABSOLUTE_PARAM_NAME);
264 if (!bt_value_is_bool(ignore_absolute)) {
265 goto error;
266 }
267
c55a9f58 268 if (bt_value_bool_get(ignore_absolute, &bool_val)) {
65ee897d
PP
269 goto error;
270 }
271
c55a9f58
PP
272 muxer_comp->ignore_absolute = (bool) bool_val;
273
65ee897d
PP
274 goto end;
275
276error:
277 ret = -1;
278
279end:
280 bt_put(default_params);
281 bt_put(real_params);
282 bt_put(ignore_absolute);
283 return ret;
284}
285
958f7d11
PP
286BT_HIDDEN
287enum bt_component_status muxer_init(
288 struct bt_private_component *priv_comp,
289 struct bt_value *params, void *init_data)
290{
291 int ret;
292 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
293 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
294
295 if (!muxer_comp) {
296 goto error;
297 }
298
65ee897d
PP
299 ret = configure_muxer_comp(muxer_comp, params);
300 if (ret) {
301 goto error;
302 }
303
958f7d11
PP
304 muxer_comp->muxer_notif_iters = g_ptr_array_new();
305 if (!muxer_comp->muxer_notif_iters) {
306 goto error;
307 }
308
309 muxer_comp->priv_comp = priv_comp;
310 ret = bt_private_component_set_user_data(priv_comp, muxer_comp);
311 assert(ret == 0);
958f7d11
PP
312 ret = ensure_available_input_port(priv_comp);
313 if (ret) {
314 goto error;
315 }
316
317 ret = create_output_port(priv_comp);
318 if (ret) {
319 goto error;
320 }
321
322 goto end;
323
324error:
325 destroy_muxer_comp(muxer_comp);
326 ret = bt_private_component_set_user_data(priv_comp, NULL);
327 assert(ret == 0);
328 status = BT_COMPONENT_STATUS_ERROR;
329
330end:
331 return status;
332}
333
334BT_HIDDEN
335void muxer_finalize(struct bt_private_component *priv_comp)
336{
337 struct muxer_comp *muxer_comp =
338 bt_private_component_get_user_data(priv_comp);
339
340 destroy_muxer_comp(muxer_comp);
341}
342
343static
344struct bt_notification_iterator *create_notif_iter_on_input_port(
345 struct bt_private_port *priv_port, int *ret)
346{
347 struct bt_port *port = bt_port_from_private_port(priv_port);
348 struct bt_notification_iterator *notif_iter = NULL;
349 struct bt_private_connection *priv_conn = NULL;
350
351 assert(ret);
352 *ret = 0;
353 assert(port);
354
355 assert(bt_port_is_connected(port));
356 priv_conn = bt_private_port_get_private_connection(priv_port);
357 if (!priv_conn) {
358 *ret = -1;
359 goto end;
360 }
361
ab11110e
PP
362 // TODO: Advance the iterator to >= the time of the latest
363 // returned notification by the muxer notification
364 // iterator which creates it.
958f7d11 365 notif_iter = bt_private_connection_create_notification_iterator(
fa054faf 366 priv_conn, NULL);
958f7d11
PP
367 if (!notif_iter) {
368 *ret = -1;
369 goto end;
370 }
371
372end:
373 bt_put(port);
374 bt_put(priv_conn);
375 return notif_iter;
376}
377
ab11110e 378static
089717de 379enum bt_notification_iterator_status muxer_upstream_notif_iter_next(
ab11110e
PP
380 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
381{
089717de 382 enum bt_notification_iterator_status status;
ab11110e 383
089717de 384 status = bt_notification_iterator_next(
ab11110e
PP
385 muxer_upstream_notif_iter->notif_iter);
386
089717de 387 switch (status) {
ab11110e 388 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
089717de
PP
389 /*
390 * Notification iterator's current notification is valid:
391 * it must be considered for muxing operations.
392 */
393 muxer_upstream_notif_iter->is_valid = true;
ab11110e
PP
394 break;
395 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
089717de
PP
396 /*
397 * Notification iterator's current notification is not
398 * valid anymore. Return
399 * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
400 * immediately.
401 */
402 muxer_upstream_notif_iter->is_valid = false;
ab11110e
PP
403 break;
404 case BT_NOTIFICATION_ITERATOR_STATUS_END:
405 /*
406 * Notification iterator reached the end: release it. It
407 * won't be considered again to find the youngest
408 * notification.
409 */
410 BT_PUT(muxer_upstream_notif_iter->notif_iter);
089717de
PP
411 muxer_upstream_notif_iter->is_valid = false;
412 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
413 break;
ab11110e
PP
414 default:
415 /* Error or unsupported status code */
089717de
PP
416 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
417 break;
ab11110e
PP
418 }
419
089717de 420 return status;
ab11110e
PP
421}
422
423static
089717de 424int muxer_notif_iter_handle_newly_connected_ports(
ab11110e
PP
425 struct muxer_notif_iter *muxer_notif_iter)
426{
ab11110e
PP
427 int ret = 0;
428
ab11110e
PP
429 /*
430 * Here we create one upstream notification iterator for each
089717de
PP
431 * newly connected port. We do not perform an initial "next" on
432 * those new upstream notification iterators: they are
433 * invalidated, to be validated later. The list of newly
434 * connected ports to handle here is updated by
435 * muxer_port_connected().
ab11110e
PP
436 */
437 while (true) {
438 GList *node = muxer_notif_iter->newly_connected_priv_ports;
439 struct bt_private_port *priv_port;
440 struct bt_port *port;
441 struct bt_notification_iterator *upstream_notif_iter = NULL;
442 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
443
444 if (!node) {
445 break;
446 }
447
448 priv_port = node->data;
449 port = bt_port_from_private_port(priv_port);
450 assert(port);
451
452 if (!bt_port_is_connected(port)) {
453 /*
454 * Looks like this port is not connected
455 * anymore: we can't create an upstream
089717de
PP
456 * notification iterator on its (non-existing)
457 * connection in this case.
ab11110e
PP
458 */
459 goto remove_node;
460 }
461
462 BT_PUT(port);
463 upstream_notif_iter = create_notif_iter_on_input_port(priv_port,
464 &ret);
465 if (ret) {
466 assert(!upstream_notif_iter);
467 bt_put(priv_port);
468 goto error;
469 }
470
471 muxer_upstream_notif_iter =
472 muxer_notif_iter_add_upstream_notif_iter(
473 muxer_notif_iter, upstream_notif_iter,
474 priv_port);
475 BT_PUT(priv_port);
476 BT_PUT(upstream_notif_iter);
477 if (!muxer_upstream_notif_iter) {
478 goto error;
479 }
480
ab11110e
PP
481remove_node:
482 bt_put(upstream_notif_iter);
483 bt_put(port);
484 bt_put(priv_port);
485 muxer_notif_iter->newly_connected_priv_ports =
486 g_list_delete_link(
487 muxer_notif_iter->newly_connected_priv_ports,
488 node);
489 }
490
491 goto end;
492
493error:
089717de 494 if (ret >= 0) {
ab11110e
PP
495 ret = -1;
496 }
497
498end:
ab11110e
PP
499 return ret;
500}
501
958f7d11
PP
502static
503int get_notif_ts_ns(struct muxer_comp *muxer_comp,
504 struct bt_notification *notif, int64_t last_returned_ts_ns,
505 int64_t *ts_ns)
506{
507 struct bt_clock_class_priority_map *cc_prio_map = NULL;
508 struct bt_ctf_clock_class *clock_class = NULL;
509 struct bt_ctf_clock_value *clock_value = NULL;
510 struct bt_ctf_event *event = NULL;
511 int ret = 0;
512
513 assert(notif);
514 assert(ts_ns);
515
516 switch (bt_notification_get_type(notif)) {
517 case BT_NOTIFICATION_TYPE_EVENT:
518 cc_prio_map =
519 bt_notification_event_get_clock_class_priority_map(
520 notif);
521 break;
522
523 case BT_NOTIFICATION_TYPE_INACTIVITY:
524 cc_prio_map =
a09c6b95 525 bt_notification_inactivity_get_clock_class_priority_map(
958f7d11
PP
526 notif);
527 break;
528 default:
089717de 529 /* All the other notifications have a higher priority */
958f7d11
PP
530 *ts_ns = last_returned_ts_ns;
531 goto end;
532 }
533
534 if (!cc_prio_map) {
535 goto error;
536 }
537
538 /*
539 * If the clock class priority map is empty, then we consider
540 * that this notification has no time. In this case it's always
541 * the youngest.
542 */
543 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) {
544 *ts_ns = last_returned_ts_ns;
545 goto end;
546 }
547
548 clock_class =
549 bt_clock_class_priority_map_get_highest_priority_clock_class(
550 cc_prio_map);
551 if (!clock_class) {
552 goto error;
553 }
554
65ee897d
PP
555 if (!muxer_comp->ignore_absolute &&
556 !bt_ctf_clock_class_is_absolute(clock_class)) {
958f7d11
PP
557 goto error;
558 }
559
560 switch (bt_notification_get_type(notif)) {
561 case BT_NOTIFICATION_TYPE_EVENT:
562 event = bt_notification_event_get_event(notif);
ab11110e 563 assert(event);
958f7d11
PP
564 clock_value = bt_ctf_event_get_clock_value(event,
565 clock_class);
566 break;
567 case BT_NOTIFICATION_TYPE_INACTIVITY:
568 clock_value = bt_notification_inactivity_get_clock_value(
569 notif, clock_class);
570 break;
571 default:
572 assert(false);
573 }
574
575 if (!clock_value) {
576 goto error;
577 }
578
579 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ts_ns);
580 if (ret) {
581 goto error;
582 }
583
584 goto end;
585
586error:
587 ret = -1;
588
589end:
590 bt_put(cc_prio_map);
591 bt_put(event);
592 bt_put(clock_class);
593 bt_put(clock_value);
594 return ret;
595}
596
ab11110e
PP
597/*
598 * This function finds the youngest available notification amongst the
599 * non-ended upstream notification iterators and returns the upstream
600 * notification iterator which has it, or
601 * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available
602 * notification.
603 *
604 * This function does NOT:
605 *
606 * * Update any upstream notification iterator.
607 * * Check for newly connected ports.
608 * * Check the upstream notification iterators to retry.
609 *
610 * On sucess, this function sets *muxer_upstream_notif_iter to the
611 * upstream notification iterator of which the current notification is
612 * the youngest, and sets *ts_ns to its time.
613 */
958f7d11
PP
614static
615enum bt_notification_iterator_status
616muxer_notif_iter_youngest_upstream_notif_iter(
617 struct muxer_comp *muxer_comp,
618 struct muxer_notif_iter *muxer_notif_iter,
619 struct muxer_upstream_notif_iter **muxer_upstream_notif_iter,
620 int64_t *ts_ns)
621{
622 size_t i;
623 int ret;
624 int64_t youngest_ts_ns = INT64_MAX;
625 enum bt_notification_iterator_status status =
626 BT_NOTIFICATION_ITERATOR_STATUS_OK;
627
628 assert(muxer_comp);
629 assert(muxer_notif_iter);
630 assert(muxer_upstream_notif_iter);
631 *muxer_upstream_notif_iter = NULL;
632
633 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
634 struct bt_notification *notif;
635 struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter =
636 g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i);
637 int64_t notif_ts_ns;
638
639 if (!cur_muxer_upstream_notif_iter->notif_iter) {
ab11110e 640 /* This upstream notification iterator is ended */
958f7d11
PP
641 continue;
642 }
643
089717de 644 assert(cur_muxer_upstream_notif_iter->is_valid);
958f7d11
PP
645 notif = bt_notification_iterator_get_notification(
646 cur_muxer_upstream_notif_iter->notif_iter);
647 assert(notif);
648 ret = get_notif_ts_ns(muxer_comp, notif,
649 muxer_notif_iter->last_returned_ts_ns, &notif_ts_ns);
650 bt_put(notif);
651 if (ret) {
652 *muxer_upstream_notif_iter = NULL;
653 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
654 goto end;
655 }
656
657 if (notif_ts_ns <= youngest_ts_ns) {
658 *muxer_upstream_notif_iter =
659 cur_muxer_upstream_notif_iter;
660 youngest_ts_ns = notif_ts_ns;
661 *ts_ns = youngest_ts_ns;
662 }
663 }
664
665 if (!*muxer_upstream_notif_iter) {
666 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
667 *ts_ns = INT64_MIN;
668 }
669
670end:
671 return status;
672}
673
674static
089717de
PP
675enum bt_notification_iterator_status validate_muxer_upstream_notif_iter(
676 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
958f7d11 677{
089717de
PP
678 enum bt_notification_iterator_status status =
679 BT_NOTIFICATION_ITERATOR_STATUS_OK;
958f7d11 680
089717de
PP
681 if (muxer_upstream_notif_iter->is_valid ||
682 !muxer_upstream_notif_iter->notif_iter) {
ab11110e
PP
683 goto end;
684 }
685
089717de
PP
686 status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter);
687
688end:
689 return status;
690}
691
692static
693enum bt_notification_iterator_status validate_muxer_upstream_notif_iters(
694 struct muxer_notif_iter *muxer_notif_iter)
695{
696 enum bt_notification_iterator_status status =
697 BT_NOTIFICATION_ITERATOR_STATUS_OK;
698 size_t i;
699
700 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
701 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
702 g_ptr_array_index(
703 muxer_notif_iter->muxer_upstream_notif_iters,
704 i);
705
706 status = validate_muxer_upstream_notif_iter(
707 muxer_upstream_notif_iter);
708 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
709 goto end;
710 }
711 }
712
713end:
714 return status;
715}
716
717static
718struct bt_notification_iterator_next_return muxer_notif_iter_do_next(
719 struct muxer_comp *muxer_comp,
720 struct muxer_notif_iter *muxer_notif_iter)
721{
722 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL;
723 struct bt_notification_iterator_next_return next_return = {
724 .notification = NULL,
725 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
726 };
727 int64_t next_return_ts;
728
729 while (true) {
730 int ret = muxer_notif_iter_handle_newly_connected_ports(
731 muxer_notif_iter);
732
733 if (ret) {
734 next_return.status =
735 BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
736 goto end;
737 }
738
739 next_return.status =
740 validate_muxer_upstream_notif_iters(muxer_notif_iter);
741 if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
742 goto end;
743 }
ab11110e 744
958f7d11 745 /*
089717de
PP
746 * At this point, we know that all the existing upstream
747 * notification iterators are valid. However the
748 * operations to validate them (during
749 * validate_muxer_upstream_notif_iters()) may have
750 * connected new ports. If no ports were connected
751 * during this operation, exit the loop.
958f7d11 752 */
089717de
PP
753 if (!muxer_notif_iter->newly_connected_priv_ports) {
754 break;
755 }
958f7d11
PP
756 }
757
089717de
PP
758 assert(!muxer_notif_iter->newly_connected_priv_ports);
759
958f7d11 760 /*
089717de
PP
761 * At this point we know that all the existing upstream
762 * notification iterators are valid. We can find the one,
763 * amongst those, of which the current notification is the
764 * youngest.
958f7d11 765 */
089717de 766 next_return.status =
958f7d11
PP
767 muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp,
768 muxer_notif_iter, &muxer_upstream_notif_iter,
089717de
PP
769 &next_return_ts);
770 if (next_return.status < 0 ||
771 next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
958f7d11
PP
772 goto end;
773 }
774
089717de
PP
775 if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) {
776 next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
958f7d11
PP
777 goto end;
778 }
779
089717de
PP
780 assert(next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK);
781 assert(muxer_upstream_notif_iter);
782 next_return.notification = bt_notification_iterator_get_notification(
783 muxer_upstream_notif_iter->notif_iter);
784 assert(next_return.notification);
958f7d11
PP
785
786 /*
089717de
PP
787 * We invalidate the upstream notification iterator so that, the
788 * next time this function is called,
789 * validate_muxer_upstream_notif_iters() will make it valid.
958f7d11 790 */
089717de
PP
791 muxer_upstream_notif_iter->is_valid = false;
792 muxer_notif_iter->last_returned_ts_ns = next_return_ts;
958f7d11
PP
793
794end:
089717de 795 return next_return;
958f7d11
PP
796}
797
798static
ab11110e
PP
799void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
800{
801 GList *node;
802
803 if (!muxer_notif_iter) {
804 return;
805 }
806
807 if (muxer_notif_iter->muxer_upstream_notif_iters) {
808 g_ptr_array_free(
809 muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
810 }
811
ab11110e
PP
812 for (node = muxer_notif_iter->newly_connected_priv_ports;
813 node; node = g_list_next(node)) {
814 bt_put(node->data);
815 }
816
817 g_list_free(muxer_notif_iter->newly_connected_priv_ports);
818 g_free(muxer_notif_iter);
819}
820
821static
822int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp,
958f7d11
PP
823 struct muxer_notif_iter *muxer_notif_iter)
824{
ab11110e 825 struct bt_component *comp;
544d0515
PP
826 int64_t count;
827 int64_t i;
ab11110e 828 int ret = 0;
958f7d11 829
ab11110e
PP
830 /*
831 * Add the connected input ports to this muxer notification
832 * iterator's list of newly connected ports. They will be
833 * handled by muxer_notif_iter_handle_newly_connected_ports().
834 */
958f7d11
PP
835 comp = bt_component_from_private_component(muxer_comp->priv_comp);
836 assert(comp);
544d0515
PP
837 count = bt_component_filter_get_input_port_count(comp);
838 if (count < 0) {
ab11110e
PP
839 goto end;
840 }
958f7d11
PP
841
842 for (i = 0; i < count; i++) {
843 struct bt_private_port *priv_port =
9ac68eb1 844 bt_private_component_filter_get_input_private_port_by_index(
958f7d11
PP
845 muxer_comp->priv_comp, i);
846 struct bt_port *port;
958f7d11
PP
847
848 assert(priv_port);
958f7d11 849 port = bt_port_from_private_port(priv_port);
ab11110e 850 assert(port);
958f7d11
PP
851
852 if (!bt_port_is_connected(port)) {
958f7d11 853 bt_put(priv_port);
ab11110e 854 bt_put(port);
958f7d11
PP
855 continue;
856 }
857
858 bt_put(port);
ab11110e
PP
859 muxer_notif_iter->newly_connected_priv_ports =
860 g_list_append(
861 muxer_notif_iter->newly_connected_priv_ports,
958f7d11 862 priv_port);
ab11110e 863 if (!muxer_notif_iter->newly_connected_priv_ports) {
958f7d11 864 bt_put(priv_port);
ab11110e
PP
865 ret = -1;
866 goto end;
958f7d11 867 }
958f7d11
PP
868 }
869
870end:
871 bt_put(comp);
872 return ret;
873}
874
958f7d11
PP
875BT_HIDDEN
876enum bt_notification_iterator_status muxer_notif_iter_init(
877 struct bt_private_notification_iterator *priv_notif_iter,
878 struct bt_private_port *output_priv_port)
879{
880 struct muxer_comp *muxer_comp = NULL;
881 struct muxer_notif_iter *muxer_notif_iter = NULL;
882 struct bt_private_component *priv_comp = NULL;
883 enum bt_notification_iterator_status status =
884 BT_NOTIFICATION_ITERATOR_STATUS_OK;
885 int ret;
886
887 priv_comp = bt_private_notification_iterator_get_private_component(
888 priv_notif_iter);
889 assert(priv_comp);
890 muxer_comp = bt_private_component_get_user_data(priv_comp);
891 assert(muxer_comp);
a09c6b95
PP
892
893 if (muxer_comp->initializing_muxer_notif_iter) {
894 /*
089717de
PP
895 * Weird, unhandled situation detected: downstream
896 * creates a muxer notification iterator while creating
897 * another muxer notification iterator (same component).
a09c6b95 898 */
958f7d11
PP
899 goto error;
900 }
901
a09c6b95
PP
902 muxer_comp->initializing_muxer_notif_iter = true;
903 muxer_notif_iter = g_new0(struct muxer_notif_iter, 1);
904 if (!muxer_notif_iter) {
ab11110e
PP
905 goto error;
906 }
907
958f7d11
PP
908 muxer_notif_iter->last_returned_ts_ns = INT64_MIN;
909 muxer_notif_iter->muxer_upstream_notif_iters =
910 g_ptr_array_new_with_free_func(
911 (GDestroyNotify) destroy_muxer_upstream_notif_iter);
912 if (!muxer_notif_iter->muxer_upstream_notif_iters) {
913 goto error;
914 }
915
a09c6b95
PP
916 /*
917 * Add the muxer notification iterator to the component's array
918 * of muxer notification iterators here because
919 * muxer_notif_iter_init_newly_connected_ports() can cause
920 * muxer_port_connected() to be called, which adds the newly
921 * connected port to each muxer notification iterator's list of
922 * newly connected ports.
923 */
924 g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
925 ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp,
926 muxer_notif_iter);
927 if (ret) {
928 goto error;
929 }
930
958f7d11
PP
931 ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
932 muxer_notif_iter);
933 assert(ret == 0);
958f7d11
PP
934 goto end;
935
936error:
a09c6b95
PP
937 if (g_ptr_array_index(muxer_comp->muxer_notif_iters,
938 muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) {
939 g_ptr_array_remove_index(muxer_comp->muxer_notif_iters,
940 muxer_comp->muxer_notif_iters->len - 1);
941 }
942
958f7d11
PP
943 destroy_muxer_notif_iter(muxer_notif_iter);
944 ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
945 NULL);
946 assert(ret == 0);
947 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
948
949end:
a09c6b95 950 muxer_comp->initializing_muxer_notif_iter = false;
958f7d11
PP
951 bt_put(priv_comp);
952 return status;
953}
954
955BT_HIDDEN
956void muxer_notif_iter_finalize(
957 struct bt_private_notification_iterator *priv_notif_iter)
958{
959 struct muxer_notif_iter *muxer_notif_iter =
960 bt_private_notification_iterator_get_user_data(priv_notif_iter);
961 struct bt_private_component *priv_comp = NULL;
962 struct muxer_comp *muxer_comp = NULL;
963
964 priv_comp = bt_private_notification_iterator_get_private_component(
965 priv_notif_iter);
966 assert(priv_comp);
967 muxer_comp = bt_private_component_get_user_data(priv_comp);
968
969 if (muxer_comp) {
970 (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters,
971 muxer_notif_iter);
972 destroy_muxer_notif_iter(muxer_notif_iter);
973 }
974
975 bt_put(priv_comp);
976}
977
978BT_HIDDEN
979struct bt_notification_iterator_next_return muxer_notif_iter_next(
980 struct bt_private_notification_iterator *priv_notif_iter)
981{
089717de 982 struct bt_notification_iterator_next_return next_ret;
958f7d11
PP
983 struct muxer_notif_iter *muxer_notif_iter =
984 bt_private_notification_iterator_get_user_data(priv_notif_iter);
985 struct bt_private_component *priv_comp = NULL;
986 struct muxer_comp *muxer_comp = NULL;
958f7d11
PP
987
988 assert(muxer_notif_iter);
989 priv_comp = bt_private_notification_iterator_get_private_component(
990 priv_notif_iter);
991 assert(priv_comp);
992 muxer_comp = bt_private_component_get_user_data(priv_comp);
993 assert(muxer_comp);
994
995 /* Are we in an error state set elsewhere? */
996 if (unlikely(muxer_comp->error)) {
089717de
PP
997 next_ret.notification = NULL;
998 next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
999 goto end;
958f7d11
PP
1000 }
1001
089717de 1002 next_ret = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter);
958f7d11
PP
1003
1004end:
1005 bt_put(priv_comp);
1006 return next_ret;
1007}
1008
1009BT_HIDDEN
1010void muxer_port_connected(
1011 struct bt_private_component *priv_comp,
1012 struct bt_private_port *self_private_port,
1013 struct bt_port *other_port)
1014{
1015 struct bt_port *self_port =
1016 bt_port_from_private_port(self_private_port);
1017 struct muxer_comp *muxer_comp =
1018 bt_private_component_get_user_data(priv_comp);
1019 size_t i;
958f7d11
PP
1020
1021 assert(self_port);
1022 assert(muxer_comp);
1023
1024 if (bt_port_get_type(self_port) == BT_PORT_TYPE_INPUT) {
1025 int ret;
1026
1027 /* One less available input port */
1028 muxer_comp->available_input_ports--;
1029 ret = ensure_available_input_port(priv_comp);
1030 if (ret) {
089717de
PP
1031 /*
1032 * Only way to report an error later since this
1033 * method does not return anything.
1034 */
958f7d11
PP
1035 muxer_comp->error = true;
1036 goto end;
1037 }
1038 }
1039
1040 for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) {
1041 struct muxer_notif_iter *muxer_notif_iter =
1042 g_ptr_array_index(muxer_comp->muxer_notif_iters, i);
1043
1044 /*
ab11110e
PP
1045 * Add this port to the list of newly connected ports
1046 * for this muxer notification iterator. We append at
1047 * the end of this list while
1048 * muxer_notif_iter_handle_newly_connected_ports()
1049 * removes the nodes from the beginning.
1050 *
1051 * The list node owns the private port.
958f7d11 1052 */
ab11110e
PP
1053 muxer_notif_iter->newly_connected_priv_ports =
1054 g_list_append(
1055 muxer_notif_iter->newly_connected_priv_ports,
1056 bt_get(self_private_port));
1057 if (!muxer_notif_iter->newly_connected_priv_ports) {
089717de 1058 /* Put reference taken by bt_get() above */
ab11110e 1059 bt_put(self_private_port);
958f7d11
PP
1060 muxer_comp->error = true;
1061 goto end;
1062 }
1063 }
1064
1065end:
1066 bt_put(self_port);
1067}
1068
1069BT_HIDDEN
1070void muxer_port_disconnected(struct bt_private_component *priv_comp,
1071 struct bt_private_port *priv_port)
1072{
1073 struct bt_port *port = bt_port_from_private_port(priv_port);
1074 struct muxer_comp *muxer_comp =
1075 bt_private_component_get_user_data(priv_comp);
1076
1077 assert(port);
1078 assert(muxer_comp);
1079
ab11110e
PP
1080 /*
1081 * There's nothing special to do when a port is disconnected
1082 * because this component deals with upstream notification
1083 * iterators which were already created thanks to connected
1084 * ports. The fact that the port is disconnected does not cancel
1085 * the upstream notification iterators created using its
089717de
PP
1086 * connection: they still exist, even if the connection is dead.
1087 * The only way to remove an upstream notification iterator is
1088 * for its "next" operation to return
1089 * BT_NOTIFICATION_ITERATOR_STATUS_END.
ab11110e 1090 */
958f7d11
PP
1091 if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
1092 /* One more available input port */
1093 muxer_comp->available_input_ports++;
1094 }
1095
1096 bt_put(port);
1097}
This page took 0.066874 seconds and 4 git commands to generate.