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