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 the VideoLAN team
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
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 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 General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #if !defined( __LIBVLC__ )
29 #error You are not libvlc or one of its plugins. You cannot include this file
32 #ifndef _VLC_THREADS_H_
33 #define _VLC_THREADS_H_
35 #if defined( UNDER_CE )
37 #elif defined( WIN32 )
38 # include <process.h> /* Win32 API */
41 #elif defined( SYS_BEOS ) /* BeOS */
42 # include <kernel/OS.h>
43 # include <kernel/scheduler.h>
44 # include <byteorder.h>
46 #else /* pthreads (like Linux & BSD) */
47 # define LIBVLC_USE_PTHREAD 1
48 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
50 # include <stdlib.h> /* lldiv_t definition (only in C99) */
51 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
53 /* Needed for pthread_cond_timedwait */
59 /*****************************************************************************
61 *****************************************************************************/
63 /* Thread priorities */
65 # define VLC_THREAD_PRIORITY_LOW (-47)
66 # define VLC_THREAD_PRIORITY_INPUT 37
67 # define VLC_THREAD_PRIORITY_AUDIO 37
68 # define VLC_THREAD_PRIORITY_VIDEO (-47)
69 # define VLC_THREAD_PRIORITY_OUTPUT 37
70 # define VLC_THREAD_PRIORITY_HIGHEST 37
72 #elif defined(SYS_BEOS)
73 # define VLC_THREAD_PRIORITY_LOW 5
74 # define VLC_THREAD_PRIORITY_INPUT 10
75 # define VLC_THREAD_PRIORITY_AUDIO 10
76 # define VLC_THREAD_PRIORITY_VIDEO 5
77 # define VLC_THREAD_PRIORITY_OUTPUT 15
78 # define VLC_THREAD_PRIORITY_HIGHEST 15
80 #elif defined(LIBVLC_USE_PTHREAD)
81 # define VLC_THREAD_PRIORITY_LOW 0
82 # define VLC_THREAD_PRIORITY_INPUT 20
83 # define VLC_THREAD_PRIORITY_AUDIO 10
84 # define VLC_THREAD_PRIORITY_VIDEO 0
85 # define VLC_THREAD_PRIORITY_OUTPUT 30
86 # define VLC_THREAD_PRIORITY_HIGHEST 40
88 #elif defined(WIN32) || defined(UNDER_CE)
89 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
90 # define VLC_THREAD_PRIORITY_LOW 0
91 # define VLC_THREAD_PRIORITY_INPUT \
92 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
93 # define VLC_THREAD_PRIORITY_AUDIO \
94 (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
95 # define VLC_THREAD_PRIORITY_VIDEO \
96 (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
97 # define VLC_THREAD_PRIORITY_OUTPUT \
98 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
99 # define VLC_THREAD_PRIORITY_HIGHEST \
100 (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
103 # define VLC_THREAD_PRIORITY_LOW 0
104 # define VLC_THREAD_PRIORITY_INPUT 0
105 # define VLC_THREAD_PRIORITY_AUDIO 0
106 # define VLC_THREAD_PRIORITY_VIDEO 0
107 # define VLC_THREAD_PRIORITY_OUTPUT 0
108 # define VLC_THREAD_PRIORITY_HIGHEST 0
112 /*****************************************************************************
114 *****************************************************************************/
116 #if defined (LIBVLC_USE_PTHREAD)
117 typedef pthread_t vlc_thread_t;
118 typedef pthread_mutex_t vlc_mutex_t;
119 typedef pthread_cond_t vlc_cond_t;
120 typedef pthread_key_t vlc_threadvar_t;
122 #elif defined( WIN32 ) || defined( UNDER_CE )
123 typedef HANDLE vlc_thread_t;
125 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
127 typedef HANDLE vlc_mutex_t;
131 volatile int i_waiting_threads;
135 typedef DWORD vlc_threadvar_t;
137 #elif defined( SYS_BEOS )
138 /* This is the BeOS implementation of the vlc threads, note that the mutex is
139 * not a real mutex and the cond_var is not like a pthread cond_var but it is
140 * enough for what we need */
142 typedef thread_id vlc_thread_t;
162 #if defined( WIN32 ) && !defined ETIMEDOUT
163 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
166 /*****************************************************************************
167 * Function definitions
168 *****************************************************************************/
169 VLC_EXPORT( int, vlc_mutex_init, ( vlc_mutex_t * ) );
170 VLC_EXPORT( int, vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
171 VLC_EXPORT( void, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
172 VLC_EXPORT( int, __vlc_cond_init, ( vlc_cond_t * ) );
173 VLC_EXPORT( void, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
174 VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
175 VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
176 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, bool ) );
177 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
178 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
179 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
181 /*****************************************************************************
182 * vlc_mutex_lock: lock a mutex
183 *****************************************************************************/
184 #define vlc_mutex_lock( P_MUTEX ) \
185 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
187 #if defined(LIBVLC_USE_PTHREAD)
188 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
190 # define VLC_THREAD_ASSERT( action ) \
192 vlc_pthread_fatal (action, val, psz_file, i_line)
194 # define VLC_THREAD_ASSERT (void)0
197 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
198 vlc_mutex_t * p_mutex )
200 #if defined(LIBVLC_USE_PTHREAD)
201 # define vlc_assert_locked( m ) \
202 assert (pthread_mutex_lock (m) == EDEADLK)
203 int val = pthread_mutex_lock( p_mutex );
204 VLC_THREAD_ASSERT ("locking mutex");
206 #elif defined( UNDER_CE )
207 (void)psz_file; (void)i_line;
209 EnterCriticalSection( &p_mutex->csection );
211 #elif defined( WIN32 )
212 (void)psz_file; (void)i_line;
214 WaitForSingleObject( *p_mutex, INFINITE );
216 #elif defined( SYS_BEOS )
217 acquire_sem( p_mutex->lock );
222 #ifndef vlc_assert_locked
223 # define vlc_assert_locked( m ) (void)0
226 /*****************************************************************************
227 * vlc_mutex_unlock: unlock a mutex
228 *****************************************************************************/
229 #define vlc_mutex_unlock( P_MUTEX ) \
230 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
232 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
233 vlc_mutex_t *p_mutex )
235 #if defined(LIBVLC_USE_PTHREAD)
236 int val = pthread_mutex_unlock( p_mutex );
237 VLC_THREAD_ASSERT ("unlocking mutex");
239 #elif defined( UNDER_CE )
240 (void)psz_file; (void)i_line;
242 LeaveCriticalSection( &p_mutex->csection );
244 #elif defined( WIN32 )
245 (void)psz_file; (void)i_line;
247 ReleaseMutex( *p_mutex );
249 #elif defined( SYS_BEOS )
250 release_sem( p_mutex->lock );
255 /*****************************************************************************
256 * vlc_mutex_destroy: destroy a mutex
257 *****************************************************************************/
258 #define vlc_mutex_destroy( P_MUTEX ) \
259 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
261 /*****************************************************************************
262 * vlc_cond_init: initialize a condition
263 *****************************************************************************/
264 #define vlc_cond_init( P_THIS, P_COND ) \
265 __vlc_cond_init( P_COND )
267 /*****************************************************************************
268 * vlc_cond_signal: start a thread on condition completion
269 *****************************************************************************/
270 #define vlc_cond_signal( P_COND ) \
271 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
273 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
274 vlc_cond_t *p_condvar )
276 #if defined(LIBVLC_USE_PTHREAD)
277 int val = pthread_cond_signal( p_condvar );
278 VLC_THREAD_ASSERT ("signaling condition variable");
280 #elif defined( UNDER_CE ) || defined( WIN32 )
281 (void)psz_file; (void)i_line;
283 /* Release one waiting thread if one is available. */
284 /* For this trick to work properly, the vlc_cond_signal must be surrounded
285 * by a mutex. This will prevent another thread from stealing the signal */
286 /* PulseEvent() only works if none of the waiting threads is suspended.
287 * This is particularily problematic under a debug session.
288 * as documented in http://support.microsoft.com/kb/q173260/ */
289 PulseEvent( p_condvar->event );
291 #elif defined( SYS_BEOS )
292 while( p_condvar->thread != -1 )
295 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
298 if( info.state != B_THREAD_SUSPENDED )
300 /* The waiting thread is not suspended so it could
301 * have been interrupted beetwen the unlock and the
302 * suspend_thread line. That is why we sleep a little
303 * before retesting p_condver->thread. */
308 /* Ok, we have to wake up that thread */
309 resume_thread( p_condvar->thread );
316 /*****************************************************************************
317 * vlc_cond_wait: wait until condition completion
318 *****************************************************************************/
319 #define vlc_cond_wait( P_COND, P_MUTEX ) \
320 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
322 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
323 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
325 #if defined(LIBVLC_USE_PTHREAD)
326 int val = pthread_cond_wait( p_condvar, p_mutex );
327 VLC_THREAD_ASSERT ("waiting on condition");
329 #elif defined( UNDER_CE )
330 p_condvar->i_waiting_threads++;
331 LeaveCriticalSection( &p_mutex->csection );
332 WaitForSingleObject( p_condvar->event, INFINITE );
333 p_condvar->i_waiting_threads--;
335 /* Reacquire the mutex before returning. */
336 vlc_mutex_lock( p_mutex );
338 #elif defined( WIN32 )
339 (void)psz_file; (void)i_line;
341 /* Increase our wait count */
342 p_condvar->i_waiting_threads++;
343 SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
344 p_condvar->i_waiting_threads--;
346 /* Reacquire the mutex before returning. */
347 vlc_mutex_lock( p_mutex );
349 #elif defined( SYS_BEOS )
350 /* The p_condvar->thread var is initialized before the unlock because
351 * it enables to identify when the thread is interrupted beetwen the
352 * unlock line and the suspend_thread line */
353 p_condvar->thread = find_thread( NULL );
354 vlc_mutex_unlock( p_mutex );
355 suspend_thread( p_condvar->thread );
356 p_condvar->thread = -1;
358 vlc_mutex_lock( p_mutex );
364 /*****************************************************************************
365 * vlc_cond_timedwait: wait until condition completion or expiration
366 *****************************************************************************
367 * Returns 0 if object signaled, an error code in case of timeout or error.
368 *****************************************************************************/
369 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
370 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
372 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
373 vlc_cond_t *p_condvar,
374 vlc_mutex_t *p_mutex,
377 #if defined(LIBVLC_USE_PTHREAD)
378 lldiv_t d = lldiv( deadline, 1000000 );
379 struct timespec ts = { d.quot, d.rem * 1000 };
381 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
382 if (val == ETIMEDOUT)
383 return ETIMEDOUT; /* this error is perfectly normal */
384 VLC_THREAD_ASSERT ("timed-waiting on condition");
386 #elif defined( UNDER_CE )
387 mtime_t delay_ms = (deadline - mdate())/1000;
392 p_condvar->i_waiting_threads++;
393 LeaveCriticalSection( &p_mutex->csection );
394 result = WaitForSingleObject( p_condvar->event, delay_ms );
395 p_condvar->i_waiting_threads--;
397 /* Reacquire the mutex before returning. */
398 vlc_mutex_lock( p_mutex );
400 if(result == WAIT_TIMEOUT)
401 return ETIMEDOUT; /* this error is perfectly normal */
403 (void)psz_file; (void)i_line;
405 #elif defined( WIN32 )
406 mtime_t delay_ms = (deadline - mdate())/1000;
411 /* Increase our wait count */
412 p_condvar->i_waiting_threads++;
413 result = SignalObjectAndWait( *p_mutex, p_condvar->event,
415 p_condvar->i_waiting_threads--;
417 /* Reacquire the mutex before returning. */
418 vlc_mutex_lock( p_mutex );
419 if(result == WAIT_TIMEOUT)
420 return ETIMEDOUT; /* this error is perfectly normal */
422 (void)psz_file; (void)i_line;
424 #elif defined( SYS_BEOS )
425 # error Unimplemented
432 /*****************************************************************************
433 * vlc_cond_destroy: destroy a condition
434 *****************************************************************************/
435 #define vlc_cond_destroy( P_COND ) \
436 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
438 /*****************************************************************************
439 * vlc_threadvar_set: create: set the value of a thread-local variable
440 *****************************************************************************/
441 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
445 #if defined(LIBVLC_USE_PTHREAD)
446 i_ret = pthread_setspecific( *p_tls, p_value );
448 #elif defined( SYS_BEOS )
451 #elif defined( UNDER_CE ) || defined( WIN32 )
452 i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
459 /*****************************************************************************
460 * vlc_threadvar_get: create: get the value of a thread-local variable
461 *****************************************************************************/
462 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
466 #if defined(LIBVLC_USE_PTHREAD)
467 p_ret = pthread_getspecific( *p_tls );
469 #elif defined( SYS_BEOS )
472 #elif defined( UNDER_CE ) || defined( WIN32 )
473 p_ret = TlsGetValue( *p_tls );
480 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
481 typedef pthread_spinlock_t vlc_spinlock_t;
484 * Initializes a spinlock.
486 static inline int vlc_spin_init (vlc_spinlock_t *spin)
488 return pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE);
492 * Acquires a spinlock.
494 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
496 pthread_spin_lock (spin);
500 * Releases a spinlock.
502 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
504 pthread_spin_unlock (spin);
508 * Deinitializes a spinlock.
510 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
512 pthread_spin_destroy (spin);
515 #elif defined( WIN32 )
517 typedef CRITICAL_SECTION vlc_spinlock_t;
520 * Initializes a spinlock.
522 static inline int vlc_spin_init (vlc_spinlock_t *spin)
524 return !InitializeCriticalSectionAndSpinCount(spin, 4000);
528 * Acquires a spinlock.
530 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
532 EnterCriticalSection(spin);
536 * Releases a spinlock.
538 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
540 LeaveCriticalSection(spin);
544 * Deinitializes a spinlock.
546 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
548 DeleteCriticalSection(spin);
553 /* Fallback to plain mutexes if spinlocks are not available */
554 typedef vlc_mutex_t vlc_spinlock_t;
556 static inline int vlc_spin_init (vlc_spinlock_t *spin)
558 return vlc_mutex_init (spin);
561 # define vlc_spin_lock vlc_mutex_lock
562 # define vlc_spin_unlock vlc_mutex_unlock
563 # define vlc_spin_destroy vlc_mutex_destroy
566 /*****************************************************************************
567 * vlc_thread_create: create a thread
568 *****************************************************************************/
569 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
570 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
572 /*****************************************************************************
573 * vlc_thread_set_priority: set the priority of the calling thread
574 *****************************************************************************/
575 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
576 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
578 /*****************************************************************************
579 * vlc_thread_ready: tell the parent thread we were successfully spawned
580 *****************************************************************************/
581 #define vlc_thread_ready( P_THIS ) \
582 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
584 /*****************************************************************************
585 * vlc_thread_join: wait until a thread exits
586 *****************************************************************************/
587 #define vlc_thread_join( P_THIS ) \
588 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
590 #endif /* !_VLC_THREADS_H */