1 /*****************************************************************************
2 * vlc_threads.h : threads implementation for the VideoLAN client
3 * This header provides portable declarations for mutexes & conditions
4 *****************************************************************************
5 * Copyright (C) 1999, 2002 VLC authors and VideoLAN
6 * Copyright © 2007-2008 Rémi Denis-Courmont
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
33 * This file defines structures and functions for handling threads in vlc
38 # include <process.h> /* Win32 API */
40 #elif defined( __OS2__ ) /* OS/2 API */
43 # define pthread_sigmask sigprocmask
45 #elif defined( __ANDROID__ ) /* pthreads subset without pthread_cancel() */
47 # define LIBVLC_NEED_SEMAPHORE
48 # define LIBVLC_NEED_RWLOCK
50 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
54 #else /* pthreads (like Linux & BSD) */
55 # define LIBVLC_USE_PTHREAD 1
56 # define LIBVLC_USE_PTHREAD_CANCEL 1
57 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
59 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
62 /* Unnamed POSIX semaphores not supported on Mac OS X, use Mach semaphores instead */
63 # if defined (__APPLE__)
64 # include <mach/semaphore.h>
65 # include <mach/task.h>
67 # include <semaphore.h>
72 /*****************************************************************************
74 *****************************************************************************/
76 /* Thread priorities */
78 # define VLC_THREAD_PRIORITY_LOW 0
79 # define VLC_THREAD_PRIORITY_INPUT 22
80 # define VLC_THREAD_PRIORITY_AUDIO 22
81 # define VLC_THREAD_PRIORITY_VIDEO 0
82 # define VLC_THREAD_PRIORITY_OUTPUT 22
83 # define VLC_THREAD_PRIORITY_HIGHEST 22
85 #elif defined(LIBVLC_USE_PTHREAD) || defined(__ANDROID__)
86 # define VLC_THREAD_PRIORITY_LOW 0
87 # define VLC_THREAD_PRIORITY_INPUT 10
88 # define VLC_THREAD_PRIORITY_AUDIO 5
89 # define VLC_THREAD_PRIORITY_VIDEO 0
90 # define VLC_THREAD_PRIORITY_OUTPUT 15
91 # define VLC_THREAD_PRIORITY_HIGHEST 20
94 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
95 # define VLC_THREAD_PRIORITY_LOW 0
96 # define VLC_THREAD_PRIORITY_INPUT \
97 THREAD_PRIORITY_ABOVE_NORMAL
98 # define VLC_THREAD_PRIORITY_AUDIO \
99 THREAD_PRIORITY_HIGHEST
100 # define VLC_THREAD_PRIORITY_VIDEO 0
101 # define VLC_THREAD_PRIORITY_OUTPUT \
102 THREAD_PRIORITY_ABOVE_NORMAL
103 # define VLC_THREAD_PRIORITY_HIGHEST \
104 THREAD_PRIORITY_TIME_CRITICAL
106 #elif defined(__OS2__)
107 # define VLC_THREAD_PRIORITY_LOW 0
108 # define VLC_THREAD_PRIORITY_INPUT MAKESHORT( PRTYD_MAXIMUM / 2, PRTYC_REGULAR )
109 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT( PRTYD_MAXIMUM, PRTYC_REGULAR )
110 # define VLC_THREAD_PRIORITY_VIDEO 0
111 # define VLC_THREAD_PRIORITY_OUTPUT MAKESHORT( PRTYD_MAXIMUM / 2, PRTYC_REGULAR )
112 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT( 0, PRTYC_TIMECRITICAL )
115 # define VLC_THREAD_PRIORITY_LOW 0
116 # define VLC_THREAD_PRIORITY_INPUT 0
117 # define VLC_THREAD_PRIORITY_AUDIO 0
118 # define VLC_THREAD_PRIORITY_VIDEO 0
119 # define VLC_THREAD_PRIORITY_OUTPUT 0
120 # define VLC_THREAD_PRIORITY_HIGHEST 0
124 /*****************************************************************************
126 *****************************************************************************/
128 #if defined (__ANDROID__)
129 typedef struct vlc_thread *vlc_thread_t;
130 typedef pthread_mutex_t vlc_mutex_t;
131 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
132 typedef pthread_cond_t vlc_cond_t;
133 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
135 typedef pthread_key_t vlc_threadvar_t;
136 typedef struct vlc_timer *vlc_timer_t;
138 #elif defined (LIBVLC_USE_PTHREAD)
139 typedef pthread_t vlc_thread_t;
140 typedef pthread_mutex_t vlc_mutex_t;
141 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
142 typedef pthread_cond_t vlc_cond_t;
143 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
144 typedef pthread_rwlock_t vlc_rwlock_t;
145 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
146 typedef pthread_key_t vlc_threadvar_t;
147 typedef struct vlc_timer *vlc_timer_t;
149 #if defined (__APPLE__)
150 typedef semaphore_t vlc_sem_t;
152 typedef sem_t vlc_sem_t;
155 #elif defined( WIN32 )
156 typedef struct vlc_thread *vlc_thread_t;
166 unsigned long contention;
168 CRITICAL_SECTION mutex;
171 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
178 #define VLC_STATIC_COND { 0, 0 }
180 typedef HANDLE vlc_sem_t;
181 #define LIBVLC_NEED_RWLOCK
182 typedef struct vlc_threadvar *vlc_threadvar_t;
183 typedef struct vlc_timer *vlc_timer_t;
185 #elif defined( __OS2__ )
186 typedef struct vlc_thread *vlc_thread_t;
196 unsigned long contention;
201 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
208 #define VLC_STATIC_COND { 0, 0 }
210 #define LIBVLC_NEED_SEMAPHORE
211 #define LIBVLC_NEED_RWLOCK
212 typedef struct vlc_threadvar *vlc_threadvar_t;
213 typedef struct vlc_timer *vlc_timer_t;
217 #ifdef LIBVLC_NEED_SEMAPHORE
218 typedef struct vlc_sem
226 #ifdef LIBVLC_NEED_RWLOCK
227 typedef struct vlc_rwlock
233 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
236 #if defined( WIN32 ) && !defined ETIMEDOUT
237 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
240 /*****************************************************************************
241 * Function definitions
242 *****************************************************************************/
243 VLC_API void vlc_mutex_init( vlc_mutex_t * );
244 VLC_API void vlc_mutex_init_recursive( vlc_mutex_t * );
245 VLC_API void vlc_mutex_destroy( vlc_mutex_t * );
246 VLC_API void vlc_mutex_lock( vlc_mutex_t * );
247 VLC_API int vlc_mutex_trylock( vlc_mutex_t * ) VLC_USED;
248 VLC_API void vlc_mutex_unlock( vlc_mutex_t * );
249 VLC_API void vlc_cond_init( vlc_cond_t * );
250 VLC_API void vlc_cond_init_daytime( vlc_cond_t * );
251 VLC_API void vlc_cond_destroy( vlc_cond_t * );
252 VLC_API void vlc_cond_signal(vlc_cond_t *);
253 VLC_API void vlc_cond_broadcast(vlc_cond_t *);
254 VLC_API void vlc_cond_wait(vlc_cond_t *, vlc_mutex_t *);
255 VLC_API int vlc_cond_timedwait(vlc_cond_t *, vlc_mutex_t *, mtime_t);
256 VLC_API void vlc_sem_init(vlc_sem_t *, unsigned);
257 VLC_API void vlc_sem_destroy(vlc_sem_t *);
258 VLC_API int vlc_sem_post(vlc_sem_t *);
259 VLC_API void vlc_sem_wait(vlc_sem_t *);
261 VLC_API void vlc_rwlock_init(vlc_rwlock_t *);
262 VLC_API void vlc_rwlock_destroy(vlc_rwlock_t *);
263 VLC_API void vlc_rwlock_rdlock(vlc_rwlock_t *);
264 VLC_API void vlc_rwlock_wrlock(vlc_rwlock_t *);
265 VLC_API void vlc_rwlock_unlock(vlc_rwlock_t *);
266 VLC_API int vlc_threadvar_create(vlc_threadvar_t * , void (*) (void *) );
267 VLC_API void vlc_threadvar_delete(vlc_threadvar_t *);
268 VLC_API int vlc_threadvar_set(vlc_threadvar_t, void *);
269 VLC_API void * vlc_threadvar_get(vlc_threadvar_t);
271 VLC_API int vlc_clone(vlc_thread_t *, void * (*) (void *), void *, int) VLC_USED;
272 VLC_API void vlc_cancel(vlc_thread_t);
273 VLC_API void vlc_join(vlc_thread_t, void **);
274 VLC_API void vlc_control_cancel (int cmd, ...);
276 VLC_API mtime_t mdate(void);
277 VLC_API void mwait(mtime_t deadline);
278 VLC_API void msleep(mtime_t delay);
280 #define VLC_HARD_MIN_SLEEP 10000 /* 10 milliseconds = 1 tick at 100Hz */
281 #define VLC_SOFT_MIN_SLEEP 9000000 /* 9 seconds */
283 #if VLC_GCC_VERSION(4,3)
284 /* Linux has 100, 250, 300 or 1000Hz
286 * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
290 __attribute__((unused))
291 __attribute__((noinline))
292 __attribute__((error("sorry, cannot sleep for such short a time")))
293 mtime_t impossible_delay( mtime_t delay )
296 return VLC_HARD_MIN_SLEEP;
300 __attribute__((unused))
301 __attribute__((noinline))
302 __attribute__((warning("use proper event handling instead of short delay")))
303 mtime_t harmful_delay( mtime_t delay )
308 # define check_delay( d ) \
309 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
310 && (d < VLC_HARD_MIN_SLEEP)) \
311 ? impossible_delay(d) \
312 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
313 && (d < VLC_SOFT_MIN_SLEEP)) \
318 __attribute__((unused))
319 __attribute__((noinline))
320 __attribute__((error("deadlines can not be constant")))
321 mtime_t impossible_deadline( mtime_t deadline )
326 # define check_deadline( d ) \
327 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
329 # define check_delay(d) (d)
330 # define check_deadline(d) (d)
333 #define msleep(d) msleep(check_delay(d))
334 #define mwait(d) mwait(check_deadline(d))
336 VLC_API int vlc_timer_create(vlc_timer_t *, void (*) (void *), void *) VLC_USED;
337 VLC_API void vlc_timer_destroy(vlc_timer_t);
338 VLC_API void vlc_timer_schedule(vlc_timer_t, bool, mtime_t, mtime_t);
339 VLC_API unsigned vlc_timer_getoverrun(vlc_timer_t) VLC_USED;
341 VLC_API unsigned vlc_GetCPUCount(void);
343 #ifndef LIBVLC_USE_PTHREAD_CANCEL
350 VLC_API int vlc_savecancel(void);
351 VLC_API void vlc_restorecancel(int state);
352 VLC_API void vlc_testcancel(void);
354 #if defined (LIBVLC_USE_PTHREAD_CANCEL) || defined(__ANDROID__)
356 * Registers a new procedure to run if the thread is cancelled (or otherwise
357 * exits prematurely). Any call to vlc_cleanup_push() <b>must</b> paired with a
358 * call to either vlc_cleanup_pop() or vlc_cleanup_run(). Branching into or out
359 * of the block between these two function calls is not allowed (read: it will
360 * likely crash the whole process). If multiple procedures are registered,
361 * they are handled in last-in first-out order.
363 * @param routine procedure to call if the thread ends
364 * @param arg argument for the procedure
366 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
369 * Removes a cleanup procedure that was previously registered with
370 * vlc_cleanup_push().
372 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
375 * Removes a cleanup procedure that was previously registered with
376 * vlc_cleanup_push(), and executes it.
378 # define vlc_cleanup_run( ) pthread_cleanup_pop (1)
380 typedef struct vlc_cleanup_t vlc_cleanup_t;
385 void (*proc) (void *);
389 /* This macros opens a code block on purpose. This is needed for multiple
390 * calls within a single function. This also prevent Win32 developers from
391 * writing code that would break on POSIX (POSIX opens a block as well). */
392 # define vlc_cleanup_push( routine, arg ) \
394 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
395 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
397 # define vlc_cleanup_pop( ) \
398 vlc_control_cancel (VLC_CLEANUP_POP); \
401 # define vlc_cleanup_run( ) \
402 vlc_control_cancel (VLC_CLEANUP_POP); \
403 vlc_cleanup_data.proc (vlc_cleanup_data.data); \
405 #endif /* LIBVLC_USE_PTHREAD_CANCEL || __ANDROID__ */
407 #if !defined (LIBVLC_USE_PTHREAD_CANCEL)
408 /* poll() with cancellation */
409 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
415 int val = poll (fds, nfds, timeout);
422 return poll (fds, nfds, timeout);
424 # define poll(u,n,t) vlc_poll(u, n, t)
426 #endif /* LIBVLC_USE_PTHREAD_CANCEL */
428 static inline void vlc_cleanup_lock (void *lock)
430 vlc_mutex_unlock ((vlc_mutex_t *)lock);
432 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
434 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
435 typedef pthread_spinlock_t vlc_spinlock_t;
438 * Initializes a spinlock.
440 static inline void vlc_spin_init (vlc_spinlock_t *spin)
442 if (pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE))
447 * Acquires a spinlock.
449 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
451 pthread_spin_lock (spin);
455 * Releases a spinlock.
457 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
459 pthread_spin_unlock (spin);
463 * Deinitializes a spinlock.
465 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
467 pthread_spin_destroy (spin);
470 #elif defined (WIN32)
472 typedef CRITICAL_SECTION vlc_spinlock_t;
475 * Initializes a spinlock.
477 static inline void vlc_spin_init (vlc_spinlock_t *spin)
479 if (!InitializeCriticalSectionAndSpinCount(spin, 4000))
484 * Acquires a spinlock.
486 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
488 EnterCriticalSection(spin);
492 * Releases a spinlock.
494 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
496 LeaveCriticalSection(spin);
500 * Deinitializes a spinlock.
502 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
504 DeleteCriticalSection(spin);
509 /* Fallback to plain mutexes if spinlocks are not available */
510 typedef vlc_mutex_t vlc_spinlock_t;
512 static inline void vlc_spin_init (vlc_spinlock_t *spin)
514 vlc_mutex_init (spin);
517 # define vlc_spin_lock vlc_mutex_lock
518 # define vlc_spin_unlock vlc_mutex_unlock
519 # define vlc_spin_destroy vlc_mutex_destroy
523 * Issues a full memory barrier.
525 #if defined (__APPLE__)
526 # include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
528 static inline void barrier (void)
530 #if defined (__GNUC__) && !defined (__APPLE__) && \
531 ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
532 __sync_synchronize ();
533 #elif defined(__APPLE__)
535 #elif defined(__powerpc__)
536 asm volatile ("sync":::"memory");
537 #elif 0 // defined(__i386__) /* Requires SSE2 support */
538 asm volatile ("mfence":::"memory");
541 vlc_spin_init (&spin);
542 vlc_spin_lock (&spin);
543 vlc_spin_unlock (&spin);
544 vlc_spin_destroy (&spin);
550 * Helper C++ class to lock a mutex.
551 * The mutex is locked when the object is created, and unlocked when the object
554 class vlc_mutex_locker
559 vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
561 vlc_mutex_lock (lock);
564 ~vlc_mutex_locker (void)
566 vlc_mutex_unlock (lock);
572 VLC_AVCODEC_MUTEX = 0,
578 /* Insert new entry HERE */
582 VLC_API void vlc_global_mutex( unsigned, bool );
583 #define vlc_global_lock( n ) vlc_global_mutex( n, true )
584 #define vlc_global_unlock( n ) vlc_global_mutex( n, false )
586 #endif /* !_VLC_THREADS_H */