1 /*****************************************************************************
2 * vlc_threads_funcs.h : threads implementation for the VideoLAN client
3 * This header provides a portable threads implementation.
4 *****************************************************************************
5 * Copyright (C) 1999-2007 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_THREADFUNCS_H_
33 #define _VLC_THREADFUNCS_H_
35 /*****************************************************************************
36 * Function definitions
37 *****************************************************************************/
38 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
39 VLC_EXPORT( int, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
40 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
41 VLC_EXPORT( int, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
42 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_object_t *, vlc_threadvar_t * ) );
43 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
44 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
45 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
46 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
48 /*****************************************************************************
49 * vlc_threads_init: initialize threads system
50 *****************************************************************************/
51 #define vlc_threads_init( P_THIS ) \
52 __vlc_threads_init( VLC_OBJECT(P_THIS) )
54 /*****************************************************************************
55 * vlc_threads_end: deinitialize threads system
56 *****************************************************************************/
57 #define vlc_threads_end( P_THIS ) \
58 __vlc_threads_end( VLC_OBJECT(P_THIS) )
60 /*****************************************************************************
61 * vlc_mutex_init: initialize a mutex
62 *****************************************************************************/
63 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
64 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
66 /*****************************************************************************
67 * vlc_mutex_lock: lock a mutex
68 *****************************************************************************/
69 #define vlc_mutex_lock( P_MUTEX ) \
70 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
73 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
75 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
78 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
79 vlc_mutex_t * p_mutex )
82 /* In case of error : */
83 unsigned long int i_thread = 0;
85 #if defined( PTH_INIT_IN_PTH_H )
86 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
88 #elif defined( ST_INIT_IN_ST_H )
89 i_result = st_mutex_lock( p_mutex->mutex );
91 #elif defined( UNDER_CE )
92 EnterCriticalSection( &p_mutex->csection );
95 #elif defined( WIN32 )
98 WaitForSingleObject( p_mutex->mutex, INFINITE );
102 EnterCriticalSection( &p_mutex->csection );
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107 if( p_mutex == NULL )
109 i_result = B_BAD_VALUE;
111 else if( p_mutex->init < 2000 )
113 i_result = B_NO_INIT;
117 i_result = acquire_sem( p_mutex->lock );
120 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
121 # define vlc_assert_locked( m ) \
122 assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK)
124 i_result = pthread_mutex_lock( &p_mutex->mutex );
127 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
131 #elif defined( HAVE_CTHREADS_H )
132 mutex_lock( p_mutex->mutex );
139 msg_Err( p_mutex->p_this,
140 "thread %li: mutex_lock failed at %s:%d (%d:%m)",
141 i_thread, psz_file, i_line, i_result );
146 #ifndef vlc_assert_locked
147 # define vlc_assert_locked( m ) (void)0
150 /*****************************************************************************
151 * vlc_mutex_unlock: unlock a mutex
152 *****************************************************************************/
153 #define vlc_mutex_unlock( P_MUTEX ) \
154 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
156 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
157 vlc_mutex_t *p_mutex )
160 /* In case of error : */
161 unsigned long int i_thread = 0;
163 #if defined( PTH_INIT_IN_PTH_H )
164 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
166 #elif defined( ST_INIT_IN_ST_H )
167 i_result = st_mutex_unlock( p_mutex->mutex );
169 #elif defined( UNDER_CE )
170 LeaveCriticalSection( &p_mutex->csection );
173 #elif defined( WIN32 )
176 ReleaseMutex( p_mutex->mutex );
180 LeaveCriticalSection( &p_mutex->csection );
184 #elif defined( HAVE_KERNEL_SCHEDULER_H )
185 if( p_mutex == NULL )
187 i_result = B_BAD_VALUE;
189 else if( p_mutex->init < 2000 )
191 i_result = B_NO_INIT;
195 release_sem( p_mutex->lock );
199 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
200 i_result = pthread_mutex_unlock( &p_mutex->mutex );
203 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
207 #elif defined( HAVE_CTHREADS_H )
208 mutex_unlock( p_mutex );
215 msg_Err( p_mutex->p_this,
216 "thread %li: mutex_unlock failed at %s:%d (%d:%m)",
217 i_thread, psz_file, i_line, i_result );
223 /*****************************************************************************
224 * vlc_mutex_destroy: destroy a mutex
225 *****************************************************************************/
226 #define vlc_mutex_destroy( P_MUTEX ) \
227 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
229 /*****************************************************************************
230 * vlc_cond_init: initialize a condition
231 *****************************************************************************/
232 #define vlc_cond_init( P_THIS, P_COND ) \
233 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
235 /*****************************************************************************
236 * vlc_cond_signal: start a thread on condition completion
237 *****************************************************************************/
238 #define vlc_cond_signal( P_COND ) \
239 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
241 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
242 vlc_cond_t *p_condvar )
245 /* In case of error : */
246 unsigned long int i_thread = 0;
248 #if defined( PTH_INIT_IN_PTH_H )
249 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
251 #elif defined( ST_INIT_IN_ST_H )
252 i_result = st_cond_signal( p_condvar->cond );
254 #elif defined( UNDER_CE )
255 PulseEvent( p_condvar->event );
258 #elif defined( WIN32 )
259 /* Release one waiting thread if one is available. */
260 /* For this trick to work properly, the vlc_cond_signal must be surrounded
261 * by a mutex. This will prevent another thread from stealing the signal */
262 if( !p_condvar->semaphore )
264 PulseEvent( p_condvar->event );
266 else if( p_condvar->i_win9x_cv == 1 )
268 /* Wait for the gate to be open */
269 WaitForSingleObject( p_condvar->event, INFINITE );
271 if( p_condvar->i_waiting_threads )
273 /* Using a semaphore exposes us to a race condition. It is
274 * possible for another thread to start waiting on the semaphore
275 * just after we signaled it and thus steal the signal.
276 * We have to prevent new threads from entering the cond_wait(). */
277 ResetEvent( p_condvar->event );
279 /* A semaphore is used here because Win9x doesn't have
280 * SignalObjectAndWait() and thus a race condition exists
281 * during the time we release the mutex and the time we start
282 * waiting on the event (more precisely, the signal can sometimes
283 * be missed by the waiting thread if we use PulseEvent()). */
284 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
289 if( p_condvar->i_waiting_threads )
291 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
293 /* Wait for the last thread to be awakened */
294 WaitForSingleObject( p_condvar->event, INFINITE );
299 #elif defined( HAVE_KERNEL_SCHEDULER_H )
300 if( p_condvar == NULL )
302 i_result = B_BAD_VALUE;
304 else if( p_condvar->init < 2000 )
306 i_result = B_NO_INIT;
310 while( p_condvar->thread != -1 )
313 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
318 if( info.state != B_THREAD_SUSPENDED )
320 /* The waiting thread is not suspended so it could
321 * have been interrupted beetwen the unlock and the
322 * suspend_thread line. That is why we sleep a little
323 * before retesting p_condver->thread. */
328 /* Ok, we have to wake up that thread */
329 resume_thread( p_condvar->thread );
336 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
337 i_result = pthread_cond_signal( &p_condvar->cond );
340 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
344 #elif defined( HAVE_CTHREADS_H )
345 /* condition_signal() */
346 if ( p_condvar->queue.head || p_condvar->implications )
348 cond_signal( (condition_t)p_condvar );
356 msg_Err( p_condvar->p_this,
357 "thread %li: cond_signal failed at %s:%d (%d:%m)",
358 i_thread, psz_file, i_line, i_result );
364 /*****************************************************************************
365 * vlc_cond_wait: wait until condition completion
366 *****************************************************************************/
367 #define vlc_cond_wait( P_COND, P_MUTEX ) \
368 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
370 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
371 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
374 /* In case of error : */
375 unsigned long int i_thread = 0;
377 #if defined( PTH_INIT_IN_PTH_H )
378 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
381 #elif defined( ST_INIT_IN_ST_H )
382 st_mutex_unlock( p_mutex->mutex );
383 i_result = st_cond_wait( p_condvar->cond );
384 st_mutex_lock( p_mutex->mutex );
386 #elif defined( UNDER_CE )
387 p_condvar->i_waiting_threads++;
388 LeaveCriticalSection( &p_mutex->csection );
389 WaitForSingleObject( p_condvar->event, INFINITE );
390 p_condvar->i_waiting_threads--;
392 /* Reacquire the mutex before returning. */
393 vlc_mutex_lock( p_mutex );
397 #elif defined( WIN32 )
398 if( !p_condvar->semaphore )
400 /* Increase our wait count */
401 p_condvar->i_waiting_threads++;
405 /* It is only possible to atomically release the mutex and
406 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
407 * SignalObjectAndWait(). */
408 p_condvar->SignalObjectAndWait( p_mutex->mutex,
414 LeaveCriticalSection( &p_mutex->csection );
415 WaitForSingleObject( p_condvar->event, INFINITE );
418 p_condvar->i_waiting_threads--;
420 else if( p_condvar->i_win9x_cv == 1 )
422 int i_waiting_threads;
424 /* Wait for the gate to be open */
425 WaitForSingleObject( p_condvar->event, INFINITE );
427 /* Increase our wait count */
428 p_condvar->i_waiting_threads++;
430 LeaveCriticalSection( &p_mutex->csection );
431 WaitForSingleObject( p_condvar->semaphore, INFINITE );
433 /* Decrement and test must be atomic */
434 EnterCriticalSection( &p_condvar->csection );
436 /* Decrease our wait count */
437 i_waiting_threads = --p_condvar->i_waiting_threads;
439 LeaveCriticalSection( &p_condvar->csection );
441 /* Reopen the gate if we were the last waiting thread */
442 if( !i_waiting_threads )
443 SetEvent( p_condvar->event );
447 int i_waiting_threads;
449 /* Increase our wait count */
450 p_condvar->i_waiting_threads++;
452 LeaveCriticalSection( &p_mutex->csection );
453 WaitForSingleObject( p_condvar->semaphore, INFINITE );
455 /* Decrement and test must be atomic */
456 EnterCriticalSection( &p_condvar->csection );
458 /* Decrease our wait count */
459 i_waiting_threads = --p_condvar->i_waiting_threads;
461 LeaveCriticalSection( &p_condvar->csection );
463 /* Signal that the last waiting thread just went through */
464 if( !i_waiting_threads )
465 SetEvent( p_condvar->event );
468 /* Reacquire the mutex before returning. */
469 vlc_mutex_lock( p_mutex );
473 #elif defined( HAVE_KERNEL_SCHEDULER_H )
474 if( p_condvar == NULL )
476 i_result = B_BAD_VALUE;
478 else if( p_mutex == NULL )
480 i_result = B_BAD_VALUE;
482 else if( p_condvar->init < 2000 )
484 i_result = B_NO_INIT;
487 /* The p_condvar->thread var is initialized before the unlock because
488 * it enables to identify when the thread is interrupted beetwen the
489 * unlock line and the suspend_thread line */
490 p_condvar->thread = find_thread( NULL );
491 vlc_mutex_unlock( p_mutex );
492 suspend_thread( p_condvar->thread );
493 p_condvar->thread = -1;
495 vlc_mutex_lock( p_mutex );
498 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
501 /* In debug mode, timeout */
502 struct timespec timeout = {
503 (mdate() / 1000000) + THREAD_COND_TIMEOUT,
504 0 /* 1Hz precision is sufficient here :-) */
507 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
510 if( i_result == ETIMEDOUT )
513 msg_Dbg( p_condvar->p_this,
514 "thread %li: possible condition deadlock "
515 "at %s:%d (%m)", CAST_PTHREAD_TO_INT(pthread_self()),
518 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
522 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
527 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
531 #elif defined( HAVE_CTHREADS_H )
532 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
539 msg_Err( p_condvar->p_this,
540 "thread %li: cond_wait failed at %s:%d (%d:%m)",
541 i_thread, psz_file, i_line, i_result );
548 /*****************************************************************************
549 * vlc_cond_timedwait: wait until condition completion or expiration
550 *****************************************************************************
551 * Returns 0 if object signaled, an error code in case of timeout or error.
552 *****************************************************************************/
553 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
554 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
556 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
557 vlc_cond_t *p_condvar,
558 vlc_mutex_t *p_mutex,
562 unsigned long int i_thread = 0;
564 #if defined( PTH_INIT_IN_PTH_H )
565 # error Unimplemented
566 #elif defined( ST_INIT_IN_ST_H )
567 # error Unimplemented
568 #elif defined( UNDER_CE )
569 # error Unimplemented
570 #elif defined( WIN32 )
572 # warning Unimplemented FIXME FIXME
574 #elif defined( HAVE_KERNEL_SCHEDULER_H )
575 # error Unimplemented
576 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
577 lldiv_t d = lldiv( deadline, 1000000 );
578 struct timespec ts = { d.quot, d.rem * 1000 };
580 i_res = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex, &ts );
581 if( i_res == ETIMEDOUT )
582 return ETIMEDOUT; /* this error is perfectly normal */
584 if ( i_res ) /* other errors = bug */
586 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
590 #elif defined( HAVE_CTHREADS_H )
591 # error Unimplemented
596 msg_Err( p_condvar->p_this,
597 "thread %li: cond_wait failed at %s:%d (%d:%m)",
598 i_thread, psz_file, i_line, i_res );
604 /*****************************************************************************
605 * vlc_cond_destroy: destroy a condition
606 *****************************************************************************/
607 #define vlc_cond_destroy( P_COND ) \
608 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
610 /*****************************************************************************
611 * vlc_threadvar_create: create a thread-local variable
612 *****************************************************************************/
613 #define vlc_threadvar_create( PTHIS, P_TLS ) \
614 __vlc_threadvar_create( PTHIS, P_TLS )
616 /*****************************************************************************
617 * vlc_threadvar_set: create: set the value of a thread-local variable
618 *****************************************************************************/
619 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
623 #if defined( PTH_INIT_IN_PTH_H )
624 return pth_key_setdata( p_tls->handle, p_value );
625 #elif defined( ST_INIT_IN_ST_H )
626 return st_thread_setspecific( p_tls->handle, p_value );
627 #elif defined( HAVE_KERNEL_SCHEDULER_H )
630 #elif defined( UNDER_CE ) || defined( WIN32 )
631 i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
633 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
634 i_ret = pthread_setspecific( p_tls->handle, p_value );
636 #elif defined( HAVE_CTHREADS_H )
637 i_ret = cthread_setspecific( p_tls->handle, p_value );
643 /*****************************************************************************
644 * vlc_threadvar_get: create: get the value of a thread-local variable
645 *****************************************************************************/
646 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
650 #if defined( PTH_INIT_IN_PTH_H )
651 p_ret = pth_key_getdata( p_handle->key );
652 #elif defined( ST_INIT_IN_ST_H )
653 p_ret = st_thread_getspecific( p_handle->key );
654 #elif defined( HAVE_KERNEL_SCHEDULER_H )
656 #elif defined( UNDER_CE ) || defined( WIN32 )
657 p_ret = TlsGetValue( p_tls->handle );
659 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
660 p_ret = pthread_getspecific( p_tls->handle );
662 #elif defined( HAVE_CTHREADS_H )
663 if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
672 /*****************************************************************************
673 * vlc_thread_create: create a thread
674 *****************************************************************************/
675 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
676 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
678 /*****************************************************************************
679 * vlc_thread_set_priority: set the priority of the calling thread
680 *****************************************************************************/
681 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
682 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
684 /*****************************************************************************
685 * vlc_thread_ready: tell the parent thread we were successfully spawned
686 *****************************************************************************/
687 #define vlc_thread_ready( P_THIS ) \
688 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
690 /*****************************************************************************
691 * vlc_thread_join: wait until a thread exits
692 *****************************************************************************/
693 #define vlc_thread_join( P_THIS ) \
694 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )