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 #ifndef _VLC_THREADS_H_
29 #define _VLC_THREADS_H_
31 #if defined( UNDER_CE )
33 #elif defined( WIN32 )
34 # include <process.h> /* Win32 API */
37 #elif defined( SYS_BEOS ) /* BeOS */
38 # include <kernel/OS.h>
39 # include <kernel/scheduler.h>
40 # include <byteorder.h>
42 #else /* pthreads (like Linux & BSD) */
43 # define LIBVLC_USE_PTHREAD 1
44 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
46 # include <stdlib.h> /* lldiv_t definition (only in C99) */
47 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
49 /* Needed for pthread_cond_timedwait */
55 /*****************************************************************************
57 *****************************************************************************/
59 /* Thread priorities */
61 # define VLC_THREAD_PRIORITY_LOW 0
62 # define VLC_THREAD_PRIORITY_INPUT 22
63 # define VLC_THREAD_PRIORITY_AUDIO 22
64 # define VLC_THREAD_PRIORITY_VIDEO 0
65 # define VLC_THREAD_PRIORITY_OUTPUT 22
66 # define VLC_THREAD_PRIORITY_HIGHEST 22
68 #elif defined(SYS_BEOS)
69 # define VLC_THREAD_PRIORITY_LOW 5
70 # define VLC_THREAD_PRIORITY_INPUT 10
71 # define VLC_THREAD_PRIORITY_AUDIO 10
72 # define VLC_THREAD_PRIORITY_VIDEO 5
73 # define VLC_THREAD_PRIORITY_OUTPUT 15
74 # define VLC_THREAD_PRIORITY_HIGHEST 15
76 #elif defined(LIBVLC_USE_PTHREAD)
77 # define VLC_THREAD_PRIORITY_LOW 0
78 # define VLC_THREAD_PRIORITY_INPUT 10
79 # define VLC_THREAD_PRIORITY_AUDIO 5
80 # define VLC_THREAD_PRIORITY_VIDEO 0
81 # define VLC_THREAD_PRIORITY_OUTPUT 15
82 # define VLC_THREAD_PRIORITY_HIGHEST 20
84 #elif defined(WIN32) || defined(UNDER_CE)
85 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
86 # define VLC_THREAD_PRIORITY_LOW 0
87 # define VLC_THREAD_PRIORITY_INPUT \
88 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
89 # define VLC_THREAD_PRIORITY_AUDIO \
90 (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
91 # define VLC_THREAD_PRIORITY_VIDEO \
92 (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
93 # define VLC_THREAD_PRIORITY_OUTPUT \
94 (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
95 # define VLC_THREAD_PRIORITY_HIGHEST \
96 (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
99 # define VLC_THREAD_PRIORITY_LOW 0
100 # define VLC_THREAD_PRIORITY_INPUT 0
101 # define VLC_THREAD_PRIORITY_AUDIO 0
102 # define VLC_THREAD_PRIORITY_VIDEO 0
103 # define VLC_THREAD_PRIORITY_OUTPUT 0
104 # define VLC_THREAD_PRIORITY_HIGHEST 0
108 /*****************************************************************************
110 *****************************************************************************/
112 #if defined (LIBVLC_USE_PTHREAD)
113 typedef pthread_t vlc_thread_t
;
114 typedef pthread_mutex_t vlc_mutex_t
;
115 typedef pthread_cond_t vlc_cond_t
;
116 typedef pthread_key_t vlc_threadvar_t
;
118 #elif defined( WIN32 ) || defined( UNDER_CE )
119 typedef HANDLE vlc_thread_t
;
121 typedef BOOL (WINAPI
*SIGNALOBJECTANDWAIT
) ( HANDLE
, HANDLE
, DWORD
, BOOL
);
123 typedef HANDLE vlc_mutex_t
;
127 volatile int i_waiting_threads
;
131 typedef DWORD vlc_threadvar_t
;
133 #elif defined( SYS_BEOS )
134 /* This is the BeOS implementation of the vlc threads, note that the mutex is
135 * not a real mutex and the cond_var is not like a pthread cond_var but it is
136 * enough for what we need */
138 typedef thread_id vlc_thread_t
;
158 #if defined( WIN32 ) && !defined ETIMEDOUT
159 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
162 /*****************************************************************************
163 * Function definitions
164 *****************************************************************************/
165 VLC_EXPORT( int, vlc_mutex_init
, ( vlc_mutex_t
* ) );
166 VLC_EXPORT( int, vlc_mutex_init_recursive
, ( vlc_mutex_t
* ) );
167 VLC_EXPORT( void, __vlc_mutex_destroy
, ( const char *, int, vlc_mutex_t
* ) );
168 VLC_EXPORT( int, __vlc_cond_init
, ( vlc_cond_t
* ) );
169 VLC_EXPORT( void, __vlc_cond_destroy
, ( const char *, int, vlc_cond_t
* ) );
170 VLC_EXPORT( int, vlc_threadvar_create
, (vlc_threadvar_t
* , void (*) (void *) ) );
171 VLC_EXPORT( void, vlc_threadvar_delete
, (vlc_threadvar_t
*) );
172 VLC_EXPORT( int, __vlc_thread_create
, ( vlc_object_t
*, const char *, int, const char *, void * ( * ) ( vlc_object_t
* ), int, bool ) );
173 VLC_EXPORT( int, __vlc_thread_set_priority
, ( vlc_object_t
*, const char *, int, int ) );
174 VLC_EXPORT( void, __vlc_thread_join
, ( vlc_object_t
*, const char *, int ) );
176 #define vlc_thread_ready vlc_object_signal
178 /*****************************************************************************
179 * vlc_mutex_lock: lock a mutex
180 *****************************************************************************/
181 #define vlc_mutex_lock( P_MUTEX ) \
182 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
184 VLC_EXPORT(void, vlc_pthread_fatal
, (const char *action
, int error
, const char *file
, unsigned line
));
186 #if defined(LIBVLC_USE_PTHREAD)
187 # define VLC_THREAD_ASSERT( action ) \
189 vlc_pthread_fatal (action, val, psz_file, i_line)
191 # define VLC_THREAD_ASSERT ((void)(val))
194 static inline void __vlc_mutex_lock( const char * psz_file
, int i_line
,
195 vlc_mutex_t
* p_mutex
)
197 #if defined(LIBVLC_USE_PTHREAD)
198 # define vlc_assert_locked( m ) \
199 assert (pthread_mutex_lock (m) == EDEADLK)
200 int val
= pthread_mutex_lock( p_mutex
);
201 VLC_THREAD_ASSERT ("locking mutex");
203 #elif defined( UNDER_CE )
204 (void)psz_file
; (void)i_line
;
206 EnterCriticalSection( &p_mutex
->csection
);
208 #elif defined( WIN32 )
209 (void)psz_file
; (void)i_line
;
211 WaitForSingleObject( *p_mutex
, INFINITE
);
213 #elif defined( SYS_BEOS )
214 acquire_sem( p_mutex
->lock
);
219 #ifndef vlc_assert_locked
220 # define vlc_assert_locked( m ) (void)0
223 /*****************************************************************************
224 * vlc_mutex_unlock: unlock a mutex
225 *****************************************************************************/
226 #define vlc_mutex_unlock( P_MUTEX ) \
227 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
229 static inline void __vlc_mutex_unlock( const char * psz_file
, int i_line
,
230 vlc_mutex_t
*p_mutex
)
232 #if defined(LIBVLC_USE_PTHREAD)
233 int val
= pthread_mutex_unlock( p_mutex
);
234 VLC_THREAD_ASSERT ("unlocking mutex");
236 #elif defined( UNDER_CE )
237 (void)psz_file
; (void)i_line
;
239 LeaveCriticalSection( &p_mutex
->csection
);
241 #elif defined( WIN32 )
242 (void)psz_file
; (void)i_line
;
244 ReleaseMutex( *p_mutex
);
246 #elif defined( SYS_BEOS )
247 release_sem( p_mutex
->lock
);
252 /*****************************************************************************
253 * vlc_mutex_destroy: destroy a mutex
254 *****************************************************************************/
255 #define vlc_mutex_destroy( P_MUTEX ) \
256 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
258 /*****************************************************************************
259 * vlc_cond_init: initialize a condition
260 *****************************************************************************/
261 #define vlc_cond_init( P_THIS, P_COND ) \
262 __vlc_cond_init( P_COND )
264 /*****************************************************************************
265 * vlc_cond_signal: start a thread on condition completion
266 *****************************************************************************/
267 #define vlc_cond_signal( P_COND ) \
268 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
270 static inline void __vlc_cond_signal( const char * psz_file
, int i_line
,
271 vlc_cond_t
*p_condvar
)
273 #if defined(LIBVLC_USE_PTHREAD)
274 int val
= pthread_cond_signal( p_condvar
);
275 VLC_THREAD_ASSERT ("signaling condition variable");
277 #elif defined( UNDER_CE ) || defined( WIN32 )
278 (void)psz_file
; (void)i_line
;
280 /* Release one waiting thread if one is available. */
281 /* For this trick to work properly, the vlc_cond_signal must be surrounded
282 * by a mutex. This will prevent another thread from stealing the signal */
283 /* PulseEvent() only works if none of the waiting threads is suspended.
284 * This is particularily problematic under a debug session.
285 * as documented in http://support.microsoft.com/kb/q173260/ */
286 PulseEvent( p_condvar
->event
);
288 #elif defined( SYS_BEOS )
289 while( p_condvar
->thread
!= -1 )
292 if( get_thread_info(p_condvar
->thread
, &info
) == B_BAD_VALUE
)
295 if( info
.state
!= B_THREAD_SUSPENDED
)
297 /* The waiting thread is not suspended so it could
298 * have been interrupted beetwen the unlock and the
299 * suspend_thread line. That is why we sleep a little
300 * before retesting p_condver->thread. */
305 /* Ok, we have to wake up that thread */
306 resume_thread( p_condvar
->thread
);
313 /*****************************************************************************
314 * vlc_cond_wait: wait until condition completion
315 *****************************************************************************/
316 #define vlc_cond_wait( P_COND, P_MUTEX ) \
317 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
319 static inline void __vlc_cond_wait( const char * psz_file
, int i_line
,
320 vlc_cond_t
*p_condvar
, vlc_mutex_t
*p_mutex
)
322 #if defined(LIBVLC_USE_PTHREAD)
323 int val
= pthread_cond_wait( p_condvar
, p_mutex
);
324 VLC_THREAD_ASSERT ("waiting on condition");
326 #elif defined( UNDER_CE )
327 p_condvar
->i_waiting_threads
++;
328 LeaveCriticalSection( &p_mutex
->csection
);
329 WaitForSingleObject( p_condvar
->event
, INFINITE
);
330 p_condvar
->i_waiting_threads
--;
332 /* Reacquire the mutex before returning. */
333 vlc_mutex_lock( p_mutex
);
335 #elif defined( WIN32 )
336 (void)psz_file
; (void)i_line
;
338 /* Increase our wait count */
339 p_condvar
->i_waiting_threads
++;
340 SignalObjectAndWait( *p_mutex
, p_condvar
->event
, INFINITE
, FALSE
);
341 p_condvar
->i_waiting_threads
--;
343 /* Reacquire the mutex before returning. */
344 vlc_mutex_lock( p_mutex
);
346 #elif defined( SYS_BEOS )
347 /* The p_condvar->thread var is initialized before the unlock because
348 * it enables to identify when the thread is interrupted beetwen the
349 * unlock line and the suspend_thread line */
350 p_condvar
->thread
= find_thread( NULL
);
351 vlc_mutex_unlock( p_mutex
);
352 suspend_thread( p_condvar
->thread
);
353 p_condvar
->thread
= -1;
355 vlc_mutex_lock( p_mutex
);
361 /*****************************************************************************
362 * vlc_cond_timedwait: wait until condition completion or expiration
363 *****************************************************************************
364 * Returns 0 if object signaled, an error code in case of timeout or error.
365 *****************************************************************************/
366 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
367 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
369 static inline int __vlc_cond_timedwait( const char * psz_file
, int i_line
,
370 vlc_cond_t
*p_condvar
,
371 vlc_mutex_t
*p_mutex
,
374 #if defined(LIBVLC_USE_PTHREAD)
375 lldiv_t d
= lldiv( deadline
, 1000000 );
376 struct timespec ts
= { d
.quot
, d
.rem
* 1000 };
378 int val
= pthread_cond_timedwait (p_condvar
, p_mutex
, &ts
);
379 if (val
== ETIMEDOUT
)
380 return ETIMEDOUT
; /* this error is perfectly normal */
381 VLC_THREAD_ASSERT ("timed-waiting on condition");
383 #elif defined( UNDER_CE )
384 mtime_t delay_ms
= (deadline
- mdate())/1000;
389 p_condvar
->i_waiting_threads
++;
390 LeaveCriticalSection( &p_mutex
->csection
);
391 result
= WaitForSingleObject( p_condvar
->event
, delay_ms
);
392 p_condvar
->i_waiting_threads
--;
394 /* Reacquire the mutex before returning. */
395 vlc_mutex_lock( p_mutex
);
397 if(result
== WAIT_TIMEOUT
)
398 return ETIMEDOUT
; /* this error is perfectly normal */
400 (void)psz_file
; (void)i_line
;
402 #elif defined( WIN32 )
403 mtime_t delay_ms
= (deadline
- mdate())/1000;
408 /* Increase our wait count */
409 p_condvar
->i_waiting_threads
++;
410 result
= SignalObjectAndWait( *p_mutex
, p_condvar
->event
,
412 p_condvar
->i_waiting_threads
--;
414 /* Reacquire the mutex before returning. */
415 vlc_mutex_lock( p_mutex
);
416 if(result
== WAIT_TIMEOUT
)
417 return ETIMEDOUT
; /* this error is perfectly normal */
419 (void)psz_file
; (void)i_line
;
421 #elif defined( SYS_BEOS )
422 # error Unimplemented
429 /*****************************************************************************
430 * vlc_cond_destroy: destroy a condition
431 *****************************************************************************/
432 #define vlc_cond_destroy( P_COND ) \
433 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
435 /*****************************************************************************
436 * vlc_threadvar_set: create: set the value of a thread-local variable
437 *****************************************************************************/
438 static inline int vlc_threadvar_set( vlc_threadvar_t
* p_tls
, void *p_value
)
442 #if defined(LIBVLC_USE_PTHREAD)
443 i_ret
= pthread_setspecific( *p_tls
, p_value
);
445 #elif defined( SYS_BEOS )
448 #elif defined( UNDER_CE ) || defined( WIN32 )
449 i_ret
= TlsSetValue( *p_tls
, p_value
) ? EINVAL
: 0;
456 /*****************************************************************************
457 * vlc_threadvar_get: create: get the value of a thread-local variable
458 *****************************************************************************/
459 static inline void* vlc_threadvar_get( vlc_threadvar_t
* p_tls
)
463 #if defined(LIBVLC_USE_PTHREAD)
464 p_ret
= pthread_getspecific( *p_tls
);
466 #elif defined( SYS_BEOS )
469 #elif defined( UNDER_CE ) || defined( WIN32 )
470 p_ret
= TlsGetValue( *p_tls
);
477 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
478 typedef pthread_spinlock_t vlc_spinlock_t
;
481 * Initializes a spinlock.
483 static inline int vlc_spin_init (vlc_spinlock_t
*spin
)
485 return pthread_spin_init (spin
, PTHREAD_PROCESS_PRIVATE
);
489 * Acquires a spinlock.
491 static inline void vlc_spin_lock (vlc_spinlock_t
*spin
)
493 pthread_spin_lock (spin
);
497 * Releases a spinlock.
499 static inline void vlc_spin_unlock (vlc_spinlock_t
*spin
)
501 pthread_spin_unlock (spin
);
505 * Deinitializes a spinlock.
507 static inline void vlc_spin_destroy (vlc_spinlock_t
*spin
)
509 pthread_spin_destroy (spin
);
512 #elif defined( WIN32 )
514 typedef CRITICAL_SECTION vlc_spinlock_t
;
517 * Initializes a spinlock.
519 static inline int vlc_spin_init (vlc_spinlock_t
*spin
)
521 return !InitializeCriticalSectionAndSpinCount(spin
, 4000);
525 * Acquires a spinlock.
527 static inline void vlc_spin_lock (vlc_spinlock_t
*spin
)
529 EnterCriticalSection(spin
);
533 * Releases a spinlock.
535 static inline void vlc_spin_unlock (vlc_spinlock_t
*spin
)
537 LeaveCriticalSection(spin
);
541 * Deinitializes a spinlock.
543 static inline void vlc_spin_destroy (vlc_spinlock_t
*spin
)
545 DeleteCriticalSection(spin
);
550 /* Fallback to plain mutexes if spinlocks are not available */
551 typedef vlc_mutex_t vlc_spinlock_t
;
553 static inline int vlc_spin_init (vlc_spinlock_t
*spin
)
555 return vlc_mutex_init (spin
);
558 # define vlc_spin_lock vlc_mutex_lock
559 # define vlc_spin_unlock vlc_mutex_unlock
560 # define vlc_spin_destroy vlc_mutex_destroy
564 * Issues a full memory barrier.
566 #if defined (__APPLE__)
567 # include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
569 static inline void barrier (void)
571 #if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
572 __sync_synchronize ();
573 #elif defined(__APPLE__)
575 #elif defined(__powerpc__)
576 asm volatile ("sync":::"memory");
577 #elif defined(__i386__)
578 asm volatile ("mfence":::"memory");
579 #elif defined (LIBVLC_USE_PTHREAD)
580 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
581 pthread_mutex_lock (&lock
);
582 pthread_mutex_unlock (&lock
);
584 # error barrier not implemented!
588 /*****************************************************************************
589 * vlc_thread_create: create a thread
590 *****************************************************************************/
591 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
592 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, FUNC, PRIORITY, WAIT )
594 /*****************************************************************************
595 * vlc_thread_set_priority: set the priority of the calling thread
596 *****************************************************************************/
597 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
598 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
600 /*****************************************************************************
601 * vlc_thread_join: wait until a thread exits
602 *****************************************************************************/
603 #define vlc_thread_join( P_THIS ) \
604 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
606 #endif /* !_VLC_THREADS_H */