28 #ifndef VLC_THREADS_H_ 29 #define VLC_THREADS_H_ 52 # define ETIMEDOUT 10060 56 # define VLC_THREAD_CANCELED NULL 57 # define LIBVLC_NEED_SLEEP 66 unsigned long contention;
68 CRITICAL_SECTION mutex;
71 #define VLC_STATIC_MUTEX { false, { { false, 0 } } } 72 #define LIBVLC_NEED_CONDVAR 73 #define LIBVLC_NEED_SEMAPHORE 74 #define LIBVLC_NEED_RWLOCK 76 #define VLC_STATIC_ONCE INIT_ONCE_STATIC_INIT 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)
92 val =
poll(fds, nfds, timeout);
97 # define poll(u,n,t) vlc_poll(u, n, t) 99 #elif defined (__OS2__) 103 #define VLC_THREAD_CANCELED NULL 112 unsigned long contention;
117 #define VLC_STATIC_MUTEX { false, { { false, 0 } } } 125 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 } 126 #define LIBVLC_NEED_SEMAPHORE 127 #define LIBVLC_NEED_RWLOCK 133 #define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX } 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;
155 CHAR szFailed[CCHMAXPATH];
157 if (DosLoadModule(szFailed,
sizeof(szFailed),
"vlccore", &hmod))
160 if (DosQueryProcAddr(hmod, 0,
"_vlc_poll_os2", (PFN *)&vlc_poll_os2))
164 return (*vlc_poll_os2)(fds, nfds, timeout);
166 # define poll(u,n,t) vlc_poll(u, n, t) 168 #elif defined (__ANDROID__) 170 # include <pthread.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 179 #define VLC_THREAD_CANCELED NULL 181 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER 183 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT 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)
200 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
202 timeout -= ugly_timeout;
205 val =
poll (fds, nfds, ugly_timeout);
207 while (val == 0 && timeout != 0);
212 # define poll(u,n,t) vlc_poll(u, n, t) 214 #elif defined (__APPLE__) 215 # define _APPLE_C_SOURCE 1 217 # include <pthread.h> 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 226 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER 228 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER 231 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER 233 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT 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 246 # include <pthread.h> 247 # include <semaphore.h> 252 # define LIBVLC_USE_PTHREAD 1 257 # define LIBVLC_USE_PTHREAD_CLEANUP 1 270 #define VLC_THREAD_CANCELED PTHREAD_CANCELED 282 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER 300 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER 319 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER 332 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT 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 353 #ifdef LIBVLC_NEED_CONDVAR 358 # define VLC_STATIC_COND { 0 } 361 #ifdef LIBVLC_NEED_SEMAPHORE 362 typedef struct vlc_sem
370 #ifdef LIBVLC_NEED_RWLOCK 371 typedef struct vlc_rwlock
377 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 } 461 #define vlc_mutex_assert(m) assert(vlc_mutex_marked(m)) 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)) 972 #define VLC_TIMER_DISARM (0) 973 #define VLC_TIMER_FIRE_ONCE (0) 1033 #if defined (LIBVLC_USE_PTHREAD_CLEANUP) 1051 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg) 1059 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0) 1062 typedef struct vlc_cleanup_t vlc_cleanup_t;
1064 struct vlc_cleanup_t
1066 vlc_cleanup_t *next;
1067 void (*proc) (
void *);
1071 # ifndef __cplusplus 1076 # define vlc_cleanup_push( routine, arg ) \ 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); \ 1089 # define vlc_cleanup_push(routine, arg) do { (routine, arg) 1090 # define vlc_cleanup_pop() } while (0) 1099 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock) 1118 class vlc_mutex_locker
1123 vlc_mutex_locker (vlc_mutex_t *m) :
lock (m)
1128 ~vlc_mutex_locker (
void)
1160 #define vlc_global_lock( n ) vlc_global_mutex(n, true) 1165 #define vlc_global_unlock( n ) vlc_global_mutex(n, false) int vlc_threadvar_set(vlc_threadvar_t key, void *value)
Sets a thread-specific variable.
Definition: thread.c:398
bool vlc_mutex_marked(const vlc_mutex_t *)
Checks if a mutex is locked.
Definition: threads.c:132
pthread_once_t vlc_once_t
One-time initialization.
Definition: vlc_threads.h:328
void vlc_cancel(vlc_thread_t)
Marks a thread as cancelled.
Definition: thread.c:300
void vlc_cond_signal(vlc_cond_t *)
Wakes up one thread waiting on a condition variable.
Definition: thread.c:219
static void vlc_cancel_addr_set(void *addr)
Definition: vlc_threads.h:1102
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: thread.c:236
int vlc_savecancel(void)
Disables thread cancellation.
Definition: thread.c:316
void vlc_sem_destroy(vlc_sem_t *)
Deinitializes a semaphore.
Definition: thread.c:294
unsigned vlc_GetCPUCount(void)
Count CPUs.
Definition: thread.c:421
pthread_mutex_t vlc_mutex_t
Mutex.
Definition: vlc_threads.h:278
static thread_local struct @77 state
#define vlc_tick_wait(d)
Definition: vlc_threads.h:943
vlc_tick_t value
Definition: timer.c:49
static void vlc_cleanup_lock(void *lock)
Definition: vlc_threads.h:1096
void vlc_mutex_init_recursive(vlc_mutex_t *)
Initializes a recursive mutex.
Definition: thread.c:99
vlc_mutex_t lock
Definition: rand.c:32
Definition: vlc_threads.h:1029
void vlc_timer_destroy(vlc_timer_t timer)
Destroys an initialized timer.
Definition: thread.c:998
void vlc_mutex_unlock(vlc_mutex_t *)
Releases a mutex.
Definition: thread.c:134
void vlc_testcancel(void)
Issues an explicit deferred cancellation point.
Definition: thread.c:334
#define vlc_tick_sleep(d)
Definition: vlc_threads.h:942
vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition: thread.c:409
struct vlc_timer * vlc_timer_t
Threaded timer handle.
Definition: vlc_threads.h:343
bool vlc_addr_timedwait(void *addr, unsigned val, vlc_tick_t delay)
Waits on an address with a time-out.
Definition: thread.c:81
void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
Waits on a condition variable.
Definition: thread.c:230
int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:266
void vlc_sem_init(vlc_sem_t *, unsigned count)
Initializes a semaphore.
Definition: thread.c:288
Definition: vlc_threads.h:1140
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
unsigned vlc_timer_getoverrun(vlc_timer_t)
Fetches and resets the overrun counter for a timer.
Definition: thread.c:1034
void vlc_rwlock_destroy(vlc_rwlock_t *)
Destroys an initialized unused read/write lock.
Definition: thread.c:338
void vlc_cond_broadcast(vlc_cond_t *)
Wakes up all threads waiting on a condition variable.
Definition: thread.c:225
pthread_key_t vlc_threadvar_t
Thread-local key handle.
Definition: vlc_threads.h:338
Definition: vlc_threads.h:1028
HANDLE handle
Definition: timer.c:32
void vlc_cond_init(vlc_cond_t *)
Initializes a condition variable.
Definition: thread.c:179
static void vlc_timer_schedule_asap(vlc_timer_t timer, vlc_tick_t interval)
Definition: vlc_threads.h:1002
int vlc_sem_post(vlc_sem_t *)
Increments the value of a semaphore.
Definition: thread.c:306
void vlc_rwlock_init(vlc_rwlock_t *)
Initializes a read/write lock.
Definition: thread.c:332
void vlc_cond_init_daytime(vlc_cond_t *)
Initializes a condition variable (wall clock).
Definition: thread.c:185
size_t count
Definition: core.c:402
int vlc_mutex_trylock(vlc_mutex_t *)
Tries to acquire a mutex.
Definition: thread.c:123
pthread_cond_t vlc_cond_t
Condition variable.
Definition: vlc_threads.h:290
Definition: vlc_fixups.h:409
int vlc_timer_create(vlc_timer_t *id, void(*func)(void *), void *data)
Initializes an asynchronous timer.
Definition: thread.c:978
void vlc_addr_signal(void *addr)
Wakes up one thread on an address.
Definition: thread.c:66
Definition: vlc_threads.h:1030
static void vlc_cancel_addr_clear(void *addr)
Definition: vlc_threads.h:1107
void vlc_once(vlc_once_t *restrict once, void(*cb)(void))
Executes a function one time.
#define VLC_API
Definition: fourcc_gen.c:31
Definition: vlc_threads.h:1141
sem_t vlc_sem_t
Semaphore.
Definition: vlc_threads.h:308
void vlc_rwlock_wrlock(vlc_rwlock_t *)
Acquires a read/write lock for writing.
Definition: thread.c:350
Definition: vlc_threads.h:1142
Definition: vlc_threads.h:1139
void vlc_cond_destroy(vlc_cond_t *)
Deinitializes a condition variable.
Definition: thread.c:191
int vlc_threadvar_create(vlc_threadvar_t *key, void(*destr)(void *))
Allocates a thread-specific variable.
Definition: thread.c:388
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:1011
void vlc_addr_wait(void *addr, unsigned val)
Waits on an address.
Definition: thread.c:76
#define VLC_HARD_MIN_SLEEP
Definition: vlc_threads.h:889
void vlc_mutex_lock(vlc_mutex_t *)
Acquires a mutex.
Definition: thread.c:116
pthread_rwlock_t vlc_rwlock_t
Read/write lock.
Definition: vlc_threads.h:315
void vlc_sem_wait(vlc_sem_t *)
Waits on a semaphore.
Definition: thread.c:320
void vlc_restorecancel(int state)
Restores the cancellation state.
Definition: thread.c:326
void vlc_global_mutex(unsigned, bool)
Internal handler for global mutexes.
Definition: threads.c:33
void vlc_mutex_init(vlc_mutex_t *)
Initializes a fast mutex.
Definition: thread.c:85
vlc_thread_t vlc_thread_self(void)
Thread handle.
Definition: thread.c:167
Definition: vlc_threads.h:1147
static void vlc_timer_disarm(vlc_timer_t timer)
Definition: vlc_threads.h:997
void vlc_rwlock_rdlock(vlc_rwlock_t *)
Acquires a read/write lock for reading.
Definition: thread.c:344
int poll(struct pollfd *, unsigned, int)
#define VLC_TIMER_DISARM
Definition: vlc_threads.h:973
Definition: fourcc_gen.c:51
int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t)
Definition: thread.c:260
void vlc_rwlock_unlock(vlc_rwlock_t *)
Releases a read/write lock.
Definition: thread.c:356
unsigned long vlc_thread_id(void)
Thread identifier.
Definition: thread.c:460
void vlc_addr_broadcast(void *addr)
Wakes up all thread on an address.
Definition: thread.c:71
void vlc_control_cancel(int cmd,...)
Internal handler for thread cancellation.
Definition: thread.c:346
Definition: vlc_threads.h:1031
void * vlc_threadvar_get(vlc_threadvar_t)
Gets the value of a thread-local variable for the calling thread.
Definition: thread.c:403
#define VLC_USED
Definition: fourcc_gen.c:32
void vlc_mutex_destroy(vlc_mutex_t *)
Deinitializes a mutex.
Definition: thread.c:110
void vlc_join(vlc_thread_t th, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:273
void vlc_threadvar_delete(vlc_threadvar_t *)
Deallocates a thread-specific variable.
Definition: thread.c:393