Commit | Line | Data |
---|---|---|
47e5a032 | 1 | /* |
0554db57 | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
47e5a032 | 3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@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 | ||
c14831f0 | 24 | #define BT_LOG_TAG "MSG-ITER" |
5af447e5 PP |
25 | #include <babeltrace/lib-logging-internal.h> |
26 | ||
3d9990ac | 27 | #include <babeltrace/compiler-internal.h> |
964f7e04 PP |
28 | #include <babeltrace/trace-ir/clock-class-internal.h> |
29 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> | |
c319d0ae | 30 | #include <babeltrace/trace-ir/field.h> |
dadf29b9 | 31 | #include <babeltrace/trace-ir/event-const.h> |
577dc45a | 32 | #include <babeltrace/trace-ir/event-internal.h> |
dadf29b9 | 33 | #include <babeltrace/trace-ir/packet-const.h> |
577dc45a PP |
34 | #include <babeltrace/trace-ir/packet-internal.h> |
35 | #include <babeltrace/trace-ir/stream-internal.h> | |
96ffdc46 | 36 | #include <babeltrace/graph/connection-const.h> |
bd14d768 | 37 | #include <babeltrace/graph/connection-internal.h> |
96ffdc46 | 38 | #include <babeltrace/graph/component-const.h> |
351946b4 | 39 | #include <babeltrace/graph/component-internal.h> |
b2e0c907 PP |
40 | #include <babeltrace/graph/component-source-internal.h> |
41 | #include <babeltrace/graph/component-class-internal.h> | |
8ed535b5 | 42 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
96ffdc46 | 43 | #include <babeltrace/graph/component-sink-const.h> |
9599065d | 44 | #include <babeltrace/graph/component-sink-internal.h> |
c14831f0 | 45 | #include <babeltrace/graph/message-const.h> |
9599065d | 46 | #include <babeltrace/graph/message-iterator-const.h> |
c14831f0 PP |
47 | #include <babeltrace/graph/message-iterator-internal.h> |
48 | #include <babeltrace/graph/self-component-port-input-message-iterator.h> | |
49 | #include <babeltrace/graph/port-output-message-iterator.h> | |
50 | #include <babeltrace/graph/message-internal.h> | |
51 | #include <babeltrace/graph/message-event-const.h> | |
52 | #include <babeltrace/graph/message-event-internal.h> | |
2accc83a PP |
53 | #include <babeltrace/graph/message-packet-beginning-const.h> |
54 | #include <babeltrace/graph/message-packet-end-const.h> | |
c14831f0 | 55 | #include <babeltrace/graph/message-packet-internal.h> |
2accc83a PP |
56 | #include <babeltrace/graph/message-stream-beginning-const.h> |
57 | #include <babeltrace/graph/message-stream-end-const.h> | |
c14831f0 | 58 | #include <babeltrace/graph/message-stream-internal.h> |
12ccec6b | 59 | #include <babeltrace/graph/message-message-iterator-inactivity-internal.h> |
902bc5f0 PP |
60 | #include <babeltrace/graph/message-discarded-items-internal.h> |
61 | #include <babeltrace/graph/message-stream-activity-internal.h> | |
96ffdc46 PP |
62 | #include <babeltrace/graph/port-const.h> |
63 | #include <babeltrace/graph/graph.h> | |
64 | #include <babeltrace/graph/graph-const.h> | |
8ed535b5 | 65 | #include <babeltrace/graph/graph-internal.h> |
c55a9f58 | 66 | #include <babeltrace/types.h> |
f6ccaed9 | 67 | #include <babeltrace/assert-internal.h> |
f42867e2 | 68 | #include <babeltrace/assert-pre-internal.h> |
fa054faf | 69 | #include <stdint.h> |
2ec84d26 | 70 | #include <inttypes.h> |
0fbb9a9f | 71 | #include <stdlib.h> |
3230ee6b | 72 | |
d4393e08 PP |
73 | /* |
74 | * TODO: Use graph's state (number of active iterators, etc.) and | |
75 | * possibly system specifications to make a better guess than this. | |
76 | */ | |
c14831f0 | 77 | #define MSG_BATCH_SIZE 15 |
d4393e08 | 78 | |
9599065d PP |
79 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
80 | BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \ | |
81 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \ | |
82 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ | |
83 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ | |
84 | "Message iterator is in the wrong state: %!+i", _iter) | |
47e5a032 | 85 | |
44b2734f PP |
86 | static inline |
87 | void _set_self_comp_port_input_msg_iterator_state( | |
88 | struct bt_self_component_port_input_message_iterator *iterator, | |
89 | enum bt_self_component_port_input_message_iterator_state state) | |
90 | { | |
91 | BT_ASSERT(iterator); | |
9599065d | 92 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
44b2734f PP |
93 | bt_self_component_port_input_message_iterator_state_string(state)); |
94 | iterator->state = state; | |
95 | } | |
96 | ||
97 | #ifdef BT_DEV_MODE | |
98 | # define set_self_comp_port_input_msg_iterator_state _set_self_comp_port_input_msg_iterator_state | |
99 | #else | |
a73035a3 | 100 | # define set_self_comp_port_input_msg_iterator_state(_a, _b) ((void) _a); ((void) _b); |
44b2734f PP |
101 | #endif |
102 | ||
8ed535b5 | 103 | static |
c14831f0 | 104 | void destroy_base_message_iterator(struct bt_object *obj) |
8ed535b5 | 105 | { |
c14831f0 | 106 | struct bt_message_iterator *iterator = (void *) obj; |
d4393e08 PP |
107 | |
108 | BT_ASSERT(iterator); | |
109 | ||
c14831f0 PP |
110 | if (iterator->msgs) { |
111 | g_ptr_array_free(iterator->msgs, TRUE); | |
112 | iterator->msgs = NULL; | |
d4393e08 PP |
113 | } |
114 | ||
115 | g_free(iterator); | |
8ed535b5 PP |
116 | } |
117 | ||
47e5a032 | 118 | static |
c14831f0 | 119 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
47e5a032 | 120 | { |
c14831f0 | 121 | struct bt_self_component_port_input_message_iterator *iterator; |
8738a040 | 122 | |
f6ccaed9 | 123 | BT_ASSERT(obj); |
d3eb6e8f | 124 | |
bd14d768 | 125 | /* |
c14831f0 | 126 | * The message iterator's reference count is 0 if we're |
bd14d768 PP |
127 | * here. Increment it to avoid a double-destroy (possibly |
128 | * infinitely recursive). This could happen for example if the | |
c14831f0 | 129 | * message iterator's finalization function does |
d27a81b2 PP |
130 | * bt_object_get_ref() (or anything that causes |
131 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
132 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
133 | * count would go from 1 to 0 again and this function would be | |
134 | * called again. | |
bd14d768 | 135 | */ |
3fea54f6 | 136 | obj->ref_count++; |
07245ac2 | 137 | iterator = (void *) obj; |
c14831f0 | 138 | BT_LIB_LOGD("Destroying self component input port message iterator object: " |
d27a81b2 | 139 | "%!+i", iterator); |
44b2734f | 140 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
d3eb6e8f | 141 | |
bd14d768 PP |
142 | if (iterator->connection) { |
143 | /* | |
144 | * Remove ourself from the originating connection so | |
145 | * that it does not try to finalize a dangling pointer | |
146 | * later. | |
147 | */ | |
148 | bt_connection_remove_iterator(iterator->connection, iterator); | |
d27a81b2 | 149 | iterator->connection = NULL; |
bd14d768 PP |
150 | } |
151 | ||
9599065d | 152 | if (iterator->auto_seek_msgs) { |
964f7e04 PP |
153 | while (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
154 | bt_object_put_no_null_check( | |
155 | g_queue_pop_tail(iterator->auto_seek_msgs)); | |
9599065d PP |
156 | } |
157 | ||
964f7e04 | 158 | g_queue_free(iterator->auto_seek_msgs); |
9599065d PP |
159 | iterator->auto_seek_msgs = NULL; |
160 | } | |
161 | ||
c14831f0 | 162 | destroy_base_message_iterator(obj); |
47e5a032 JG |
163 | } |
164 | ||
bd14d768 | 165 | BT_HIDDEN |
44b2734f | 166 | void bt_self_component_port_input_message_iterator_try_finalize( |
c14831f0 | 167 | struct bt_self_component_port_input_message_iterator *iterator) |
bd14d768 | 168 | { |
d27a81b2 PP |
169 | typedef void (*method_t)(void *); |
170 | ||
bd14d768 | 171 | struct bt_component_class *comp_class = NULL; |
d27a81b2 | 172 | method_t method = NULL; |
bd14d768 | 173 | |
f6ccaed9 | 174 | BT_ASSERT(iterator); |
bd14d768 PP |
175 | |
176 | switch (iterator->state) { | |
c14831f0 | 177 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 | 178 | /* Skip user finalization if user initialization failed */ |
c14831f0 | 179 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
d27a81b2 | 180 | "%!+i", iterator); |
44b2734f | 181 | goto end; |
c14831f0 | 182 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
bd14d768 | 183 | /* Already finalized */ |
c14831f0 | 184 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
d27a81b2 | 185 | "%!+i", iterator); |
44b2734f PP |
186 | goto end; |
187 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: | |
188 | /* Already finalized */ | |
189 | BT_LIB_LOGF("Message iterator is already being finalized: " | |
190 | "%!+i", iterator); | |
191 | abort(); | |
bd14d768 PP |
192 | default: |
193 | break; | |
194 | } | |
195 | ||
c14831f0 | 196 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
44b2734f PP |
197 | set_self_comp_port_input_msg_iterator_state(iterator, |
198 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); | |
f6ccaed9 | 199 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
200 | comp_class = iterator->upstream_component->class; |
201 | ||
202 | /* Call user-defined destroy method */ | |
203 | switch (comp_class->type) { | |
204 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
205 | { | |
d27a81b2 PP |
206 | struct bt_component_class_source *src_comp_cls = |
207 | (void *) comp_class; | |
bd14d768 | 208 | |
c14831f0 | 209 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
210 | break; |
211 | } | |
212 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
213 | { | |
d27a81b2 PP |
214 | struct bt_component_class_filter *flt_comp_cls = |
215 | (void *) comp_class; | |
bd14d768 | 216 | |
c14831f0 | 217 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
218 | break; |
219 | } | |
220 | default: | |
221 | /* Unreachable */ | |
0fbb9a9f | 222 | abort(); |
bd14d768 PP |
223 | } |
224 | ||
d27a81b2 PP |
225 | if (method) { |
226 | BT_LIB_LOGD("Calling user's finalization method: %!+i", | |
5af447e5 | 227 | iterator); |
d27a81b2 | 228 | method(iterator); |
bd14d768 PP |
229 | } |
230 | ||
bd14d768 PP |
231 | iterator->upstream_component = NULL; |
232 | iterator->upstream_port = NULL; | |
44b2734f PP |
233 | set_self_comp_port_input_msg_iterator_state(iterator, |
234 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); | |
c14831f0 | 235 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
44b2734f PP |
236 | |
237 | end: | |
238 | return; | |
bd14d768 PP |
239 | } |
240 | ||
241 | BT_HIDDEN | |
c14831f0 PP |
242 | void bt_self_component_port_input_message_iterator_set_connection( |
243 | struct bt_self_component_port_input_message_iterator *iterator, | |
bd14d768 PP |
244 | struct bt_connection *connection) |
245 | { | |
f6ccaed9 | 246 | BT_ASSERT(iterator); |
bd14d768 | 247 | iterator->connection = connection; |
c14831f0 | 248 | BT_LIB_LOGV("Set message iterator's connection: " |
d27a81b2 | 249 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
bd14d768 PP |
250 | } |
251 | ||
90157d89 | 252 | static |
c14831f0 PP |
253 | int init_message_iterator(struct bt_message_iterator *iterator, |
254 | enum bt_message_iterator_type type, | |
90157d89 PP |
255 | bt_object_release_func destroy) |
256 | { | |
d4393e08 PP |
257 | int ret = 0; |
258 | ||
3fea54f6 | 259 | bt_object_init_shared(&iterator->base, destroy); |
90157d89 | 260 | iterator->type = type; |
c14831f0 PP |
261 | iterator->msgs = g_ptr_array_new(); |
262 | if (!iterator->msgs) { | |
d4393e08 PP |
263 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
264 | ret = -1; | |
265 | goto end; | |
266 | } | |
267 | ||
c14831f0 | 268 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
d4393e08 PP |
269 | |
270 | end: | |
271 | return ret; | |
90157d89 PP |
272 | } |
273 | ||
9599065d PP |
274 | static |
275 | bt_bool can_seek_ns_from_origin_true( | |
276 | struct bt_self_component_port_input_message_iterator *iterator, | |
277 | int64_t ns_from_origin) | |
278 | { | |
279 | return BT_TRUE; | |
280 | } | |
281 | ||
282 | static | |
283 | bt_bool can_seek_beginning_true( | |
284 | struct bt_self_component_port_input_message_iterator *iterator) | |
285 | { | |
286 | return BT_TRUE; | |
287 | } | |
288 | ||
d27a81b2 | 289 | static |
c14831f0 PP |
290 | struct bt_self_component_port_input_message_iterator * |
291 | bt_self_component_port_input_message_iterator_create_initial( | |
3230ee6b | 292 | struct bt_component *upstream_comp, |
d27a81b2 | 293 | struct bt_port *upstream_port) |
47e5a032 | 294 | { |
d4393e08 | 295 | int ret; |
c14831f0 | 296 | struct bt_self_component_port_input_message_iterator *iterator = NULL; |
47e5a032 | 297 | |
f6ccaed9 PP |
298 | BT_ASSERT(upstream_comp); |
299 | BT_ASSERT(upstream_port); | |
f6ccaed9 | 300 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
c14831f0 | 301 | BT_LIB_LOGD("Creating initial message iterator on self component input port: " |
d27a81b2 PP |
302 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); |
303 | BT_ASSERT(bt_component_get_class_type(upstream_comp) == | |
304 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
305 | bt_component_get_class_type(upstream_comp) == | |
306 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
307 | iterator = g_new0( | |
c14831f0 | 308 | struct bt_self_component_port_input_message_iterator, 1); |
47e5a032 | 309 | if (!iterator) { |
d27a81b2 | 310 | BT_LOGE_STR("Failed to allocate one self component input port " |
c14831f0 | 311 | "message iterator."); |
73d5c1ad | 312 | goto end; |
47e5a032 JG |
313 | } |
314 | ||
c14831f0 PP |
315 | ret = init_message_iterator((void *) iterator, |
316 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT, | |
317 | bt_self_component_port_input_message_iterator_destroy); | |
d4393e08 | 318 | if (ret) { |
c14831f0 | 319 | /* init_message_iterator() logs errors */ |
d27a81b2 | 320 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
321 | goto end; |
322 | } | |
3230ee6b | 323 | |
964f7e04 | 324 | iterator->auto_seek_msgs = g_queue_new(); |
9599065d | 325 | if (!iterator->auto_seek_msgs) { |
964f7e04 | 326 | BT_LOGE_STR("Failed to allocate a GQueue."); |
9599065d | 327 | ret = -1; |
73d5c1ad | 328 | goto end; |
3230ee6b PP |
329 | } |
330 | ||
bd14d768 PP |
331 | iterator->upstream_component = upstream_comp; |
332 | iterator->upstream_port = upstream_port; | |
d27a81b2 | 333 | iterator->connection = iterator->upstream_port->connection; |
5c563278 | 334 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
44b2734f PP |
335 | set_self_comp_port_input_msg_iterator_state(iterator, |
336 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); | |
9599065d PP |
337 | |
338 | switch (iterator->upstream_component->class->type) { | |
339 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
340 | { | |
341 | struct bt_component_class_source *src_comp_cls = | |
342 | (void *) iterator->upstream_component->class; | |
343 | ||
344 | iterator->methods.next = | |
345 | (bt_self_component_port_input_message_iterator_next_method) | |
346 | src_comp_cls->methods.msg_iter_next; | |
347 | iterator->methods.seek_ns_from_origin = | |
348 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
349 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
350 | iterator->methods.seek_beginning = | |
351 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
352 | src_comp_cls->methods.msg_iter_seek_beginning; | |
353 | iterator->methods.can_seek_ns_from_origin = | |
354 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
355 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
356 | iterator->methods.can_seek_beginning = | |
357 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
358 | src_comp_cls->methods.msg_iter_can_seek_beginning; | |
359 | break; | |
360 | } | |
361 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
362 | { | |
363 | struct bt_component_class_filter *flt_comp_cls = | |
364 | (void *) iterator->upstream_component->class; | |
365 | ||
366 | iterator->methods.next = | |
367 | (bt_self_component_port_input_message_iterator_next_method) | |
368 | flt_comp_cls->methods.msg_iter_next; | |
369 | iterator->methods.seek_ns_from_origin = | |
370 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
371 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
372 | iterator->methods.seek_beginning = | |
373 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
374 | flt_comp_cls->methods.msg_iter_seek_beginning; | |
375 | iterator->methods.can_seek_ns_from_origin = | |
376 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
377 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
378 | iterator->methods.can_seek_beginning = | |
379 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
380 | flt_comp_cls->methods.msg_iter_can_seek_beginning; | |
381 | break; | |
382 | } | |
383 | default: | |
384 | abort(); | |
385 | } | |
386 | ||
387 | if (iterator->methods.seek_ns_from_origin && | |
388 | !iterator->methods.can_seek_ns_from_origin) { | |
389 | iterator->methods.can_seek_ns_from_origin = | |
390 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
391 | can_seek_ns_from_origin_true; | |
392 | } | |
393 | ||
394 | if (iterator->methods.seek_beginning && | |
395 | !iterator->methods.can_seek_beginning) { | |
396 | iterator->methods.can_seek_beginning = | |
397 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
398 | can_seek_beginning_true; | |
399 | } | |
400 | ||
c14831f0 | 401 | BT_LIB_LOGD("Created initial message iterator on self component input port: " |
d27a81b2 PP |
402 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
403 | upstream_port, upstream_comp, iterator); | |
3230ee6b | 404 | |
47e5a032 | 405 | end: |
d27a81b2 | 406 | return iterator; |
47e5a032 JG |
407 | } |
408 | ||
c14831f0 PP |
409 | struct bt_self_component_port_input_message_iterator * |
410 | bt_self_component_port_input_message_iterator_create( | |
d27a81b2 | 411 | struct bt_self_component_port_input *self_port) |
ea8d3e58 | 412 | { |
c14831f0 | 413 | typedef enum bt_self_message_iterator_status (*init_method_t)( |
d27a81b2 PP |
414 | void *, void *, void *); |
415 | ||
416 | init_method_t init_method = NULL; | |
c14831f0 | 417 | struct bt_self_component_port_input_message_iterator *iterator = |
d27a81b2 PP |
418 | NULL; |
419 | struct bt_port *port = (void *) self_port; | |
420 | struct bt_port *upstream_port; | |
421 | struct bt_component *comp; | |
422 | struct bt_component *upstream_comp; | |
423 | struct bt_component_class *upstream_comp_cls; | |
424 | ||
425 | BT_ASSERT_PRE_NON_NULL(port, "Port"); | |
96ffdc46 | 426 | comp = bt_port_borrow_component_inline(port); |
d27a81b2 PP |
427 | BT_ASSERT_PRE(bt_port_is_connected(port), |
428 | "Port is not connected: %![port-]+p", port); | |
429 | BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p", | |
430 | port); | |
431 | BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp), | |
432 | "Port's component's graph is canceled: " | |
433 | "%![port-]+p, %![comp-]+c", port, comp); | |
434 | BT_ASSERT(port->connection); | |
435 | upstream_port = port->connection->upstream_port; | |
436 | BT_ASSERT(upstream_port); | |
96ffdc46 | 437 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
d27a81b2 | 438 | BT_ASSERT(upstream_comp); |
734a0572 PP |
439 | BT_ASSERT_PRE( |
440 | bt_component_borrow_graph(upstream_comp)->config_state != | |
441 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
442 | "Graph is not configured: %!+g", | |
443 | bt_component_borrow_graph(upstream_comp)); | |
d27a81b2 PP |
444 | upstream_comp_cls = upstream_comp->class; |
445 | BT_ASSERT(upstream_comp->class->type == | |
446 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
447 | upstream_comp->class->type == | |
448 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
c14831f0 | 449 | iterator = bt_self_component_port_input_message_iterator_create_initial( |
d27a81b2 PP |
450 | upstream_comp, upstream_port); |
451 | if (!iterator) { | |
452 | BT_LOGW_STR("Cannot create self component input port " | |
c14831f0 | 453 | "message iterator."); |
d27a81b2 PP |
454 | goto end; |
455 | } | |
890882ef | 456 | |
d27a81b2 PP |
457 | switch (upstream_comp_cls->type) { |
458 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
459 | { | |
460 | struct bt_component_class_source *src_comp_cls = | |
461 | (void *) upstream_comp_cls; | |
462 | ||
463 | init_method = | |
c14831f0 | 464 | (init_method_t) src_comp_cls->methods.msg_iter_init; |
d27a81b2 PP |
465 | break; |
466 | } | |
467 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
468 | { | |
469 | struct bt_component_class_filter *flt_comp_cls = | |
470 | (void *) upstream_comp_cls; | |
471 | ||
472 | init_method = | |
c14831f0 | 473 | (init_method_t) flt_comp_cls->methods.msg_iter_init; |
d27a81b2 PP |
474 | break; |
475 | } | |
476 | default: | |
477 | /* Unreachable */ | |
478 | abort(); | |
479 | } | |
480 | ||
481 | if (init_method) { | |
482 | int iter_status; | |
483 | ||
484 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); | |
485 | iter_status = init_method(iterator, upstream_comp, | |
486 | upstream_port); | |
487 | BT_LOGD("User method returned: status=%s", | |
c14831f0 PP |
488 | bt_message_iterator_status_string(iter_status)); |
489 | if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
d27a81b2 | 490 | BT_LOGW_STR("Initialization method failed."); |
44b2734f | 491 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d27a81b2 PP |
492 | goto end; |
493 | } | |
494 | } | |
495 | ||
44b2734f PP |
496 | set_self_comp_port_input_msg_iterator_state(iterator, |
497 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
d27a81b2 | 498 | g_ptr_array_add(port->connection->iterators, iterator); |
c14831f0 | 499 | BT_LIB_LOGD("Created message iterator on self component input port: " |
d27a81b2 PP |
500 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
501 | upstream_port, upstream_comp, iterator); | |
502 | ||
503 | end: | |
504 | return iterator; | |
ea8d3e58 JG |
505 | } |
506 | ||
c14831f0 PP |
507 | void *bt_self_message_iterator_get_data( |
508 | const struct bt_self_message_iterator *self_iterator) | |
ea8d3e58 | 509 | { |
c14831f0 | 510 | struct bt_self_component_port_input_message_iterator *iterator = |
d27a81b2 | 511 | (void *) self_iterator; |
ea8d3e58 | 512 | |
c14831f0 | 513 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d27a81b2 | 514 | return iterator->user_data; |
8738a040 | 515 | } |
413bc2c4 | 516 | |
c14831f0 PP |
517 | void bt_self_message_iterator_set_data( |
518 | struct bt_self_message_iterator *self_iterator, void *data) | |
5c563278 | 519 | { |
c14831f0 | 520 | struct bt_self_component_port_input_message_iterator *iterator = |
d27a81b2 | 521 | (void *) self_iterator; |
5c563278 | 522 | |
c14831f0 | 523 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d27a81b2 | 524 | iterator->user_data = data; |
c14831f0 | 525 | BT_LIB_LOGV("Set message iterator's user data: " |
d27a81b2 | 526 | "%!+i, user-data-addr=%p", iterator, data); |
5c563278 PP |
527 | } |
528 | ||
c14831f0 PP |
529 | enum bt_message_iterator_status |
530 | bt_self_component_port_input_message_iterator_next( | |
531 | struct bt_self_component_port_input_message_iterator *iterator, | |
532 | bt_message_array_const *msgs, uint64_t *user_count) | |
3230ee6b | 533 | { |
c14831f0 | 534 | int status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d27a81b2 | 535 | |
c14831f0 PP |
536 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
537 | BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)"); | |
538 | BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)"); | |
f42867e2 | 539 | BT_ASSERT_PRE(iterator->state == |
c14831f0 PP |
540 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
541 | "Message iterator's \"next\" called, but " | |
9599065d | 542 | "message iterator is in the wrong state: %!+i", iterator); |
f42867e2 PP |
543 | BT_ASSERT(iterator->upstream_component); |
544 | BT_ASSERT(iterator->upstream_component->class); | |
734a0572 PP |
545 | BT_ASSERT_PRE( |
546 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
547 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
0d508772 PP |
548 | "Graph is not configured: %!+g", |
549 | bt_component_borrow_graph(iterator->upstream_component)); | |
d27a81b2 | 550 | BT_LIB_LOGD("Getting next self component input port " |
c14831f0 | 551 | "message iterator's messages: %!+i", iterator); |
d3eb6e8f | 552 | |
3230ee6b | 553 | /* |
c14831f0 | 554 | * Call the user's "next" method to get the next messages |
fa054faf | 555 | * and status. |
3230ee6b | 556 | */ |
9599065d | 557 | BT_ASSERT(iterator->methods.next); |
f42867e2 | 558 | BT_LOGD_STR("Calling user's \"next\" method."); |
9599065d PP |
559 | status = iterator->methods.next(iterator, |
560 | (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE, | |
561 | user_count); | |
f42867e2 | 562 | BT_LOGD("User method returned: status=%s", |
c14831f0 | 563 | bt_message_iterator_status_string(status)); |
d4393e08 | 564 | if (status < 0) { |
f42867e2 | 565 | BT_LOGW_STR("User method failed."); |
f42867e2 PP |
566 | goto end; |
567 | } | |
3230ee6b | 568 | |
44b2734f PP |
569 | #ifdef BT_DEV_MODE |
570 | /* | |
571 | * There is no way that this iterator could have been finalized | |
572 | * during its "next" method, as the only way to do this is to | |
573 | * put the last iterator's reference, and this can only be done | |
574 | * by its downstream owner. | |
9599065d PP |
575 | * |
576 | * For the same reason, there is no way that this iterator could | |
577 | * have seeked (cannot seek a self message iterator). | |
44b2734f PP |
578 | */ |
579 | BT_ASSERT(iterator->state == | |
580 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
581 | #endif | |
8cf27cc5 | 582 | |
d4393e08 | 583 | switch (status) { |
c14831f0 | 584 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
9599065d PP |
585 | BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE, |
586 | "Invalid returned message count: greater than " | |
587 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
588 | *user_count, MSG_BATCH_SIZE); | |
c14831f0 | 589 | *msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 590 | break; |
c14831f0 | 591 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
d4393e08 | 592 | goto end; |
c14831f0 | 593 | case BT_MESSAGE_ITERATOR_STATUS_END: |
44b2734f PP |
594 | set_self_comp_port_input_msg_iterator_state(iterator, |
595 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); | |
f42867e2 | 596 | goto end; |
f42867e2 PP |
597 | default: |
598 | /* Unknown non-error status */ | |
599 | abort(); | |
41a2b7ae PP |
600 | } |
601 | ||
602 | end: | |
3230ee6b PP |
603 | return status; |
604 | } | |
605 | ||
44b2734f | 606 | enum bt_message_iterator_status bt_port_output_message_iterator_next( |
c14831f0 PP |
607 | struct bt_port_output_message_iterator *iterator, |
608 | bt_message_array_const *msgs_to_user, | |
d4393e08 | 609 | uint64_t *count_to_user) |
3230ee6b | 610 | { |
c14831f0 | 611 | enum bt_message_iterator_status status; |
07245ac2 | 612 | enum bt_graph_status graph_status; |
3230ee6b | 613 | |
c14831f0 PP |
614 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
615 | BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)"); | |
616 | BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)"); | |
617 | BT_LIB_LOGD("Getting next output port message iterator's messages: " | |
07245ac2 | 618 | "%!+i", iterator); |
d27a81b2 PP |
619 | graph_status = bt_graph_consume_sink_no_check(iterator->graph, |
620 | iterator->colander); | |
07245ac2 PP |
621 | switch (graph_status) { |
622 | case BT_GRAPH_STATUS_CANCELED: | |
07245ac2 | 623 | case BT_GRAPH_STATUS_AGAIN: |
07245ac2 | 624 | case BT_GRAPH_STATUS_END: |
07245ac2 | 625 | case BT_GRAPH_STATUS_NOMEM: |
d27a81b2 | 626 | status = (int) graph_status; |
07245ac2 PP |
627 | break; |
628 | case BT_GRAPH_STATUS_OK: | |
c14831f0 | 629 | status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
630 | |
631 | /* | |
c14831f0 | 632 | * On success, the colander sink moves the messages |
d4393e08 | 633 | * to this iterator's array and sets this iterator's |
c14831f0 | 634 | * message count: move them to the user. |
d4393e08 | 635 | */ |
c14831f0 | 636 | *msgs_to_user = (void *) iterator->base.msgs->pdata; |
d27a81b2 | 637 | *count_to_user = iterator->count; |
90157d89 | 638 | break; |
90157d89 | 639 | default: |
07245ac2 | 640 | /* Other errors */ |
c14831f0 | 641 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
90157d89 | 642 | } |
3230ee6b | 643 | |
3230ee6b | 644 | return status; |
53d45b87 JG |
645 | } |
646 | ||
9599065d PP |
647 | struct bt_component * |
648 | bt_self_component_port_input_message_iterator_borrow_component( | |
c14831f0 | 649 | struct bt_self_component_port_input_message_iterator *iterator) |
d27a81b2 | 650 | { |
c14831f0 | 651 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d27a81b2 PP |
652 | return iterator->upstream_component; |
653 | } | |
654 | ||
9599065d PP |
655 | const struct bt_component * |
656 | bt_self_component_port_input_message_iterator_borrow_component_const( | |
657 | const struct bt_self_component_port_input_message_iterator *iterator) | |
658 | { | |
659 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
660 | return iterator->upstream_component; | |
661 | } | |
662 | ||
c14831f0 PP |
663 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
664 | struct bt_self_message_iterator *self_iterator) | |
413bc2c4 | 665 | { |
c14831f0 | 666 | struct bt_self_component_port_input_message_iterator *iterator = |
d27a81b2 | 667 | (void *) self_iterator; |
90157d89 | 668 | |
c14831f0 | 669 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d27a81b2 | 670 | return (void *) iterator->upstream_component; |
413bc2c4 JG |
671 | } |
672 | ||
c14831f0 PP |
673 | struct bt_self_port_output *bt_self_message_iterator_borrow_port( |
674 | struct bt_self_message_iterator *self_iterator) | |
91457551 | 675 | { |
c14831f0 | 676 | struct bt_self_component_port_input_message_iterator *iterator = |
d27a81b2 PP |
677 | (void *) self_iterator; |
678 | ||
c14831f0 | 679 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d27a81b2 | 680 | return (void *) iterator->upstream_port; |
91457551 | 681 | } |
8ed535b5 PP |
682 | |
683 | static | |
c14831f0 | 684 | void bt_port_output_message_iterator_destroy(struct bt_object *obj) |
8ed535b5 | 685 | { |
c14831f0 | 686 | struct bt_port_output_message_iterator *iterator = (void *) obj; |
8ed535b5 | 687 | |
c14831f0 | 688 | BT_LIB_LOGD("Destroying output port message iterator object: %!+i", |
8ed535b5 PP |
689 | iterator); |
690 | BT_LOGD_STR("Putting graph."); | |
d27a81b2 | 691 | BT_OBJECT_PUT_REF_AND_RESET(iterator->graph); |
8ed535b5 | 692 | BT_LOGD_STR("Putting colander sink component."); |
d27a81b2 | 693 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
c14831f0 | 694 | destroy_base_message_iterator(obj); |
8ed535b5 PP |
695 | } |
696 | ||
c14831f0 | 697 | struct bt_port_output_message_iterator * |
9599065d | 698 | bt_port_output_message_iterator_create(struct bt_graph *graph, |
96ffdc46 | 699 | const struct bt_port_output *output_port) |
8ed535b5 | 700 | { |
c14831f0 | 701 | struct bt_port_output_message_iterator *iterator = NULL; |
d27a81b2 | 702 | struct bt_component_class_sink *colander_comp_cls = NULL; |
8ed535b5 | 703 | struct bt_component *output_port_comp = NULL; |
d27a81b2 | 704 | struct bt_component_sink *colander_comp; |
8ed535b5 | 705 | enum bt_graph_status graph_status; |
d27a81b2 | 706 | struct bt_port_input *colander_in_port = NULL; |
8ed535b5 | 707 | struct bt_component_class_sink_colander_data colander_data; |
d4393e08 | 708 | int ret; |
8ed535b5 | 709 | |
d27a81b2 | 710 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
f42867e2 | 711 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
96ffdc46 PP |
712 | output_port_comp = bt_port_borrow_component_inline( |
713 | (const void *) output_port); | |
f42867e2 PP |
714 | BT_ASSERT_PRE(output_port_comp, |
715 | "Output port has no component: %!+p", output_port); | |
d27a81b2 PP |
716 | BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) == |
717 | (void *) graph, | |
718 | "Output port is not part of graph: %![graph-]+g, %![port-]+p", | |
719 | graph, output_port); | |
ad421f93 PP |
720 | BT_ASSERT_PRE(!graph->has_sink, |
721 | "Graph already has a sink component: %![graph-]+g"); | |
8ed535b5 | 722 | |
c14831f0 PP |
723 | /* Create message iterator */ |
724 | BT_LIB_LOGD("Creating message iterator on output port: " | |
d27a81b2 | 725 | "%![port-]+p, %![comp-]+c", output_port, output_port_comp); |
c14831f0 | 726 | iterator = g_new0(struct bt_port_output_message_iterator, 1); |
8ed535b5 | 727 | if (!iterator) { |
c14831f0 | 728 | BT_LOGE_STR("Failed to allocate one output port message iterator."); |
8ed535b5 PP |
729 | goto error; |
730 | } | |
731 | ||
c14831f0 PP |
732 | ret = init_message_iterator((void *) iterator, |
733 | BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT, | |
734 | bt_port_output_message_iterator_destroy); | |
d4393e08 | 735 | if (ret) { |
c14831f0 | 736 | /* init_message_iterator() logs errors */ |
e2317362 | 737 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
738 | goto end; |
739 | } | |
8ed535b5 PP |
740 | |
741 | /* Create colander component */ | |
742 | colander_comp_cls = bt_component_class_sink_colander_get(); | |
743 | if (!colander_comp_cls) { | |
744 | BT_LOGW("Cannot get colander sink component class."); | |
745 | goto error; | |
746 | } | |
747 | ||
5e8988cb PP |
748 | iterator->graph = graph; |
749 | bt_object_get_no_null_check(iterator->graph); | |
c14831f0 | 750 | colander_data.msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 751 | colander_data.count_addr = &iterator->count; |
badaa238 PP |
752 | |
753 | /* Hope that nobody uses this very unique name */ | |
d27a81b2 | 754 | graph_status = |
96ffdc46 | 755 | bt_graph_add_sink_component_with_init_method_data( |
badaa238 PP |
756 | (void *) graph, colander_comp_cls, |
757 | "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1", | |
96ffdc46 | 758 | NULL, &colander_data, (void *) &iterator->colander); |
8ed535b5 | 759 | if (graph_status != BT_GRAPH_STATUS_OK) { |
d27a81b2 PP |
760 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
761 | "%1[graph-]+g, status=%s", graph, | |
8ed535b5 PP |
762 | bt_graph_status_string(graph_status)); |
763 | goto error; | |
764 | } | |
765 | ||
766 | /* | |
767 | * Connect provided output port to the colander component's | |
768 | * input port. | |
769 | */ | |
96ffdc46 PP |
770 | colander_in_port = |
771 | (void *) bt_component_sink_borrow_input_port_by_index_const( | |
772 | (void *) iterator->colander, 0); | |
f6ccaed9 | 773 | BT_ASSERT(colander_in_port); |
96ffdc46 | 774 | graph_status = bt_graph_connect_ports(graph, |
8ed535b5 PP |
775 | output_port, colander_in_port, NULL); |
776 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
d27a81b2 PP |
777 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
778 | "%![graph-]+g, %![comp-]+c, status=%s", graph, | |
779 | iterator->colander, | |
8ed535b5 PP |
780 | bt_graph_status_string(graph_status)); |
781 | goto error; | |
782 | } | |
783 | ||
784 | /* | |
785 | * At this point everything went fine. Make the graph | |
c14831f0 | 786 | * nonconsumable forever so that only this message iterator |
8ed535b5 | 787 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
c14831f0 PP |
788 | * This avoids leaking the message created by the colander |
789 | * sink and moved to the message iterator's message | |
07245ac2 | 790 | * member. |
8ed535b5 | 791 | */ |
d27a81b2 | 792 | bt_graph_set_can_consume(iterator->graph, false); |
734a0572 PP |
793 | |
794 | /* | |
795 | * Also set the graph as being configured: it has no active sink | |
796 | * anyway, so we don't need to call bt_graph_configure(). | |
797 | */ | |
798 | graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED; | |
8ed535b5 PP |
799 | goto end; |
800 | ||
801 | error: | |
802 | if (iterator && iterator->graph && iterator->colander) { | |
803 | int ret; | |
804 | ||
805 | /* Remove created colander component from graph if any */ | |
806 | colander_comp = iterator->colander; | |
e2317362 | 807 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
8ed535b5 PP |
808 | |
809 | /* | |
810 | * At this point the colander component's reference | |
811 | * count is 0 because iterator->colander was the only | |
812 | * owner. We also know that it is not connected because | |
813 | * this is the last operation before this function | |
814 | * succeeds. | |
815 | * | |
816 | * Since we honor the preconditions here, | |
817 | * bt_graph_remove_unconnected_component() always | |
818 | * succeeds. | |
819 | */ | |
820 | ret = bt_graph_remove_unconnected_component(iterator->graph, | |
d27a81b2 | 821 | (void *) colander_comp); |
f6ccaed9 | 822 | BT_ASSERT(ret == 0); |
8ed535b5 PP |
823 | } |
824 | ||
e2317362 | 825 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
8ed535b5 PP |
826 | |
827 | end: | |
e2317362 | 828 | bt_object_put_ref(colander_comp_cls); |
8ed535b5 PP |
829 | return (void *) iterator; |
830 | } | |
317e35ec | 831 | |
9599065d PP |
832 | bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
833 | struct bt_self_component_port_input_message_iterator *iterator, | |
834 | int64_t ns_from_origin) | |
835 | { | |
836 | bt_bool can = BT_FALSE; | |
837 | ||
838 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
839 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
734a0572 PP |
840 | BT_ASSERT_PRE( |
841 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
842 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9599065d PP |
843 | "Graph is not configured: %!+g", |
844 | bt_component_borrow_graph(iterator->upstream_component)); | |
845 | ||
846 | if (iterator->methods.can_seek_ns_from_origin) { | |
847 | can = iterator->methods.can_seek_ns_from_origin(iterator, | |
848 | ns_from_origin); | |
849 | goto end; | |
850 | } | |
851 | ||
852 | /* | |
853 | * Automatic seeking fall back: if we can seek to the beginning, | |
854 | * then we can automatically seek to any message. | |
855 | */ | |
856 | if (iterator->methods.can_seek_beginning) { | |
857 | can = iterator->methods.can_seek_beginning(iterator); | |
858 | } | |
859 | ||
860 | end: | |
861 | return can; | |
862 | } | |
863 | ||
864 | bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( | |
865 | struct bt_self_component_port_input_message_iterator *iterator) | |
866 | { | |
867 | bt_bool can = BT_FALSE; | |
868 | ||
869 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
870 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
734a0572 PP |
871 | BT_ASSERT_PRE( |
872 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
873 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9599065d PP |
874 | "Graph is not configured: %!+g", |
875 | bt_component_borrow_graph(iterator->upstream_component)); | |
876 | ||
877 | if (iterator->methods.can_seek_beginning) { | |
878 | can = iterator->methods.can_seek_beginning(iterator); | |
879 | } | |
880 | ||
881 | return can; | |
882 | } | |
883 | ||
884 | static inline | |
885 | void _set_iterator_state_after_seeking( | |
886 | struct bt_self_component_port_input_message_iterator *iterator, | |
887 | enum bt_message_iterator_status status) | |
888 | { | |
889 | enum bt_self_component_port_input_message_iterator_state new_state = 0; | |
890 | ||
891 | /* Set iterator's state depending on seeking status */ | |
892 | switch (status) { | |
893 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
894 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; | |
895 | break; | |
896 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
897 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; | |
898 | break; | |
899 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
900 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
901 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; | |
902 | break; | |
903 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
904 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; | |
905 | break; | |
906 | default: | |
907 | abort(); | |
908 | } | |
909 | ||
910 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); | |
911 | } | |
912 | ||
913 | #ifdef BT_DEV_MODE | |
914 | # define set_iterator_state_after_seeking _set_iterator_state_after_seeking | |
915 | #else | |
a73035a3 | 916 | # define set_iterator_state_after_seeking(_iter, _status) ((void) _iter); ((void) _status); |
9599065d PP |
917 | #endif |
918 | ||
919 | enum bt_message_iterator_status | |
920 | bt_self_component_port_input_message_iterator_seek_beginning( | |
921 | struct bt_self_component_port_input_message_iterator *iterator) | |
922 | { | |
923 | int status; | |
924 | ||
925 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
926 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
734a0572 PP |
927 | BT_ASSERT_PRE( |
928 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
929 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9599065d PP |
930 | "Graph is not configured: %!+g", |
931 | bt_component_borrow_graph(iterator->upstream_component)); | |
932 | BT_ASSERT_PRE( | |
933 | bt_self_component_port_input_message_iterator_can_seek_beginning( | |
934 | iterator), | |
935 | "Message iterator cannot seek beginning: %!+i", iterator); | |
936 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); | |
937 | set_self_comp_port_input_msg_iterator_state(iterator, | |
938 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
939 | status = iterator->methods.seek_beginning(iterator); | |
940 | BT_LOGD("User method returned: status=%s", | |
941 | bt_message_iterator_status_string(status)); | |
942 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
943 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
944 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
945 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
946 | "Unexpected status: %![iter-]+i, status=%s", | |
1acf719d | 947 | iterator, bt_common_self_message_iterator_status_string(status)); |
9599065d PP |
948 | set_iterator_state_after_seeking(iterator, status); |
949 | return status; | |
950 | } | |
951 | ||
952 | static inline | |
964f7e04 PP |
953 | enum bt_message_iterator_status auto_seek_handle_message( |
954 | struct bt_self_component_port_input_message_iterator *iterator, | |
955 | int64_t ns_from_origin, const struct bt_message *msg, | |
956 | bool *got_first) | |
9599065d | 957 | { |
964f7e04 PP |
958 | enum bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; |
959 | int64_t msg_ns_from_origin; | |
9599065d | 960 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
964f7e04 PP |
961 | int ret; |
962 | ||
963 | BT_ASSERT(msg); | |
964 | BT_ASSERT(got_first); | |
9599065d PP |
965 | |
966 | switch (msg->type) { | |
967 | case BT_MESSAGE_TYPE_EVENT: | |
968 | { | |
969 | const struct bt_message_event *event_msg = | |
970 | (const void *) msg; | |
971 | ||
aae87991 | 972 | clk_snapshot = event_msg->default_cs; |
9599065d | 973 | BT_ASSERT_PRE(clk_snapshot, |
e4ed0682 PP |
974 | "Event message has no default clock snapshot: %!+n", |
975 | event_msg); | |
9599065d PP |
976 | break; |
977 | } | |
12ccec6b | 978 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
9599065d | 979 | { |
12ccec6b | 980 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
9599065d PP |
981 | (const void *) msg; |
982 | ||
9599065d | 983 | clk_snapshot = inactivity_msg->default_cs; |
902bc5f0 | 984 | BT_ASSERT(clk_snapshot); |
9599065d PP |
985 | break; |
986 | } | |
902bc5f0 PP |
987 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
988 | case BT_MESSAGE_TYPE_PACKET_END: | |
e4ed0682 PP |
989 | { |
990 | const struct bt_message_packet *packet_msg = | |
991 | (const void *) msg; | |
992 | ||
993 | clk_snapshot = packet_msg->default_cs; | |
994 | BT_ASSERT_PRE(clk_snapshot, | |
995 | "Packet message has no default clock snapshot: %!+n", | |
996 | packet_msg); | |
997 | break; | |
998 | } | |
902bc5f0 PP |
999 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1000 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
9599065d | 1001 | { |
964f7e04 PP |
1002 | struct bt_message_discarded_items *msg_disc_items = |
1003 | (void *) msg; | |
1004 | ||
1005 | BT_ASSERT_PRE(msg_disc_items->default_begin_cs && | |
1006 | msg_disc_items->default_end_cs, | |
1007 | "Discarded events/packets message has no default clock snapshots: %!+n", | |
1008 | msg_disc_items); | |
1009 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1010 | msg_disc_items->default_begin_cs, | |
1011 | &msg_ns_from_origin); | |
1012 | if (ret) { | |
1013 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1014 | goto end; | |
1015 | } | |
9599065d | 1016 | |
964f7e04 PP |
1017 | if (msg_ns_from_origin >= ns_from_origin) { |
1018 | *got_first = true; | |
1019 | goto push_msg; | |
1020 | } | |
1021 | ||
1022 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1023 | msg_disc_items->default_end_cs, | |
1024 | &msg_ns_from_origin); | |
1025 | if (ret) { | |
1026 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1027 | goto end; | |
1028 | } | |
1029 | ||
1030 | if (msg_ns_from_origin >= ns_from_origin) { | |
1031 | /* | |
1032 | * The discarded items message's beginning time | |
1033 | * is before the requested seeking time, but its | |
1034 | * end time is after. Modify the message so as | |
1035 | * to set its beginning time to the requested | |
1036 | * seeking time, and make its item count unknown | |
1037 | * as we don't know if items were really | |
1038 | * discarded within the new time range. | |
1039 | */ | |
1040 | uint64_t new_begin_raw_value; | |
1041 | ||
1042 | ret = bt_clock_class_clock_value_from_ns_from_origin( | |
1043 | msg_disc_items->default_end_cs->clock_class, | |
1044 | ns_from_origin, &new_begin_raw_value); | |
1045 | if (ret) { | |
1046 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1047 | goto end; | |
1048 | } | |
1049 | ||
1050 | bt_clock_snapshot_set_raw_value( | |
1051 | msg_disc_items->default_begin_cs, | |
1052 | new_begin_raw_value); | |
1053 | msg_disc_items->count.base.avail = | |
1054 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; | |
1055 | ||
1056 | /* | |
1057 | * It is safe to push it because its beginning | |
1058 | * time is exactly the requested seeking time. | |
1059 | */ | |
1060 | goto push_msg; | |
1061 | } else { | |
1062 | goto skip_msg; | |
1063 | } | |
9599065d | 1064 | } |
902bc5f0 | 1065 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
9599065d | 1066 | { |
902bc5f0 | 1067 | const struct bt_message_stream_activity *stream_act_msg = |
9599065d PP |
1068 | (const void *) msg; |
1069 | ||
902bc5f0 PP |
1070 | switch (stream_act_msg->default_cs_state) { |
1071 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1072 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: | |
1073 | /* | |
1074 | * -inf is always less than any requested time, | |
1075 | * and we can't assume any specific time for an | |
1076 | * unknown clock snapshot, so skip this. | |
1077 | */ | |
964f7e04 | 1078 | goto skip_msg; |
902bc5f0 PP |
1079 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1080 | clk_snapshot = stream_act_msg->default_cs; | |
1081 | BT_ASSERT(clk_snapshot); | |
1082 | break; | |
1083 | default: | |
1084 | abort(); | |
1085 | } | |
1086 | ||
9599065d PP |
1087 | break; |
1088 | } | |
902bc5f0 PP |
1089 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
1090 | { | |
1091 | const struct bt_message_stream_activity *stream_act_msg = | |
1092 | (const void *) msg; | |
1093 | ||
1094 | switch (stream_act_msg->default_cs_state) { | |
1095 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1096 | /* | |
1097 | * We can't assume any specific time for an | |
1098 | * unknown clock snapshot, so skip this. | |
1099 | */ | |
964f7e04 | 1100 | goto skip_msg; |
902bc5f0 PP |
1101 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: |
1102 | /* | |
1103 | * +inf is always greater than any requested | |
1104 | * time. | |
1105 | */ | |
964f7e04 PP |
1106 | *got_first = true; |
1107 | goto push_msg; | |
902bc5f0 PP |
1108 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1109 | clk_snapshot = stream_act_msg->default_cs; | |
1110 | BT_ASSERT(clk_snapshot); | |
1111 | break; | |
1112 | default: | |
1113 | abort(); | |
1114 | } | |
1115 | ||
1116 | break; | |
1117 | } | |
1118 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1119 | case BT_MESSAGE_TYPE_STREAM_END: | |
1120 | /* Ignore */ | |
964f7e04 | 1121 | goto skip_msg; |
9599065d PP |
1122 | default: |
1123 | abort(); | |
1124 | } | |
1125 | ||
964f7e04 PP |
1126 | BT_ASSERT(clk_snapshot); |
1127 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, | |
1128 | &msg_ns_from_origin); | |
1129 | if (ret) { | |
1130 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
9599065d PP |
1131 | goto end; |
1132 | } | |
1133 | ||
964f7e04 PP |
1134 | if (msg_ns_from_origin >= ns_from_origin) { |
1135 | *got_first = true; | |
1136 | goto push_msg; | |
1137 | } | |
1138 | ||
1139 | skip_msg: | |
1140 | bt_object_put_no_null_check(msg); | |
8e3f3243 | 1141 | msg = NULL; |
964f7e04 PP |
1142 | goto end; |
1143 | ||
1144 | push_msg: | |
1145 | g_queue_push_head(iterator->auto_seek_msgs, (void *) msg); | |
1146 | msg = NULL; | |
9599065d PP |
1147 | |
1148 | end: | |
964f7e04 PP |
1149 | BT_ASSERT(!msg || status != BT_MESSAGE_ITERATOR_STATUS_OK); |
1150 | return status; | |
9599065d PP |
1151 | } |
1152 | ||
1153 | static | |
1154 | enum bt_message_iterator_status find_message_ge_ns_from_origin( | |
1155 | struct bt_self_component_port_input_message_iterator *iterator, | |
1156 | int64_t ns_from_origin) | |
1157 | { | |
1158 | int status; | |
1159 | enum bt_self_component_port_input_message_iterator_state init_state = | |
1160 | iterator->state; | |
1161 | const struct bt_message *messages[MSG_BATCH_SIZE]; | |
1162 | uint64_t user_count = 0; | |
1163 | uint64_t i; | |
964f7e04 | 1164 | bool got_first = false; |
9599065d PP |
1165 | |
1166 | BT_ASSERT(iterator); | |
1167 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); | |
1168 | ||
1169 | /* | |
1170 | * Make this iterator temporarily active (not seeking) to call | |
1171 | * the "next" method. | |
1172 | */ | |
1173 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1174 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1175 | ||
1176 | BT_ASSERT(iterator->methods.next); | |
1177 | ||
a218c70d | 1178 | while (!got_first) { |
9599065d PP |
1179 | /* |
1180 | * Call the user's "next" method to get the next | |
1181 | * messages and status. | |
1182 | */ | |
1183 | BT_LOGD_STR("Calling user's \"next\" method."); | |
1184 | status = iterator->methods.next(iterator, | |
1185 | &messages[0], MSG_BATCH_SIZE, &user_count); | |
1186 | BT_LOGD("User method returned: status=%s", | |
1187 | bt_message_iterator_status_string(status)); | |
1188 | ||
1189 | #ifdef BT_DEV_MODE | |
1190 | /* | |
1191 | * The user's "next" method must not do any action which | |
1192 | * would change the iterator's state. | |
1193 | */ | |
1194 | BT_ASSERT(iterator->state == | |
1195 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1196 | #endif | |
1197 | ||
1198 | switch (status) { | |
1199 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1200 | BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE, | |
1201 | "Invalid returned message count: greater than " | |
1202 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
1203 | user_count, MSG_BATCH_SIZE); | |
1204 | break; | |
1205 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1206 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1207 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1208 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
1209 | goto end; | |
1210 | default: | |
1211 | abort(); | |
1212 | } | |
1213 | ||
9599065d | 1214 | for (i = 0; i < user_count; i++) { |
964f7e04 PP |
1215 | if (got_first) { |
1216 | g_queue_push_head(iterator->auto_seek_msgs, | |
1217 | (void *) messages[i]); | |
1218 | messages[i] = NULL; | |
9599065d PP |
1219 | continue; |
1220 | } | |
1221 | ||
964f7e04 PP |
1222 | status = auto_seek_handle_message(iterator, |
1223 | ns_from_origin, messages[i], &got_first); | |
1224 | if (status == BT_MESSAGE_ITERATOR_STATUS_OK) { | |
a218c70d | 1225 | /* Message was either pushed or moved */ |
964f7e04 PP |
1226 | messages[i] = NULL; |
1227 | } else { | |
9599065d PP |
1228 | goto end; |
1229 | } | |
9599065d PP |
1230 | } |
1231 | } | |
1232 | ||
1233 | end: | |
1234 | for (i = 0; i < user_count; i++) { | |
1235 | if (messages[i]) { | |
1236 | bt_object_put_no_null_check(messages[i]); | |
1237 | } | |
1238 | } | |
1239 | ||
1240 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); | |
1241 | return status; | |
1242 | } | |
1243 | ||
1244 | static | |
1245 | enum bt_self_message_iterator_status post_auto_seek_next( | |
1246 | struct bt_self_component_port_input_message_iterator *iterator, | |
1247 | bt_message_array_const msgs, uint64_t capacity, | |
1248 | uint64_t *count) | |
1249 | { | |
964f7e04 PP |
1250 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek_msgs)); |
1251 | *count = 0; | |
9599065d PP |
1252 | |
1253 | /* | |
1254 | * Move auto-seek messages to the output array (which is this | |
964f7e04 | 1255 | * iterator's base message array). |
9599065d | 1256 | */ |
964f7e04 PP |
1257 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek_msgs)) { |
1258 | msgs[*count] = g_queue_pop_tail(iterator->auto_seek_msgs); | |
1259 | capacity--; | |
1260 | (*count)++; | |
9599065d | 1261 | } |
9599065d | 1262 | |
964f7e04 PP |
1263 | BT_ASSERT(*count > 0); |
1264 | ||
1265 | if (g_queue_is_empty(iterator->auto_seek_msgs)) { | |
1266 | /* No more auto-seek messages */ | |
1267 | switch (iterator->upstream_component->class->type) { | |
1268 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1269 | { | |
1270 | struct bt_component_class_source *src_comp_cls = | |
1271 | (void *) iterator->upstream_component->class; | |
1272 | ||
1273 | iterator->methods.next = | |
1274 | (bt_self_component_port_input_message_iterator_next_method) | |
1275 | src_comp_cls->methods.msg_iter_next; | |
1276 | break; | |
1277 | } | |
1278 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1279 | { | |
1280 | struct bt_component_class_filter *flt_comp_cls = | |
1281 | (void *) iterator->upstream_component->class; | |
1282 | ||
1283 | iterator->methods.next = | |
1284 | (bt_self_component_port_input_message_iterator_next_method) | |
1285 | flt_comp_cls->methods.msg_iter_next; | |
1286 | break; | |
1287 | } | |
1288 | default: | |
1289 | abort(); | |
1290 | } | |
9599065d PP |
1291 | } |
1292 | ||
1293 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
1294 | } | |
1295 | ||
1296 | enum bt_message_iterator_status | |
1297 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1298 | struct bt_self_component_port_input_message_iterator *iterator, | |
1299 | int64_t ns_from_origin) | |
1300 | { | |
1301 | int status; | |
1302 | ||
1303 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1304 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
734a0572 PP |
1305 | BT_ASSERT_PRE( |
1306 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1307 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9599065d PP |
1308 | "Graph is not configured: %!+g", |
1309 | bt_component_borrow_graph(iterator->upstream_component)); | |
1310 | BT_ASSERT_PRE( | |
1311 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1312 | iterator, ns_from_origin), | |
1313 | "Message iterator cannot seek nanoseconds from origin: %!+i, " | |
1314 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); | |
1315 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1316 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
1317 | ||
1318 | if (iterator->methods.seek_ns_from_origin) { | |
1319 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " | |
1320 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); | |
1321 | status = iterator->methods.seek_ns_from_origin(iterator, | |
1322 | ns_from_origin); | |
1323 | BT_LOGD("User method returned: status=%s", | |
1324 | bt_message_iterator_status_string(status)); | |
1325 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1326 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1327 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1328 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1329 | "Unexpected status: %![iter-]+i, status=%s", | |
1330 | iterator, | |
1acf719d | 1331 | bt_common_self_message_iterator_status_string(status)); |
9599065d PP |
1332 | } else { |
1333 | /* Start automatic seeking: seek beginning first */ | |
1334 | BT_ASSERT(iterator->methods.can_seek_beginning(iterator)); | |
1335 | BT_ASSERT(iterator->methods.seek_beginning); | |
1336 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", | |
1337 | iterator); | |
1338 | status = iterator->methods.seek_beginning(iterator); | |
1339 | BT_LOGD("User method returned: status=%s", | |
1340 | bt_message_iterator_status_string(status)); | |
1341 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1342 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1343 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1344 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1345 | "Unexpected status: %![iter-]+i, status=%s", | |
1346 | iterator, | |
1acf719d | 1347 | bt_common_self_message_iterator_status_string(status)); |
9599065d PP |
1348 | switch (status) { |
1349 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1350 | break; | |
1351 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1352 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1353 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1354 | goto end; | |
1355 | default: | |
1356 | abort(); | |
1357 | } | |
1358 | ||
1359 | /* | |
1360 | * Find the first message which has a default clock | |
1361 | * snapshot greater than or equal to the requested | |
964f7e04 PP |
1362 | * seeking time, and move the received messages from |
1363 | * this point in the batch to this iterator's auto-seek | |
1364 | * message queue. | |
9599065d | 1365 | */ |
964f7e04 PP |
1366 | while (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
1367 | bt_object_put_no_null_check( | |
1368 | g_queue_pop_tail(iterator->auto_seek_msgs)); | |
1369 | } | |
1370 | ||
9599065d PP |
1371 | status = find_message_ge_ns_from_origin(iterator, |
1372 | ns_from_origin); | |
1373 | switch (status) { | |
1374 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
964f7e04 | 1375 | case BT_MESSAGE_ITERATOR_STATUS_END: |
9599065d | 1376 | /* |
964f7e04 PP |
1377 | * If there are messages in the auto-seek |
1378 | * message queue, replace the user's "next" | |
1379 | * method with a custom, temporary "next" method | |
1380 | * which returns them. | |
9599065d | 1381 | */ |
964f7e04 PP |
1382 | if (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
1383 | iterator->methods.next = | |
1384 | (bt_self_component_port_input_message_iterator_next_method) | |
1385 | post_auto_seek_next; | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * `BT_MESSAGE_ITERATOR_STATUS_END` becomes | |
1390 | * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next | |
1391 | * time this iterator's "next" method is called, | |
1392 | * it will return | |
1393 | * `BT_MESSAGE_ITERATOR_STATUS_END`. | |
1394 | */ | |
1395 | status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
9599065d PP |
1396 | break; |
1397 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1398 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1399 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1400 | goto end; | |
9599065d PP |
1401 | default: |
1402 | abort(); | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | end: | |
1407 | set_iterator_state_after_seeking(iterator, status); | |
9599065d PP |
1408 | return status; |
1409 | } | |
1410 | ||
1411 | static inline | |
1412 | bt_self_component_port_input_message_iterator * | |
1413 | borrow_output_port_message_iterator_upstream_iterator( | |
1414 | struct bt_port_output_message_iterator *iterator) | |
1415 | { | |
1416 | struct bt_component_class_sink_colander_priv_data *colander_data; | |
1417 | ||
1418 | BT_ASSERT(iterator); | |
1419 | colander_data = (void *) iterator->colander->parent.user_data; | |
1420 | BT_ASSERT(colander_data); | |
1421 | BT_ASSERT(colander_data->msg_iter); | |
1422 | return colander_data->msg_iter; | |
1423 | } | |
1424 | ||
1425 | bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( | |
1426 | struct bt_port_output_message_iterator *iterator, | |
1427 | int64_t ns_from_origin) | |
1428 | { | |
1429 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1430 | return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1431 | borrow_output_port_message_iterator_upstream_iterator( | |
1432 | iterator), ns_from_origin); | |
1433 | } | |
1434 | ||
1435 | bt_bool bt_port_output_message_iterator_can_seek_beginning( | |
1436 | struct bt_port_output_message_iterator *iterator) | |
1437 | { | |
1438 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1439 | return bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1440 | borrow_output_port_message_iterator_upstream_iterator( | |
1441 | iterator)); | |
1442 | } | |
1443 | ||
1444 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin( | |
1445 | struct bt_port_output_message_iterator *iterator, | |
1446 | int64_t ns_from_origin) | |
1447 | { | |
1448 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1449 | return bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1450 | borrow_output_port_message_iterator_upstream_iterator(iterator), | |
1451 | ns_from_origin); | |
1452 | } | |
1453 | ||
1454 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning( | |
1455 | struct bt_port_output_message_iterator *iterator) | |
1456 | { | |
1457 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1458 | return bt_self_component_port_input_message_iterator_seek_beginning( | |
1459 | borrow_output_port_message_iterator_upstream_iterator( | |
1460 | iterator)); | |
1461 | } | |
1462 | ||
c14831f0 PP |
1463 | void bt_port_output_message_iterator_get_ref( |
1464 | const struct bt_port_output_message_iterator *iterator) | |
317e35ec PP |
1465 | { |
1466 | bt_object_get_ref(iterator); | |
1467 | } | |
1468 | ||
c14831f0 PP |
1469 | void bt_port_output_message_iterator_put_ref( |
1470 | const struct bt_port_output_message_iterator *iterator) | |
317e35ec PP |
1471 | { |
1472 | bt_object_put_ref(iterator); | |
1473 | } | |
1474 | ||
c14831f0 PP |
1475 | void bt_self_component_port_input_message_iterator_get_ref( |
1476 | const struct bt_self_component_port_input_message_iterator *iterator) | |
317e35ec PP |
1477 | { |
1478 | bt_object_get_ref(iterator); | |
1479 | } | |
1480 | ||
c14831f0 PP |
1481 | void bt_self_component_port_input_message_iterator_put_ref( |
1482 | const struct bt_self_component_port_input_message_iterator *iterator) | |
317e35ec PP |
1483 | { |
1484 | bt_object_put_ref(iterator); | |
1485 | } |