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 63 #define LIBVLC_NEED_RWLOCK 67 # define VLC_THREAD_PRIORITY_LOW 0 68 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL 69 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST 70 # define VLC_THREAD_PRIORITY_VIDEO 0 71 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL 72 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL 74 static inline int vlc_poll(
struct pollfd *fds,
unsigned nfds,
int timeout)
79 val =
poll(fds, nfds, timeout);
84 # define poll(u,n,t) vlc_poll(u, n, t) 86 #elif defined (__OS2__) 90 #define VLC_THREAD_CANCELED NULL 92 #define LIBVLC_NEED_RWLOCK 96 # define VLC_THREAD_PRIORITY_LOW 0 97 # define VLC_THREAD_PRIORITY_INPUT \ 98 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR) 99 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR) 100 # define VLC_THREAD_PRIORITY_VIDEO 0 101 # define VLC_THREAD_PRIORITY_OUTPUT \ 102 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR) 103 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL) 105 # define pthread_sigmask sigprocmask 107 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
109 static int (*vlc_poll_os2)(
struct pollfd *, unsigned, int) = NULL;
114 CHAR szFailed[CCHMAXPATH];
116 if (DosLoadModule(szFailed,
sizeof(szFailed),
"vlccore", &hmod))
119 if (DosQueryProcAddr(hmod, 0,
"_vlc_poll_os2", (PFN *)&vlc_poll_os2))
123 return (*vlc_poll_os2)(fds, nfds, timeout);
125 # define poll(u,n,t) vlc_poll(u, n, t) 127 #elif defined (__ANDROID__) 129 # include <pthread.h> 131 # define LIBVLC_USE_PTHREAD_CLEANUP 1 132 # define LIBVLC_NEED_SLEEP 133 # define LIBVLC_NEED_RWLOCK 136 #define VLC_THREAD_CANCELED NULL 140 # define VLC_THREAD_PRIORITY_LOW 0 141 # define VLC_THREAD_PRIORITY_INPUT 0 142 # define VLC_THREAD_PRIORITY_AUDIO 0 143 # define VLC_THREAD_PRIORITY_VIDEO 0 144 # define VLC_THREAD_PRIORITY_OUTPUT 0 145 # define VLC_THREAD_PRIORITY_HIGHEST 0 147 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
153 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
155 timeout -= ugly_timeout;
158 val =
poll (fds, nfds, ugly_timeout);
160 while (val == 0 && timeout != 0);
165 # define poll(u,n,t) vlc_poll(u, n, t) 167 #elif defined (__APPLE__) 168 # define _APPLE_C_SOURCE 1 170 # include <pthread.h> 172 # include <mach/semaphore.h> 173 # include <mach/task.h> 174 # define LIBVLC_USE_PTHREAD_CLEANUP 1 176 typedef pthread_t vlc_thread_t;
177 #define VLC_THREAD_CANCELED PTHREAD_CANCELED 179 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER 183 # define VLC_THREAD_PRIORITY_LOW 0 184 # define VLC_THREAD_PRIORITY_INPUT 22 185 # define VLC_THREAD_PRIORITY_AUDIO 22 186 # define VLC_THREAD_PRIORITY_VIDEO 0 187 # define VLC_THREAD_PRIORITY_OUTPUT 22 188 # define VLC_THREAD_PRIORITY_HIGHEST 22 192 # include <pthread.h> 197 # define LIBVLC_USE_PTHREAD 1 202 # define LIBVLC_USE_PTHREAD_CLEANUP 1 215 #define VLC_THREAD_CANCELED PTHREAD_CANCELED 231 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER 247 # define VLC_THREAD_PRIORITY_LOW 0 248 # define VLC_THREAD_PRIORITY_INPUT 10 249 # define VLC_THREAD_PRIORITY_AUDIO 5 250 # define VLC_THREAD_PRIORITY_VIDEO 0 251 # define VLC_THREAD_PRIORITY_OUTPUT 15 252 # define VLC_THREAD_PRIORITY_HIGHEST 20 271 atomic_uint recursion;
272 _Atomic (
const void *) owner;
277 unsigned int recursion;
289 #define VLC_STATIC_MUTEX { \ 290 .value = ATOMIC_VAR_INIT(0), \ 291 .recursion = ATOMIC_VAR_INIT(0), \ 292 .owner = ATOMIC_VAR_INIT(NULL), \ 368 #define vlc_mutex_assert(m) assert(vlc_mutex_held(m)) 398 #define VLC_STATIC_COND { NULL, VLC_STATIC_MUTEX } 563 #ifdef LIBVLC_NEED_RWLOCK 564 typedef struct vlc_rwlock
570 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 } 626 #define VLC_STATIC_ONCE { ATOMIC_VAR_INIT(0) } 649 if (
unlikely(atomic_load_explicit(&once->value, memory_order_acquire) < 3))
652 #define vlc_once(once, cb) vlc_once_inline(once, cb) 888 #define VLC_HARD_MIN_SLEEP VLC_TICK_FROM_MS(10) 889 #define VLC_SOFT_MIN_SLEEP VLC_TICK_FROM_SEC(9) 891 #if defined (__GNUC__) && !defined (__clang__) 898 __attribute__((unused))
899 __attribute__((noinline))
900 __attribute__((error(
"sorry, cannot sleep for such short a time")))
908 __attribute__((unused))
909 __attribute__((noinline))
910 __attribute__((warning(
"use proper event handling instead of short 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)) \ 926 __attribute__((unused))
927 __attribute__((noinline))
928 __attribute__((error(
"deadlines can not be constant")))
934 # define check_deadline( d ) \ 935 (__builtin_constant_p(d) ? impossible_deadline(d) : d) 937 # define check_delay(d) (d) 938 # define check_deadline(d) (d) 941 #define vlc_tick_sleep(d) vlc_tick_sleep(check_delay(d)) 942 #define vlc_tick_wait(d) vlc_tick_wait(check_deadline(d)) 976 #define VLC_TIMER_DISARM (0) 977 #define VLC_TIMER_FIRE_ONCE (0) 1031 #if defined (LIBVLC_USE_PTHREAD_CLEANUP) 1049 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg) 1057 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0) 1063 void (*proc) (
void *);
1067 # ifndef __cplusplus 1072 # define vlc_cleanup_push( routine, arg ) \ 1074 vlc_control_cancel(&(vlc_cleanup_t){ NULL, routine, arg }) 1076 # define vlc_cleanup_pop( ) \ 1077 vlc_control_cancel (NULL); \ 1080 # define vlc_cleanup_push(routine, arg) \ 1081 static_assert(false, "don't use vlc_cleanup_push in portable C++ code") 1082 # define vlc_cleanup_pop() \ 1083 static_assert(false, "don't use vlc_cleanup_pop in portable C++ code") 1092 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock) 1104 class vlc_mutex_locker
1114 ~vlc_mutex_locker (
void)
1146 #define vlc_global_lock( n ) vlc_global_mutex(n, true) 1151 #define vlc_global_unlock( n ) vlc_global_mutex(n, false) Semaphore.
Definition: vlc_threads.h:498
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:700
void vlc_cancel(vlc_thread_t)
Marks a thread as cancelled.
Definition: thread.c:198
void vlc_rwlock_destroy(vlc_rwlock_t *)
Destroys an initialized unused read/write lock.
Definition: thread.c:124
vlc_mutex_t * mutex
Definition: threads.c:247
vlc_cond_t * cond
Definition: threads.c:246
static void vlc_timer_disarm(vlc_timer_t timer)
Definition: vlc_threads.h:1001
int vlc_savecancel(void)
Disables thread cancellation.
Definition: thread.c:214
int vlc_threadvar_set(vlc_threadvar_t key, void *value)
Sets a thread-specific variable.
Definition: thread.c:281
void vlc_threadvar_delete(vlc_threadvar_t *)
Deallocates a thread-specific variable.
Definition: thread.c:276
void vlc_control_cancel(vlc_cleanup_t *)
Internal handler for thread cancellation.
Definition: missing.c:281
unsigned vlc_GetCPUCount(void)
Count CPUs.
Definition: thread.c:304
static thread_local struct @77 state
Definition: vlc_threads.h:1126
#define vlc_tick_wait(d)
Definition: vlc_threads.h:943
void vlc_cancel_addr_clear(atomic_uint *addr)
Definition: thread.c:256
Definition: vlc_threads.h:1127
pthread_key_t vlc_threadvar_t
Thread-local key handle.
Definition: vlc_threads.h:239
vlc_tick_t value
Definition: timer.c:48
Definition: vlc_threads.h:1125
static void vlc_cleanup_lock(void *lock)
Definition: vlc_threads.h:1089
#define VLC_TIMER_DISARM
Definition: vlc_threads.h:977
void vlc_rwlock_rdlock(vlc_rwlock_t *)
Acquires a read/write lock for reading.
Definition: thread.c:130
void vlc_rwlock_wrlock(vlc_rwlock_t *)
Acquires a read/write lock for writing.
Definition: thread.c:136
void vlc_testcancel(void)
Issues an explicit deferred cancellation point.
Definition: thread.c:232
#define vlc_tick_sleep(d)
Definition: vlc_threads.h:942
pthread_rwlock_t vlc_rwlock_t
Read/write lock.
Definition: vlc_threads.h:225
void vlc_cancel_addr_set(atomic_uint *addr)
Definition: thread.c:244
vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition: thread.c:292
Definition: vlc_threads.h:1133
int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:167
void vlc_rwlock_unlock(vlc_rwlock_t *)
Releases a read/write lock.
Definition: thread.c:142
void vlc_atomic_notify_one(void *addr)
Wakes up one thread on an address.
Definition: thread.c:70
int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t)
Definition: threads.c:382
void(* func)(void *)
Definition: thread.c:642
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
void vlc_mutex_unlock(vlc_mutex_t *)
Releases a mutex.
Definition: threads.c:212
int vlc_atomic_timedwait_daytime(void *addr, unsigned val, time_t deadline)
Definition: thread.c:115
ULONG interval
Definition: thread.c:640
int vlc_threadvar_create(vlc_threadvar_t *key, void(*destr)(void *))
Allocates a thread-specific variable.
Definition: thread.c:271
HANDLE handle
Definition: timer.c:32
void vlc_atomic_notify_all(void *addr)
Wakes up all thread on an address.
Definition: thread.c:75
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:223
size_t count
Definition: core.c:402
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:367
Mutex.
Definition: vlc_threads.h:266
void vlc_rwlock_init(vlc_rwlock_t *)
Initializes a read/write lock.
Definition: thread.c:118
void vlc_cond_broadcast(vlc_cond_t *)
Wakes up all threads waiting on a condition variable.
Definition: threads.c:285
Definition: vlc_threads.h:1128
int vlc_sem_post(vlc_sem_t *)
Increments the value of a semaphore.
Definition: threads.c:484
Definition: vlc_fixups.h:409
static void vlc_timer_schedule_asap(vlc_timer_t timer, vlc_tick_t interval)
Definition: vlc_threads.h:1006
int vlc_sem_timedwait(vlc_sem_t *sem, vlc_tick_t deadline)
Waits on a semaphore within a deadline.
Definition: threads.c:516
bool vlc_mutex_held(const vlc_mutex_t *)
Checks if a mutex is locked.
Definition: threads.c:136
#define VLC_API
Definition: fourcc_gen.c:31
Condition variable.
Definition: vlc_threads.h:390
int vlc_timer_create(vlc_timer_t *id, void(*func)(void *), void *data)
Initializes an asynchronous timer.
Definition: thread.c:667
Definition: threads.c:243
#define vlc_once(once, cb)
Definition: vlc_threads.h:653
struct vlc_timer * vlc_timer_t
Threaded timer handle.
Definition: vlc_threads.h:246
void vlc_sem_wait(vlc_sem_t *)
Waits on a semaphore.
Definition: threads.c:500
void vlc_cond_signal(vlc_cond_t *)
Wakes up one thread waiting on a condition variable.
Definition: threads.c:258
int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t deadline)
Waits on an address with a time-out.
Definition: thread.c:87
#define VLC_HARD_MIN_SLEEP
Definition: vlc_threads.h:889
unsigned vlc_timer_getoverrun(vlc_timer_t)
Fetches and resets the overrun counter for a timer.
Definition: thread.c:723
void * vlc_threadvar_get(vlc_threadvar_t)
Gets the value of a thread-local variable for the calling thread.
Definition: thread.c:286
void vlc_mutex_init_recursive(vlc_mutex_t *)
Initializes a recursive mutex.
Definition: threads.c:128
void vlc_once_inline(vlc_once_t *restrict once, void(*cb)(void))
Executes a function one time.
Definition: vlc_threads.h:647
void vlc_restorecancel(int state)
Restores the cancellation state.
Definition: thread.c:224
void vlc_atomic_wait(void *addr, unsigned val)
Waits on an address.
Definition: thread.c:80
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:824
void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
Waits on a condition variable.
Definition: threads.c:356
int poll(struct pollfd *, unsigned, int)
Definition: fourcc_gen.c:51
void vlc_timer_destroy(vlc_timer_t timer)
Destroys an initialized timer.
Definition: thread.c:687
int vlc_mutex_trylock(vlc_mutex_t *)
Tries to acquire a mutex.
Definition: threads.c:181
vlc_mutex_t lock
Definition: timer.c:45
One-time initialization.
Definition: vlc_threads.h:619
void vlc_sem_init(vlc_sem_t *, unsigned count)
Initializes a semaphore.
Definition: threads.c:479
unsigned long vlc_thread_id(void)
Thread identifier.
Definition: thread.c:235
void * data
Definition: thread.c:643
void vlc_cond_init(vlc_cond_t *)
Initializes a condition variable.
Definition: threads.c:237
#define VLC_USED
Definition: fourcc_gen.c:32
void vlc_mutex_init(vlc_mutex_t *)
Initializes a fast mutex.
Definition: threads.c:123
void vlc_mutex_lock(vlc_mutex_t *)
Acquires a mutex.
Definition: threads.c:158
void vlc_join(vlc_thread_t th, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:174