lib: add "borrow" functions where "get" functions exist
[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->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
351 BT_LOGD("Created notification iterator: "
352 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
353 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
354 "conn-addr=%p, iter-addr=%p",
355 upstream_comp, bt_component_get_name(upstream_comp),
356 upstream_port, bt_port_get_name(upstream_port),
357 connection, iterator);
358
359 /* Move reference to user */
360 *user_iterator = iterator;
361 iterator = NULL;
362
363 end:
364 bt_put(iterator);
365 return status;
366 }
367
368 void *bt_private_connection_private_notification_iterator_get_user_data(
369 struct bt_private_connection_private_notification_iterator *private_iterator)
370 {
371 struct bt_notification_iterator_private_connection *iterator =
372 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
373
374 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
375 return iterator->user_data;
376 }
377
378 enum bt_notification_iterator_status
379 bt_private_connection_private_notification_iterator_set_user_data(
380 struct bt_private_connection_private_notification_iterator *private_iterator,
381 void *data)
382 {
383 struct bt_notification_iterator_private_connection *iterator =
384 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
385
386 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
387 iterator->user_data = data;
388 BT_LOGV("Set notification iterator's user data: "
389 "iter-addr=%p, user-data-addr=%p", iterator, data);
390 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
391 }
392
393 struct bt_notification *bt_notification_iterator_borrow_notification(
394 struct bt_notification_iterator *iterator)
395 {
396 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
397 return bt_notification_iterator_borrow_current_notification(iterator);
398 }
399
400 BT_ASSERT_PRE_FUNC
401 static inline
402 void bt_notification_borrow_packet_stream(struct bt_notification *notif,
403 struct bt_stream **stream, struct bt_packet **packet)
404 {
405 BT_ASSERT(notif);
406
407 switch (notif->type) {
408 case BT_NOTIFICATION_TYPE_EVENT:
409 *packet = bt_event_borrow_packet(
410 bt_notification_event_borrow_event(notif));
411 *stream = bt_packet_borrow_stream(*packet);
412 break;
413 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
414 *stream = bt_notification_stream_begin_borrow_stream(notif);
415 break;
416 case BT_NOTIFICATION_TYPE_STREAM_END:
417 *stream = bt_notification_stream_end_borrow_stream(notif);
418 break;
419 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
420 *packet = bt_notification_packet_begin_borrow_packet(notif);
421 *stream = bt_packet_borrow_stream(*packet);
422 break;
423 case BT_NOTIFICATION_TYPE_PACKET_END:
424 *packet = bt_notification_packet_end_borrow_packet(notif);
425 *stream = bt_packet_borrow_stream(*packet);
426 break;
427 default:
428 break;
429 }
430 }
431
432 BT_ASSERT_PRE_FUNC
433 static inline
434 bool validate_notification(
435 struct bt_notification_iterator_private_connection *iterator,
436 struct bt_notification *notif)
437 {
438 bool is_valid = true;
439 struct stream_state *stream_state;
440 struct bt_stream *stream = NULL;
441 struct bt_packet *packet = NULL;
442
443 BT_ASSERT(notif);
444 bt_notification_borrow_packet_stream(notif, &stream, &packet);
445
446 if (!stream) {
447 /* we don't care about notifications not attached to streams */
448 goto end;
449 }
450
451 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
452 if (!stream_state) {
453 /*
454 * No stream state for this stream: this notification
455 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
456 * and its sequence number must be 0.
457 */
458 if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
459 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
460 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
461 "notification prior to this notification: "
462 "%![stream-]+s", stream);
463 is_valid = false;
464 goto end;
465 }
466
467 if (notif->seq_num == -1ULL) {
468 notif->seq_num = 0;
469 }
470
471 if (notif->seq_num != 0) {
472 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
473 "number for this notification iterator: "
474 "this is the first notification for this "
475 "stream, expecting sequence number 0: "
476 "seq-num=%" PRIu64 ", %![stream-]+s",
477 notif->seq_num, stream);
478 is_valid = false;
479 goto end;
480 }
481
482 stream_state = create_stream_state(stream);
483 if (!stream_state) {
484 abort();
485 }
486
487 g_hash_table_insert(iterator->stream_states, stream,
488 stream_state);
489 stream_state->expected_notif_seq_num++;
490 goto end;
491 }
492
493 if (stream_state->is_ended) {
494 /*
495 * There's a new notification which has a reference to a
496 * stream which, from this iterator's point of view, is
497 * ended ("end of stream" notification was returned).
498 * This is bad: the API guarantees that it can never
499 * happen.
500 */
501 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
502 stream);
503 is_valid = false;
504 goto end;
505 }
506
507 if (notif->seq_num == -1ULL) {
508 notif->seq_num = stream_state->expected_notif_seq_num;
509 }
510
511 if (notif->seq_num != -1ULL &&
512 notif->seq_num != stream_state->expected_notif_seq_num) {
513 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
514 "seq-num=%" PRIu64 ", "
515 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
516 notif->seq_num, stream_state->expected_notif_seq_num,
517 stream);
518 is_valid = false;
519 goto end;
520 }
521
522 switch (notif->type) {
523 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
524 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
525 "notification at this point: notif-seq-num=%" PRIu64 ", "
526 "%![stream-]+s", notif->seq_num, stream);
527 is_valid = false;
528 goto end;
529 case BT_NOTIFICATION_TYPE_STREAM_END:
530 if (stream_state->cur_packet) {
531 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
532 "notification: missing a "
533 "BT_NOTIFICATION_TYPE_PACKET_END notification "
534 "prior to this notification: "
535 "notif-seq-num=%" PRIu64 ", "
536 "%![stream-]+s", notif->seq_num, stream);
537 is_valid = false;
538 goto end;
539 }
540 stream_state->expected_notif_seq_num++;
541 stream_state->is_ended = true;
542 goto end;
543 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
544 if (stream_state->cur_packet) {
545 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
546 "notification at this point: missing a "
547 "BT_NOTIFICATION_TYPE_PACKET_END notification "
548 "prior to this notification: "
549 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
550 "%![packet-]+a", notif->seq_num, stream,
551 packet);
552 is_valid = false;
553 goto end;
554 }
555 stream_state->expected_notif_seq_num++;
556 stream_state->cur_packet = bt_get(packet);
557 goto end;
558 case BT_NOTIFICATION_TYPE_PACKET_END:
559 if (!stream_state->cur_packet) {
560 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
561 "notification at this point: missing a "
562 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
563 "prior to this notification: "
564 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
565 "%![packet-]+a", notif->seq_num, stream,
566 packet);
567 is_valid = false;
568 goto end;
569 }
570 stream_state->expected_notif_seq_num++;
571 BT_PUT(stream_state->cur_packet);
572 goto end;
573 case BT_NOTIFICATION_TYPE_EVENT:
574 if (packet != stream_state->cur_packet) {
575 BT_ASSERT_PRE_MSG("Unexpected packet for "
576 "BT_NOTIFICATION_TYPE_EVENT notification: "
577 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
578 "%![notif-packet-]+a, %![expected-packet-]+a",
579 notif->seq_num, stream,
580 stream_state->cur_packet, packet);
581 is_valid = false;
582 goto end;
583 }
584 stream_state->expected_notif_seq_num++;
585 goto end;
586 default:
587 break;
588 }
589
590 end:
591 return is_valid;
592 }
593
594 BT_ASSERT_PRE_FUNC
595 static inline bool priv_conn_notif_iter_can_end(
596 struct bt_notification_iterator_private_connection *iterator)
597 {
598 GHashTableIter iter;
599 gpointer stream_key, state_value;
600 bool ret = true;
601
602 /*
603 * Verify that this iterator received a
604 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
605 * which has a state.
606 */
607
608 g_hash_table_iter_init(&iter, iterator->stream_states);
609
610 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
611 struct stream_state *stream_state = (void *) state_value;
612
613 BT_ASSERT(stream_state);
614 BT_ASSERT(stream_key);
615
616 if (!stream_state->is_ended) {
617 BT_ASSERT_PRE_MSG("Ending notification iterator, "
618 "but stream is not ended: "
619 "%![stream-]s", stream_key);
620 ret = false;
621 goto end;
622 }
623 }
624
625 end:
626 return ret;
627 }
628
629 static
630 enum bt_notification_iterator_status
631 bt_priv_conn_private_notification_iterator_next(
632 struct bt_notification_iterator_private_connection *iterator)
633 {
634 struct bt_private_connection_private_notification_iterator *priv_iterator =
635 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator);
636 bt_component_class_notification_iterator_next_method next_method = NULL;
637 struct bt_notification_iterator_next_method_return next_return = {
638 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
639 .notification = NULL,
640 };
641 enum bt_notification_iterator_status status =
642 BT_NOTIFICATION_ITERATOR_STATUS_OK;
643
644 BT_ASSERT(iterator);
645 BT_LIB_LOGD("Getting next notification iterator's notification: %!+i",
646 iterator);
647 BT_ASSERT_PRE(iterator->state ==
648 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
649 "Notification iterator's \"next\" called, but "
650 "iterator is in the wrong state: %!+i", iterator);
651 BT_ASSERT(iterator->upstream_component);
652 BT_ASSERT(iterator->upstream_component->class);
653
654 /* Pick the appropriate "next" method */
655 switch (iterator->upstream_component->class->type) {
656 case BT_COMPONENT_CLASS_TYPE_SOURCE:
657 {
658 struct bt_component_class_source *source_class =
659 container_of(iterator->upstream_component->class,
660 struct bt_component_class_source, parent);
661
662 BT_ASSERT(source_class->methods.iterator.next);
663 next_method = source_class->methods.iterator.next;
664 break;
665 }
666 case BT_COMPONENT_CLASS_TYPE_FILTER:
667 {
668 struct bt_component_class_filter *filter_class =
669 container_of(iterator->upstream_component->class,
670 struct bt_component_class_filter, parent);
671
672 BT_ASSERT(filter_class->methods.iterator.next);
673 next_method = filter_class->methods.iterator.next;
674 break;
675 }
676 default:
677 abort();
678 }
679
680 /*
681 * Call the user's "next" method to get the next notification
682 * and status.
683 */
684 BT_ASSERT(next_method);
685 BT_LOGD_STR("Calling user's \"next\" method.");
686 next_return = next_method(priv_iterator);
687 BT_LOGD("User method returned: status=%s",
688 bt_notification_iterator_status_string(next_return.status));
689 if (next_return.status < 0) {
690 BT_LOGW_STR("User method failed.");
691 status = next_return.status;
692 goto end;
693 }
694
695 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
696 iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
697 /*
698 * The user's "next" method, somehow, cancelled its own
699 * notification iterator. This can happen, for example,
700 * when the user's method removes the port on which
701 * there's the connection from which the iterator was
702 * created. In this case, said connection is ended, and
703 * all its notification iterators are finalized.
704 *
705 * Only bt_put() the returned notification if
706 * the status is
707 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
708 * otherwise this field could be garbage.
709 */
710 if (next_return.status ==
711 BT_NOTIFICATION_ITERATOR_STATUS_OK) {
712 bt_put(next_return.notification);
713 }
714
715 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
716 goto end;
717 }
718
719 switch (next_return.status) {
720 case BT_NOTIFICATION_ITERATOR_STATUS_END:
721 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
722 "Notification iterator cannot end at this point: "
723 "%!+i", iterator);
724 BT_ASSERT(iterator->state ==
725 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE);
726 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED;
727 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
728 BT_LOGD("Set new status: status=%s",
729 bt_notification_iterator_status_string(status));
730 goto end;
731 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
732 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
733 goto end;
734 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
735 BT_ASSERT_PRE(next_return.notification,
736 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
737 "%!+i", iterator);
738 BT_ASSERT_PRE(validate_notification(iterator,
739 next_return.notification),
740 "Notification is invalid at this point: "
741 "%![notif-iter-]+i, %![notif-]+n",
742 iterator, next_return.notification);
743 bt_notification_iterator_replace_current_notification(
744 (void *) iterator, next_return.notification);
745 bt_put(next_return.notification);
746 break;
747 default:
748 /* Unknown non-error status */
749 abort();
750 }
751
752 end:
753 return status;
754 }
755
756 enum bt_notification_iterator_status
757 bt_notification_iterator_next(struct bt_notification_iterator *iterator)
758 {
759 enum bt_notification_iterator_status status;
760
761 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
762 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
763 BT_ASSERT(iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION ||
764 iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT);
765
766 switch (iterator->type) {
767 case BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION:
768 {
769 struct bt_notification_iterator_private_connection *priv_conn_iter =
770 (void *) iterator;
771
772 /*
773 * Make sure that the iterator's queue contains at least
774 * one notification.
775 */
776 status = bt_priv_conn_private_notification_iterator_next(
777 priv_conn_iter);
778 break;
779 }
780 case BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT:
781 {
782 struct bt_notification_iterator_output_port *out_port_iter =
783 (void *) iterator;
784
785 /*
786 * Keep current notification in case there's an error:
787 * restore this notification so that the current
788 * notification is not changed from the user's point of
789 * view.
790 */
791 struct bt_notification *old_notif =
792 bt_get(bt_notification_iterator_borrow_current_notification(iterator));
793 enum bt_graph_status graph_status;
794
795 /*
796 * Put current notification since it's possibly
797 * about to be replaced by a new one by the
798 * colander sink.
799 */
800 bt_notification_iterator_replace_current_notification(
801 iterator, NULL);
802 graph_status = bt_graph_consume_sink_no_check(
803 out_port_iter->graph, out_port_iter->colander);
804 switch (graph_status) {
805 case BT_GRAPH_STATUS_CANCELED:
806 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
807 break;
808 case BT_GRAPH_STATUS_AGAIN:
809 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
810 break;
811 case BT_GRAPH_STATUS_END:
812 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
813 break;
814 case BT_GRAPH_STATUS_NOMEM:
815 status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
816 break;
817 case BT_GRAPH_STATUS_OK:
818 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
819 BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator));
820 break;
821 default:
822 /* Other errors */
823 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
824 }
825
826 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
827 /* Error/exception: restore old notification */
828 bt_notification_iterator_replace_current_notification(
829 iterator, old_notif);
830 }
831
832 bt_put(old_notif);
833 break;
834 }
835 default:
836 abort();
837 }
838
839 return status;
840 }
841
842 struct bt_component *bt_private_connection_notification_iterator_get_component(
843 struct bt_notification_iterator *iterator)
844 {
845 struct bt_notification_iterator_private_connection *iter_priv_conn;
846
847 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
848 BT_ASSERT_PRE(iterator->type ==
849 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
850 "Notification iterator was not created from a private connection: "
851 "%!+i", iterator);
852 iter_priv_conn = (void *) iterator;
853 return bt_get(iter_priv_conn->upstream_component);
854 }
855
856 struct bt_private_component *
857 bt_private_connection_private_notification_iterator_get_private_component(
858 struct bt_private_connection_private_notification_iterator *private_iterator)
859 {
860 return bt_private_component_from_component(
861 bt_private_connection_notification_iterator_get_component(
862 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
863 }
864
865 static
866 void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
867 {
868 struct bt_notification_iterator_output_port *iterator =
869 (void *) container_of(obj, struct bt_notification_iterator, base);
870
871 BT_LOGD("Destroying output port notification iterator object: addr=%p",
872 iterator);
873 BT_LOGD_STR("Putting graph.");
874 bt_put(iterator->graph);
875 BT_LOGD_STR("Putting output port.");
876 bt_put(iterator->output_port);
877 BT_LOGD_STR("Putting colander sink component.");
878 bt_put(iterator->colander);
879 destroy_base_notification_iterator(obj);
880 }
881
882 struct bt_notification_iterator *bt_output_port_notification_iterator_create(
883 struct bt_port *output_port,
884 const char *colander_component_name)
885 {
886 struct bt_notification_iterator *iterator_base = NULL;
887 struct bt_notification_iterator_output_port *iterator = NULL;
888 struct bt_component_class *colander_comp_cls = NULL;
889 struct bt_component *output_port_comp = NULL;
890 struct bt_component *colander_comp;
891 struct bt_graph *graph = NULL;
892 enum bt_graph_status graph_status;
893 const char *colander_comp_name;
894 struct bt_port *colander_in_port = NULL;
895 struct bt_component_class_sink_colander_data colander_data;
896
897 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
898 BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
899 "Port is not an output port: %!+p", output_port);
900 output_port_comp = bt_port_get_component(output_port);
901 BT_ASSERT_PRE(output_port_comp,
902 "Output port has no component: %!+p", output_port);
903 graph = bt_component_get_graph(output_port_comp);
904 BT_ASSERT(graph);
905
906 /* Create notification iterator */
907 BT_LOGD("Creating notification iterator on output port: "
908 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
909 output_port_comp, bt_component_get_name(output_port_comp),
910 output_port, bt_port_get_name(output_port));
911 iterator = g_new0(struct bt_notification_iterator_output_port, 1);
912 if (!iterator) {
913 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
914 goto error;
915 }
916
917 init_notification_iterator((void *) iterator,
918 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
919 bt_output_port_notification_iterator_destroy);
920
921 /* Create colander component */
922 colander_comp_cls = bt_component_class_sink_colander_get();
923 if (!colander_comp_cls) {
924 BT_LOGW("Cannot get colander sink component class.");
925 goto error;
926 }
927
928 BT_MOVE(iterator->graph, graph);
929 iterator_base = (void *) iterator;
930 colander_comp_name =
931 colander_component_name ? colander_component_name : "colander";
932 colander_data.notification = &iterator_base->current_notification;
933 graph_status = bt_graph_add_component_with_init_method_data(
934 iterator->graph, colander_comp_cls, colander_comp_name,
935 NULL, &colander_data, &iterator->colander);
936 if (graph_status != BT_GRAPH_STATUS_OK) {
937 BT_LOGW("Cannot add colander sink component to graph: "
938 "graph-addr=%p, name=\"%s\", graph-status=%s",
939 iterator->graph, colander_comp_name,
940 bt_graph_status_string(graph_status));
941 goto error;
942 }
943
944 /*
945 * Connect provided output port to the colander component's
946 * input port.
947 */
948 colander_in_port = bt_component_sink_get_input_port_by_index(
949 iterator->colander, 0);
950 BT_ASSERT(colander_in_port);
951 graph_status = bt_graph_connect_ports(iterator->graph,
952 output_port, colander_in_port, NULL);
953 if (graph_status != BT_GRAPH_STATUS_OK) {
954 BT_LOGW("Cannot add colander sink component to graph: "
955 "graph-addr=%p, name=\"%s\", graph-status=%s",
956 iterator->graph, colander_comp_name,
957 bt_graph_status_string(graph_status));
958 goto error;
959 }
960
961 /*
962 * At this point everything went fine. Make the graph
963 * nonconsumable forever so that only this notification iterator
964 * can consume (thanks to bt_graph_consume_sink_no_check()).
965 * This avoids leaking the notification created by the colander
966 * sink and moved to the base notification iterator's current
967 * notification member.
968 */
969 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
970 goto end;
971
972 error:
973 if (iterator && iterator->graph && iterator->colander) {
974 int ret;
975
976 /* Remove created colander component from graph if any */
977 colander_comp = iterator->colander;
978 BT_PUT(iterator->colander);
979
980 /*
981 * At this point the colander component's reference
982 * count is 0 because iterator->colander was the only
983 * owner. We also know that it is not connected because
984 * this is the last operation before this function
985 * succeeds.
986 *
987 * Since we honor the preconditions here,
988 * bt_graph_remove_unconnected_component() always
989 * succeeds.
990 */
991 ret = bt_graph_remove_unconnected_component(iterator->graph,
992 colander_comp);
993 BT_ASSERT(ret == 0);
994 }
995
996 BT_PUT(iterator);
997
998 end:
999 bt_put(colander_in_port);
1000 bt_put(colander_comp_cls);
1001 bt_put(output_port_comp);
1002 bt_put(graph);
1003 return (void *) iterator;
1004 }
1005
1006 struct bt_notification_iterator *
1007 bt_private_connection_notification_iterator_from_private(
1008 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
1009 {
1010 return bt_get(
1011 bt_private_connection_notification_iterator_borrow_from_private(
1012 private_notification_iterator));
1013 }
This page took 0.080618 seconds and 4 git commands to generate.