Fix: bt_graph_consume(): return status
[babeltrace.git] / lib / graph / iterator.c
1 /*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
30
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/fields.h>
34 #include <babeltrace/ctf-ir/field-types.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/packet-internal.h>
38 #include <babeltrace/ctf-ir/stream-internal.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/connection-internal.h>
41 #include <babeltrace/graph/component.h>
42 #include <babeltrace/graph/component-source-internal.h>
43 #include <babeltrace/graph/component-class-internal.h>
44 #include <babeltrace/graph/component-class-sink-colander-internal.h>
45 #include <babeltrace/graph/component-sink.h>
46 #include <babeltrace/graph/notification.h>
47 #include <babeltrace/graph/notification-iterator.h>
48 #include <babeltrace/graph/notification-iterator-internal.h>
49 #include <babeltrace/graph/notification-internal.h>
50 #include <babeltrace/graph/notification-event.h>
51 #include <babeltrace/graph/notification-event-internal.h>
52 #include <babeltrace/graph/notification-packet.h>
53 #include <babeltrace/graph/notification-packet-internal.h>
54 #include <babeltrace/graph/notification-stream.h>
55 #include <babeltrace/graph/notification-stream-internal.h>
56 #include <babeltrace/graph/notification-discarded-elements-internal.h>
57 #include <babeltrace/graph/port.h>
58 #include <babeltrace/graph/graph-internal.h>
59 #include <babeltrace/types.h>
60 #include <babeltrace/assert-internal.h>
61 #include <babeltrace/assert-pre-internal.h>
62 #include <stdint.h>
63 #include <inttypes.h>
64 #include <stdlib.h>
65
66 struct discarded_elements_state {
67 struct bt_clock_value *cur_begin;
68 uint64_t cur_count;
69 };
70
71 struct stream_state {
72 struct bt_stream *stream; /* owned by this */
73 struct bt_packet *cur_packet; /* owned by this */
74 struct discarded_elements_state discarded_packets_state;
75 struct discarded_elements_state discarded_events_state;
76 uint64_t expected_notif_seq_num;
77 bt_bool is_ended;
78 };
79
80 static
81 void stream_destroy_listener(struct bt_stream_common *stream, void *data)
82 {
83 struct bt_notification_iterator_private_connection *iterator = data;
84
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator->stream_states, stream);
87 }
88
89 static
90 void destroy_stream_state(struct stream_state *stream_state)
91 {
92 if (!stream_state) {
93 return;
94 }
95
96 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
97 BT_LOGV_STR("Putting stream state's current packet.");
98 bt_put(stream_state->cur_packet);
99 BT_LOGV_STR("Putting stream state's stream.");
100 bt_put(stream_state->stream);
101 bt_put(stream_state->discarded_packets_state.cur_begin);
102 bt_put(stream_state->discarded_events_state.cur_begin);
103 g_free(stream_state);
104 }
105
106 static
107 struct stream_state *create_stream_state(struct bt_stream *stream)
108 {
109 struct stream_state *stream_state = g_new0(struct stream_state, 1);
110
111 if (!stream_state) {
112 BT_LOGE_STR("Failed to allocate one stream state.");
113 goto end;
114 }
115
116 /*
117 * The packet index is a monotonic counter which may not start
118 * at 0 at the beginning of the stream. We therefore need to
119 * have an internal object initial state of -1ULL to distinguish
120 * between initial state and having seen a packet with
121 * the sequence number 0.
122 */
123 stream_state->discarded_packets_state.cur_count = -1ULL;
124
125 /*
126 * We keep a reference to the stream until we know it's ended.
127 */
128 stream_state->stream = bt_get(stream);
129 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
130 "stream-state-addr=%p",
131 stream, bt_stream_get_name(stream), stream_state);
132
133 end:
134 return stream_state;
135 }
136
137 static
138 void destroy_base_notification_iterator(struct bt_object *obj)
139 {
140 struct bt_notification_iterator *iterator =
141 container_of(obj, struct bt_notification_iterator, base);
142
143 BT_LOGD_STR("Putting current notification.");
144 bt_put(iterator->current_notification);
145 g_free(iterator);
146 }
147
148 static
149 void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
150 {
151 struct bt_notification_iterator_private_connection *iterator;
152
153 BT_ASSERT(obj);
154
155 /*
156 * The notification iterator's reference count is 0 if we're
157 * here. Increment it to avoid a double-destroy (possibly
158 * infinitely recursive). This could happen for example if the
159 * notification iterator's finalization function does bt_get()
160 * (or anything that causes bt_get() to be called) on itself
161 * (ref. count goes from 0 to 1), and then bt_put(): the
162 * reference count would go from 1 to 0 again and this function
163 * would be called again.
164 */
165 obj->ref_count.count++;
166 iterator = (void *) container_of(obj, struct bt_notification_iterator, base);
167 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
168 iterator);
169 bt_private_connection_notification_iterator_finalize(iterator);
170
171 if (iterator->stream_states) {
172 /*
173 * Remove our destroy listener from each stream which
174 * has a state in this iterator. Otherwise the destroy
175 * listener would be called with an invalid/other
176 * notification iterator object.
177 */
178 GHashTableIter ht_iter;
179 gpointer stream_gptr, stream_state_gptr;
180
181 g_hash_table_iter_init(&ht_iter, iterator->stream_states);
182
183 while (g_hash_table_iter_next(&ht_iter, &stream_gptr, &stream_state_gptr)) {
184 BT_ASSERT(stream_gptr);
185
186 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
187 bt_stream_common_remove_destroy_listener(
188 (void *) stream_gptr, stream_destroy_listener,
189 iterator);
190 }
191
192 g_hash_table_destroy(iterator->stream_states);
193 }
194
195 if (iterator->connection) {
196 /*
197 * Remove ourself from the originating connection so
198 * that it does not try to finalize a dangling pointer
199 * later.
200 */
201 bt_connection_remove_iterator(iterator->connection, iterator);
202 }
203
204 destroy_base_notification_iterator(obj);
205 }
206
207 BT_HIDDEN
208 void bt_private_connection_notification_iterator_finalize(
209 struct bt_notification_iterator_private_connection *iterator)
210 {
211 struct bt_component_class *comp_class = NULL;
212 bt_component_class_notification_iterator_finalize_method
213 finalize_method = NULL;
214
215 BT_ASSERT(iterator);
216
217 switch (iterator->state) {
218 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
219 /* Skip user finalization if user initialization failed */
220 BT_LOGD("Not finalizing non-initialized notification iterator: "
221 "addr=%p", iterator);
222 return;
223 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
224 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
225 /* Already finalized */
226 BT_LOGD("Not finalizing notification iterator: already finalized: "
227 "addr=%p", iterator);
228 return;
229 default:
230 break;
231 }
232
233 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
234
235 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
236 BT_LOGD("Updating notification iterator's state: "
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
238 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
239 } else {
240 BT_LOGD("Updating notification iterator's state: "
241 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
242 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
243 }
244
245 BT_ASSERT(iterator->upstream_component);
246 comp_class = iterator->upstream_component->class;
247
248 /* Call user-defined destroy method */
249 switch (comp_class->type) {
250 case BT_COMPONENT_CLASS_TYPE_SOURCE:
251 {
252 struct bt_component_class_source *source_class;
253
254 source_class = container_of(comp_class, struct bt_component_class_source, parent);
255 finalize_method = source_class->methods.iterator.finalize;
256 break;
257 }
258 case BT_COMPONENT_CLASS_TYPE_FILTER:
259 {
260 struct bt_component_class_filter *filter_class;
261
262 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
263 finalize_method = filter_class->methods.iterator.finalize;
264 break;
265 }
266 default:
267 /* Unreachable */
268 abort();
269 }
270
271 if (finalize_method) {
272 BT_LOGD("Calling user's finalization method: addr=%p",
273 iterator);
274 finalize_method(
275 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
276 }
277
278 iterator->upstream_component = NULL;
279 iterator->upstream_port = NULL;
280 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
281 }
282
283 BT_HIDDEN
284 void bt_private_connection_notification_iterator_set_connection(
285 struct bt_notification_iterator_private_connection *iterator,
286 struct bt_connection *connection)
287 {
288 BT_ASSERT(iterator);
289 iterator->connection = connection;
290 BT_LOGV("Set notification iterator's connection: "
291 "iter-addr=%p, conn-addr=%p", iterator, connection);
292 }
293
294 static
295 void init_notification_iterator(struct bt_notification_iterator *iterator,
296 enum bt_notification_iterator_type type,
297 bt_object_release_func destroy)
298 {
299 bt_object_init(iterator, destroy);
300 iterator->type = type;
301 }
302
303 BT_HIDDEN
304 enum bt_connection_status bt_private_connection_notification_iterator_create(
305 struct bt_component *upstream_comp,
306 struct bt_port *upstream_port,
307 struct bt_connection *connection,
308 struct bt_notification_iterator_private_connection **user_iterator)
309 {
310 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
311 enum bt_component_class_type type;
312 struct bt_notification_iterator_private_connection *iterator = NULL;
313
314 BT_ASSERT(upstream_comp);
315 BT_ASSERT(upstream_port);
316 BT_ASSERT(bt_port_is_connected(upstream_port));
317 BT_ASSERT(user_iterator);
318 BT_LOGD("Creating notification iterator on private connection: "
319 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
320 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
321 "conn-addr=%p",
322 upstream_comp, bt_component_get_name(upstream_comp),
323 upstream_port, bt_port_get_name(upstream_port),
324 connection);
325 type = bt_component_get_class_type(upstream_comp);
326 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
327 type == BT_COMPONENT_CLASS_TYPE_FILTER);
328 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
329 if (!iterator) {
330 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
331 status = BT_CONNECTION_STATUS_NOMEM;
332 goto end;
333 }
334
335 init_notification_iterator((void *) iterator,
336 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
337 bt_private_connection_notification_iterator_destroy);
338
339 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
340 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
341 if (!iterator->stream_states) {
342 BT_LOGE_STR("Failed to allocate a GHashTable.");
343 status = BT_CONNECTION_STATUS_NOMEM;
344 goto end;
345 }
346
347 iterator->upstream_component = upstream_comp;
348 iterator->upstream_port = upstream_port;
349 iterator->connection = connection;
350 iterator->graph = bt_component_borrow_graph(upstream_comp);
351 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
352 BT_LOGD("Created notification iterator: "
353 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
354 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
355 "conn-addr=%p, iter-addr=%p",
356 upstream_comp, bt_component_get_name(upstream_comp),
357 upstream_port, bt_port_get_name(upstream_port),
358 connection, iterator);
359
360 /* Move reference to user */
361 *user_iterator = iterator;
362 iterator = NULL;
363
364 end:
365 bt_put(iterator);
366 return status;
367 }
368
369 void *bt_private_connection_private_notification_iterator_get_user_data(
370 struct bt_private_connection_private_notification_iterator *private_iterator)
371 {
372 struct bt_notification_iterator_private_connection *iterator = (void *)
373 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
374
375 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
376 return iterator->user_data;
377 }
378
379 enum bt_notification_iterator_status
380 bt_private_connection_private_notification_iterator_set_user_data(
381 struct bt_private_connection_private_notification_iterator *private_iterator,
382 void *data)
383 {
384 struct bt_notification_iterator_private_connection *iterator = (void *)
385 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
386
387 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
388 iterator->user_data = data;
389 BT_LOGV("Set notification iterator's user data: "
390 "iter-addr=%p, user-data-addr=%p", iterator, data);
391 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
392 }
393
394 struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph(
395 struct bt_private_connection_private_notification_iterator *private_iterator)
396 {
397 struct bt_notification_iterator_private_connection *iterator = (void *)
398 bt_private_connection_notification_iterator_borrow_from_private(
399 private_iterator);
400
401 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
402 return iterator->graph;
403 }
404
405 struct bt_notification *bt_notification_iterator_borrow_notification(
406 struct bt_notification_iterator *iterator)
407 {
408 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
409 return bt_notification_iterator_borrow_current_notification(iterator);
410 }
411
412 BT_ASSERT_PRE_FUNC
413 static inline
414 void bt_notification_borrow_packet_stream(struct bt_notification *notif,
415 struct bt_stream **stream, struct bt_packet **packet)
416 {
417 BT_ASSERT(notif);
418
419 switch (notif->type) {
420 case BT_NOTIFICATION_TYPE_EVENT:
421 *packet = bt_event_borrow_packet(
422 bt_notification_event_borrow_event(notif));
423 *stream = bt_packet_borrow_stream(*packet);
424 break;
425 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
426 *stream = bt_notification_stream_begin_borrow_stream(notif);
427 break;
428 case BT_NOTIFICATION_TYPE_STREAM_END:
429 *stream = bt_notification_stream_end_borrow_stream(notif);
430 break;
431 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
432 *packet = bt_notification_packet_begin_borrow_packet(notif);
433 *stream = bt_packet_borrow_stream(*packet);
434 break;
435 case BT_NOTIFICATION_TYPE_PACKET_END:
436 *packet = bt_notification_packet_end_borrow_packet(notif);
437 *stream = bt_packet_borrow_stream(*packet);
438 break;
439 default:
440 break;
441 }
442 }
443
444 BT_ASSERT_PRE_FUNC
445 static inline
446 bool validate_notification(
447 struct bt_notification_iterator_private_connection *iterator,
448 struct bt_notification *notif)
449 {
450 bool is_valid = true;
451 struct stream_state *stream_state;
452 struct bt_stream *stream = NULL;
453 struct bt_packet *packet = NULL;
454
455 BT_ASSERT(notif);
456 bt_notification_borrow_packet_stream(notif, &stream, &packet);
457
458 if (!stream) {
459 /* we don't care about notifications not attached to streams */
460 goto end;
461 }
462
463 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
464 if (!stream_state) {
465 /*
466 * No stream state for this stream: this notification
467 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
468 * and its sequence number must be 0.
469 */
470 if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
471 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
472 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
473 "notification prior to this notification: "
474 "%![stream-]+s", stream);
475 is_valid = false;
476 goto end;
477 }
478
479 if (notif->seq_num == -1ULL) {
480 notif->seq_num = 0;
481 }
482
483 if (notif->seq_num != 0) {
484 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
485 "number for this notification iterator: "
486 "this is the first notification for this "
487 "stream, expecting sequence number 0: "
488 "seq-num=%" PRIu64 ", %![stream-]+s",
489 notif->seq_num, stream);
490 is_valid = false;
491 goto end;
492 }
493
494 stream_state = create_stream_state(stream);
495 if (!stream_state) {
496 abort();
497 }
498
499 g_hash_table_insert(iterator->stream_states, stream,
500 stream_state);
501 stream_state->expected_notif_seq_num++;
502 goto end;
503 }
504
505 if (stream_state->is_ended) {
506 /*
507 * There's a new notification which has a reference to a
508 * stream which, from this iterator's point of view, is
509 * ended ("end of stream" notification was returned).
510 * This is bad: the API guarantees that it can never
511 * happen.
512 */
513 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
514 stream);
515 is_valid = false;
516 goto end;
517 }
518
519 if (notif->seq_num == -1ULL) {
520 notif->seq_num = stream_state->expected_notif_seq_num;
521 }
522
523 if (notif->seq_num != -1ULL &&
524 notif->seq_num != stream_state->expected_notif_seq_num) {
525 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
526 "seq-num=%" PRIu64 ", "
527 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
528 notif->seq_num, stream_state->expected_notif_seq_num,
529 stream);
530 is_valid = false;
531 goto end;
532 }
533
534 switch (notif->type) {
535 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
536 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
537 "notification at this point: notif-seq-num=%" PRIu64 ", "
538 "%![stream-]+s", notif->seq_num, stream);
539 is_valid = false;
540 goto end;
541 case BT_NOTIFICATION_TYPE_STREAM_END:
542 if (stream_state->cur_packet) {
543 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
544 "notification: missing a "
545 "BT_NOTIFICATION_TYPE_PACKET_END notification "
546 "prior to this notification: "
547 "notif-seq-num=%" PRIu64 ", "
548 "%![stream-]+s", notif->seq_num, stream);
549 is_valid = false;
550 goto end;
551 }
552 stream_state->expected_notif_seq_num++;
553 stream_state->is_ended = true;
554 goto end;
555 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
556 if (stream_state->cur_packet) {
557 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
558 "notification at this point: missing a "
559 "BT_NOTIFICATION_TYPE_PACKET_END notification "
560 "prior to this notification: "
561 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
562 "%![packet-]+a", notif->seq_num, stream,
563 packet);
564 is_valid = false;
565 goto end;
566 }
567 stream_state->expected_notif_seq_num++;
568 stream_state->cur_packet = bt_get(packet);
569 goto end;
570 case BT_NOTIFICATION_TYPE_PACKET_END:
571 if (!stream_state->cur_packet) {
572 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
573 "notification at this point: missing a "
574 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
575 "prior to this notification: "
576 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
577 "%![packet-]+a", notif->seq_num, stream,
578 packet);
579 is_valid = false;
580 goto end;
581 }
582 stream_state->expected_notif_seq_num++;
583 BT_PUT(stream_state->cur_packet);
584 goto end;
585 case BT_NOTIFICATION_TYPE_EVENT:
586 if (packet != stream_state->cur_packet) {
587 BT_ASSERT_PRE_MSG("Unexpected packet for "
588 "BT_NOTIFICATION_TYPE_EVENT notification: "
589 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
590 "%![notif-packet-]+a, %![expected-packet-]+a",
591 notif->seq_num, stream,
592 stream_state->cur_packet, packet);
593 is_valid = false;
594 goto end;
595 }
596 stream_state->expected_notif_seq_num++;
597 goto end;
598 default:
599 break;
600 }
601
602 end:
603 return is_valid;
604 }
605
606 BT_ASSERT_PRE_FUNC
607 static inline bool priv_conn_notif_iter_can_end(
608 struct bt_notification_iterator_private_connection *iterator)
609 {
610 GHashTableIter iter;
611 gpointer stream_key, state_value;
612 bool ret = true;
613
614 /*
615 * Verify that this iterator received a
616 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
617 * which has a state.
618 */
619
620 g_hash_table_iter_init(&iter, iterator->stream_states);
621
622 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
623 struct stream_state *stream_state = (void *) state_value;
624
625 BT_ASSERT(stream_state);
626 BT_ASSERT(stream_key);
627
628 if (!stream_state->is_ended) {
629 BT_ASSERT_PRE_MSG("Ending notification iterator, "
630 "but stream is not ended: "
631 "%![stream-]s", stream_key);
632 ret = false;
633 goto end;
634 }
635 }
636
637 end:
638 return ret;
639 }
640
641 static
642 enum bt_notification_iterator_status
643 bt_priv_conn_private_notification_iterator_next(
644 struct bt_notification_iterator_private_connection *iterator)
645 {
646 struct bt_private_connection_private_notification_iterator *priv_iterator =
647 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator);
648 bt_component_class_notification_iterator_next_method next_method = NULL;
649 struct bt_notification_iterator_next_method_return next_return = {
650 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
651 .notification = NULL,
652 };
653 enum bt_notification_iterator_status status =
654 BT_NOTIFICATION_ITERATOR_STATUS_OK;
655
656 BT_ASSERT(iterator);
657 BT_LIB_LOGD("Getting next notification iterator's notification: %!+i",
658 iterator);
659 BT_ASSERT_PRE(iterator->state ==
660 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
661 "Notification iterator's \"next\" called, but "
662 "iterator is in the wrong state: %!+i", iterator);
663 BT_ASSERT(iterator->upstream_component);
664 BT_ASSERT(iterator->upstream_component->class);
665
666 /* Pick the appropriate "next" method */
667 switch (iterator->upstream_component->class->type) {
668 case BT_COMPONENT_CLASS_TYPE_SOURCE:
669 {
670 struct bt_component_class_source *source_class =
671 container_of(iterator->upstream_component->class,
672 struct bt_component_class_source, parent);
673
674 BT_ASSERT(source_class->methods.iterator.next);
675 next_method = source_class->methods.iterator.next;
676 break;
677 }
678 case BT_COMPONENT_CLASS_TYPE_FILTER:
679 {
680 struct bt_component_class_filter *filter_class =
681 container_of(iterator->upstream_component->class,
682 struct bt_component_class_filter, parent);
683
684 BT_ASSERT(filter_class->methods.iterator.next);
685 next_method = filter_class->methods.iterator.next;
686 break;
687 }
688 default:
689 abort();
690 }
691
692 /*
693 * Call the user's "next" method to get the next notification
694 * and status.
695 */
696 BT_ASSERT(next_method);
697 BT_LOGD_STR("Calling user's \"next\" method.");
698 next_return = next_method(priv_iterator);
699 BT_LOGD("User method returned: status=%s",
700 bt_notification_iterator_status_string(next_return.status));
701 if (next_return.status < 0) {
702 BT_LOGW_STR("User method failed.");
703 status = next_return.status;
704 goto end;
705 }
706
707 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
708 iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
709 /*
710 * The user's "next" method, somehow, cancelled its own
711 * notification iterator. This can happen, for example,
712 * when the user's method removes the port on which
713 * there's the connection from which the iterator was
714 * created. In this case, said connection is ended, and
715 * all its notification iterators are finalized.
716 *
717 * Only bt_put() the returned notification if
718 * the status is
719 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
720 * otherwise this field could be garbage.
721 */
722 if (next_return.status ==
723 BT_NOTIFICATION_ITERATOR_STATUS_OK) {
724 bt_put(next_return.notification);
725 }
726
727 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
728 goto end;
729 }
730
731 switch (next_return.status) {
732 case BT_NOTIFICATION_ITERATOR_STATUS_END:
733 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
734 "Notification iterator cannot end at this point: "
735 "%!+i", iterator);
736 BT_ASSERT(iterator->state ==
737 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE);
738 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED;
739 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
740 BT_LOGD("Set new status: status=%s",
741 bt_notification_iterator_status_string(status));
742 goto end;
743 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
744 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
745 goto end;
746 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
747 BT_ASSERT_PRE(next_return.notification,
748 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
749 "%!+i", iterator);
750 BT_ASSERT_PRE(validate_notification(iterator,
751 next_return.notification),
752 "Notification is invalid at this point: "
753 "%![notif-iter-]+i, %![notif-]+n",
754 iterator, next_return.notification);
755 bt_notification_iterator_replace_current_notification(
756 (void *) iterator, next_return.notification);
757 bt_put(next_return.notification);
758 break;
759 default:
760 /* Unknown non-error status */
761 abort();
762 }
763
764 end:
765 return status;
766 }
767
768 enum bt_notification_iterator_status
769 bt_notification_iterator_next(struct bt_notification_iterator *iterator)
770 {
771 enum bt_notification_iterator_status status;
772
773 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
774 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
775 BT_ASSERT(iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION ||
776 iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT);
777
778 switch (iterator->type) {
779 case BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION:
780 {
781 struct bt_notification_iterator_private_connection *priv_conn_iter =
782 (void *) iterator;
783
784 /*
785 * Make sure that the iterator's queue contains at least
786 * one notification.
787 */
788 status = bt_priv_conn_private_notification_iterator_next(
789 priv_conn_iter);
790 break;
791 }
792 case BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT:
793 {
794 struct bt_notification_iterator_output_port *out_port_iter =
795 (void *) iterator;
796
797 /*
798 * Keep current notification in case there's an error:
799 * restore this notification so that the current
800 * notification is not changed from the user's point of
801 * view.
802 */
803 struct bt_notification *old_notif =
804 bt_get(bt_notification_iterator_borrow_current_notification(iterator));
805 enum bt_graph_status graph_status;
806
807 /*
808 * Put current notification since it's possibly
809 * about to be replaced by a new one by the
810 * colander sink.
811 */
812 bt_notification_iterator_replace_current_notification(
813 iterator, NULL);
814 graph_status = bt_graph_consume_sink_no_check(
815 out_port_iter->graph, out_port_iter->colander);
816 switch (graph_status) {
817 case BT_GRAPH_STATUS_CANCELED:
818 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
819 break;
820 case BT_GRAPH_STATUS_AGAIN:
821 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
822 break;
823 case BT_GRAPH_STATUS_END:
824 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
825 break;
826 case BT_GRAPH_STATUS_NOMEM:
827 status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
828 break;
829 case BT_GRAPH_STATUS_OK:
830 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
831 BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator));
832 break;
833 default:
834 /* Other errors */
835 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
836 }
837
838 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
839 /* Error/exception: restore old notification */
840 bt_notification_iterator_replace_current_notification(
841 iterator, old_notif);
842 }
843
844 bt_put(old_notif);
845 break;
846 }
847 default:
848 abort();
849 }
850
851 return status;
852 }
853
854 struct bt_component *bt_private_connection_notification_iterator_get_component(
855 struct bt_notification_iterator *iterator)
856 {
857 struct bt_notification_iterator_private_connection *iter_priv_conn;
858
859 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
860 BT_ASSERT_PRE(iterator->type ==
861 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
862 "Notification iterator was not created from a private connection: "
863 "%!+i", iterator);
864 iter_priv_conn = (void *) iterator;
865 return bt_get(iter_priv_conn->upstream_component);
866 }
867
868 struct bt_private_component *
869 bt_private_connection_private_notification_iterator_get_private_component(
870 struct bt_private_connection_private_notification_iterator *private_iterator)
871 {
872 return bt_private_component_from_component(
873 bt_private_connection_notification_iterator_get_component(
874 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
875 }
876
877 static
878 void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
879 {
880 struct bt_notification_iterator_output_port *iterator =
881 (void *) container_of(obj, struct bt_notification_iterator, base);
882
883 BT_LOGD("Destroying output port notification iterator object: addr=%p",
884 iterator);
885 BT_LOGD_STR("Putting graph.");
886 bt_put(iterator->graph);
887 BT_LOGD_STR("Putting output port.");
888 bt_put(iterator->output_port);
889 BT_LOGD_STR("Putting colander sink component.");
890 bt_put(iterator->colander);
891 destroy_base_notification_iterator(obj);
892 }
893
894 struct bt_notification_iterator *bt_output_port_notification_iterator_create(
895 struct bt_port *output_port,
896 const char *colander_component_name)
897 {
898 struct bt_notification_iterator *iterator_base = NULL;
899 struct bt_notification_iterator_output_port *iterator = NULL;
900 struct bt_component_class *colander_comp_cls = NULL;
901 struct bt_component *output_port_comp = NULL;
902 struct bt_component *colander_comp;
903 struct bt_graph *graph = NULL;
904 enum bt_graph_status graph_status;
905 const char *colander_comp_name;
906 struct bt_port *colander_in_port = NULL;
907 struct bt_component_class_sink_colander_data colander_data;
908
909 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
910 BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
911 "Port is not an output port: %!+p", output_port);
912 output_port_comp = bt_port_get_component(output_port);
913 BT_ASSERT_PRE(output_port_comp,
914 "Output port has no component: %!+p", output_port);
915 graph = bt_component_get_graph(output_port_comp);
916 BT_ASSERT(graph);
917
918 /* Create notification iterator */
919 BT_LOGD("Creating notification iterator on output port: "
920 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
921 output_port_comp, bt_component_get_name(output_port_comp),
922 output_port, bt_port_get_name(output_port));
923 iterator = g_new0(struct bt_notification_iterator_output_port, 1);
924 if (!iterator) {
925 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
926 goto error;
927 }
928
929 init_notification_iterator((void *) iterator,
930 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
931 bt_output_port_notification_iterator_destroy);
932
933 /* Create colander component */
934 colander_comp_cls = bt_component_class_sink_colander_get();
935 if (!colander_comp_cls) {
936 BT_LOGW("Cannot get colander sink component class.");
937 goto error;
938 }
939
940 BT_MOVE(iterator->graph, graph);
941 iterator_base = (void *) iterator;
942 colander_comp_name =
943 colander_component_name ? colander_component_name : "colander";
944 colander_data.notification = &iterator_base->current_notification;
945 graph_status = bt_graph_add_component_with_init_method_data(
946 iterator->graph, colander_comp_cls, colander_comp_name,
947 NULL, &colander_data, &iterator->colander);
948 if (graph_status != BT_GRAPH_STATUS_OK) {
949 BT_LOGW("Cannot add colander sink component to graph: "
950 "graph-addr=%p, name=\"%s\", graph-status=%s",
951 iterator->graph, colander_comp_name,
952 bt_graph_status_string(graph_status));
953 goto error;
954 }
955
956 /*
957 * Connect provided output port to the colander component's
958 * input port.
959 */
960 colander_in_port = bt_component_sink_get_input_port_by_index(
961 iterator->colander, 0);
962 BT_ASSERT(colander_in_port);
963 graph_status = bt_graph_connect_ports(iterator->graph,
964 output_port, colander_in_port, NULL);
965 if (graph_status != BT_GRAPH_STATUS_OK) {
966 BT_LOGW("Cannot add colander sink component to graph: "
967 "graph-addr=%p, name=\"%s\", graph-status=%s",
968 iterator->graph, colander_comp_name,
969 bt_graph_status_string(graph_status));
970 goto error;
971 }
972
973 /*
974 * At this point everything went fine. Make the graph
975 * nonconsumable forever so that only this notification iterator
976 * can consume (thanks to bt_graph_consume_sink_no_check()).
977 * This avoids leaking the notification created by the colander
978 * sink and moved to the base notification iterator's current
979 * notification member.
980 */
981 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
982 goto end;
983
984 error:
985 if (iterator && iterator->graph && iterator->colander) {
986 int ret;
987
988 /* Remove created colander component from graph if any */
989 colander_comp = iterator->colander;
990 BT_PUT(iterator->colander);
991
992 /*
993 * At this point the colander component's reference
994 * count is 0 because iterator->colander was the only
995 * owner. We also know that it is not connected because
996 * this is the last operation before this function
997 * succeeds.
998 *
999 * Since we honor the preconditions here,
1000 * bt_graph_remove_unconnected_component() always
1001 * succeeds.
1002 */
1003 ret = bt_graph_remove_unconnected_component(iterator->graph,
1004 colander_comp);
1005 BT_ASSERT(ret == 0);
1006 }
1007
1008 BT_PUT(iterator);
1009
1010 end:
1011 bt_put(colander_in_port);
1012 bt_put(colander_comp_cls);
1013 bt_put(output_port_comp);
1014 bt_put(graph);
1015 return (void *) iterator;
1016 }
1017
1018 struct bt_notification_iterator *
1019 bt_private_connection_notification_iterator_borrow_from_private(
1020 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
1021 {
1022 return (void *) private_notification_iterator;
1023 }
This page took 0.063628 seconds and 5 git commands to generate.