4 * Babeltrace Notification Iterator
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
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>
66 struct discarded_elements_state
{
67 struct bt_clock_value
*cur_begin
;
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
;
81 void stream_destroy_listener(struct bt_stream_common
*stream
, void *data
)
83 struct bt_notification_iterator_private_connection
*iterator
= data
;
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator
->stream_states
, stream
);
90 void destroy_stream_state(struct stream_state
*stream_state
)
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
);
107 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
109 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
112 BT_LOGE_STR("Failed to allocate one stream state.");
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.
123 stream_state
->discarded_packets_state
.cur_count
= -1ULL;
126 * We keep a reference to the stream until we know it's ended.
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
);
138 void destroy_base_notification_iterator(struct bt_object
*obj
)
140 struct bt_notification_iterator
*iterator
=
141 container_of(obj
, struct bt_notification_iterator
, base
);
143 BT_LOGD_STR("Putting current notification.");
144 bt_put(iterator
->current_notification
);
149 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
151 struct bt_notification_iterator_private_connection
*iterator
;
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.
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",
169 bt_private_connection_notification_iterator_finalize(iterator
);
171 if (iterator
->stream_states
) {
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.
178 GHashTableIter ht_iter
;
179 gpointer stream_gptr
, stream_state_gptr
;
181 g_hash_table_iter_init(&ht_iter
, iterator
->stream_states
);
183 while (g_hash_table_iter_next(&ht_iter
, &stream_gptr
, &stream_state_gptr
)) {
184 BT_ASSERT(stream_gptr
);
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
,
192 g_hash_table_destroy(iterator
->stream_states
);
195 if (iterator
->connection
) {
197 * Remove ourself from the originating connection so
198 * that it does not try to finalize a dangling pointer
201 bt_connection_remove_iterator(iterator
->connection
, iterator
);
204 destroy_base_notification_iterator(obj
);
208 void bt_private_connection_notification_iterator_finalize(
209 struct bt_notification_iterator_private_connection
*iterator
)
211 struct bt_component_class
*comp_class
= NULL
;
212 bt_component_class_notification_iterator_finalize_method
213 finalize_method
= NULL
;
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
);
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
);
233 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
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
;
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
;
245 BT_ASSERT(iterator
->upstream_component
);
246 comp_class
= iterator
->upstream_component
->class;
248 /* Call user-defined destroy method */
249 switch (comp_class
->type
) {
250 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
252 struct bt_component_class_source
*source_class
;
254 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
255 finalize_method
= source_class
->methods
.iterator
.finalize
;
258 case BT_COMPONENT_CLASS_TYPE_FILTER
:
260 struct bt_component_class_filter
*filter_class
;
262 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
263 finalize_method
= filter_class
->methods
.iterator
.finalize
;
271 if (finalize_method
) {
272 BT_LOGD("Calling user's finalization method: addr=%p",
275 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
278 iterator
->upstream_component
= NULL
;
279 iterator
->upstream_port
= NULL
;
280 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
284 void bt_private_connection_notification_iterator_set_connection(
285 struct bt_notification_iterator_private_connection
*iterator
,
286 struct bt_connection
*connection
)
289 iterator
->connection
= connection
;
290 BT_LOGV("Set notification iterator's connection: "
291 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
295 void init_notification_iterator(struct bt_notification_iterator
*iterator
,
296 enum bt_notification_iterator_type type
,
297 bt_object_release_func destroy
)
299 bt_object_init(iterator
, destroy
);
300 iterator
->type
= type
;
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
)
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
;
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\", "
322 upstream_comp
, bt_component_get_name(upstream_comp
),
323 upstream_port
, bt_port_get_name(upstream_port
),
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);
330 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
331 status
= BT_CONNECTION_STATUS_NOMEM
;
335 init_notification_iterator((void *) iterator
,
336 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
337 bt_private_connection_notification_iterator_destroy
);
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
;
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
);
360 /* Move reference to user */
361 *user_iterator
= iterator
;
369 void *bt_private_connection_private_notification_iterator_get_user_data(
370 struct bt_private_connection_private_notification_iterator
*private_iterator
)
372 struct bt_notification_iterator_private_connection
*iterator
= (void *)
373 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
375 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
376 return iterator
->user_data
;
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
,
384 struct bt_notification_iterator_private_connection
*iterator
= (void *)
385 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
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
;
394 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
395 struct bt_private_connection_private_notification_iterator
*private_iterator
)
397 struct bt_notification_iterator_private_connection
*iterator
= (void *)
398 bt_private_connection_notification_iterator_borrow_from_private(
401 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
402 return iterator
->graph
;
405 struct bt_notification
*bt_notification_iterator_borrow_notification(
406 struct bt_notification_iterator
*iterator
)
408 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
409 return bt_notification_iterator_borrow_current_notification(iterator
);
414 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
415 struct bt_stream
**stream
, struct bt_packet
**packet
)
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
);
425 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
426 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
428 case BT_NOTIFICATION_TYPE_STREAM_END
:
429 *stream
= bt_notification_stream_end_borrow_stream(notif
);
431 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
432 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
433 *stream
= bt_packet_borrow_stream(*packet
);
435 case BT_NOTIFICATION_TYPE_PACKET_END
:
436 *packet
= bt_notification_packet_end_borrow_packet(notif
);
437 *stream
= bt_packet_borrow_stream(*packet
);
446 bool validate_notification(
447 struct bt_notification_iterator_private_connection
*iterator
,
448 struct bt_notification
*notif
)
450 bool is_valid
= true;
451 struct stream_state
*stream_state
;
452 struct bt_stream
*stream
= NULL
;
453 struct bt_packet
*packet
= NULL
;
456 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
459 /* we don't care about notifications not attached to streams */
463 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
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.
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
);
479 if (notif
->seq_num
== -1ULL) {
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
);
494 stream_state
= create_stream_state(stream
);
499 g_hash_table_insert(iterator
->stream_states
, stream
,
501 stream_state
->expected_notif_seq_num
++;
505 if (stream_state
->is_ended
) {
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
513 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
519 if (notif
->seq_num
== -1ULL) {
520 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
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
,
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
);
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
);
552 stream_state
->expected_notif_seq_num
++;
553 stream_state
->is_ended
= true;
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
,
567 stream_state
->expected_notif_seq_num
++;
568 stream_state
->cur_packet
= bt_get(packet
);
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
,
582 stream_state
->expected_notif_seq_num
++;
583 BT_PUT(stream_state
->cur_packet
);
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
);
596 stream_state
->expected_notif_seq_num
++;
607 static inline bool priv_conn_notif_iter_can_end(
608 struct bt_notification_iterator_private_connection
*iterator
)
611 gpointer stream_key
, state_value
;
615 * Verify that this iterator received a
616 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
620 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
622 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
623 struct stream_state
*stream_state
= (void *) state_value
;
625 BT_ASSERT(stream_state
);
626 BT_ASSERT(stream_key
);
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
);
642 enum bt_notification_iterator_status
643 bt_priv_conn_private_notification_iterator_next(
644 struct bt_notification_iterator_private_connection
*iterator
)
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
,
653 enum bt_notification_iterator_status status
=
654 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
657 BT_LIB_LOGD("Getting next notification iterator's notification: %!+i",
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);
666 /* Pick the appropriate "next" method */
667 switch (iterator
->upstream_component
->class->type
) {
668 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
670 struct bt_component_class_source
*source_class
=
671 container_of(iterator
->upstream_component
->class,
672 struct bt_component_class_source
, parent
);
674 BT_ASSERT(source_class
->methods
.iterator
.next
);
675 next_method
= source_class
->methods
.iterator
.next
;
678 case BT_COMPONENT_CLASS_TYPE_FILTER
:
680 struct bt_component_class_filter
*filter_class
=
681 container_of(iterator
->upstream_component
->class,
682 struct bt_component_class_filter
, parent
);
684 BT_ASSERT(filter_class
->methods
.iterator
.next
);
685 next_method
= filter_class
->methods
.iterator
.next
;
693 * Call the user's "next" method to get the next notification
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
;
707 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
708 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
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.
717 * Only bt_put() the returned notification if
719 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
720 * otherwise this field could be garbage.
722 if (next_return
.status
==
723 BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
724 bt_put(next_return
.notification
);
727 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
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: "
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
));
743 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
744 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
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: "
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
);
760 /* Unknown non-error status */
768 enum bt_notification_iterator_status
769 bt_notification_iterator_next(struct bt_notification_iterator
*iterator
)
771 enum bt_notification_iterator_status status
;
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
);
778 switch (iterator
->type
) {
779 case BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
:
781 struct bt_notification_iterator_private_connection
*priv_conn_iter
=
785 * Make sure that the iterator's queue contains at least
788 status
= bt_priv_conn_private_notification_iterator_next(
792 case BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
:
794 struct bt_notification_iterator_output_port
*out_port_iter
=
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
803 struct bt_notification
*old_notif
=
804 bt_get(bt_notification_iterator_borrow_current_notification(iterator
));
805 enum bt_graph_status graph_status
;
808 * Put current notification since it's possibly
809 * about to be replaced by a new one by the
812 bt_notification_iterator_replace_current_notification(
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
;
820 case BT_GRAPH_STATUS_AGAIN
:
821 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
823 case BT_GRAPH_STATUS_END
:
824 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
826 case BT_GRAPH_STATUS_NOMEM
:
827 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
829 case BT_GRAPH_STATUS_OK
:
830 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
831 BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator
));
835 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
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
);
854 struct bt_component
*bt_private_connection_notification_iterator_get_component(
855 struct bt_notification_iterator
*iterator
)
857 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
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: "
864 iter_priv_conn
= (void *) iterator
;
865 return bt_get(iter_priv_conn
->upstream_component
);
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
)
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
)));
878 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
880 struct bt_notification_iterator_output_port
*iterator
=
881 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
883 BT_LOGD("Destroying output port notification iterator object: addr=%p",
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
);
894 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
895 struct bt_port
*output_port
,
896 const char *colander_component_name
)
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
;
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
);
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);
925 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
929 init_notification_iterator((void *) iterator
,
930 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
931 bt_output_port_notification_iterator_destroy
);
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.");
940 BT_MOVE(iterator
->graph
, graph
);
941 iterator_base
= (void *) iterator
;
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
));
957 * Connect provided output port to the colander component's
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
));
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.
981 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
985 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
988 /* Remove created colander component from graph if any */
989 colander_comp
= iterator
->colander
;
990 BT_PUT(iterator
->colander
);
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
999 * Since we honor the preconditions here,
1000 * bt_graph_remove_unconnected_component() always
1003 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
1005 BT_ASSERT(ret
== 0);
1011 bt_put(colander_in_port
);
1012 bt_put(colander_comp_cls
);
1013 bt_put(output_port_comp
);
1015 return (void *) iterator
;
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
)
1022 return (void *) private_notification_iterator
;