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
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
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 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
33 * This file defines structures and functions for handling threads in vlc
37 #if defined( UNDER_CE )
39 #elif defined( WIN32 )
40 # include <process.h> /* Win32 API */
43 #elif defined( SYS_BEOS ) /* BeOS */
44 # include <kernel/OS.h>
45 # include <kernel/scheduler.h>
46 # include <byteorder.h>
48 #else /* pthreads (like Linux & BSD) */
49 # define LIBVLC_USE_PTHREAD 1
50 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
52 # include <stdlib.h> /* lldiv_t definition (only in C99) */
53 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
55 /* Needed for pthread_cond_timedwait */
61 /*****************************************************************************
63 *****************************************************************************/
65 /* Thread priorities */
67 # define VLC_THREAD_PRIORITY_LOW 0
68 # define VLC_THREAD_PRIORITY_INPUT 22
69 # define VLC_THREAD_PRIORITY_AUDIO 22
70 # define VLC_THREAD_PRIORITY_VIDEO 0
71 # define VLC_THREAD_PRIORITY_OUTPUT 22
72 # define VLC_THREAD_PRIORITY_HIGHEST 22
74 #elif defined(SYS_BEOS)
75 # define VLC_THREAD_PRIORITY_LOW 5
76 # define VLC_THREAD_PRIORITY_INPUT 10
77 # define VLC_THREAD_PRIORITY_AUDIO 10
78 # define VLC_THREAD_PRIORITY_VIDEO 5
79 # define VLC_THREAD_PRIORITY_OUTPUT 15
80 # define VLC_THREAD_PRIORITY_HIGHEST 15
82 #elif defined(LIBVLC_USE_PTHREAD)
83 # define VLC_THREAD_PRIORITY_LOW 0
84 # define VLC_THREAD_PRIORITY_INPUT 10
85 # define VLC_THREAD_PRIORITY_AUDIO 5
86 # define VLC_THREAD_PRIORITY_VIDEO 0
87 # define VLC_THREAD_PRIORITY_OUTPUT 15
88 # define VLC_THREAD_PRIORITY_HIGHEST 20
90 #elif defined(WIN32) || defined(UNDER_CE)
91 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
92 # define VLC_THREAD_PRIORITY_LOW 0
93 # define VLC_THREAD_PRIORITY_INPUT \
94 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
95 # define VLC_THREAD_PRIORITY_AUDIO \
96 (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
97 # define VLC_THREAD_PRIORITY_VIDEO \
98 (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
99 # define VLC_THREAD_PRIORITY_OUTPUT \
100 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
101 # define VLC_THREAD_PRIORITY_HIGHEST \
102 (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
105 # define VLC_THREAD_PRIORITY_LOW 0
106 # define VLC_THREAD_PRIORITY_INPUT 0
107 # define VLC_THREAD_PRIORITY_AUDIO 0
108 # define VLC_THREAD_PRIORITY_VIDEO 0
109 # define VLC_THREAD_PRIORITY_OUTPUT 0
110 # define VLC_THREAD_PRIORITY_HIGHEST 0
114 /*****************************************************************************
116 *****************************************************************************/
118 #if defined (LIBVLC_USE_PTHREAD)
119 typedef pthread_t vlc_thread_t;
120 typedef pthread_mutex_t vlc_mutex_t;
121 typedef pthread_cond_t vlc_cond_t;
122 typedef pthread_key_t vlc_threadvar_t;
124 #elif defined( WIN32 ) || defined( UNDER_CE )
128 void *(*entry) (void *);
132 typedef HANDLE vlc_mutex_t;
133 typedef HANDLE vlc_cond_t;
134 typedef DWORD vlc_threadvar_t;
136 #elif defined( SYS_BEOS )
137 /* This is the BeOS implementation of the vlc threads, note that the mutex is
138 * not a real mutex and the cond_var is not like a pthread cond_var but it is
139 * enough for what we need */
141 typedef thread_id vlc_thread_t;
161 #if defined( WIN32 ) && !defined ETIMEDOUT
162 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
165 /*****************************************************************************
166 * Function definitions
167 *****************************************************************************/
168 VLC_EXPORT( int, vlc_mutex_init, ( vlc_mutex_t * ) );
169 VLC_EXPORT( int, vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
170 VLC_EXPORT( void, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
171 VLC_EXPORT( int, __vlc_cond_init, ( vlc_cond_t * ) );
172 VLC_EXPORT( void, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
173 VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
174 VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
175 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( vlc_object_t * ), int, bool ) );
176 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
177 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
179 VLC_EXPORT( int, vlc_clone, (vlc_thread_t *, void * (*) (void *), void *, int) );
180 VLC_EXPORT( void, vlc_cancel, (vlc_thread_t) );
181 VLC_EXPORT( int, vlc_join, (vlc_thread_t, void **) );
183 #define vlc_thread_ready vlc_object_signal
185 /*****************************************************************************
186 * vlc_mutex_lock: lock a mutex
187 *****************************************************************************/
188 #define vlc_mutex_lock( P_MUTEX ) \
189 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
191 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
193 #if defined(LIBVLC_USE_PTHREAD)
194 # define VLC_THREAD_ASSERT( action ) \
196 vlc_pthread_fatal (action, val, psz_file, i_line)
198 # define VLC_THREAD_ASSERT ((void)(val))
201 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
202 vlc_mutex_t * p_mutex )
204 #if defined(LIBVLC_USE_PTHREAD)
205 # define vlc_assert_locked( m ) \
206 assert (pthread_mutex_lock (m) == EDEADLK)
207 int val = pthread_mutex_lock( p_mutex );
208 VLC_THREAD_ASSERT ("locking mutex");
210 #elif defined( UNDER_CE )
211 (void)psz_file; (void)i_line;
213 EnterCriticalSection( &p_mutex->csection );
215 #elif defined( WIN32 )
216 (void)psz_file; (void)i_line;
218 WaitForSingleObject( *p_mutex, INFINITE );
220 #elif defined( SYS_BEOS )
221 acquire_sem( p_mutex->lock );
226 #ifndef vlc_assert_locked
227 # define vlc_assert_locked( m ) (void)m
230 /*****************************************************************************
231 * vlc_mutex_unlock: unlock a mutex
232 *****************************************************************************/
233 #define vlc_mutex_unlock( P_MUTEX ) \
234 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
236 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
237 vlc_mutex_t *p_mutex )
239 #if defined(LIBVLC_USE_PTHREAD)
240 int val = pthread_mutex_unlock( p_mutex );
241 VLC_THREAD_ASSERT ("unlocking mutex");
243 #elif defined( UNDER_CE )
244 (void)psz_file; (void)i_line;
246 LeaveCriticalSection( &p_mutex->csection );
248 #elif defined( WIN32 )
249 (void)psz_file; (void)i_line;
251 ReleaseMutex( *p_mutex );
253 #elif defined( SYS_BEOS )
254 release_sem( p_mutex->lock );
259 /*****************************************************************************
260 * vlc_mutex_destroy: destroy a mutex
261 *****************************************************************************/
262 #define vlc_mutex_destroy( P_MUTEX ) \
263 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
265 /*****************************************************************************
266 * vlc_cond_init: initialize a condition
267 *****************************************************************************/
268 #define vlc_cond_init( P_THIS, P_COND ) \
269 __vlc_cond_init( P_COND )
271 /*****************************************************************************
272 * vlc_cond_signal: start a thread on condition completion
273 *****************************************************************************/
274 #define vlc_cond_signal( P_COND ) \
275 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
277 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
278 vlc_cond_t *p_condvar )
280 #if defined(LIBVLC_USE_PTHREAD)
281 int val = pthread_cond_signal( p_condvar );
282 VLC_THREAD_ASSERT ("signaling condition variable");
284 #elif defined( UNDER_CE ) || defined( WIN32 )
285 (void)psz_file; (void)i_line;
287 /* Release one waiting thread if one is available. */
288 /* For this trick to work properly, the vlc_cond_signal must be surrounded
289 * by a mutex. This will prevent another thread from stealing the signal */
290 /* PulseEvent() only works if none of the waiting threads is suspended.
291 * This is particularily problematic under a debug session.
292 * as documented in http://support.microsoft.com/kb/q173260/ */
293 PulseEvent( *p_condvar );
295 #elif defined( SYS_BEOS )
296 while( p_condvar->thread != -1 )
299 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
302 if( info.state != B_THREAD_SUSPENDED )
304 /* The waiting thread is not suspended so it could
305 * have been interrupted beetwen the unlock and the
306 * suspend_thread line. That is why we sleep a little
307 * before retesting p_condver->thread. */
312 /* Ok, we have to wake up that thread */
313 resume_thread( p_condvar->thread );
320 /*****************************************************************************
321 * vlc_cond_wait: wait until condition completion
322 *****************************************************************************/
323 #define vlc_cond_wait( P_COND, P_MUTEX ) \
324 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
326 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
327 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
329 #if defined(LIBVLC_USE_PTHREAD)
330 int val = pthread_cond_wait( p_condvar, p_mutex );
331 VLC_THREAD_ASSERT ("waiting on condition");
333 #elif defined( UNDER_CE )
334 LeaveCriticalSection( &p_mutex->csection );
335 WaitForSingleObject( *p_condvar, INFINITE );
337 /* Reacquire the mutex before returning. */
338 vlc_mutex_lock( p_mutex );
340 #elif defined( WIN32 )
341 (void)psz_file; (void)i_line;
343 /* Increase our wait count */
344 SignalObjectAndWait( *p_mutex, *p_condvar, INFINITE, FALSE );
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 LeaveCriticalSection( &p_mutex->csection );
393 result = WaitForSingleObject( *p_condvar, delay_ms );
395 /* Reacquire the mutex before returning. */
396 vlc_mutex_lock( p_mutex );
398 if(result == WAIT_TIMEOUT)
399 return ETIMEDOUT; /* this error is perfectly normal */
401 (void)psz_file; (void)i_line;
403 #elif defined( WIN32 )
404 mtime_t total = (deadline - mdate())/1000;
411 DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
412 result = SignalObjectAndWait( *p_mutex, *p_condvar,
415 vlc_mutex_lock (p_mutex);
419 /* Reacquire the mutex before returning. */
420 if(result == WAIT_TIMEOUT)
421 return ETIMEDOUT; /* this error is perfectly normal */
423 (void)psz_file; (void)i_line;
425 #elif defined( SYS_BEOS )
426 # error Unimplemented
433 /*****************************************************************************
434 * vlc_cond_destroy: destroy a condition
435 *****************************************************************************/
436 #define vlc_cond_destroy( P_COND ) \
437 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
439 /*****************************************************************************
440 * vlc_threadvar_set: create: set the value of a thread-local variable
441 *****************************************************************************/
442 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
446 #if defined(LIBVLC_USE_PTHREAD)
447 i_ret = pthread_setspecific( *p_tls, p_value );
449 #elif defined( SYS_BEOS )
452 #elif defined( UNDER_CE ) || defined( WIN32 )
453 i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
460 /*****************************************************************************
461 * vlc_threadvar_get: create: get the value of a thread-local variable
462 *****************************************************************************/
463 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
467 #if defined(LIBVLC_USE_PTHREAD)
468 p_ret = pthread_getspecific( *p_tls );
470 #elif defined( SYS_BEOS )
473 #elif defined( UNDER_CE ) || defined( WIN32 )
474 p_ret = TlsGetValue( *p_tls );
481 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
482 typedef pthread_spinlock_t vlc_spinlock_t;
485 * Initializes a spinlock.
487 static inline int vlc_spin_init (vlc_spinlock_t *spin)
489 return pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE);
493 * Acquires a spinlock.
495 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
497 pthread_spin_lock (spin);
501 * Releases a spinlock.
503 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
505 pthread_spin_unlock (spin);
509 * Deinitializes a spinlock.
511 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
513 pthread_spin_destroy (spin);
516 #elif defined( WIN32 )
518 typedef CRITICAL_SECTION vlc_spinlock_t;
521 * Initializes a spinlock.
523 static inline int vlc_spin_init (vlc_spinlock_t *spin)
525 return !InitializeCriticalSectionAndSpinCount(spin, 4000);
529 * Acquires a spinlock.
531 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
533 EnterCriticalSection(spin);
537 * Releases a spinlock.
539 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
541 LeaveCriticalSection(spin);
545 * Deinitializes a spinlock.
547 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
549 DeleteCriticalSection(spin);
554 /* Fallback to plain mutexes if spinlocks are not available */
555 typedef vlc_mutex_t vlc_spinlock_t;
557 static inline int vlc_spin_init (vlc_spinlock_t *spin)
559 return vlc_mutex_init (spin);
562 # define vlc_spin_lock vlc_mutex_lock
563 # define vlc_spin_unlock vlc_mutex_unlock
564 # define vlc_spin_destroy vlc_mutex_destroy
568 * Issues a full memory barrier.
570 #if defined (__APPLE__)
571 # include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
573 static inline void barrier (void)
575 #if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
576 __sync_synchronize ();
577 #elif defined(__APPLE__)
579 #elif defined(__powerpc__)
580 asm volatile ("sync":::"memory");
581 #elif defined(__i386__)
582 asm volatile ("mfence":::"memory");
585 vlc_spin_init (&spin);
586 vlc_spin_lock (&spin);
587 vlc_spin_unlock (&spin);
588 vlc_spin_destroy (&spin);
593 * Save the cancellation state and disable cancellation for the calling thread.
594 * This function must be called before entering a piece of code that is not
596 * @param p_state storage for the previous cancellation state
597 * @return Nothing, always succeeds.
599 static inline void vlc_savecancel (int *p_state)
601 #if defined (LIBVLC_USE_PTHREAD)
602 (void) pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, p_state);
607 * Restore the cancellation state for the calling thread.
608 * @param state previous state as returned by vlc_savecancel().
609 * @return Nothing, always succeeds.
611 static inline void vlc_restorecancel (int state)
613 #if defined (LIBVLC_USE_PTHREAD)
614 (void) pthread_setcancelstate (state, NULL);
618 /*****************************************************************************
619 * vlc_thread_create: create a thread
620 *****************************************************************************/
621 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
622 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, FUNC, PRIORITY, WAIT )
624 /*****************************************************************************
625 * vlc_thread_set_priority: set the priority of the calling thread
626 *****************************************************************************/
627 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
628 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
630 /*****************************************************************************
631 * vlc_thread_join: wait until a thread exits
632 *****************************************************************************/
633 #define vlc_thread_join( P_THIS ) \
634 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
636 #endif /* !_VLC_THREADS_H */