28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
40 # define ETIMEDOUT 10060
52 unsigned long contention;
54 CRITICAL_SECTION mutex;
57 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
63 #define VLC_STATIC_COND { 0, 0 }
65 #define LIBVLC_NEED_RWLOCK
69 # define VLC_THREAD_PRIORITY_LOW 0
70 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
71 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
72 # define VLC_THREAD_PRIORITY_VIDEO 0
73 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
74 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
76 #elif defined (__OS2__)
88 unsigned long contention;
93 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
99 #define VLC_STATIC_COND { 0, 0 }
100 #define LIBVLC_NEED_SEMAPHORE
101 #define LIBVLC_NEED_RWLOCK
105 # define VLC_THREAD_PRIORITY_LOW 0
106 # define VLC_THREAD_PRIORITY_INPUT \
107 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
108 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
109 # define VLC_THREAD_PRIORITY_VIDEO 0
110 # define VLC_THREAD_PRIORITY_OUTPUT \
111 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
112 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
114 # define pthread_sigmask sigprocmask
116 #elif defined (__ANDROID__)
118 # include <pthread.h>
120 # define LIBVLC_USE_PTHREAD_CLEANUP 1
121 # define LIBVLC_NEED_SEMAPHORE
122 # define LIBVLC_NEED_RWLOCK
126 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
132 #define VLC_STATIC_COND { PTHREAD_COND_INITIALIZER, CLOCK_REALTIME }
137 # define VLC_THREAD_PRIORITY_LOW 0
138 # define VLC_THREAD_PRIORITY_INPUT 0
139 # define VLC_THREAD_PRIORITY_AUDIO 0
140 # define VLC_THREAD_PRIORITY_VIDEO 0
141 # define VLC_THREAD_PRIORITY_OUTPUT 0
142 # define VLC_THREAD_PRIORITY_HIGHEST 0
144 #elif defined (__APPLE__)
145 # define _APPLE_C_SOURCE 1
147 # include <pthread.h>
149 # include <mach/semaphore.h>
150 # include <mach/task.h>
151 # define LIBVLC_USE_PTHREAD 1
152 # define LIBVLC_USE_PTHREAD_CLEANUP 1
153 # define LIBVLC_USE_PTHREAD_CANCEL 1
157 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
159 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
162 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
166 # define VLC_THREAD_PRIORITY_LOW 0
167 # define VLC_THREAD_PRIORITY_INPUT 22
168 # define VLC_THREAD_PRIORITY_AUDIO 22
169 # define VLC_THREAD_PRIORITY_VIDEO 0
170 # define VLC_THREAD_PRIORITY_OUTPUT 22
171 # define VLC_THREAD_PRIORITY_HIGHEST 22
175 # include <pthread.h>
176 # include <semaphore.h>
177 # define LIBVLC_USE_PTHREAD 1
178 # define LIBVLC_USE_PTHREAD_CLEANUP 1
179 # define LIBVLC_USE_PTHREAD_CANCEL 1
183 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
185 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
188 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
192 # define VLC_THREAD_PRIORITY_LOW 0
193 # define VLC_THREAD_PRIORITY_INPUT 10
194 # define VLC_THREAD_PRIORITY_AUDIO 5
195 # define VLC_THREAD_PRIORITY_VIDEO 0
196 # define VLC_THREAD_PRIORITY_OUTPUT 15
197 # define VLC_THREAD_PRIORITY_HIGHEST 20
201 #ifdef LIBVLC_NEED_SEMAPHORE
202 typedef struct vlc_sem
210 #ifdef LIBVLC_NEED_RWLOCK
211 typedef struct vlc_rwlock
217 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
260 #define VLC_HARD_MIN_SLEEP 10000
261 #define VLC_SOFT_MIN_SLEEP 9000000
263 #if VLC_GCC_VERSION(4,3)
270 __attribute__((unused))
271 __attribute__((noinline))
272 __attribute__((error("sorry, cannot sleep for such
short a time")))
273 mtime_t impossible_delay( mtime_t delay )
280 __attribute__((unused))
281 __attribute__((noinline))
282 __attribute__((warning("use proper event handling instead of
short delay")))
283 mtime_t harmful_delay( mtime_t delay )
288 # define check_delay( d ) \
289 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
290 && (d < VLC_HARD_MIN_SLEEP)) \
291 ? impossible_delay(d) \
292 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
293 && (d < VLC_SOFT_MIN_SLEEP)) \
298 __attribute__((unused))
299 __attribute__((noinline))
300 __attribute__((error("deadlines can not be constant")))
301 mtime_t impossible_deadline( mtime_t deadline )
306 # define check_deadline( d ) \
307 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
309 # define check_delay(d) (d)
310 # define check_deadline(d) (d)
313 #define msleep(d) msleep(check_delay(d))
314 #define mwait(d) mwait(check_deadline(d))
327 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
339 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
345 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
351 # define vlc_cleanup_run( ) pthread_cleanup_pop (1)
359 typedef struct vlc_cleanup_t vlc_cleanup_t;
364 void (*proc) (
void *);
371 # define vlc_cleanup_push( routine, arg ) \
373 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
374 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
376 # define vlc_cleanup_pop( ) \
377 vlc_control_cancel (VLC_CLEANUP_POP); \
380 # define vlc_cleanup_run( ) \
381 vlc_control_cancel (VLC_CLEANUP_POP); \
382 vlc_cleanup_data.proc (vlc_cleanup_data.data); \
387 #ifndef LIBVLC_USE_PTHREAD_CANCEL
392 static inline int vlc_poll (
struct pollfd *fds,
unsigned nfds,
int timeout)
398 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
400 timeout -= ugly_timeout;
403 val =
poll (fds, nfds, ugly_timeout);
405 while (val == 0 && timeout != 0);
411 # define poll(u,n,t) vlc_poll(u, n, t)
419 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
427 class vlc_mutex_locker
432 vlc_mutex_locker (vlc_mutex_t *m) :
lock (m)
437 ~vlc_mutex_locker (
void)
457 #define vlc_global_lock( n ) vlc_global_mutex( n, true )
458 #define vlc_global_unlock( n ) vlc_global_mutex( n, false )