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 ) );
50 /*****************************************************************************
51 * vlc_threads_init: initialize threads system
52 *****************************************************************************/
53 #define vlc_threads_init( P_THIS ) \
54 __vlc_threads_init( VLC_OBJECT(P_THIS) )
56 /*****************************************************************************
57 * vlc_threads_end: deinitialize threads system
58 *****************************************************************************/
59 #define vlc_threads_end( P_THIS ) \
60 __vlc_threads_end( VLC_OBJECT(P_THIS) )
62 /*****************************************************************************
63 * vlc_mutex_init: initialize a mutex
64 *****************************************************************************/
65 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
66 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
68 /*****************************************************************************
69 * vlc_mutex_lock: lock a mutex
70 *****************************************************************************/
71 #define vlc_mutex_lock( P_MUTEX ) \
72 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
75 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
77 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
80 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
81 vlc_mutex_t * p_mutex )
84 /* In case of error : */
85 unsigned long int i_thread = 0;
86 const char * psz_error = "";
88 #if defined( PTH_INIT_IN_PTH_H )
89 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
91 #elif defined( ST_INIT_IN_ST_H )
92 i_result = st_mutex_lock( p_mutex->mutex );
94 #elif defined( UNDER_CE )
95 EnterCriticalSection( &p_mutex->csection );
98 #elif defined( WIN32 )
101 WaitForSingleObject( p_mutex->mutex, INFINITE );
105 EnterCriticalSection( &p_mutex->csection );
109 #elif defined( HAVE_KERNEL_SCHEDULER_H )
110 if( p_mutex == NULL )
112 i_result = B_BAD_VALUE;
114 else if( p_mutex->init < 2000 )
116 i_result = B_NO_INIT;
120 i_result = acquire_sem( p_mutex->lock );
123 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
124 # define vlc_assert_locked( m ) \
125 assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK)
127 i_result = pthread_mutex_lock( &p_mutex->mutex );
130 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
131 psz_error = strerror(i_result);
134 #elif defined( HAVE_CTHREADS_H )
135 mutex_lock( p_mutex->mutex );
142 msg_Err( p_mutex->p_this,
143 "thread %li: mutex_lock failed at %s:%d (%d:%s)",
144 i_thread, psz_file, i_line, i_result, psz_error );
149 #ifndef vlc_assert_locked
150 # define vlc_assert_locked( m ) (void)0
153 /*****************************************************************************
154 * vlc_mutex_unlock: unlock a mutex
155 *****************************************************************************/
156 #define vlc_mutex_unlock( P_MUTEX ) \
157 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
159 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
160 vlc_mutex_t *p_mutex )
163 /* In case of error : */
164 unsigned long int i_thread = 0;
165 const char * psz_error = "";
167 #if defined( PTH_INIT_IN_PTH_H )
168 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
170 #elif defined( ST_INIT_IN_ST_H )
171 i_result = st_mutex_unlock( p_mutex->mutex );
173 #elif defined( UNDER_CE )
174 LeaveCriticalSection( &p_mutex->csection );
177 #elif defined( WIN32 )
180 ReleaseMutex( p_mutex->mutex );
184 LeaveCriticalSection( &p_mutex->csection );
188 #elif defined( HAVE_KERNEL_SCHEDULER_H )
189 if( p_mutex == NULL )
191 i_result = B_BAD_VALUE;
193 else if( p_mutex->init < 2000 )
195 i_result = B_NO_INIT;
199 release_sem( p_mutex->lock );
203 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
204 i_result = pthread_mutex_unlock( &p_mutex->mutex );
207 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
208 psz_error = strerror(i_result);
211 #elif defined( HAVE_CTHREADS_H )
212 mutex_unlock( p_mutex );
219 msg_Err( p_mutex->p_this,
220 "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
221 i_thread, psz_file, i_line, i_result, psz_error );
227 /*****************************************************************************
228 * vlc_mutex_destroy: destroy a mutex
229 *****************************************************************************/
230 #define vlc_mutex_destroy( P_MUTEX ) \
231 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
233 /*****************************************************************************
234 * vlc_cond_init: initialize a condition
235 *****************************************************************************/
236 #define vlc_cond_init( P_THIS, P_COND ) \
237 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
239 /*****************************************************************************
240 * vlc_cond_signal: start a thread on condition completion
241 *****************************************************************************/
242 #define vlc_cond_signal( P_COND ) \
243 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
245 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
246 vlc_cond_t *p_condvar )
249 /* In case of error : */
250 unsigned long int i_thread = 0;
251 const char * psz_error = "";
253 #if defined( PTH_INIT_IN_PTH_H )
254 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
256 #elif defined( ST_INIT_IN_ST_H )
257 i_result = st_cond_signal( p_condvar->cond );
259 #elif defined( UNDER_CE )
260 PulseEvent( p_condvar->event );
263 #elif defined( WIN32 )
264 /* Release one waiting thread if one is available. */
265 /* For this trick to work properly, the vlc_cond_signal must be surrounded
266 * by a mutex. This will prevent another thread from stealing the signal */
267 if( !p_condvar->semaphore )
269 PulseEvent( p_condvar->event );
271 else if( p_condvar->i_win9x_cv == 1 )
273 /* Wait for the gate to be open */
274 WaitForSingleObject( p_condvar->event, INFINITE );
276 if( p_condvar->i_waiting_threads )
278 /* Using a semaphore exposes us to a race condition. It is
279 * possible for another thread to start waiting on the semaphore
280 * just after we signaled it and thus steal the signal.
281 * We have to prevent new threads from entering the cond_wait(). */
282 ResetEvent( p_condvar->event );
284 /* A semaphore is used here because Win9x doesn't have
285 * SignalObjectAndWait() and thus a race condition exists
286 * during the time we release the mutex and the time we start
287 * waiting on the event (more precisely, the signal can sometimes
288 * be missed by the waiting thread if we use PulseEvent()). */
289 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
294 if( p_condvar->i_waiting_threads )
296 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
298 /* Wait for the last thread to be awakened */
299 WaitForSingleObject( p_condvar->event, INFINITE );
304 #elif defined( HAVE_KERNEL_SCHEDULER_H )
305 if( p_condvar == NULL )
307 i_result = B_BAD_VALUE;
309 else if( p_condvar->init < 2000 )
311 i_result = B_NO_INIT;
315 while( p_condvar->thread != -1 )
318 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
323 if( info.state != B_THREAD_SUSPENDED )
325 /* The waiting thread is not suspended so it could
326 * have been interrupted beetwen the unlock and the
327 * suspend_thread line. That is why we sleep a little
328 * before retesting p_condver->thread. */
333 /* Ok, we have to wake up that thread */
334 resume_thread( p_condvar->thread );
341 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
342 i_result = pthread_cond_signal( &p_condvar->cond );
345 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
346 psz_error = strerror(i_result);
349 #elif defined( HAVE_CTHREADS_H )
350 /* condition_signal() */
351 if ( p_condvar->queue.head || p_condvar->implications )
353 cond_signal( (condition_t)p_condvar );
361 msg_Err( p_condvar->p_this,
362 "thread %li: cond_signal failed at %s:%d (%d:%s)",
363 i_thread, psz_file, i_line, i_result, psz_error );
369 /*****************************************************************************
370 * vlc_cond_wait: wait until condition completion
371 *****************************************************************************/
372 #define vlc_cond_wait( P_COND, P_MUTEX ) \
373 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
375 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
376 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
379 /* In case of error : */
380 unsigned long int i_thread = 0;
381 const char * psz_error = "";
383 #if defined( PTH_INIT_IN_PTH_H )
384 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
387 #elif defined( ST_INIT_IN_ST_H )
388 st_mutex_unlock( p_mutex->mutex );
389 i_result = st_cond_wait( p_condvar->cond );
390 st_mutex_lock( p_mutex->mutex );
392 #elif defined( UNDER_CE )
393 p_condvar->i_waiting_threads++;
394 LeaveCriticalSection( &p_mutex->csection );
395 WaitForSingleObject( p_condvar->event, INFINITE );
396 p_condvar->i_waiting_threads--;
398 /* Reacquire the mutex before returning. */
399 vlc_mutex_lock( p_mutex );
403 #elif defined( WIN32 )
404 if( !p_condvar->semaphore )
406 /* Increase our wait count */
407 p_condvar->i_waiting_threads++;
411 /* It is only possible to atomically release the mutex and
412 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
413 * SignalObjectAndWait(). */
414 p_condvar->SignalObjectAndWait( p_mutex->mutex,
420 LeaveCriticalSection( &p_mutex->csection );
421 WaitForSingleObject( p_condvar->event, INFINITE );
424 p_condvar->i_waiting_threads--;
426 else if( p_condvar->i_win9x_cv == 1 )
428 int i_waiting_threads;
430 /* Wait for the gate to be open */
431 WaitForSingleObject( p_condvar->event, INFINITE );
433 /* Increase our wait count */
434 p_condvar->i_waiting_threads++;
436 LeaveCriticalSection( &p_mutex->csection );
437 WaitForSingleObject( p_condvar->semaphore, INFINITE );
439 /* Decrement and test must be atomic */
440 EnterCriticalSection( &p_condvar->csection );
442 /* Decrease our wait count */
443 i_waiting_threads = --p_condvar->i_waiting_threads;
445 LeaveCriticalSection( &p_condvar->csection );
447 /* Reopen the gate if we were the last waiting thread */
448 if( !i_waiting_threads )
449 SetEvent( p_condvar->event );
453 int i_waiting_threads;
455 /* Increase our wait count */
456 p_condvar->i_waiting_threads++;
458 LeaveCriticalSection( &p_mutex->csection );
459 WaitForSingleObject( p_condvar->semaphore, INFINITE );
461 /* Decrement and test must be atomic */
462 EnterCriticalSection( &p_condvar->csection );
464 /* Decrease our wait count */
465 i_waiting_threads = --p_condvar->i_waiting_threads;
467 LeaveCriticalSection( &p_condvar->csection );
469 /* Signal that the last waiting thread just went through */
470 if( !i_waiting_threads )
471 SetEvent( p_condvar->event );
474 /* Reacquire the mutex before returning. */
475 vlc_mutex_lock( p_mutex );
479 #elif defined( HAVE_KERNEL_SCHEDULER_H )
480 if( p_condvar == NULL )
482 i_result = B_BAD_VALUE;
484 else if( p_mutex == NULL )
486 i_result = B_BAD_VALUE;
488 else if( p_condvar->init < 2000 )
490 i_result = B_NO_INIT;
493 /* The p_condvar->thread var is initialized before the unlock because
494 * it enables to identify when the thread is interrupted beetwen the
495 * unlock line and the suspend_thread line */
496 p_condvar->thread = find_thread( NULL );
497 vlc_mutex_unlock( p_mutex );
498 suspend_thread( p_condvar->thread );
499 p_condvar->thread = -1;
501 vlc_mutex_lock( p_mutex );
504 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
507 /* In debug mode, timeout */
508 struct timespec timeout;
510 # if defined (_POSIX_CLOCK_MONOTONIC) && (_POSIX_CLOCK_MONOTONIC >= 0)
511 if( clock_gettime( CLOCK_MONOTONIC, &timeout ) )
513 clock_gettime( CLOCK_REALTIME, &timeout );
515 timeout.tv_sec += THREAD_COND_TIMEOUT;
517 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
520 if( i_result == ETIMEDOUT )
522 msg_Dbg( p_condvar->p_this,
523 "thread %li: possible condition deadlock "
524 "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
525 psz_file, i_line, strerror(i_result) );
527 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
531 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
536 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
537 psz_error = strerror(i_result);
540 #elif defined( HAVE_CTHREADS_H )
541 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
548 msg_Err( p_condvar->p_this,
549 "thread %li: cond_wait failed at %s:%d (%d:%s)",
550 i_thread, psz_file, i_line, i_result, psz_error );
557 /*****************************************************************************
558 * vlc_cond_timedwait: wait until condition completion or expiration
559 *****************************************************************************/
560 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
561 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
563 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
564 vlc_cond_t *p_condvar,
565 vlc_mutex_t *p_mutex,
569 unsigned long int i_thread = 0;
570 const char * psz_error = "";
572 #if defined( PTH_INIT_IN_PTH_H )
573 # error Unimplemented
574 #elif defined( ST_INIT_IN_ST_H )
575 # error Unimplemented
576 #elif defined( UNDER_CE )
577 # error Unimplemented
578 #elif defined( WIN32 )
579 # error Unimplemented FIXME FIXME
581 #elif defined( HAVE_KERNEL_SCHEDULER_H )
582 # error Unimplemented
583 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
584 lldiv_t d = lldiv( deadline, 1000000 );
585 struct timespec ts = { d.quot, d.rem * 1000 };
587 i_res = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex, &ts );
588 if( i_res == ETIMEDOUT )
589 i_res = 0; /* this error is perfectly normal */
591 if ( i_res ) /* other errors = bug */
593 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
594 psz_error = strerror(i_res);
597 #elif defined( HAVE_CTHREADS_H )
598 # error Unimplemented
603 msg_Err( p_condvar->p_this,
604 "thread %li: cond_wait failed at %s:%d (%d:%s)",
605 i_thread, psz_file, i_line, i_res, psz_error );
611 /*****************************************************************************
612 * vlc_cond_destroy: destroy a condition
613 *****************************************************************************/
614 #define vlc_cond_destroy( P_COND ) \
615 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
617 /*****************************************************************************
618 * vlc_threadvar_create: create a thread-local variable
619 *****************************************************************************/
620 #define vlc_threadvar_create( PTHIS, P_TLS ) \
621 __vlc_threadvar_create( PTHIS, P_TLS )
623 /*****************************************************************************
624 * vlc_threadvar_set: create: set the value of a thread-local variable
625 *****************************************************************************/
626 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
630 #if defined( PTH_INIT_IN_PTH_H )
631 return pth_key_setdata( p_tls->handle, p_value );
632 #elif defined( ST_INIT_IN_ST_H )
633 return st_thread_setspecific( p_tls->handle, p_value );
634 #elif defined( HAVE_KERNEL_SCHEDULER_H )
637 #elif defined( UNDER_CE ) || defined( WIN32 )
638 i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
640 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
641 i_ret = pthread_setspecific( p_tls->handle, p_value );
643 #elif defined( HAVE_CTHREADS_H )
644 i_ret = cthread_setspecific( p_tls->handle, p_value );
650 /*****************************************************************************
651 * vlc_threadvar_get: create: get the value of a thread-local variable
652 *****************************************************************************/
653 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
657 #if defined( PTH_INIT_IN_PTH_H )
658 p_ret = pth_key_getdata( p_handle->key );
659 #elif defined( ST_INIT_IN_ST_H )
660 p_ret = st_thread_getspecific( p_handle->key );
661 #elif defined( HAVE_KERNEL_SCHEDULER_H )
663 #elif defined( UNDER_CE ) || defined( WIN32 )
664 p_ret = TlsGetValue( p_tls->handle );
666 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
667 p_ret = pthread_getspecific( p_tls->handle );
669 #elif defined( HAVE_CTHREADS_H )
670 if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
679 /*****************************************************************************
680 * vlc_thread_create: create a thread
681 *****************************************************************************/
682 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
683 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
685 /*****************************************************************************
686 * vlc_thread_set_priority: set the priority of the calling thread
687 *****************************************************************************/
688 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
689 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
691 /*****************************************************************************
692 * vlc_thread_ready: tell the parent thread we were successfully spawned
693 *****************************************************************************/
694 #define vlc_thread_ready( P_THIS ) \
695 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
697 /*****************************************************************************
698 * vlc_thread_join: wait until a thread exits
699 *****************************************************************************/
700 #define vlc_thread_join( P_THIS ) \
701 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )