demux: mp4: use struct for coreaudio layout params
[vlc.git] / include / vlc_threads.h
blob315729a0976e2085767c6781a8ff1fcffb1d83d8
1 /*****************************************************************************
2 * vlc_threads.h : threads implementation for the VideoLAN client
3 * This header provides portable declarations for mutexes & conditions
4 *****************************************************************************
5 * Copyright (C) 1999, 2002 VLC authors and VideoLAN
6 * Copyright © 2007-2016 Rémi Denis-Courmont
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
31 /**
32 * \ingroup os
33 * \defgroup thread Threads and synchronization primitives
34 * @{
35 * \file
36 * Thread primitive declarations
39 /**
40 * Issues an explicit deferred cancellation point.
42 * This has no effects if thread cancellation is disabled.
43 * This can be called when there is a rather slow non-sleeping operation.
44 * This is also used to force a cancellation point in a function that would
45 * otherwise <em>not always</em> be one (block_FifoGet() is an example).
47 VLC_API void vlc_testcancel(void);
49 #if defined (_WIN32)
50 # include <process.h>
51 # ifndef ETIMEDOUT
52 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
53 # endif
55 typedef struct vlc_thread *vlc_thread_t;
56 # define VLC_THREAD_CANCELED NULL
57 # define LIBVLC_NEED_SLEEP
58 typedef struct
60 bool dynamic;
61 union
63 struct
65 bool locked;
66 unsigned long contention;
68 CRITICAL_SECTION mutex;
70 } vlc_mutex_t;
71 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
72 #define LIBVLC_NEED_CONDVAR
73 #define LIBVLC_NEED_SEMAPHORE
74 #define LIBVLC_NEED_RWLOCK
75 typedef INIT_ONCE vlc_once_t;
76 #define VLC_STATIC_ONCE INIT_ONCE_STATIC_INIT
77 typedef struct vlc_threadvar *vlc_threadvar_t;
78 typedef struct vlc_timer *vlc_timer_t;
80 # define VLC_THREAD_PRIORITY_LOW 0
81 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
82 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
83 # define VLC_THREAD_PRIORITY_VIDEO 0
84 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
85 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
87 static inline int vlc_poll(struct pollfd *fds, unsigned nfds, int timeout)
89 int val;
91 vlc_testcancel();
92 val = poll(fds, nfds, timeout);
93 if (val < 0)
94 vlc_testcancel();
95 return val;
97 # define poll(u,n,t) vlc_poll(u, n, t)
99 #elif defined (__OS2__)
100 # include <errno.h>
102 typedef struct vlc_thread *vlc_thread_t;
103 #define VLC_THREAD_CANCELED NULL
104 typedef struct
106 bool dynamic;
107 union
109 struct
111 bool locked;
112 unsigned long contention;
114 HMTX hmtx;
116 } vlc_mutex_t;
117 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
118 typedef struct
120 HEV hev;
121 unsigned waiters;
122 HEV hevAck;
123 unsigned signaled;
124 } vlc_cond_t;
125 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
126 #define LIBVLC_NEED_SEMAPHORE
127 #define LIBVLC_NEED_RWLOCK
128 typedef struct
130 unsigned done;
131 vlc_mutex_t mutex;
132 } vlc_once_t;
133 #define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }
134 typedef struct vlc_threadvar *vlc_threadvar_t;
135 typedef struct vlc_timer *vlc_timer_t;
137 # define VLC_THREAD_PRIORITY_LOW 0
138 # define VLC_THREAD_PRIORITY_INPUT \
139 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
140 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
141 # define VLC_THREAD_PRIORITY_VIDEO 0
142 # define VLC_THREAD_PRIORITY_OUTPUT \
143 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
144 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
146 # define pthread_sigmask sigprocmask
148 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
150 static int (*vlc_poll_os2)(struct pollfd *, unsigned, int) = NULL;
152 if (!vlc_poll_os2)
154 HMODULE hmod;
155 CHAR szFailed[CCHMAXPATH];
157 if (DosLoadModule(szFailed, sizeof(szFailed), "vlccore", &hmod))
158 return -1;
160 if (DosQueryProcAddr(hmod, 0, "_vlc_poll_os2", (PFN *)&vlc_poll_os2))
161 return -1;
164 return (*vlc_poll_os2)(fds, nfds, timeout);
166 # define poll(u,n,t) vlc_poll(u, n, t)
168 #elif defined (__ANDROID__) /* pthreads subset without pthread_cancel() */
169 # include <unistd.h>
170 # include <pthread.h>
171 # include <poll.h>
172 # define LIBVLC_USE_PTHREAD_CLEANUP 1
173 # define LIBVLC_NEED_SLEEP
174 # define LIBVLC_NEED_CONDVAR
175 # define LIBVLC_NEED_SEMAPHORE
176 # define LIBVLC_NEED_RWLOCK
178 typedef struct vlc_thread *vlc_thread_t;
179 #define VLC_THREAD_CANCELED NULL
180 typedef pthread_mutex_t vlc_mutex_t;
181 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
182 typedef pthread_once_t vlc_once_t;
183 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
184 typedef pthread_key_t vlc_threadvar_t;
185 typedef struct vlc_timer *vlc_timer_t;
187 # define VLC_THREAD_PRIORITY_LOW 0
188 # define VLC_THREAD_PRIORITY_INPUT 0
189 # define VLC_THREAD_PRIORITY_AUDIO 0
190 # define VLC_THREAD_PRIORITY_VIDEO 0
191 # define VLC_THREAD_PRIORITY_OUTPUT 0
192 # define VLC_THREAD_PRIORITY_HIGHEST 0
194 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
196 int val;
200 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
201 if (timeout >= 0)
202 timeout -= ugly_timeout;
204 vlc_testcancel ();
205 val = poll (fds, nfds, ugly_timeout);
207 while (val == 0 && timeout != 0);
209 return val;
212 # define poll(u,n,t) vlc_poll(u, n, t)
214 #elif defined (__APPLE__)
215 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
216 # include <unistd.h>
217 # include <pthread.h>
218 /* Unnamed POSIX semaphores not supported on Mac OS X */
219 # include <mach/semaphore.h>
220 # include <mach/task.h>
221 # define LIBVLC_USE_PTHREAD_CLEANUP 1
223 typedef pthread_t vlc_thread_t;
224 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
225 typedef pthread_mutex_t vlc_mutex_t;
226 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
227 typedef pthread_cond_t vlc_cond_t;
228 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
229 typedef semaphore_t vlc_sem_t;
230 typedef pthread_rwlock_t vlc_rwlock_t;
231 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
232 typedef pthread_once_t vlc_once_t;
233 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
234 typedef pthread_key_t vlc_threadvar_t;
235 typedef struct vlc_timer *vlc_timer_t;
237 # define VLC_THREAD_PRIORITY_LOW 0
238 # define VLC_THREAD_PRIORITY_INPUT 22
239 # define VLC_THREAD_PRIORITY_AUDIO 22
240 # define VLC_THREAD_PRIORITY_VIDEO 0
241 # define VLC_THREAD_PRIORITY_OUTPUT 22
242 # define VLC_THREAD_PRIORITY_HIGHEST 22
244 #else /* POSIX threads */
245 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
246 # include <pthread.h>
247 # include <semaphore.h>
250 * Whether LibVLC threads are based on POSIX threads.
252 # define LIBVLC_USE_PTHREAD 1
255 * Whether LibVLC thread cancellation is based on POSIX threads.
257 # define LIBVLC_USE_PTHREAD_CLEANUP 1
260 * Thread handle.
262 typedef struct
264 pthread_t handle;
265 } vlc_thread_t;
268 * Return value of a canceled thread.
270 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
273 * Mutex.
275 * Storage space for a mutual exclusion lock.
277 typedef pthread_mutex_t vlc_mutex_t;
280 * Static initializer for (static) mutex.
282 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
285 * Condition variable.
287 * Storage space for a thread condition variable.
289 typedef pthread_cond_t vlc_cond_t;
292 * Static initializer for (static) condition variable.
294 * \note
295 * The condition variable will use the default clock, which is OS-dependent.
296 * Therefore, where timed waits are necessary the condition variable should
297 * always be initialized dynamically explicit instead of using this
298 * initializer.
300 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
303 * Semaphore.
305 * Storage space for a thread-safe semaphore.
307 typedef sem_t vlc_sem_t;
310 * Read/write lock.
312 * Storage space for a slim reader/writer lock.
314 typedef pthread_rwlock_t vlc_rwlock_t;
317 * Static initializer for (static) read/write lock.
319 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
322 * One-time initialization.
324 * A one-time initialization object must always be initialized assigned to
325 * \ref VLC_STATIC_ONCE before use.
327 typedef pthread_once_t vlc_once_t;
330 * Static initializer for one-time initialization.
332 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
335 * Thread-local key handle.
337 typedef pthread_key_t vlc_threadvar_t;
340 * Threaded timer handle.
342 typedef struct vlc_timer *vlc_timer_t;
344 # define VLC_THREAD_PRIORITY_LOW 0
345 # define VLC_THREAD_PRIORITY_INPUT 10
346 # define VLC_THREAD_PRIORITY_AUDIO 5
347 # define VLC_THREAD_PRIORITY_VIDEO 0
348 # define VLC_THREAD_PRIORITY_OUTPUT 15
349 # define VLC_THREAD_PRIORITY_HIGHEST 20
351 #endif
353 #ifdef LIBVLC_NEED_CONDVAR
354 typedef struct
356 unsigned value;
357 } vlc_cond_t;
358 # define VLC_STATIC_COND { 0 }
359 #endif
361 #ifdef LIBVLC_NEED_SEMAPHORE
362 typedef struct vlc_sem
364 vlc_mutex_t lock;
365 vlc_cond_t wait;
366 unsigned value;
367 } vlc_sem_t;
368 #endif
370 #ifdef LIBVLC_NEED_RWLOCK
371 typedef struct vlc_rwlock
373 vlc_mutex_t mutex;
374 vlc_cond_t wait;
375 long state;
376 } vlc_rwlock_t;
377 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
378 #endif
381 * Initializes a fast mutex.
383 * Recursive locking of a fast mutex is undefined behaviour. (In debug builds,
384 * recursive locking will cause an assertion failure.)
386 VLC_API void vlc_mutex_init(vlc_mutex_t *);
389 * Initializes a recursive mutex.
390 * \warning This is strongly discouraged. Please use normal mutexes.
392 VLC_API void vlc_mutex_init_recursive(vlc_mutex_t *);
395 * Deinitializes a mutex.
397 * The mutex must not be locked, otherwise behaviour is undefined.
399 VLC_API void vlc_mutex_destroy(vlc_mutex_t *);
402 * Acquires a mutex.
404 * If needed, this waits for any other thread to release it.
406 * \warning Beware of deadlocks when locking multiple mutexes at the same time,
407 * or when using mutexes from callbacks.
409 * \note This function is not a cancellation point.
411 VLC_API void vlc_mutex_lock(vlc_mutex_t *);
414 * Tries to acquire a mutex.
416 * This function acquires the mutex if and only if it is not currently held by
417 * another thread. This function never sleeps and can be used in delay-critical
418 * code paths.
420 * \note This function is not a cancellation point.
422 * \warning If this function fails, then the mutex is held... by another
423 * thread. The calling thread must deal with the error appropriately. That
424 * typically implies postponing the operations that would have required the
425 * mutex. If the thread cannot defer those operations, then it must use
426 * vlc_mutex_lock(). If in doubt, use vlc_mutex_lock() instead.
428 * @return 0 if the mutex could be acquired, an error code otherwise.
430 VLC_API int vlc_mutex_trylock( vlc_mutex_t * ) VLC_USED;
433 * Releases a mutex.
435 * If the mutex is not held by the calling thread, the behaviour is undefined.
437 * \note This function is not a cancellation point.
439 VLC_API void vlc_mutex_unlock(vlc_mutex_t *);
442 * Checks if a mutex is locked.
444 * Do not use this function directly. Use vlc_mutex_assert() instead.
446 * @note
447 * This function has no effects.
448 * It is only meant to be use in run-time assertions.
450 * @retval false in debug builds of LibVLC,
451 * if the mutex is not locked by the calling thread;
452 * @retval true in debug builds of LibVLC,
453 * if the mutex is locked by the calling thread;
454 * @retval true in release builds of LibVLC.
456 VLC_API bool vlc_mutex_marked(const vlc_mutex_t *) VLC_USED;
459 * Asserts that a mutex is locked by the calling thread.
461 #define vlc_mutex_assert(m) assert(vlc_mutex_marked(m))
464 * Initializes a condition variable.
466 VLC_API void vlc_cond_init(vlc_cond_t *);
469 * Initializes a condition variable (wall clock).
471 * This function initializes a condition variable for timed waiting using the
472 * UTC wall clock time. The time reference is the same as with time() and with
473 * timespec_get() and TIME_UTC.
474 * vlc_cond_timedwait_daytime() must be instead of
475 * vlc_cond_timedwait() for actual waiting.
477 void vlc_cond_init_daytime(vlc_cond_t *);
480 * Deinitializes a condition variable.
482 * No threads shall be waiting or signaling the condition, otherwise the
483 * behavior is undefined.
485 VLC_API void vlc_cond_destroy(vlc_cond_t *);
488 * Wakes up one thread waiting on a condition variable.
490 * If any thread is currently waiting on the condition variable, at least one
491 * of those threads will be woken up. Otherwise, this function has no effects.
493 * \note This function is not a cancellation point.
495 VLC_API void vlc_cond_signal(vlc_cond_t *);
498 * Wakes up all threads waiting on a condition variable.
500 * \note This function is not a cancellation point.
502 VLC_API void vlc_cond_broadcast(vlc_cond_t *);
505 * Waits on a condition variable.
507 * The calling thread will be suspended until another thread calls
508 * vlc_cond_signal() or vlc_cond_broadcast() on the same condition variable,
509 * the thread is cancelled with vlc_cancel(), or the system causes a
510 * <em>spurious</em> unsolicited wake-up.
512 * A mutex is needed to wait on a condition variable. It must <b>not</b> be
513 * a recursive mutex. Although it is possible to use the same mutex for
514 * multiple condition, it is not valid to use different mutexes for the same
515 * condition variable at the same time from different threads.
517 * The canonical way to use a condition variable to wait for event foobar is:
518 @code
519 vlc_mutex_lock(&lock);
520 mutex_cleanup_push(&lock); // release the mutex in case of cancellation
522 while (!foobar)
523 vlc_cond_wait(&wait, &lock);
525 // -- foobar is now true, do something about it here --
527 vlc_cleanup_pop();
528 vlc_mutex_unlock(&lock);
529 @endcode
531 * \note This function is a cancellation point. In case of thread cancellation,
532 * the mutex is always locked before cancellation proceeds.
534 * \param cond condition variable to wait on
535 * \param mutex mutex which is unlocked while waiting,
536 * then locked again when waking up.
538 VLC_API void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex);
541 * Waits on a condition variable up to a certain date.
543 * This works like vlc_cond_wait() but with an additional time-out.
544 * The time-out is expressed as an absolute timestamp using the same arbitrary
545 * time reference as the vlc_tick_now() and vlc_tick_wait() functions.
547 * \note This function is a cancellation point. In case of thread cancellation,
548 * the mutex is always locked before cancellation proceeds.
550 * \param cond condition variable to wait on
551 * \param mutex mutex which is unlocked while waiting,
552 * then locked again when waking up
553 * \param deadline <b>absolute</b> timeout
555 * \warning If the variable was initialized with vlc_cond_init_daytime(), or
556 * was statically initialized with \ref VLC_STATIC_COND, the time reference
557 * used by this function is unspecified (depending on the implementation, it
558 * might be the Unix epoch or the vlc_tick_now() clock).
560 * \return 0 if the condition was signaled, an error code in case of timeout.
562 VLC_API int vlc_cond_timedwait(vlc_cond_t *cond, vlc_mutex_t *mutex,
563 vlc_tick_t deadline);
565 int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t);
568 * Initializes a semaphore.
570 * @param count initial semaphore value (typically 0)
572 VLC_API void vlc_sem_init(vlc_sem_t *, unsigned count);
575 * Deinitializes a semaphore.
577 VLC_API void vlc_sem_destroy(vlc_sem_t *);
580 * Increments the value of a semaphore.
582 * \note This function is not a cancellation point.
584 * \return 0 on success, EOVERFLOW in case of integer overflow.
586 VLC_API int vlc_sem_post(vlc_sem_t *);
589 * Waits on a semaphore.
591 * This function atomically waits for the semaphore to become non-zero then
592 * decrements it, and returns. If the semaphore is non-zero on entry, it is
593 * immediately decremented.
595 * \note This function may be a point of cancellation.
597 VLC_API void vlc_sem_wait(vlc_sem_t *);
600 * Initializes a read/write lock.
602 VLC_API void vlc_rwlock_init(vlc_rwlock_t *);
605 * Destroys an initialized unused read/write lock.
607 VLC_API void vlc_rwlock_destroy(vlc_rwlock_t *);
610 * Acquires a read/write lock for reading.
612 * \note Recursion is allowed.
613 * \note This function may be a point of cancellation.
615 VLC_API void vlc_rwlock_rdlock(vlc_rwlock_t *);
618 * Acquires a read/write lock for writing. Recursion is not allowed.
619 * \note This function may be a point of cancellation.
621 VLC_API void vlc_rwlock_wrlock(vlc_rwlock_t *);
624 * Releases a read/write lock.
626 * The calling thread must hold the lock. Otherwise behaviour is undefined.
628 * \note This function is not a cancellation point.
630 VLC_API void vlc_rwlock_unlock(vlc_rwlock_t *);
633 * Executes a function one time.
635 * The first time this function is called with a given one-time initialization
636 * object, it executes the provided callback.
637 * Any further call with the same object will be a no-op.
639 * In the corner case that the first time execution is ongoing in another
640 * thread, then the function will wait for completion on the other thread
641 * (and then synchronize memory) before it returns.
642 * This ensures that, no matter what, the callback has been executed exactly
643 * once and its side effects are visible after the function returns.
645 * \param once a one-time initialization object
646 * \param cb callback to execute (the first time)
648 VLC_API void vlc_once(vlc_once_t *restrict once, void (*cb)(void));
651 * Allocates a thread-specific variable.
653 * @param key where to store the thread-specific variable handle
654 * @param destr a destruction callback. It is called whenever a thread exits
655 * and the thread-specific variable has a non-NULL value.
657 * @return 0 on success, a system error code otherwise.
658 * This function can actually fail: on most systems, there is a fixed limit to
659 * the number of thread-specific variables in a given process.
661 VLC_API int vlc_threadvar_create(vlc_threadvar_t *key, void (*destr) (void *));
664 * Deallocates a thread-specific variable.
666 VLC_API void vlc_threadvar_delete(vlc_threadvar_t *);
669 * Sets a thread-specific variable.
671 * \param key thread-local variable key (created with vlc_threadvar_create())
672 * \param value new value for the variable for the calling thread
673 * \return 0 on success, a system error code otherwise.
675 VLC_API int vlc_threadvar_set(vlc_threadvar_t key, void *value);
678 * Gets the value of a thread-local variable for the calling thread.
679 * This function cannot fail.
681 * \return the value associated with the given variable for the calling
682 * or NULL if no value was set.
684 VLC_API void *vlc_threadvar_get(vlc_threadvar_t);
687 * Waits on an address.
689 * Puts the calling thread to sleep if a specific value is stored at a
690 * specified address. The thread will sleep until it is woken up by a call to
691 * vlc_addr_signal() or vlc_addr_broadcast() in another thread, or spuriously.
693 * If the value does not match, do nothing and return immediately.
695 * \param addr address to check for
696 * \param val value to match at the address
698 void vlc_addr_wait(void *addr, unsigned val);
701 * Waits on an address with a time-out.
703 * This function operates as vlc_addr_wait() but provides an additional
704 * time-out. If the time-out elapses, the thread resumes and the function
705 * returns.
707 * \param addr address to check for
708 * \param val value to match at the address
709 * \param delay time-out duration
711 * \return true if the function was woken up before the time-out,
712 * false if the time-out elapsed.
714 bool vlc_addr_timedwait(void *addr, unsigned val, vlc_tick_t delay);
717 * Wakes up one thread on an address.
719 * Wakes up (at least) one of the thread sleeping on the specified address.
720 * The address must be equal to the first parameter given by at least one
721 * thread sleeping within the vlc_addr_wait() or vlc_addr_timedwait()
722 * functions. If no threads are found, this function does nothing.
724 * \param addr address identifying which threads may be woken up
726 void vlc_addr_signal(void *addr);
729 * Wakes up all thread on an address.
731 * Wakes up all threads sleeping on the specified address (if any).
732 * Any thread sleeping within a call to vlc_addr_wait() or vlc_addr_timedwait()
733 * with the specified address as first call parameter will be woken up.
735 * \param addr address identifying which threads to wake up
737 void vlc_addr_broadcast(void *addr);
740 * Creates and starts a new thread.
742 * The thread must be <i>joined</i> with vlc_join() to reclaim resources
743 * when it is not needed anymore.
745 * @param th storage space for the handle of the new thread (cannot be NULL)
746 * [OUT]
747 * @param entry entry point for the thread
748 * @param data data parameter given to the entry point
749 * @param priority thread priority value
750 * @return 0 on success, a standard error code on error.
751 * @note In case of error, the value of *th is undefined.
753 VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data,
754 int priority) VLC_USED;
757 * Marks a thread as cancelled.
759 * Next time the target thread reaches a cancellation point (while not having
760 * disabled cancellation), it will run its cancellation cleanup handler, the
761 * thread variable destructors, and terminate.
763 * vlc_join() must be used regardless of a thread being cancelled or not, to
764 * avoid leaking resources.
766 VLC_API void vlc_cancel(vlc_thread_t);
769 * Waits for a thread to complete (if needed), then destroys it.
771 * \note This is a cancellation point. In case of cancellation, the thread is
772 * <b>not</b> joined.
774 * \warning A thread cannot join itself (normally VLC will abort if this is
775 * attempted). Also a detached thread <b>cannot</b> be joined.
777 * @param th thread handle
778 * @param result [OUT] pointer to write the thread return value or NULL
780 VLC_API void vlc_join(vlc_thread_t th, void **result);
783 * Disables thread cancellation.
785 * This functions saves the current cancellation state (enabled or disabled),
786 * then disables cancellation for the calling thread. It must be called before
787 * entering a piece of code that is not cancellation-safe, unless it can be
788 * proven that the calling thread will not be cancelled.
790 * \note This function is not a cancellation point.
792 * \return Previous cancellation state (opaque value for vlc_restorecancel()).
794 VLC_API int vlc_savecancel(void);
797 * Restores the cancellation state.
799 * This function restores the cancellation state of the calling thread to
800 * a state previously saved by vlc_savecancel().
802 * \note This function is not a cancellation point.
804 * \param state previous state as returned by vlc_savecancel().
806 VLC_API void vlc_restorecancel(int state);
809 * Internal handler for thread cancellation.
811 * Do not call this function directly. Use wrapper macros instead:
812 * vlc_cleanup_push(), vlc_cleanup_pop().
814 VLC_API void vlc_control_cancel(int cmd, ...);
817 * Thread handle.
819 * This function returns the thread handle of the calling thread.
821 * \note The exact type of the thread handle depends on the platform,
822 * including an integer type, a pointer type or a compound type of any size.
823 * If you need an integer identifier, use vlc_thread_id() instead.
825 * \note vlc_join(vlc_thread_self(), NULL) is undefined,
826 * as it obviously does not make any sense (it might result in a deadlock, but
827 * there are no warranties that it will).
829 * \return the thread handle
831 VLC_API vlc_thread_t vlc_thread_self(void) VLC_USED;
834 * Thread identifier.
836 * This function returns the identifier of the calling thread. The identifier
837 * cannot change for the entire duration of the thread, and no other thread can
838 * have the same identifier at the same time in the same process. Typically,
839 * the identifier is also unique across all running threads of all existing
840 * processes, but that depends on the operating system.
842 * There are no particular semantics to the thread ID with LibVLC.
843 * It is provided mainly for tracing and debugging.
845 * \warning This function is not currently implemented on all supported
846 * platforms. Where not implemented, it returns (unsigned long)-1.
848 * \return the thread identifier (or -1 if unimplemented)
850 VLC_API unsigned long vlc_thread_id(void) VLC_USED;
853 * Precision monotonic clock.
855 * In principles, the clock has a precision of 1 MHz. But the actual resolution
856 * may be much lower, especially when it comes to sleeping with vlc_tick_wait() or
857 * vlc_tick_sleep(). Most general-purpose operating systems provide a resolution of
858 * only 100 to 1000 Hz.
860 * \warning The origin date (time value "zero") is not specified. It is
861 * typically the time the kernel started, but this is platform-dependent.
862 * If you need wall clock time, use gettimeofday() instead.
864 * \return a timestamp in microseconds.
866 VLC_API vlc_tick_t vlc_tick_now(void);
869 * Waits until a deadline.
871 * \param deadline timestamp to wait for (\ref vlc_tick_now())
873 * \note The deadline may be exceeded due to OS scheduling.
874 * \note This function is a cancellation point.
876 VLC_API void vlc_tick_wait(vlc_tick_t deadline);
879 * Waits for an interval of time.
881 * \param delay how long to wait (in microseconds)
883 * \note The delay may be exceeded due to OS scheduling.
884 * \note This function is a cancellation point.
886 VLC_API void vlc_tick_sleep(vlc_tick_t delay);
888 #define VLC_HARD_MIN_SLEEP VLC_TICK_FROM_MS(10) /* 10 milliseconds = 1 tick at 100Hz */
889 #define VLC_SOFT_MIN_SLEEP VLC_TICK_FROM_SEC(9) /* 9 seconds */
891 #if defined (__GNUC__) && !defined (__clang__)
892 /* Linux has 100, 250, 300 or 1000Hz
894 * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
897 static
898 __attribute__((unused))
899 __attribute__((noinline))
900 __attribute__((error("sorry, cannot sleep for such short a time")))
901 vlc_tick_t impossible_delay( vlc_tick_t delay )
903 (void) delay;
904 return VLC_HARD_MIN_SLEEP;
907 static
908 __attribute__((unused))
909 __attribute__((noinline))
910 __attribute__((warning("use proper event handling instead of short delay")))
911 vlc_tick_t harmful_delay( vlc_tick_t delay )
913 return delay;
916 # define check_delay( d ) \
917 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
918 && (d < VLC_HARD_MIN_SLEEP)) \
919 ? impossible_delay(d) \
920 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
921 && (d < VLC_SOFT_MIN_SLEEP)) \
922 ? harmful_delay(d) \
923 : d))
925 static
926 __attribute__((unused))
927 __attribute__((noinline))
928 __attribute__((error("deadlines can not be constant")))
929 vlc_tick_t impossible_deadline( vlc_tick_t deadline )
931 return deadline;
934 # define check_deadline( d ) \
935 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
936 #else
937 # define check_delay(d) (d)
938 # define check_deadline(d) (d)
939 #endif
941 #define vlc_tick_sleep(d) vlc_tick_sleep(check_delay(d))
942 #define vlc_tick_wait(d) vlc_tick_wait(check_deadline(d))
945 * Initializes an asynchronous timer.
947 * \param id pointer to timer to be initialized
948 * \param func function that the timer will call
949 * \param data parameter for the timer function
950 * \return 0 on success, a system error code otherwise.
952 * \warning Asynchronous timers are processed from an unspecified thread.
953 * \note Multiple occurrences of a single interval timer are serialized:
954 * they cannot run concurrently.
956 VLC_API int vlc_timer_create(vlc_timer_t *id, void (*func)(void *), void *data)
957 VLC_USED;
960 * Destroys an initialized timer.
962 * If needed, the timer is first disarmed. Behaviour is undefined if the
963 * specified timer is not initialized.
965 * \warning This function <b>must</b> be called before the timer data can be
966 * freed and before the timer callback function can be unmapped/unloaded.
968 * \param timer timer to destroy
970 VLC_API void vlc_timer_destroy(vlc_timer_t timer);
972 #define VLC_TIMER_DISARM (0)
973 #define VLC_TIMER_FIRE_ONCE (0)
976 * Arms or disarms an initialized timer.
978 * This functions overrides any previous call to itself.
980 * \note A timer can fire later than requested due to system scheduling
981 * limitations. An interval timer can fail to trigger sometimes, either because
982 * the system is busy or suspended, or because a previous iteration of the
983 * timer is still running. See also vlc_timer_getoverrun().
985 * \param timer initialized timer
986 * \param absolute the timer value origin is the same as vlc_tick_now() if true,
987 * the timer value is relative to now if false.
988 * \param value zero to disarm the timer, otherwise the initial time to wait
989 * before firing the timer.
990 * \param interval zero to fire the timer just once, otherwise the timer
991 * repetition interval.
993 VLC_API void vlc_timer_schedule(vlc_timer_t timer, bool absolute,
994 vlc_tick_t value, vlc_tick_t interval);
996 static inline void vlc_timer_disarm(vlc_timer_t timer)
998 vlc_timer_schedule( timer, false, VLC_TIMER_DISARM, 0 );
1001 static inline void vlc_timer_schedule_asap(vlc_timer_t timer, vlc_tick_t interval)
1003 vlc_timer_schedule(timer, false, 1, interval);
1007 * Fetches and resets the overrun counter for a timer.
1009 * This functions returns the number of times that the interval timer should
1010 * have fired, but the callback was not invoked due to scheduling problems.
1011 * The call resets the counter to zero.
1013 * \param timer initialized timer
1014 * \return the timer overrun counter (typically zero)
1016 VLC_API unsigned vlc_timer_getoverrun(vlc_timer_t) VLC_USED;
1019 * Count CPUs.
1021 * \return number of available (logical) CPUs.
1023 VLC_API unsigned vlc_GetCPUCount(void);
1025 enum
1027 VLC_CLEANUP_PUSH,
1028 VLC_CLEANUP_POP,
1029 VLC_CANCEL_ADDR_SET,
1030 VLC_CANCEL_ADDR_CLEAR,
1033 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
1035 * Registers a thread cancellation handler.
1037 * This pushes a function to run if the thread is cancelled (or otherwise
1038 * exits prematurely).
1040 * If multiple procedures are registered,
1041 * they are handled in last-in first-out order.
1043 * \note Any call to vlc_cleanup_push() <b>must</b> paired with a call to
1044 * vlc_cleanup_pop().
1045 * \warning Branching into or out of the block between these two function calls
1046 * is not allowed (read: it will likely crash the whole process).
1048 * \param routine procedure to call if the thread ends
1049 * \param arg argument for the procedure
1051 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
1054 * Unregisters the last cancellation handler.
1056 * This pops the cancellation handler that was last pushed with
1057 * vlc_cleanup_push() in the calling thread.
1059 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
1061 #else
1062 typedef struct vlc_cleanup_t vlc_cleanup_t;
1064 struct vlc_cleanup_t
1066 vlc_cleanup_t *next;
1067 void (*proc) (void *);
1068 void *data;
1071 # ifndef __cplusplus
1072 /* This macros opens a code block on purpose: It reduces the chance of
1073 * not pairing the push and pop. It also matches the POSIX Thread internals.
1074 * That way, Win32 developers will not accidentally break other platforms.
1076 # define vlc_cleanup_push( routine, arg ) \
1077 do { \
1078 vlc_control_cancel(VLC_CLEANUP_PUSH, \
1079 &(vlc_cleanup_t){ NULL, routine, arg })
1081 # define vlc_cleanup_pop( ) \
1082 vlc_control_cancel (VLC_CLEANUP_POP); \
1083 } while (0)
1084 # else
1085 /* Those macros do not work in C++. However common C/C++ helpers may call them
1086 * anyway - this is fine if the code is never cancelled in C++ case.
1087 * So define the macros to do nothing.
1089 # define vlc_cleanup_push(routine, arg) do { (routine, arg)
1090 # define vlc_cleanup_pop() } while (0)
1091 # endif
1093 #endif /* !LIBVLC_USE_PTHREAD_CLEANUP */
1095 static inline void vlc_cleanup_lock (void *lock)
1097 vlc_mutex_unlock ((vlc_mutex_t *)lock);
1099 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
1101 static inline void vlc_cancel_addr_set(void *addr)
1103 vlc_control_cancel(VLC_CANCEL_ADDR_SET, addr);
1106 static inline void vlc_cancel_addr_clear(void *addr)
1108 vlc_control_cancel(VLC_CANCEL_ADDR_CLEAR, addr);
1111 #ifdef __cplusplus
1113 * Helper C++ class to lock a mutex.
1115 * The mutex is locked when the object is created, and unlocked when the object
1116 * is destroyed.
1118 class vlc_mutex_locker
1120 private:
1121 vlc_mutex_t *lock;
1122 public:
1123 vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
1125 vlc_mutex_lock (lock);
1128 ~vlc_mutex_locker (void)
1130 vlc_mutex_unlock (lock);
1134 #endif
1136 enum
1138 VLC_AVCODEC_MUTEX = 0,
1139 VLC_GCRYPT_MUTEX,
1140 VLC_XLIB_MUTEX,
1141 VLC_MOSAIC_MUTEX,
1142 #ifdef _WIN32
1143 VLC_MTA_MUTEX,
1144 #endif
1145 /* Insert new entry HERE */
1146 VLC_MAX_MUTEX
1150 * Internal handler for global mutexes.
1152 * Do not use this function directly. Use helper macros instead:
1153 * vlc_global_lock(), vlc_global_unlock().
1155 VLC_API void vlc_global_mutex(unsigned, bool);
1158 * Acquires a global mutex.
1160 #define vlc_global_lock( n ) vlc_global_mutex(n, true)
1163 * Releases a global mutex.
1165 #define vlc_global_unlock( n ) vlc_global_mutex(n, false)
1167 /** @} */
1169 #endif /* !_VLC_THREADS_H */