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, 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_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 ) );
49 /*****************************************************************************
50 * vlc_threads_init: initialize threads system
51 *****************************************************************************/
52 #define vlc_threads_init( P_THIS ) \
53 __vlc_threads_init( VLC_OBJECT(P_THIS) )
55 /*****************************************************************************
56 * vlc_threads_end: deinitialize threads system
57 *****************************************************************************/
58 #define vlc_threads_end( P_THIS ) \
59 __vlc_threads_end( VLC_OBJECT(P_THIS) )
61 /*****************************************************************************
62 * vlc_mutex_init: initialize a mutex
63 *****************************************************************************/
64 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
65 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
67 /*****************************************************************************
68 * vlc_mutex_lock: lock a mutex
69 *****************************************************************************/
70 #define vlc_mutex_lock( P_MUTEX ) \
71 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
74 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
76 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
79 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
80 vlc_mutex_t * p_mutex )
83 /* In case of error : */
84 unsigned long int i_thread = 0;
85 const char * psz_error = "";
87 #if defined( PTH_INIT_IN_PTH_H )
88 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
90 #elif defined( ST_INIT_IN_ST_H )
91 i_result = st_mutex_lock( p_mutex->mutex );
93 #elif defined( UNDER_CE )
94 EnterCriticalSection( &p_mutex->csection );
97 #elif defined( WIN32 )
100 WaitForSingleObject( p_mutex->mutex, INFINITE );
104 EnterCriticalSection( &p_mutex->csection );
108 #elif defined( HAVE_KERNEL_SCHEDULER_H )
109 if( p_mutex == NULL )
111 i_result = B_BAD_VALUE;
113 else if( p_mutex->init < 2000 )
115 i_result = B_NO_INIT;
119 i_result = acquire_sem( p_mutex->lock );
122 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
123 # define vlc_assert_locked( m ) \
124 assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK)
126 i_result = pthread_mutex_lock( &p_mutex->mutex );
129 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
130 psz_error = strerror(i_result);
133 #elif defined( HAVE_CTHREADS_H )
134 mutex_lock( p_mutex->mutex );
141 msg_Err( p_mutex->p_this,
142 "thread %li: mutex_lock failed at %s:%d (%d:%s)",
143 i_thread, psz_file, i_line, i_result, psz_error );
148 #ifndef vlc_assert_locked
149 # define vlc_assert_locked( m ) (void)0
152 /*****************************************************************************
153 * vlc_mutex_unlock: unlock a mutex
154 *****************************************************************************/
155 #define vlc_mutex_unlock( P_MUTEX ) \
156 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
158 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
159 vlc_mutex_t *p_mutex )
162 /* In case of error : */
163 unsigned long int i_thread = 0;
164 const char * psz_error = "";
166 #if defined( PTH_INIT_IN_PTH_H )
167 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
169 #elif defined( ST_INIT_IN_ST_H )
170 i_result = st_mutex_unlock( p_mutex->mutex );
172 #elif defined( UNDER_CE )
173 LeaveCriticalSection( &p_mutex->csection );
176 #elif defined( WIN32 )
179 ReleaseMutex( p_mutex->mutex );
183 LeaveCriticalSection( &p_mutex->csection );
187 #elif defined( HAVE_KERNEL_SCHEDULER_H )
188 if( p_mutex == NULL )
190 i_result = B_BAD_VALUE;
192 else if( p_mutex->init < 2000 )
194 i_result = B_NO_INIT;
198 release_sem( p_mutex->lock );
202 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
203 i_result = pthread_mutex_unlock( &p_mutex->mutex );
206 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
207 psz_error = strerror(i_result);
210 #elif defined( HAVE_CTHREADS_H )
211 mutex_unlock( p_mutex );
218 msg_Err( p_mutex->p_this,
219 "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
220 i_thread, psz_file, i_line, i_result, psz_error );
226 /*****************************************************************************
227 * vlc_mutex_destroy: destroy a mutex
228 *****************************************************************************/
229 #define vlc_mutex_destroy( P_MUTEX ) \
230 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
232 /*****************************************************************************
233 * vlc_cond_init: initialize a condition
234 *****************************************************************************/
235 #define vlc_cond_init( P_THIS, P_COND ) \
236 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
238 /*****************************************************************************
239 * vlc_cond_signal: start a thread on condition completion
240 *****************************************************************************/
241 #define vlc_cond_signal( P_COND ) \
242 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
244 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
245 vlc_cond_t *p_condvar )
248 /* In case of error : */
249 unsigned long int i_thread = 0;
250 const char * psz_error = "";
252 #if defined( PTH_INIT_IN_PTH_H )
253 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
255 #elif defined( ST_INIT_IN_ST_H )
256 i_result = st_cond_signal( p_condvar->cond );
258 #elif defined( UNDER_CE )
259 PulseEvent( p_condvar->event );
262 #elif defined( WIN32 )
263 /* Release one waiting thread if one is available. */
264 /* For this trick to work properly, the vlc_cond_signal must be surrounded
265 * by a mutex. This will prevent another thread from stealing the signal */
266 if( !p_condvar->semaphore )
268 PulseEvent( p_condvar->event );
270 else if( p_condvar->i_win9x_cv == 1 )
272 /* Wait for the gate to be open */
273 WaitForSingleObject( p_condvar->event, INFINITE );
275 if( p_condvar->i_waiting_threads )
277 /* Using a semaphore exposes us to a race condition. It is
278 * possible for another thread to start waiting on the semaphore
279 * just after we signaled it and thus steal the signal.
280 * We have to prevent new threads from entering the cond_wait(). */
281 ResetEvent( p_condvar->event );
283 /* A semaphore is used here because Win9x doesn't have
284 * SignalObjectAndWait() and thus a race condition exists
285 * during the time we release the mutex and the time we start
286 * waiting on the event (more precisely, the signal can sometimes
287 * be missed by the waiting thread if we use PulseEvent()). */
288 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
293 if( p_condvar->i_waiting_threads )
295 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
297 /* Wait for the last thread to be awakened */
298 WaitForSingleObject( p_condvar->event, INFINITE );
303 #elif defined( HAVE_KERNEL_SCHEDULER_H )
304 if( p_condvar == NULL )
306 i_result = B_BAD_VALUE;
308 else if( p_condvar->init < 2000 )
310 i_result = B_NO_INIT;
314 while( p_condvar->thread != -1 )
317 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
322 if( info.state != B_THREAD_SUSPENDED )
324 /* The waiting thread is not suspended so it could
325 * have been interrupted beetwen the unlock and the
326 * suspend_thread line. That is why we sleep a little
327 * before retesting p_condver->thread. */
332 /* Ok, we have to wake up that thread */
333 resume_thread( p_condvar->thread );
340 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
341 i_result = pthread_cond_signal( &p_condvar->cond );
344 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
345 psz_error = strerror(i_result);
348 #elif defined( HAVE_CTHREADS_H )
349 /* condition_signal() */
350 if ( p_condvar->queue.head || p_condvar->implications )
352 cond_signal( (condition_t)p_condvar );
360 msg_Err( p_condvar->p_this,
361 "thread %li: cond_signal failed at %s:%d (%d:%s)",
362 i_thread, psz_file, i_line, i_result, psz_error );
368 /*****************************************************************************
369 * vlc_cond_wait: wait until condition completion
370 *****************************************************************************/
371 #define vlc_cond_wait( P_COND, P_MUTEX ) \
372 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
374 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
375 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
378 /* In case of error : */
379 unsigned long int i_thread = 0;
380 const char * psz_error = "";
382 #if defined( PTH_INIT_IN_PTH_H )
383 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
386 #elif defined( ST_INIT_IN_ST_H )
387 st_mutex_unlock( p_mutex->mutex );
388 i_result = st_cond_wait( p_condvar->cond );
389 st_mutex_lock( p_mutex->mutex );
391 #elif defined( UNDER_CE )
392 p_condvar->i_waiting_threads++;
393 LeaveCriticalSection( &p_mutex->csection );
394 WaitForSingleObject( p_condvar->event, INFINITE );
395 p_condvar->i_waiting_threads--;
397 /* Reacquire the mutex before returning. */
398 vlc_mutex_lock( p_mutex );
402 #elif defined( WIN32 )
403 if( !p_condvar->semaphore )
405 /* Increase our wait count */
406 p_condvar->i_waiting_threads++;
410 /* It is only possible to atomically release the mutex and
411 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
412 * SignalObjectAndWait(). */
413 p_condvar->SignalObjectAndWait( p_mutex->mutex,
419 LeaveCriticalSection( &p_mutex->csection );
420 WaitForSingleObject( p_condvar->event, INFINITE );
423 p_condvar->i_waiting_threads--;
425 else if( p_condvar->i_win9x_cv == 1 )
427 int i_waiting_threads;
429 /* Wait for the gate to be open */
430 WaitForSingleObject( p_condvar->event, INFINITE );
432 /* Increase our wait count */
433 p_condvar->i_waiting_threads++;
435 LeaveCriticalSection( &p_mutex->csection );
436 WaitForSingleObject( p_condvar->semaphore, INFINITE );
438 /* Decrement and test must be atomic */
439 EnterCriticalSection( &p_condvar->csection );
441 /* Decrease our wait count */
442 i_waiting_threads = --p_condvar->i_waiting_threads;
444 LeaveCriticalSection( &p_condvar->csection );
446 /* Reopen the gate if we were the last waiting thread */
447 if( !i_waiting_threads )
448 SetEvent( p_condvar->event );
452 int i_waiting_threads;
454 /* Increase our wait count */
455 p_condvar->i_waiting_threads++;
457 LeaveCriticalSection( &p_mutex->csection );
458 WaitForSingleObject( p_condvar->semaphore, INFINITE );
460 /* Decrement and test must be atomic */
461 EnterCriticalSection( &p_condvar->csection );
463 /* Decrease our wait count */
464 i_waiting_threads = --p_condvar->i_waiting_threads;
466 LeaveCriticalSection( &p_condvar->csection );
468 /* Signal that the last waiting thread just went through */
469 if( !i_waiting_threads )
470 SetEvent( p_condvar->event );
473 /* Reacquire the mutex before returning. */
474 vlc_mutex_lock( p_mutex );
478 #elif defined( HAVE_KERNEL_SCHEDULER_H )
479 if( p_condvar == NULL )
481 i_result = B_BAD_VALUE;
483 else if( p_mutex == NULL )
485 i_result = B_BAD_VALUE;
487 else if( p_condvar->init < 2000 )
489 i_result = B_NO_INIT;
492 /* The p_condvar->thread var is initialized before the unlock because
493 * it enables to identify when the thread is interrupted beetwen the
494 * unlock line and the suspend_thread line */
495 p_condvar->thread = find_thread( NULL );
496 vlc_mutex_unlock( p_mutex );
497 suspend_thread( p_condvar->thread );
498 p_condvar->thread = -1;
500 vlc_mutex_lock( p_mutex );
503 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
506 /* In debug mode, timeout */
508 struct timespec timeout;
510 gettimeofday( &now, NULL );
511 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
512 timeout.tv_nsec = now.tv_usec * 1000;
514 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
517 if( i_result == ETIMEDOUT )
519 msg_Dbg( p_condvar->p_this,
520 "thread %li: possible condition deadlock "
521 "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
522 psz_file, i_line, strerror(i_result) );
524 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
528 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
533 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
534 psz_error = strerror(i_result);
537 #elif defined( HAVE_CTHREADS_H )
538 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
545 msg_Err( p_condvar->p_this,
546 "thread %li: cond_wait failed at %s:%d (%d:%s)",
547 i_thread, psz_file, i_line, i_result, psz_error );
553 /*****************************************************************************
554 * vlc_cond_destroy: destroy a condition
555 *****************************************************************************/
556 #define vlc_cond_destroy( P_COND ) \
557 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
559 /*****************************************************************************
560 * vlc_threadvar_create: create a thread-local variable
561 *****************************************************************************/
562 #define vlc_threadvar_create( PTHIS, P_TLS ) \
563 __vlc_threadvar_create( PTHIS, P_TLS )
565 /*****************************************************************************
566 * vlc_threadvar_set: create: set the value of a thread-local variable
567 *****************************************************************************/
568 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
572 #if defined( PTH_INIT_IN_PTH_H )
573 return pth_key_setdata( p_tls->handle, p_value );
574 #elif defined( ST_INIT_IN_ST_H )
575 return st_thread_setspecific( p_tls->handle, p_value );
576 #elif defined( HAVE_KERNEL_SCHEDULER_H )
579 #elif defined( UNDER_CE ) || defined( WIN32 )
580 i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
582 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
583 i_ret = pthread_setspecific( p_tls->handle, p_value );
585 #elif defined( HAVE_CTHREADS_H )
586 i_ret = cthread_setspecific( p_tls->handle, p_value );
592 /*****************************************************************************
593 * vlc_threadvar_get: create: get the value of a thread-local variable
594 *****************************************************************************/
595 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
599 #if defined( PTH_INIT_IN_PTH_H )
600 p_ret = pth_key_getdata( p_handle->key );
601 #elif defined( ST_INIT_IN_ST_H )
602 p_ret = st_thread_getspecific( p_handle->key );
603 #elif defined( HAVE_KERNEL_SCHEDULER_H )
605 #elif defined( UNDER_CE ) || defined( WIN32 )
606 p_ret = TlsGetValue( p_tls->handle );
608 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
609 p_ret = pthread_getspecific( p_tls->handle );
611 #elif defined( HAVE_CTHREADS_H )
612 if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
621 /*****************************************************************************
622 * vlc_thread_create: create a thread
623 *****************************************************************************/
624 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
625 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
627 /*****************************************************************************
628 * vlc_thread_set_priority: set the priority of the calling thread
629 *****************************************************************************/
630 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
631 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
633 /*****************************************************************************
634 * vlc_thread_ready: tell the parent thread we were successfully spawned
635 *****************************************************************************/
636 #define vlc_thread_ready( P_THIS ) \
637 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
639 /*****************************************************************************
640 * vlc_thread_join: wait until a thread exits
641 *****************************************************************************/
642 #define vlc_thread_join( P_THIS ) \
643 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )