Go to the documentation of this file.
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
78 # define VLC_THREAD_PRIORITY_LOW 0
79 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
80 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
81 # define VLC_THREAD_PRIORITY_VIDEO 0
82 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
83 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
85 static inline int vlc_poll(
struct pollfd *fds,
unsigned nfds,
int timeout)
90 val =
poll(fds, nfds, timeout);
95 # define poll(u,n,t) vlc_poll(u, n, t)
97 #elif defined (__OS2__)
101 #define VLC_THREAD_CANCELED NULL
110 unsigned long contention;
115 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
123 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
124 #define LIBVLC_NEED_SEMAPHORE
125 #define LIBVLC_NEED_RWLOCK
129 # define VLC_THREAD_PRIORITY_LOW 0
130 # define VLC_THREAD_PRIORITY_INPUT \
131 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
132 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
133 # define VLC_THREAD_PRIORITY_VIDEO 0
134 # define VLC_THREAD_PRIORITY_OUTPUT \
135 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
136 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
138 # define pthread_sigmask sigprocmask
140 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
142 static int (*vlc_poll_os2)(
struct pollfd *, unsigned, int) = NULL;
147 CHAR szFailed[CCHMAXPATH];
149 if (DosLoadModule(szFailed,
sizeof(szFailed),
"vlccore", &hmod))
152 if (DosQueryProcAddr(hmod, 0,
"_vlc_poll_os2", (PFN *)&vlc_poll_os2))
156 return (*vlc_poll_os2)(fds, nfds, timeout);
158 # define poll(u,n,t) vlc_poll(u, n, t)
160 #elif defined (__ANDROID__)
162 # include <pthread.h>
164 # define LIBVLC_USE_PTHREAD_CLEANUP 1
165 # define LIBVLC_NEED_SLEEP
166 # define LIBVLC_NEED_CONDVAR
167 # define LIBVLC_NEED_SEMAPHORE
168 # define LIBVLC_NEED_RWLOCK
171 #define VLC_THREAD_CANCELED NULL
173 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
178 # define VLC_THREAD_PRIORITY_LOW 0
179 # define VLC_THREAD_PRIORITY_INPUT 0
180 # define VLC_THREAD_PRIORITY_AUDIO 0
181 # define VLC_THREAD_PRIORITY_VIDEO 0
182 # define VLC_THREAD_PRIORITY_OUTPUT 0
183 # define VLC_THREAD_PRIORITY_HIGHEST 0
185 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
191 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
193 timeout -= ugly_timeout;
196 val =
poll (fds, nfds, ugly_timeout);
198 while (val == 0 && timeout != 0);
203 # define poll(u,n,t) vlc_poll(u, n, t)
205 #elif defined (__APPLE__)
206 # define _APPLE_C_SOURCE 1
208 # include <pthread.h>
210 # include <mach/semaphore.h>
211 # include <mach/task.h>
212 # define LIBVLC_USE_PTHREAD 1
213 # define LIBVLC_USE_PTHREAD_CLEANUP 1
216 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
218 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
220 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
223 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
227 # define VLC_THREAD_PRIORITY_LOW 0
228 # define VLC_THREAD_PRIORITY_INPUT 22
229 # define VLC_THREAD_PRIORITY_AUDIO 22
230 # define VLC_THREAD_PRIORITY_VIDEO 0
231 # define VLC_THREAD_PRIORITY_OUTPUT 22
232 # define VLC_THREAD_PRIORITY_HIGHEST 22
236 # include <pthread.h>
237 # include <semaphore.h>
242 # define LIBVLC_USE_PTHREAD 1
247 # define LIBVLC_USE_PTHREAD_CLEANUP 1
260 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
272 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
290 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
309 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
321 # define VLC_THREAD_PRIORITY_LOW 0
322 # define VLC_THREAD_PRIORITY_INPUT 10
323 # define VLC_THREAD_PRIORITY_AUDIO 5
324 # define VLC_THREAD_PRIORITY_VIDEO 0
325 # define VLC_THREAD_PRIORITY_OUTPUT 15
326 # define VLC_THREAD_PRIORITY_HIGHEST 20
330 #ifdef LIBVLC_NEED_CONDVAR
335 # define VLC_STATIC_COND { 0 }
338 #ifdef LIBVLC_NEED_SEMAPHORE
339 typedef struct vlc_sem
347 #ifdef LIBVLC_NEED_RWLOCK
348 typedef struct vlc_rwlock
354 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
825 #define VLC_HARD_MIN_SLEEP 10000
826 #define VLC_SOFT_MIN_SLEEP 9000000
828 #if defined (__GNUC__) && !defined (__clang__)
835 __attribute__((unused))
836 __attribute__((noinline))
837 __attribute__((error(
"sorry, cannot sleep for such short a time")))
845 __attribute__((unused))
846 __attribute__((noinline))
847 __attribute__((warning(
"use proper event handling instead of short delay")))
853 # define check_delay( d ) \
854 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
855 && (d < VLC_HARD_MIN_SLEEP)) \
856 ? impossible_delay(d) \
857 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
858 && (d < VLC_SOFT_MIN_SLEEP)) \
863 __attribute__((unused))
864 __attribute__((noinline))
865 __attribute__((error(
"deadlines can not be constant")))
871 # define check_deadline( d ) \
872 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
874 # define check_delay(d) (d)
875 # define check_deadline(d) (d)
878 #define msleep(d) msleep(check_delay(d))
879 #define mwait(d) mwait(check_deadline(d))
957 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
975 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
983 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
986 typedef struct vlc_cleanup_t vlc_cleanup_t;
991 void (*proc) (
void *);
998 # define vlc_cleanup_push( routine, arg ) \
1000 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
1001 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
1003 # define vlc_cleanup_pop( ) \
1004 vlc_control_cancel (VLC_CLEANUP_POP); \
1013 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
1032 class vlc_mutex_locker
1042 ~vlc_mutex_locker (
void)
1074 #define vlc_global_lock( n ) vlc_global_mutex(n, true)
1079 #define vlc_global_unlock( n ) vlc_global_mutex(n, false)
static void vlc_cleanup_lock(void *lock)
Definition: vlc_threads.h:1009
vlc_tick_t value
Definition: timer.c:65
size_t count
Definition: core.c:461
#define VLC_API
Definition: fourcc_gen.c:30
void vlc_sem_init(vlc_sem_t *, unsigned count)
Initializes a semaphore.
Definition: thread.c:325
int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t)
Definition: thread.c:297
void vlc_restorecancel(int state)
Restores the cancellation state.
Definition: thread.c:323
Definition: vlc_threads.h:1054
int vlc_mutex_trylock(vlc_mutex_t *)
Tries to acquire a mutex.
Definition: thread.c:129
vlc_tick_t mdate(void)
Precision monotonic clock.
Definition: thread.c:406
void vlc_sem_destroy(vlc_sem_t *)
Deinitializes a semaphore.
Definition: thread.c:331
Definition: vlc_threads.h:952
void vlc_threadvar_delete(vlc_threadvar_t *)
Deallocates a thread-specific variable.
Definition: thread.c:390
void vlc_mutex_init_recursive(vlc_mutex_t *)
Initializes a recursive mutex.
Definition: thread.c:99
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_common.h:150
pthread_key_t vlc_threadvar_t
Thread-local key handle.
Definition: vlc_threads.h:314
Definition: vlc_fixups.h:414
int vlc_sem_post(vlc_sem_t *)
Increments the value of a semaphore.
Definition: thread.c:343
unsigned vlc_timer_getoverrun(vlc_timer_t)
Fetches and resets the overrun counter for a timer.
Definition: thread.c:997
Definition: vlc_threads.h:1052
#define msleep(d)
Definition: vlc_threads.h:878
void vlc_timer_destroy(vlc_timer_t timer)
Destroys an initialized timer.
Definition: thread.c:961
int vlc_threadvar_set(vlc_threadvar_t key, void *value)
Sets a thread-specific variable.
Definition: thread.c:395
int poll(struct pollfd *, unsigned, int)
void vlc_cond_broadcast(vlc_cond_t *)
Wakes up all threads waiting on a condition variable.
Definition: thread.c:262
vlc_thread_t vlc_thread_self(void)
Thread handle.
Definition: thread.c:164
bool vlc_addr_timedwait(void *addr, unsigned val, vlc_tick_t delay)
Waits on an address with a time-out.
Definition: thread.c:101
int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:263
unsigned vlc_GetCPUCount(void)
Count CPUs.
Definition: thread.c:418
#define VLC_HARD_MIN_SLEEP
Definition: vlc_threads.h:825
HANDLE handle
Definition: timer.c:48
void vlc_cond_signal(vlc_cond_t *)
Wakes up one thread waiting on a condition variable.
Definition: thread.c:256
pthread_cond_t vlc_cond_t
Condition variable.
Definition: vlc_threads.h:279
void vlc_rwlock_unlock(vlc_rwlock_t *)
Releases a read/write lock.
Definition: thread.c:393
Definition: vlc_threads.h:953
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:273
unsigned long vlc_thread_id(void)
Thread identifier.
Definition: thread.c:491
Definition: vlc_threads.h:951
Definition: vlc_threads.h:954
Definition: vlc_threads.h:1051
static void vlc_cancel_addr_clear(void *addr)
Definition: vlc_threads.h:1020
static vlc_mutex_t lock
Definition: interface.c:62
void vlc_rwlock_wrlock(vlc_rwlock_t *)
Acquires a read/write lock for writing.
Definition: thread.c:387
void vlc_testcancel(void)
Issues an explicit deferred cancellation point.
Definition: thread.c:331
int vlc_threadvar_create(vlc_threadvar_t *key, void(*destr)(void *))
Allocates a thread-specific variable.
Definition: thread.c:385
void vlc_rwlock_init(vlc_rwlock_t *)
Initializes a read/write lock.
Definition: thread.c:369
void vlc_cond_init_daytime(vlc_cond_t *)
Initializes a condition variable (wall clock).
Definition: thread.c:222
void vlc_sem_wait(vlc_sem_t *)
Waits on a semaphore.
Definition: thread.c:357
Thread handle.
Definition: vlc_threads.h:252
void vlc_mutex_init(vlc_mutex_t *)
Initializes a fast mutex.
Definition: thread.c:85
void vlc_rwlock_destroy(vlc_rwlock_t *)
Destroys an initialized unused read/write lock.
Definition: thread.c:375
int vlc_savecancel(void)
Disables thread cancellation.
Definition: thread.c:313
Definition: vlc_threads.h:1053
pthread_mutex_t vlc_mutex_t
Mutex.
Definition: vlc_threads.h:267
#define VLC_USED
Definition: fourcc_gen.c:31
void vlc_cond_destroy(vlc_cond_t *)
Deinitializes a condition variable.
Definition: thread.c:228
void vlc_addr_broadcast(void *addr)
Wakes up all thread on an address.
Definition: thread.c:91
void vlc_cond_init(vlc_cond_t *)
Initializes a condition variable.
Definition: thread.c:216
Definition: fourcc_gen.c:50
void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
Waits on a condition variable.
Definition: thread.c:267
pthread_rwlock_t vlc_rwlock_t
Read/write lock.
Definition: vlc_threads.h:304
void * vlc_threadvar_get(vlc_threadvar_t)
Gets the value of a thread-local variable for the calling thread.
Definition: thread.c:400
void vlc_addr_signal(void *addr)
Wakes up one thread on an address.
Definition: thread.c:86
void vlc_mutex_destroy(vlc_mutex_t *)
Deinitializes a mutex.
Definition: thread.c:110
void vlc_mutex_unlock(vlc_mutex_t *)
Releases a mutex.
Definition: thread.c:138
Definition: vlc_threads.h:1060
struct vlc_timer * vlc_timer_t
Threaded timer handle.
Definition: vlc_threads.h:319
sem_t vlc_sem_t
Semaphore.
Definition: vlc_threads.h:297
void vlc_cancel(vlc_thread_t)
Marks a thread as cancelled.
Definition: thread.c:297
void vlc_rwlock_rdlock(vlc_rwlock_t *)
Acquires a read/write lock for reading.
Definition: thread.c:381
int vlc_timer_create(vlc_timer_t *id, void(*func)(void *), void *data)
Initializes an asynchronous timer.
Definition: thread.c:941
pthread_t handle
Definition: vlc_threads.h:254
#define mwait(d)
Definition: vlc_threads.h:879
static void vlc_cancel_addr_set(void *addr)
Definition: vlc_threads.h:1015
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:974
Definition: vlc_threads.h:1055
void vlc_addr_wait(void *addr, unsigned val)
Waits on an address.
Definition: thread.c:96
void vlc_control_cancel(int cmd,...)
Internal handler for thread cancellation.
Definition: thread.c:343
void vlc_mutex_lock(vlc_mutex_t *)
Acquires a mutex.
Definition: thread.c:123
void vlc_join(vlc_thread_t th, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:270
void vlc_global_mutex(unsigned, bool)
Internal handler for global mutexes.
Definition: threads.c:31