+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 572 lines · 18.6 KB · SPDX: MIT
main
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4  
5 #ifndef SPA_LOOP_H
6 #define SPA_LOOP_H
7  
8 #include <errno.h>
9  
10 #include <spa/utils/defs.h>
11 #include <spa/utils/hook.h>
12 #include <spa/support/system.h>
13  
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17  
18 #ifndef SPA_API_LOOP
19 #ifdef SPA_API_IMPL
20 #define SPA_API_LOOP SPA_API_IMPL
21 #else
22 #define SPA_API_LOOP static inline
23 #endif
24 #endif
25  
26 /** \defgroup spa_loop Loop
27 * Event loop interface
28 */
29  
30 /**
31 * \addtogroup spa_loop
32 * \{
33 */
34  
35 #define SPA_TYPE_INTERFACE_Loop SPA_TYPE_INFO_INTERFACE_BASE "Loop"
36 #define SPA_TYPE_INTERFACE_DataLoop SPA_TYPE_INFO_INTERFACE_BASE "DataLoop"
37 #define SPA_VERSION_LOOP 0
38 struct spa_loop { struct spa_interface iface; };
39  
40 #define SPA_TYPE_INTERFACE_LoopControl SPA_TYPE_INFO_INTERFACE_BASE "LoopControl"
41 #define SPA_VERSION_LOOP_CONTROL 2
42 struct spa_loop_control { struct spa_interface iface; };
43  
44 #define SPA_TYPE_INTERFACE_LoopUtils SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils"
45 #define SPA_VERSION_LOOP_UTILS 0
46 struct spa_loop_utils { struct spa_interface iface; };
47  
48 struct spa_source;
49  
50 typedef void (*spa_source_func_t) (struct spa_source *source);
51  
52 struct spa_source {
53 struct spa_loop *loop;
54 spa_source_func_t func;
55 void *data;
56 int fd;
57 uint32_t mask;
58 uint32_t rmask;
59 /* private data for the loop implementer */
60 void *priv;
61 };
62  
63 typedef int (*spa_invoke_func_t) (struct spa_loop *loop,
64 bool async,
65 uint32_t seq,
66 const void *data,
67 size_t size,
68 void *user_data);
69  
70 /**
71 * Register sources and work items to an event loop
72 */
73 struct spa_loop_methods {
74 /* the version of this structure. This can be used to expand this
75 * structure in the future */
76 #define SPA_VERSION_LOOP_METHODS 0
77 uint32_t version;
78  
79 /** Add a source to the loop. Must be called from the loop's own thread.
80 *
81 * \param[in] object The callbacks data.
82 * \param[in] source The source.
83 * \return 0 on success, negative errno-style value on failure.
84 */
85 int (*add_source) (void *object,
86 struct spa_source *source);
87  
88 /** Update the source io mask. Must be called from the loop's own thread.
89 *
90 * \param[in] object The callbacks data.
91 * \param[in] source The source.
92 * \return 0 on success, negative errno-style value on failure.
93 */
94 int (*update_source) (void *object,
95 struct spa_source *source);
96  
97 /** Remove a source from the loop. Must be called from the loop's own thread.
98 *
99 * \param[in] object The callbacks data.
100 * \param[in] source The source.
101 * \return 0 on success, negative errno-style value on failure.
102 */
103 int (*remove_source) (void *object,
104 struct spa_source *source);
105  
106 /** Invoke a function in the context of this loop.
107 * May be called from any thread and multiple threads at the same time.
108 *
109 * If called from the loop's thread, all callbacks previously queued with
110 * invoke() will be run synchronously, which might cause unexpected
111 * reentrancy problems.
112 *
113 * \param[in] object The callbacks data.
114 * \param func The function to be invoked.
115 * \param seq An opaque sequence number. This will be made
116 * available to func.
117 * \param[in] data Data that will be copied into the internal ring buffer and made
118 * available to func. Because this data is copied, it is okay to
119 * pass a pointer to a local variable, but do not pass a pointer to
120 * an object that has identity.
121 * \param size The size of data to copy.
122 * \param block If \true, do not return until func has been called. Otherwise,
123 * returns immediately. Passing \true can cause a deadlock when
124 * the calling thread is holding the loop context lock. A blocking
125 * invoke should never be done from a realtime thread. Also beware
126 * of blocking invokes between 2 threads as you can easily end up
127 * in a deadly embrace.
128 * \param user_data An opaque pointer passed to func.
129 * \return `-EPIPE` if the internal ring buffer filled up,
130 * if block is \false, 0 if seq was SPA_ID_INVALID or
131 * seq with the ASYNC flag set
132 * or the return value of func otherwise. */
133 int (*invoke) (void *object,
134 spa_invoke_func_t func,
135 uint32_t seq,
136 const void *data,
137 size_t size,
138 bool block,
139 void *user_data);
140  
141 /** Call a function with the loop lock acquired
142 * May be called from any thread and multiple threads at the same time.
143 *
144 * \param[in] object The callbacks data.
145 * \param func The function to be called.
146 * \param seq An opaque sequence number. This will be made
147 * available to func.
148 * \param[in] data Data that will be passed to func.
149 * \param size The size of data.
150 * \param user_data An opaque pointer passed to func.
151 * \return the return value of func. */
152 int (*locked) (void *object,
153 spa_invoke_func_t func,
154 uint32_t seq,
155 const void *data,
156 size_t size,
157 void *user_data);
158 };
159  
160 SPA_API_LOOP int spa_loop_add_source(struct spa_loop *object, struct spa_source *source)
161 {
162 return spa_api_method_r(int, -ENOTSUP,
163 spa_loop, &object->iface, add_source, 0, source);
164 }
165 SPA_API_LOOP int spa_loop_update_source(struct spa_loop *object, struct spa_source *source)
166 {
167 return spa_api_method_r(int, -ENOTSUP,
168 spa_loop, &object->iface, update_source, 0, source);
169 }
170 SPA_API_LOOP int spa_loop_remove_source(struct spa_loop *object, struct spa_source *source)
171 {
172 return spa_api_method_r(int, -ENOTSUP,
173 spa_loop, &object->iface, remove_source, 0, source);
174 }
175 SPA_API_LOOP int spa_loop_invoke(struct spa_loop *object,
176 spa_invoke_func_t func, uint32_t seq, const void *data,
177 size_t size, bool block, void *user_data)
178 {
179 return spa_api_method_r(int, -ENOTSUP,
180 spa_loop, &object->iface, invoke, 0, func, seq, data,
181 size, block, user_data);
182 }
183 SPA_API_LOOP int spa_loop_locked(struct spa_loop *object,
184 spa_invoke_func_t func, uint32_t seq, const void *data,
185 size_t size, void *user_data)
186 {
187 return spa_api_method_r(int, -ENOTSUP,
188 spa_loop, &object->iface, locked, 0, func, seq, data,
189 size, user_data);
190 }
191  
192  
193 /** Control hooks. These hooks can't be removed from their
194 * callbacks and must be removed from a safe place (when the loop
195 * is not running or when it is locked). */
196 struct spa_loop_control_hooks {
197 #define SPA_VERSION_LOOP_CONTROL_HOOKS 0
198 uint32_t version;
199 /** Executed right before waiting for events. It is typically used to
200 * release locks or integrate other fds into the loop. */
201 void (*before) (void *data);
202 /** Executed right after waiting for events. It is typically used to
203 * reacquire locks or integrate other fds into the loop. */
204 void (*after) (void *data);
205 };
206  
207 SPA_API_LOOP void spa_loop_control_hook_before(struct spa_hook_list *l)
208 {
209 struct spa_hook *h;
210 spa_list_for_each_reverse(h, &l->list, link)
211 spa_callbacks_call_fast(&h->cb, struct spa_loop_control_hooks, before, 0);
212 }
213  
214 SPA_API_LOOP void spa_loop_control_hook_after(struct spa_hook_list *l)
215 {
216 struct spa_hook *h;
217 spa_list_for_each(h, &l->list, link)
218 spa_callbacks_call_fast(&h->cb, struct spa_loop_control_hooks, after, 0);
219 }
220  
221 /**
222 * Control an event loop
223 *
224 * The event loop control function provide API to run the event loop.
225 *
226 * The below (pseudo)code is a minimal example outlining the use of the loop
227 * control:
228 * \code{.c}
229 * spa_loop_control_enter(loop);
230 * while (running) {
231 * spa_loop_control_iterate(loop, -1);
232 * }
233 * spa_loop_control_leave(loop);
234 * \endcode
235 *
236 * It is also possible to add the loop to an existing event loop by using the
237 * spa_loop_control_get_fd() call. This fd will become readable when activity
238 * has been detected on the sources in the loop. spa_loop_control_iterate() with
239 * a 0 timeout should be called to process the pending sources.
240 *
241 * spa_loop_control_enter() and spa_loop_control_leave() should be called once
242 * from the thread that will run the iterate() function.
243 */
244 struct spa_loop_control_methods {
245 /* the version of this structure. This can be used to expand this
246 * structure in the future */
247 #define SPA_VERSION_LOOP_CONTROL_METHODS 2
248 uint32_t version;
249  
250 /** get the loop fd
251 * \param object the control to query
252 *
253 * Get the fd of this loop control. This fd will be readable when a
254 * source in the loop has activity. The user should call iterate()
255 * with a 0 timeout to schedule one iteration of the loop and dispatch
256 * the sources.
257 * \return the fd of the loop
258 */
259 int (*get_fd) (void *object);
260  
261 /** Add a hook
262 * \param object the control to change
263 * \param hooks the hooks to add
264 *
265 * Adds hooks to the loop controlled by \a ctrl.
266 */
267 void (*add_hook) (void *object,
268 struct spa_hook *hook,
269 const struct spa_loop_control_hooks *hooks,
270 void *data);
271  
272 /** Enter a loop
273 * \param object the control
274 *
275 * This function should be called before calling iterate and is
276 * typically used to capture the thread that this loop will run in.
277 * It should ideally be called once from the thread that will run
278 * the loop. This function will lock the loop.
279 */
280 void (*enter) (void *object);
281 /** Leave a loop
282 * \param object the control
283 *
284 * It should ideally be called once after calling iterate when the loop
285 * will no longer be iterated from the thread that called enter().
286 *
287 * This function will unlock the loop.
288 */
289 void (*leave) (void *object);
290  
291 /** Perform one iteration of the loop.
292 * \param ctrl the control
293 * \param timeout an optional timeout in milliseconds.
294 * 0 for no timeout, -1 for infinite timeout.
295 *
296 * This function will first unlock the loop and then block
297 * up to \a timeout milliseconds, lock the loop again and then
298 * dispatch the fds with activity.
299 *
300 * The number of dispatched fds is returned.
301 */
302 int (*iterate) (void *object, int timeout);
303  
304 /** Check context of the loop
305 * \param ctrl the control
306 *
307 * This function will check if the current thread is currently the
308 * one that did the enter call. Since version 1:1.
309 *
310 * returns 1 on success, 0 or negative errno value on error.
311 */
312 int (*check) (void *object);
313  
314 /** Lock the loop.
315 * This will ensure the loop is not in the process of dispatching
316 * callbacks. Since version 2:2
317 *
318 * \param[in] object the control
319 * \return 0 on success or a negative return value on error.
320 */
321 int (*lock) (void *object);
322  
323 /** Unlock the loop.
324 * Unlocks the loop again so that callbacks can be dispatched
325 * again. Since version 2:2
326 *
327 * \param[in] object the control
328 * \return 0 on success or a negative return value on error.
329 */
330 int (*unlock) (void *object);
331  
332 /** get the absolute time
333 * Get the current time with \ref timeout that can be used in wait.
334 * Since version 2:2
335 *
336 * This function can be called from any thread.
337 */
338 int (*get_time) (void *object, struct timespec *abstime, int64_t timeout);
339  
340 /** Wait for a signal
341 * Wait until a thread performs signal. Since version 2:2
342 *
343 * This function must be called with the loop lock. Because this is a
344 * blocking call, it should not be performed from a realtime thread.
345 *
346 * \param[in] object the control
347 * \param[in] abstime the maximum time to wait for the signal or NULL
348 * \return 0 on success or a negative return value on error.
349 */
350 int (*wait) (void *object, const struct timespec *abstime);
351  
352 /** Signal waiters
353 * Wake up all threads blocked in wait. Since version 2:2
354 * When wait_for_accept is set, this functions blocks until all
355 * threads performed accept.
356 *
357 * This function must be called with the loop lock and is safe to
358 * call from a realtime thread source dispatch functions when
359 * wait_for_accept is false.
360 *
361 * \param[in] object the control
362 * \param[in] wait_for_accept block for accept
363 * \return 0 on success or a negative return value on error.
364 */
365 int (*signal) (void *object, bool wait_for_accept);
366  
367 /** Accept signalers
368 * Resume the thread that signaled with wait_for accept.
369 *
370 * This function must be called with the loop lock and is safe to
371 * call from a realtime thread source dispatch functions.
372 *
373 * \param[in] object the control
374 * \return 0 on success or a negative return value on error.
375 */
376 int (*accept) (void *object);
377 };
378  
379 SPA_API_LOOP int spa_loop_control_get_fd(struct spa_loop_control *object)
380 {
381 return spa_api_method_r(int, -ENOTSUP,
382 spa_loop_control, &object->iface, get_fd, 0);
383 }
384 SPA_API_LOOP void spa_loop_control_add_hook(struct spa_loop_control *object,
385 struct spa_hook *hook, const struct spa_loop_control_hooks *hooks,
386 void *data)
387 {
388 spa_api_method_v(spa_loop_control, &object->iface, add_hook, 0,
389 hook, hooks, data);
390 }
391 SPA_API_LOOP void spa_loop_control_enter(struct spa_loop_control *object)
392 {
393 spa_api_method_v(spa_loop_control, &object->iface, enter, 0);
394 }
395 SPA_API_LOOP void spa_loop_control_leave(struct spa_loop_control *object)
396 {
397 spa_api_method_v(spa_loop_control, &object->iface, leave, 0);
398 }
399 SPA_API_LOOP int spa_loop_control_iterate(struct spa_loop_control *object,
400 int timeout)
401 {
402 return spa_api_method_r(int, -ENOTSUP,
403 spa_loop_control, &object->iface, iterate, 0, timeout);
404 }
405 SPA_API_LOOP int spa_loop_control_iterate_fast(struct spa_loop_control *object,
406 int timeout)
407 {
408 return spa_api_method_fast_r(int, -ENOTSUP,
409 spa_loop_control, &object->iface, iterate, 0, timeout);
410 }
411 SPA_API_LOOP int spa_loop_control_check(struct spa_loop_control *object)
412 {
413 return spa_api_method_r(int, -ENOTSUP,
414 spa_loop_control, &object->iface, check, 1);
415 }
416 SPA_API_LOOP int spa_loop_control_lock(struct spa_loop_control *object)
417 {
418 return spa_api_method_r(int, -ENOTSUP,
419 spa_loop_control, &object->iface, lock, 2);
420 }
421 SPA_API_LOOP int spa_loop_control_unlock(struct spa_loop_control *object)
422 {
423 return spa_api_method_r(int, -ENOTSUP,
424 spa_loop_control, &object->iface, unlock, 2);
425 }
426 SPA_API_LOOP int spa_loop_control_get_time(struct spa_loop_control *object,
427 struct timespec *abstime, int64_t timeout)
428 {
429 return spa_api_method_r(int, -ENOTSUP,
430 spa_loop_control, &object->iface, get_time, 2, abstime, timeout);
431 }
432 SPA_API_LOOP int spa_loop_control_wait(struct spa_loop_control *object,
433 const struct timespec *abstime)
434 {
435 return spa_api_method_r(int, -ENOTSUP,
436 spa_loop_control, &object->iface, wait, 2, abstime);
437 }
438 SPA_API_LOOP int spa_loop_control_signal(struct spa_loop_control *object, bool wait_for_accept)
439 {
440 return spa_api_method_r(int, -ENOTSUP,
441 spa_loop_control, &object->iface, signal, 2, wait_for_accept);
442 }
443 SPA_API_LOOP int spa_loop_control_accept(struct spa_loop_control *object)
444 {
445 return spa_api_method_r(int, -ENOTSUP,
446 spa_loop_control, &object->iface, accept, 2);
447 }
448  
449 typedef void (*spa_source_io_func_t) (void *data, int fd, uint32_t mask);
450 typedef void (*spa_source_idle_func_t) (void *data);
451 typedef void (*spa_source_event_func_t) (void *data, uint64_t count);
452 typedef void (*spa_source_timer_func_t) (void *data, uint64_t expirations);
453 typedef void (*spa_source_signal_func_t) (void *data, int signal_number);
454  
455 /**
456 * Create sources for an event loop
457 */
458 struct spa_loop_utils_methods {
459 /* the version of this structure. This can be used to expand this
460 * structure in the future */
461 #define SPA_VERSION_LOOP_UTILS_METHODS 0
462 uint32_t version;
463  
464 struct spa_source *(*add_io) (void *object,
465 int fd,
466 uint32_t mask,
467 bool close,
468 spa_source_io_func_t func, void *data);
469  
470 int (*update_io) (void *object, struct spa_source *source, uint32_t mask);
471  
472 struct spa_source *(*add_idle) (void *object,
473 bool enabled,
474 spa_source_idle_func_t func, void *data);
475 int (*enable_idle) (void *object, struct spa_source *source, bool enabled);
476  
477 struct spa_source *(*add_event) (void *object,
478 spa_source_event_func_t func, void *data);
479 int (*signal_event) (void *object, struct spa_source *source);
480  
481 struct spa_source *(*add_timer) (void *object,
482 spa_source_timer_func_t func, void *data);
483 int (*update_timer) (void *object,
484 struct spa_source *source,
485 struct timespec *value,
486 struct timespec *interval,
487 bool absolute);
488 struct spa_source *(*add_signal) (void *object,
489 int signal_number,
490 spa_source_signal_func_t func, void *data);
491  
492 /** destroy a source allocated with this interface. This function
493 * should only be called when the loop is not running or from the
494 * context of the running loop */
495 void (*destroy_source) (void *object, struct spa_source *source);
496 };
497  
498 SPA_API_LOOP struct spa_source *
499 spa_loop_utils_add_io(struct spa_loop_utils *object, int fd, uint32_t mask,
500 bool close, spa_source_io_func_t func, void *data)
501 {
502 return spa_api_method_r(struct spa_source *, NULL,
503 spa_loop_utils, &object->iface, add_io, 0, fd, mask, close, func, data);
504 }
505 SPA_API_LOOP int spa_loop_utils_update_io(struct spa_loop_utils *object,
506 struct spa_source *source, uint32_t mask)
507 {
508 return spa_api_method_r(int, -ENOTSUP,
509 spa_loop_utils, &object->iface, update_io, 0, source, mask);
510 }
511 SPA_API_LOOP struct spa_source *
512 spa_loop_utils_add_idle(struct spa_loop_utils *object, bool enabled,
513 spa_source_idle_func_t func, void *data)
514 {
515 return spa_api_method_r(struct spa_source *, NULL,
516 spa_loop_utils, &object->iface, add_idle, 0, enabled, func, data);
517 }
518 SPA_API_LOOP int spa_loop_utils_enable_idle(struct spa_loop_utils *object,
519 struct spa_source *source, bool enabled)
520 {
521 return spa_api_method_r(int, -ENOTSUP,
522 spa_loop_utils, &object->iface, enable_idle, 0, source, enabled);
523 }
524 SPA_API_LOOP struct spa_source *
525 spa_loop_utils_add_event(struct spa_loop_utils *object, spa_source_event_func_t func, void *data)
526 {
527 return spa_api_method_r(struct spa_source *, NULL,
528 spa_loop_utils, &object->iface, add_event, 0, func, data);
529 }
530 SPA_API_LOOP int spa_loop_utils_signal_event(struct spa_loop_utils *object,
531 struct spa_source *source)
532 {
533 return spa_api_method_r(int, -ENOTSUP,
534 spa_loop_utils, &object->iface, signal_event, 0, source);
535 }
536 SPA_API_LOOP struct spa_source *
537 spa_loop_utils_add_timer(struct spa_loop_utils *object, spa_source_timer_func_t func, void *data)
538 {
539 return spa_api_method_r(struct spa_source *, NULL,
540 spa_loop_utils, &object->iface, add_timer, 0, func, data);
541 }
542 SPA_API_LOOP int spa_loop_utils_update_timer(struct spa_loop_utils *object,
543 struct spa_source *source, struct timespec *value,
544 struct timespec *interval, bool absolute)
545 {
546 return spa_api_method_r(int, -ENOTSUP,
547 spa_loop_utils, &object->iface, update_timer, 0, source,
548 value, interval, absolute);
549 }
550 SPA_API_LOOP struct spa_source *
551 spa_loop_utils_add_signal(struct spa_loop_utils *object, int signal_number,
552 spa_source_signal_func_t func, void *data)
553 {
554 return spa_api_method_r(struct spa_source *, NULL,
555 spa_loop_utils, &object->iface, add_signal, 0,
556 signal_number, func, data);
557 }
558 SPA_API_LOOP void spa_loop_utils_destroy_source(struct spa_loop_utils *object,
559 struct spa_source *source)
560 {
561 spa_api_method_v(spa_loop_utils, &object->iface, destroy_source, 0, source);
562 }
563  
564 /**
565 * \}
566 */
567  
568 #ifdef __cplusplus
569 } /* extern "C" */
570 #endif
571  
572 #endif /* SPA_LOOP_H */