Go to the documentation of this file.
28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
32 #include <stdatomic.h>
56 # define ETIMEDOUT 10060
60 # define VLC_THREAD_CANCELED NULL
62 # define LIBVLC_NEED_SLEEP
66 # define VLC_THREAD_PRIORITY_LOW 0
67 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
68 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
69 # define VLC_THREAD_PRIORITY_VIDEO 0
70 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
71 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
73 static inline int vlc_poll(
struct pollfd *fds,
unsigned nfds,
int timeout)
78 val =
poll(fds, nfds, timeout);
83 # define poll(u,n,t) vlc_poll(u, n, t)
85 #elif defined (__OS2__)
89 #define VLC_THREAD_CANCELED NULL
94 # define VLC_THREAD_PRIORITY_LOW 0
95 # define VLC_THREAD_PRIORITY_INPUT 1
96 # define VLC_THREAD_PRIORITY_AUDIO VLC_THREAD_PRIORITY_HIGHEST
97 # define VLC_THREAD_PRIORITY_VIDEO 0
98 # define VLC_THREAD_PRIORITY_OUTPUT 1
99 # define VLC_THREAD_PRIORITY_HIGHEST 2
101 # define pthread_sigmask sigprocmask
103 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
105 static int (*vlc_poll_os2)(
struct pollfd *, unsigned, int) = NULL;
110 CHAR szFailed[CCHMAXPATH];
112 if (DosLoadModule(szFailed,
sizeof(szFailed),
"vlccore", &hmod))
115 if (DosQueryProcAddr(hmod, 0,
"_vlc_poll_os2", (PFN *)&vlc_poll_os2))
119 return (*vlc_poll_os2)(fds, nfds, timeout);
121 # define poll(u,n,t) vlc_poll(u, n, t)
123 #elif defined (__ANDROID__)
125 # include <pthread.h>
127 # define LIBVLC_USE_PTHREAD_CLEANUP 1
128 # define LIBVLC_NEED_SLEEP
131 #define VLC_THREAD_CANCELED NULL
135 # define VLC_THREAD_PRIORITY_LOW 0
136 # define VLC_THREAD_PRIORITY_INPUT 0
137 # define VLC_THREAD_PRIORITY_AUDIO 0
138 # define VLC_THREAD_PRIORITY_VIDEO 0
139 # define VLC_THREAD_PRIORITY_OUTPUT 0
140 # define VLC_THREAD_PRIORITY_HIGHEST 0
142 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
148 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
150 timeout -= ugly_timeout;
153 val =
poll (fds, nfds, ugly_timeout);
155 while (val == 0 && timeout != 0);
160 # define poll(u,n,t) vlc_poll(u, n, t)
164 # include <pthread.h>
169 # define LIBVLC_USE_PTHREAD 1
174 # define LIBVLC_USE_PTHREAD_CLEANUP 1
187 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
206 # define VLC_THREAD_PRIORITY_LOW 0
207 # define VLC_THREAD_PRIORITY_INPUT 0
208 # define VLC_THREAD_PRIORITY_AUDIO 0
209 # define VLC_THREAD_PRIORITY_VIDEO 0
210 # define VLC_THREAD_PRIORITY_OUTPUT 0
211 # define VLC_THREAD_PRIORITY_HIGHEST 0
230 atomic_uint recursion;
231 _Atomic (
const void *) owner;
236 unsigned int recursion;
248 #define VLC_STATIC_MUTEX { \
249 .value = ATOMIC_VAR_INIT(0), \
250 .recursion = ATOMIC_VAR_INIT(0), \
251 .owner = ATOMIC_VAR_INIT(NULL), \
327 #define vlc_mutex_assert(m) assert(vlc_mutex_held(m))
357 #define VLC_STATIC_COND { NULL, VLC_STATIC_MUTEX }
541 #define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
586 #define VLC_STATIC_ONCE { ATOMIC_VAR_INIT(0) }
609 if (
unlikely(atomic_load_explicit(&once->value, memory_order_acquire) < 3))
612 #define vlc_once(once, cb) vlc_once_inline(once, cb)
848 #define VLC_HARD_MIN_SLEEP VLC_TICK_FROM_MS(10)
849 #define VLC_SOFT_MIN_SLEEP VLC_TICK_FROM_SEC(9)
851 #if defined (__GNUC__) && !defined (__clang__)
858 __attribute__((unused))
859 __attribute__((noinline))
860 __attribute__((error(
"sorry, cannot sleep for such short a time")))
868 __attribute__((unused))
869 __attribute__((noinline))
870 __attribute__((warning(
"use proper event handling instead of short delay")))
876 # define check_delay( d ) \
877 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
878 && (d < VLC_HARD_MIN_SLEEP)) \
879 ? impossible_delay(d) \
880 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
881 && (d < VLC_SOFT_MIN_SLEEP)) \
886 __attribute__((unused))
887 __attribute__((noinline))
888 __attribute__((error(
"deadlines can not be constant")))
894 # define check_deadline( d ) \
895 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
897 # define check_delay(d) (d)
898 # define check_deadline(d) (d)
901 #define vlc_tick_sleep(d) vlc_tick_sleep(check_delay(d))
902 #define vlc_tick_wait(d) vlc_tick_wait(check_deadline(d))
936 #define VLC_TIMER_DISARM (0)
937 #define VLC_TIMER_FIRE_ONCE (0)
991 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
1009 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
1017 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
1023 void (*proc) (
void *);
1027 # ifndef __cplusplus
1032 # define vlc_cleanup_push( routine, arg ) \
1034 vlc_control_cancel(&(vlc_cleanup_t){ NULL, routine, arg })
1036 # define vlc_cleanup_pop( ) \
1037 vlc_control_cancel (NULL); \
1040 # define vlc_cleanup_push(routine, arg) \
1041 static_assert(false, "don't use vlc_cleanup_push in portable C++ code")
1042 # define vlc_cleanup_pop() \
1043 static_assert(false, "don't use vlc_cleanup_pop in portable C++ code")
1052 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
1064 class vlc_mutex_locker
1074 ~vlc_mutex_locker (
void)
1106 #define vlc_global_lock( n ) vlc_global_mutex(n, true)
1111 #define vlc_global_unlock( n ) vlc_global_mutex(n, false)
static void vlc_cleanup_lock(void *lock)
Definition: vlc_threads.h:1049
vlc_tick_t value
Definition: timer.c:66
VLC_EXPORT void vlc_cond_broadcast(vlc_cond_t *)
Wakes up all threads waiting on a condition variable.
Definition: threads.c:280
VLC_EXPORT void vlc_mutex_init(vlc_mutex_t *)
Initializes a fast mutex.
Definition: threads.c:123
size_t count
Definition: core.c:401
#define VLC_API
Definition: fourcc_gen.c:31
VLC_EXPORT void vlc_rwlock_rdlock(vlc_rwlock_t *)
Acquires a read/write lock for reading.
Definition: threads.c:394
const void * owner
Definition: vlc_threads.h:238
VLC_EXPORT void vlc_global_mutex(unsigned, bool)
Internal handler for global mutexes.
Definition: threads.c:43
struct vlc_cleanup_t vlc_cleanup_t
Definition: vlc_threads.h:784
VLC_EXPORT unsigned long vlc_thread_id(void)
Thread identifier.
Definition: thread.c:36
VLC_EXPORT void vlc_sem_init(vlc_sem_t *, unsigned count)
Initializes a semaphore.
Definition: threads.c:442
atomic_uint value
Definition: vlc_threads.h:581
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:227
int vlc_atomic_timedwait_daytime(void *addr, unsigned val, time_t deadline)
Definition: thread.c:105
pthread_key_t vlc_threadvar_t
Thread-local key handle.
Definition: vlc_threads.h:195
Read/write lock.
Definition: vlc_threads.h:532
@ VLC_MOSAIC_MUTEX
Definition: vlc_threads.h:1088
int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t deadline)
Waits on an address with a time-out.
Definition: thread.c:84
Definition: vlc_fixups.h:417
VLC_EXPORT void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
Waits on a condition variable.
Definition: threads.c:340
VLC_EXPORT unsigned vlc_timer_getoverrun(vlc_timer_t)
Fetches and resets the overrun counter for a timer.
Definition: thread.c:913
static void vlc_timer_disarm(vlc_timer_t timer)
Definition: vlc_threads.h:961
#define vlc_tick_sleep(d)
Definition: vlc_threads.h:902
int poll(struct pollfd *, unsigned, int)
unsigned int recursion
Definition: vlc_threads.h:237
atomic_uint value
Definition: vlc_threads.h:453
#define VLC_HARD_MIN_SLEEP
Definition: vlc_threads.h:849
HANDLE handle
Definition: timer.c:50
VLC_EXPORT int vlc_threadvar_set(vlc_threadvar_t key, void *value)
Sets a thread-specific variable.
Definition: thread.c:250
#define VLC_TIMER_DISARM
Definition: vlc_threads.h:937
VLC_EXPORT bool vlc_mutex_held(const vlc_mutex_t *)
Checks if a mutex is locked.
Definition: threads.c:136
struct vlc_rwlock vlc_rwlock_t
Read/write lock.
void vlc_cancel_addr_clear(atomic_uint *addr)
Definition: thread.c:225
VLC_EXPORT void vlc_cond_signal(vlc_cond_t *)
Wakes up one thread waiting on a condition variable.
Definition: threads.c:253
@ VLC_GCRYPT_MUTEX
Definition: vlc_threads.h:1086
Mutex.
Definition: vlc_threads.h:225
VLC_EXPORT void vlc_timer_schedule(vlc_timer_t timer, bool absolute, vlc_tick_t value, vlc_tick_t interval)
Arms or disarms an initialized timer.
Definition: thread.c:890
VLC_EXPORT int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:144
VLC_EXPORT void vlc_mutex_init_recursive(vlc_mutex_t *)
Initializes a recursive mutex.
Definition: threads.c:128
VLC_EXPORT int vlc_savecancel(void)
Disables thread cancellation.
Definition: thread.c:183
VLC_EXPORT vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition: thread.c:261
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
vlc_mutex_t mutex
Definition: vlc_threads.h:534
vlc_cond_t wait
Definition: vlc_threads.h:535
void vlc_atomic_notify_all(void *addr)
Wakes up all thread on an address.
Definition: thread.c:72
vlc_mutex_t lock
Definition: rand.c:50
#define vlc_once(once, cb)
Definition: vlc_threads.h:613
void vlc_atomic_notify_one(void *addr)
Wakes up one thread on an address.
Definition: thread.c:67
VLC_EXPORT int vlc_threadvar_create(vlc_threadvar_t *key, void(*destr)(void *))
Allocates a thread-specific variable.
Definition: thread.c:240
void vlc_cancel_addr_set(atomic_uint *addr)
Definition: thread.c:213
Thread handle.
Definition: vlc_threads.h:180
@ VLC_AVCODEC_MUTEX
Definition: vlc_threads.h:1085
#define vlc_tick_wait(d)
Definition: vlc_threads.h:903
VLC_EXPORT void vlc_once_inline(vlc_once_t *restrict once, void(*cb)(void))
Executes a function one time.
Definition: vlc_threads.h:607
atomic_uint recursion
Definition: vlc_threads.h:231
VLC_EXPORT void vlc_rwlock_unlock(vlc_rwlock_t *)
Releases a read/write lock.
Definition: threads.c:420
VLC_EXPORT void * vlc_threadvar_get(vlc_threadvar_t)
Gets the value of a thread-local variable for the calling thread.
Definition: thread.c:255
VLC_EXPORT void vlc_restorecancel(int state)
Restores the cancellation state.
Definition: thread.c:193
int dummy
Definition: vlc_threads.h:455
VLC_EXPORT void vlc_sem_wait(vlc_sem_t *)
Waits on a semaphore.
Definition: threads.c:463
@ VLC_XLIB_MUTEX
Definition: vlc_threads.h:1087
vlc_mutex_t lock
Definition: vlc_threads.h:352
Semaphore.
Definition: vlc_threads.h:449
struct vlc_timer * vlc_timer_t
Threaded timer handle.
Definition: vlc_threads.h:202
atomic_uint value
Definition: threads.c:242
#define VLC_USED
Definition: fourcc_gen.c:32
VLC_EXPORT int vlc_sem_trywait(vlc_sem_t *sem)
Tries to decrement a semaphore.
Definition: threads.c:500
unsigned int value
Definition: vlc_threads.h:236
VLC_EXPORT void vlc_mutex_lock(vlc_mutex_t *)
Acquires a mutex.
Definition: threads.c:158
long state
Definition: vlc_threads.h:536
VLC_EXPORT void vlc_timer_destroy(vlc_timer_t timer)
Destroys an initialized timer.
Definition: thread.c:877
static void vlc_timer_schedule_asap(vlc_timer_t timer, vlc_tick_t interval)
Definition: vlc_threads.h:966
Definition: fourcc_gen.c:51
Condition variable.
Definition: vlc_threads.h:349
VLC_EXPORT void vlc_control_cancel(vlc_cleanup_t *)
Internal handler for thread cancellation.
Definition: missing.c:280
VLC_EXPORT int vlc_sem_post(vlc_sem_t *)
Increments the value of a semaphore.
Definition: threads.c:447
void vlc_atomic_wait(void *addr, unsigned val)
Waits on an address.
Definition: thread.c:77
One-time initialization.
Definition: vlc_threads.h:579
VLC_EXPORT void vlc_threadvar_delete(vlc_threadvar_t *)
Deallocates a thread-specific variable.
Definition: thread.c:245
VLC_EXPORT void vlc_rwlock_wrlock(vlc_rwlock_t *)
Acquires a read/write lock for writing.
Definition: threads.c:410
struct vlc_cond_waiter * head
Definition: vlc_threads.h:351
VLC_EXPORT int vlc_sem_timedwait(vlc_sem_t *sem, vlc_tick_t deadline)
Waits on a semaphore within a deadline.
Definition: threads.c:479
VLC_EXPORT int vlc_mutex_trylock(vlc_mutex_t *)
Tries to acquire a mutex.
Definition: threads.c:178
VLC_EXPORT void vlc_rwlock_init(vlc_rwlock_t *)
Initializes a read/write lock.
Definition: threads.c:387
pthread_t handle
Definition: vlc_threads.h:182
VLC_EXPORT void vlc_cond_init(vlc_cond_t *)
Initializes a condition variable.
Definition: threads.c:234
VLC_EXPORT int vlc_cond_timedwait(vlc_cond_t *cond, vlc_mutex_t *mutex, vlc_tick_t deadline)
Waits on a condition variable up to a certain date.
Definition: threads.c:349
static thread_local struct @76 state
VLC_EXPORT void vlc_join(vlc_thread_t th, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:151
atomic_uint value
Definition: vlc_threads.h:230
VLC_EXPORT void vlc_mutex_unlock(vlc_mutex_t *)
Releases a mutex.
Definition: threads.c:209
int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t)
Definition: threads.c:362
Definition: threads.c:240
VLC_EXPORT int vlc_timer_create(vlc_timer_t *id, void(*func)(void *), void *data)
Initializes an asynchronous timer.
Definition: thread.c:857
VLC_EXPORT void vlc_testcancel(void)
Issues an explicit deferred cancellation point.
Definition: thread.c:201
VLC_EXPORT unsigned vlc_GetCPUCount(void)
Count CPUs.
Definition: thread.c:273
VLC_EXPORT void vlc_cancel(vlc_thread_t)
Marks a thread as cancelled.
Definition: thread.c:167
@ VLC_MAX_MUTEX
Definition: vlc_threads.h:1093