lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
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
5af447e5
PP
28#define BT_LOG_TAG "NOTIF-ITER"
29#include <babeltrace/lib-logging-internal.h>
30
3d9990ac 31#include <babeltrace/compiler-internal.h>
b8a06801 32#include <babeltrace/ref.h>
2ec84d26
PP
33#include <babeltrace/ctf-ir/fields.h>
34#include <babeltrace/ctf-ir/field-types.h>
35#include <babeltrace/ctf-ir/field-types-internal.h>
3230ee6b
PP
36#include <babeltrace/ctf-ir/event-internal.h>
37#include <babeltrace/ctf-ir/packet-internal.h>
38#include <babeltrace/ctf-ir/stream-internal.h>
73d5c1ad 39#include <babeltrace/graph/connection.h>
bd14d768 40#include <babeltrace/graph/connection-internal.h>
b2e0c907
PP
41#include <babeltrace/graph/component.h>
42#include <babeltrace/graph/component-source-internal.h>
43#include <babeltrace/graph/component-class-internal.h>
8ed535b5
PP
44#include <babeltrace/graph/component-class-sink-colander-internal.h>
45#include <babeltrace/graph/component-sink.h>
fa054faf 46#include <babeltrace/graph/notification.h>
b2e0c907
PP
47#include <babeltrace/graph/notification-iterator.h>
48#include <babeltrace/graph/notification-iterator-internal.h>
e7fa96c3 49#include <babeltrace/graph/notification-internal.h>
3230ee6b
PP
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>
2ec84d26 56#include <babeltrace/graph/notification-discarded-elements-internal.h>
3230ee6b 57#include <babeltrace/graph/port.h>
8ed535b5 58#include <babeltrace/graph/graph-internal.h>
c55a9f58 59#include <babeltrace/types.h>
f6ccaed9 60#include <babeltrace/assert-internal.h>
f42867e2 61#include <babeltrace/assert-pre-internal.h>
fa054faf 62#include <stdint.h>
2ec84d26 63#include <inttypes.h>
0fbb9a9f 64#include <stdlib.h>
3230ee6b 65
2ec84d26 66struct discarded_elements_state {
50842bdc 67 struct bt_clock_value *cur_begin;
2ec84d26
PP
68 uint64_t cur_count;
69};
70
3230ee6b 71struct stream_state {
50842bdc
PP
72 struct bt_stream *stream; /* owned by this */
73 struct bt_packet *cur_packet; /* owned by this */
2ec84d26
PP
74 struct discarded_elements_state discarded_packets_state;
75 struct discarded_elements_state discarded_events_state;
f42867e2 76 uint64_t expected_notif_seq_num;
c55a9f58 77 bt_bool is_ended;
3230ee6b
PP
78};
79
3230ee6b 80static
3dca2276 81void stream_destroy_listener(struct bt_stream_common *stream, void *data)
3230ee6b 82{
90157d89 83 struct bt_notification_iterator_private_connection *iterator = data;
3230ee6b
PP
84
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator->stream_states, stream);
87}
88
89static
90void destroy_stream_state(struct stream_state *stream_state)
91{
92 if (!stream_state) {
93 return;
94 }
95
5af447e5
PP
96 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
97 BT_LOGV_STR("Putting stream state's current packet.");
3230ee6b 98 bt_put(stream_state->cur_packet);
5af447e5 99 BT_LOGV_STR("Putting stream state's stream.");
3230ee6b 100 bt_put(stream_state->stream);
2ec84d26
PP
101 bt_put(stream_state->discarded_packets_state.cur_begin);
102 bt_put(stream_state->discarded_events_state.cur_begin);
3230ee6b
PP
103 g_free(stream_state);
104}
105
3230ee6b 106static
50842bdc 107struct stream_state *create_stream_state(struct bt_stream *stream)
3230ee6b
PP
108{
109 struct stream_state *stream_state = g_new0(struct stream_state, 1);
110
111 if (!stream_state) {
5af447e5 112 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
113 goto end;
114 }
115
126215b4
MD
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
f42867e2 121 * the sequence number 0.
126215b4
MD
122 */
123 stream_state->discarded_packets_state.cur_count = -1ULL;
124
3230ee6b 125 /*
f42867e2 126 * We keep a reference to the stream until we know it's ended.
3230ee6b
PP
127 */
128 stream_state->stream = bt_get(stream);
5af447e5
PP
129 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
130 "stream-state-addr=%p",
50842bdc 131 stream, bt_stream_get_name(stream), stream_state);
3230ee6b
PP
132
133end:
134 return stream_state;
135}
47e5a032 136
8ed535b5
PP
137static
138void 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
47e5a032 148static
90157d89 149void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
47e5a032 150{
90157d89 151 struct bt_notification_iterator_private_connection *iterator;
8738a040 152
f6ccaed9 153 BT_ASSERT(obj);
d3eb6e8f 154
bd14d768
PP
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++;
90157d89 166 iterator = (void *) container_of(obj, struct bt_notification_iterator, base);
8ed535b5 167 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
5af447e5 168 iterator);
90157d89 169 bt_private_connection_notification_iterator_finalize(iterator);
d3eb6e8f 170
3230ee6b
PP
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)) {
f6ccaed9 184 BT_ASSERT(stream_gptr);
5af447e5
PP
185
186 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
3dca2276 187 bt_stream_common_remove_destroy_listener(
3230ee6b
PP
188 (void *) stream_gptr, stream_destroy_listener,
189 iterator);
190 }
191
192 g_hash_table_destroy(iterator->stream_states);
193 }
194
bd14d768
PP
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
8ed535b5 204 destroy_base_notification_iterator(obj);
47e5a032
JG
205}
206
bd14d768 207BT_HIDDEN
90157d89
PP
208void bt_private_connection_notification_iterator_finalize(
209 struct bt_notification_iterator_private_connection *iterator)
bd14d768
PP
210{
211 struct bt_component_class *comp_class = NULL;
212 bt_component_class_notification_iterator_finalize_method
213 finalize_method = NULL;
214
f6ccaed9 215 BT_ASSERT(iterator);
bd14d768
PP
216
217 switch (iterator->state) {
90157d89 218 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
088d4023
PP
219 /* Skip user finalization if user initialization failed */
220 BT_LOGD("Not finalizing non-initialized notification iterator: "
221 "addr=%p", iterator);
222 return;
90157d89
PP
223 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
224 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
bd14d768 225 /* Already finalized */
5af447e5
PP
226 BT_LOGD("Not finalizing notification iterator: already finalized: "
227 "addr=%p", iterator);
bd14d768
PP
228 return;
229 default:
230 break;
231 }
232
5af447e5
PP
233 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
234
90157d89 235 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
5af447e5 236 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
238 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
df14f8af 239 } else {
5af447e5 240 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
241 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
242 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
df14f8af
MD
243 }
244
f6ccaed9 245 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
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 */
0fbb9a9f 268 abort();
bd14d768
PP
269 }
270
271 if (finalize_method) {
5af447e5
PP
272 BT_LOGD("Calling user's finalization method: addr=%p",
273 iterator);
bd14d768 274 finalize_method(
90157d89 275 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
bd14d768
PP
276 }
277
bd14d768
PP
278 iterator->upstream_component = NULL;
279 iterator->upstream_port = NULL;
5af447e5 280 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
bd14d768
PP
281}
282
283BT_HIDDEN
90157d89
PP
284void bt_private_connection_notification_iterator_set_connection(
285 struct bt_notification_iterator_private_connection *iterator,
bd14d768
PP
286 struct bt_connection *connection)
287{
f6ccaed9 288 BT_ASSERT(iterator);
bd14d768 289 iterator->connection = connection;
5af447e5
PP
290 BT_LOGV("Set notification iterator's connection: "
291 "iter-addr=%p, conn-addr=%p", iterator, connection);
bd14d768
PP
292}
293
90157d89
PP
294static
295void 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
47e5a032 303BT_HIDDEN
90157d89 304enum bt_connection_status bt_private_connection_notification_iterator_create(
3230ee6b 305 struct bt_component *upstream_comp,
fa054faf 306 struct bt_port *upstream_port,
73d5c1ad 307 struct bt_connection *connection,
90157d89 308 struct bt_notification_iterator_private_connection **user_iterator)
47e5a032 309{
73d5c1ad 310 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
d3e4dcd8 311 enum bt_component_class_type type;
90157d89 312 struct bt_notification_iterator_private_connection *iterator = NULL;
47e5a032 313
f6ccaed9
PP
314 BT_ASSERT(upstream_comp);
315 BT_ASSERT(upstream_port);
f6ccaed9
PP
316 BT_ASSERT(bt_port_is_connected(upstream_port));
317 BT_ASSERT(user_iterator);
8ed535b5 318 BT_LOGD("Creating notification iterator on private connection: "
5af447e5
PP
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);
3230ee6b 325 type = bt_component_get_class_type(upstream_comp);
f6ccaed9 326 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
ef2f7566 327 type == BT_COMPONENT_CLASS_TYPE_FILTER);
90157d89 328 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
47e5a032 329 if (!iterator) {
8ed535b5 330 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
73d5c1ad
PP
331 status = BT_CONNECTION_STATUS_NOMEM;
332 goto end;
47e5a032
JG
333 }
334
90157d89
PP
335 init_notification_iterator((void *) iterator,
336 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
337 bt_private_connection_notification_iterator_destroy);
3230ee6b
PP
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) {
5af447e5 342 BT_LOGE_STR("Failed to allocate a GHashTable.");
73d5c1ad
PP
343 status = BT_CONNECTION_STATUS_NOMEM;
344 goto end;
3230ee6b
PP
345 }
346
bd14d768
PP
347 iterator->upstream_component = upstream_comp;
348 iterator->upstream_port = upstream_port;
349 iterator->connection = connection;
90157d89 350 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
5af447e5
PP
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);
1a6a376a
PP
358
359 /* Move reference to user */
360 *user_iterator = iterator;
361 iterator = NULL;
3230ee6b 362
47e5a032 363end:
73d5c1ad
PP
364 bt_put(iterator);
365 return status;
47e5a032
JG
366}
367
90157d89
PP
368void *bt_private_connection_private_notification_iterator_get_user_data(
369 struct bt_private_connection_private_notification_iterator *private_iterator)
ea8d3e58 370{
90157d89 371 struct bt_notification_iterator_private_connection *iterator =
6d137876 372 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
890882ef 373
f42867e2
PP
374 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
375 return iterator->user_data;
ea8d3e58
JG
376}
377
378enum bt_notification_iterator_status
90157d89
PP
379bt_private_connection_private_notification_iterator_set_user_data(
380 struct bt_private_connection_private_notification_iterator *private_iterator,
890882ef 381 void *data)
ea8d3e58 382{
90157d89 383 struct bt_notification_iterator_private_connection *iterator =
6d137876 384 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
ea8d3e58 385
f42867e2 386 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
ea8d3e58 387 iterator->user_data = data;
5af447e5
PP
388 BT_LOGV("Set notification iterator's user data: "
389 "iter-addr=%p, user-data-addr=%p", iterator, data);
f42867e2 390 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
8738a040 391}
413bc2c4 392
094ff7c0 393struct bt_notification *bt_notification_iterator_borrow_notification(
53d45b87
JG
394 struct bt_notification_iterator *iterator)
395{
f42867e2 396 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
094ff7c0 397 return bt_notification_iterator_borrow_current_notification(iterator);
53d45b87
JG
398}
399
f42867e2
PP
400BT_ASSERT_PRE_FUNC
401static inline
402void bt_notification_borrow_packet_stream(struct bt_notification *notif,
403 struct bt_stream **stream, struct bt_packet **packet)
fa054faf 404{
f42867e2 405 BT_ASSERT(notif);
fa054faf 406
f42867e2 407 switch (notif->type) {
fa054faf 408 case BT_NOTIFICATION_TYPE_EVENT:
f42867e2
PP
409 *packet = bt_event_borrow_packet(
410 bt_notification_event_borrow_event(notif));
411 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
412 break;
413 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
f42867e2 414 *stream = bt_notification_stream_begin_borrow_stream(notif);
fa054faf
PP
415 break;
416 case BT_NOTIFICATION_TYPE_STREAM_END:
f42867e2 417 *stream = bt_notification_stream_end_borrow_stream(notif);
fa054faf
PP
418 break;
419 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
f42867e2
PP
420 *packet = bt_notification_packet_begin_borrow_packet(notif);
421 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
422 break;
423 case BT_NOTIFICATION_TYPE_PACKET_END:
f42867e2
PP
424 *packet = bt_notification_packet_end_borrow_packet(notif);
425 *stream = bt_packet_borrow_stream(*packet);
2ec84d26 426 break;
fa054faf 427 default:
f42867e2 428 break;
fa054faf 429 }
fa054faf
PP
430}
431
f42867e2
PP
432BT_ASSERT_PRE_FUNC
433static inline
434bool validate_notification(
90157d89 435 struct bt_notification_iterator_private_connection *iterator,
f42867e2 436 struct bt_notification *notif)
3230ee6b 437{
f42867e2 438 bool is_valid = true;
3230ee6b 439 struct stream_state *stream_state;
f42867e2
PP
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 }
3230ee6b 450
f42867e2
PP
451 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
452 if (!stream_state) {
3230ee6b 453 /*
f42867e2
PP
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.
3230ee6b 457 */
f42867e2
PP
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;
3230ee6b
PP
464 goto end;
465 }
466
f42867e2
PP
467 if (notif->seq_num == -1ULL) {
468 notif->seq_num = 0;
3230ee6b
PP
469 }
470
f42867e2
PP
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;
3230ee6b 479 goto end;
3230ee6b 480 }
3230ee6b 481
f42867e2
PP
482 stream_state = create_stream_state(stream);
483 if (!stream_state) {
484 abort();
485 }
fa054faf 486
f42867e2
PP
487 g_hash_table_insert(iterator->stream_states, stream,
488 stream_state);
489 stream_state->expected_notif_seq_num++;
490 goto end;
fa054faf
PP
491 }
492
f42867e2
PP
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;
fa054faf
PP
504 goto end;
505 }
506
f42867e2
PP
507 if (notif->seq_num == -1ULL) {
508 notif->seq_num = stream_state->expected_notif_seq_num;
3230ee6b
PP
509 }
510
f42867e2
PP
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;
fa054faf
PP
519 goto end;
520 }
521
f42867e2
PP
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;
3230ee6b
PP
588 }
589
3230ee6b 590end:
f42867e2 591 return is_valid;
3230ee6b
PP
592}
593
f42867e2
PP
594BT_ASSERT_PRE_FUNC
595static inline bool priv_conn_notif_iter_can_end(
596 struct bt_notification_iterator_private_connection *iterator)
3230ee6b 597{
f42867e2
PP
598 GHashTableIter iter;
599 gpointer stream_key, state_value;
600 bool ret = true;
3230ee6b 601
f42867e2
PP
602 /*
603 * Verify that this iterator received a
604 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
605 * which has a state.
606 */
3230ee6b 607
f42867e2 608 g_hash_table_iter_init(&iter, iterator->stream_states);
3230ee6b 609
f42867e2
PP
610 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
611 struct stream_state *stream_state = (void *) state_value;
3230ee6b 612
f42867e2
PP
613 BT_ASSERT(stream_state);
614 BT_ASSERT(stream_key);
fa054faf 615
f42867e2
PP
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 }
3230ee6b
PP
623 }
624
3230ee6b 625end:
3230ee6b
PP
626 return ret;
627}
628
629static
f42867e2
PP
630enum bt_notification_iterator_status
631bt_priv_conn_private_notification_iterator_next(
632 struct bt_notification_iterator_private_connection *iterator)
3230ee6b 633{
f42867e2
PP
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,
3230ee6b 640 };
f42867e2
PP
641 enum bt_notification_iterator_status status =
642 BT_NOTIFICATION_ITERATOR_STATUS_OK;
3230ee6b 643
f42867e2
PP
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);
d3eb6e8f 653
3230ee6b
PP
654 /* Pick the appropriate "next" method */
655 switch (iterator->upstream_component->class->type) {
d3eb6e8f
PP
656 case BT_COMPONENT_CLASS_TYPE_SOURCE:
657 {
658 struct bt_component_class_source *source_class =
3230ee6b 659 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
660 struct bt_component_class_source, parent);
661
f6ccaed9 662 BT_ASSERT(source_class->methods.iterator.next);
d3eb6e8f
PP
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 =
3230ee6b 669 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
670 struct bt_component_class_filter, parent);
671
f6ccaed9 672 BT_ASSERT(filter_class->methods.iterator.next);
d3eb6e8f
PP
673 next_method = filter_class->methods.iterator.next;
674 break;
675 }
676 default:
0fbb9a9f 677 abort();
d3eb6e8f
PP
678 }
679
3230ee6b
PP
680 /*
681 * Call the user's "next" method to get the next notification
fa054faf 682 * and status.
3230ee6b 683 */
f6ccaed9 684 BT_ASSERT(next_method);
f42867e2
PP
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 }
3230ee6b 694
f42867e2
PP
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);
3230ee6b
PP
713 }
714
f42867e2
PP
715 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
716 goto end;
717 }
8cf27cc5 718
f42867e2
PP
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();
41a2b7ae
PP
750 }
751
752end:
3230ee6b
PP
753 return status;
754}
755
756enum bt_notification_iterator_status
757bt_notification_iterator_next(struct bt_notification_iterator *iterator)
758{
759 enum bt_notification_iterator_status status;
760
f42867e2 761 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
5af447e5 762 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
f42867e2
PP
763 BT_ASSERT(iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION ||
764 iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT);
5af447e5 765
90157d89
PP
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;
3230ee6b 771
90157d89
PP
772 /*
773 * Make sure that the iterator's queue contains at least
774 * one notification.
775 */
f42867e2
PP
776 status = bt_priv_conn_private_notification_iterator_next(
777 priv_conn_iter);
8ed535b5
PP
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(
f42867e2 803 out_port_iter->graph, out_port_iter->colander);
8ed535b5
PP
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;
f6ccaed9 819 BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator));
8ed535b5
PP
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);
90157d89
PP
833 break;
834 }
835 default:
90157d89
PP
836 abort();
837 }
3230ee6b 838
3230ee6b 839 return status;
53d45b87
JG
840}
841
90157d89 842struct bt_component *bt_private_connection_notification_iterator_get_component(
413bc2c4
JG
843 struct bt_notification_iterator *iterator)
844{
90157d89
PP
845 struct bt_notification_iterator_private_connection *iter_priv_conn;
846
f42867e2
PP
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);
90157d89 852 iter_priv_conn = (void *) iterator;
f42867e2 853 return bt_get(iter_priv_conn->upstream_component);
413bc2c4
JG
854}
855
91457551 856struct bt_private_component *
90157d89
PP
857bt_private_connection_private_notification_iterator_get_private_component(
858 struct bt_private_connection_private_notification_iterator *private_iterator)
91457551
PP
859{
860 return bt_private_component_from_component(
90157d89 861 bt_private_connection_notification_iterator_get_component(
6d137876 862 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
91457551 863}
8ed535b5
PP
864
865static
866void 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
882struct bt_notification_iterator *bt_output_port_notification_iterator_create(
883 struct bt_port *output_port,
f42867e2 884 const char *colander_component_name)
8ed535b5
PP
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
f42867e2
PP
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);
8ed535b5 900 output_port_comp = bt_port_get_component(output_port);
f42867e2
PP
901 BT_ASSERT_PRE(output_port_comp,
902 "Output port has no component: %!+p", output_port);
8ed535b5 903 graph = bt_component_get_graph(output_port_comp);
f6ccaed9 904 BT_ASSERT(graph);
8ed535b5
PP
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;
8ed535b5
PP
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);
f6ccaed9 950 BT_ASSERT(colander_in_port);
8ed535b5
PP
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
972error:
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);
f6ccaed9 993 BT_ASSERT(ret == 0);
8ed535b5
PP
994 }
995
996 BT_PUT(iterator);
997
998end:
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}
25b68514
PP
1005
1006struct bt_notification_iterator *
1007bt_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.088724 seconds and 4 git commands to generate.