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 /*****************************************************************************
33 * Function definitions
34 *****************************************************************************/
35 VLC_EXPORT( int, __vlc_threads_init, ( vlc_object_t * ) );
36 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
37 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
38 VLC_EXPORT( int, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
39 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
40 VLC_EXPORT( int, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
41 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_object_t *, vlc_threadvar_t * ) );
42 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
43 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
44 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
45 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;
84 const char * psz_error = "";
86 #if defined( PTH_INIT_IN_PTH_H )
87 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
89 #elif defined( ST_INIT_IN_ST_H )
90 i_result = st_mutex_lock( p_mutex->mutex );
92 #elif defined( UNDER_CE )
93 EnterCriticalSection( &p_mutex->csection );
96 #elif defined( WIN32 )
99 WaitForSingleObject( p_mutex->mutex, INFINITE );
103 EnterCriticalSection( &p_mutex->csection );
107 #elif defined( HAVE_KERNEL_SCHEDULER_H )
108 if( p_mutex == NULL )
110 i_result = B_BAD_VALUE;
112 else if( p_mutex->init < 2000 )
114 i_result = B_NO_INIT;
118 i_result = acquire_sem( p_mutex->lock );
121 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
122 i_result = pthread_mutex_lock( &p_mutex->mutex );
125 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
126 psz_error = strerror(i_result);
129 #elif defined( HAVE_CTHREADS_H )
130 mutex_lock( p_mutex->mutex );
137 msg_Err( p_mutex->p_this,
138 "thread %li: mutex_lock failed at %s:%d (%d:%s)",
139 i_thread, psz_file, i_line, i_result, psz_error );
144 /*****************************************************************************
145 * vlc_mutex_unlock: unlock a mutex
146 *****************************************************************************/
147 #define vlc_mutex_unlock( P_MUTEX ) \
148 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
150 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
151 vlc_mutex_t *p_mutex )
154 /* In case of error : */
155 unsigned long int i_thread = 0;
156 const char * psz_error = "";
158 #if defined( PTH_INIT_IN_PTH_H )
159 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
161 #elif defined( ST_INIT_IN_ST_H )
162 i_result = st_mutex_unlock( p_mutex->mutex );
164 #elif defined( UNDER_CE )
165 LeaveCriticalSection( &p_mutex->csection );
168 #elif defined( WIN32 )
171 ReleaseMutex( p_mutex->mutex );
175 LeaveCriticalSection( &p_mutex->csection );
179 #elif defined( HAVE_KERNEL_SCHEDULER_H )
180 if( p_mutex == NULL )
182 i_result = B_BAD_VALUE;
184 else if( p_mutex->init < 2000 )
186 i_result = B_NO_INIT;
190 release_sem( p_mutex->lock );
194 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
195 i_result = pthread_mutex_unlock( &p_mutex->mutex );
198 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
199 psz_error = strerror(i_result);
202 #elif defined( HAVE_CTHREADS_H )
203 mutex_unlock( p_mutex );
210 msg_Err( p_mutex->p_this,
211 "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
212 i_thread, psz_file, i_line, i_result, psz_error );
218 /*****************************************************************************
219 * vlc_mutex_destroy: destroy a mutex
220 *****************************************************************************/
221 #define vlc_mutex_destroy( P_MUTEX ) \
222 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
224 /*****************************************************************************
225 * vlc_cond_init: initialize a condition
226 *****************************************************************************/
227 #define vlc_cond_init( P_THIS, P_COND ) \
228 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
230 /*****************************************************************************
231 * vlc_cond_signal: start a thread on condition completion
232 *****************************************************************************/
233 #define vlc_cond_signal( P_COND ) \
234 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
236 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
237 vlc_cond_t *p_condvar )
240 /* In case of error : */
241 unsigned long int i_thread = 0;
242 const char * psz_error = "";
244 #if defined( PTH_INIT_IN_PTH_H )
245 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
247 #elif defined( ST_INIT_IN_ST_H )
248 i_result = st_cond_signal( p_condvar->cond );
250 #elif defined( UNDER_CE )
251 PulseEvent( p_condvar->event );
254 #elif defined( WIN32 )
255 /* Release one waiting thread if one is available. */
256 /* For this trick to work properly, the vlc_cond_signal must be surrounded
257 * by a mutex. This will prevent another thread from stealing the signal */
258 if( !p_condvar->semaphore )
260 PulseEvent( p_condvar->event );
262 else if( p_condvar->i_win9x_cv == 1 )
264 /* Wait for the gate to be open */
265 WaitForSingleObject( p_condvar->event, INFINITE );
267 if( p_condvar->i_waiting_threads )
269 /* Using a semaphore exposes us to a race condition. It is
270 * possible for another thread to start waiting on the semaphore
271 * just after we signaled it and thus steal the signal.
272 * We have to prevent new threads from entering the cond_wait(). */
273 ResetEvent( p_condvar->event );
275 /* A semaphore is used here because Win9x doesn't have
276 * SignalObjectAndWait() and thus a race condition exists
277 * during the time we release the mutex and the time we start
278 * waiting on the event (more precisely, the signal can sometimes
279 * be missed by the waiting thread if we use PulseEvent()). */
280 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
285 if( p_condvar->i_waiting_threads )
287 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
289 /* Wait for the last thread to be awakened */
290 WaitForSingleObject( p_condvar->event, INFINITE );
295 #elif defined( HAVE_KERNEL_SCHEDULER_H )
296 if( p_condvar == NULL )
298 i_result = B_BAD_VALUE;
300 else if( p_condvar->init < 2000 )
302 i_result = B_NO_INIT;
306 while( p_condvar->thread != -1 )
309 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
314 if( info.state != B_THREAD_SUSPENDED )
316 /* The waiting thread is not suspended so it could
317 * have been interrupted beetwen the unlock and the
318 * suspend_thread line. That is why we sleep a little
319 * before retesting p_condver->thread. */
324 /* Ok, we have to wake up that thread */
325 resume_thread( p_condvar->thread );
332 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
333 i_result = pthread_cond_signal( &p_condvar->cond );
336 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
337 psz_error = strerror(i_result);
340 #elif defined( HAVE_CTHREADS_H )
341 /* condition_signal() */
342 if ( p_condvar->queue.head || p_condvar->implications )
344 cond_signal( (condition_t)p_condvar );
352 msg_Err( p_condvar->p_this,
353 "thread %li: cond_signal failed at %s:%d (%d:%s)",
354 i_thread, psz_file, i_line, i_result, psz_error );
360 /*****************************************************************************
361 * vlc_cond_wait: wait until condition completion
362 *****************************************************************************/
363 #define vlc_cond_wait( P_COND, P_MUTEX ) \
364 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
366 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
367 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
370 /* In case of error : */
371 unsigned long int i_thread = 0;
372 const char * psz_error = "";
374 #if defined( PTH_INIT_IN_PTH_H )
375 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
378 #elif defined( ST_INIT_IN_ST_H )
379 st_mutex_unlock( p_mutex->mutex );
380 i_result = st_cond_wait( p_condvar->cond );
381 st_mutex_lock( p_mutex->mutex );
383 #elif defined( UNDER_CE )
384 p_condvar->i_waiting_threads++;
385 LeaveCriticalSection( &p_mutex->csection );
386 WaitForSingleObject( p_condvar->event, INFINITE );
387 p_condvar->i_waiting_threads--;
389 /* Reacquire the mutex before returning. */
390 vlc_mutex_lock( p_mutex );
394 #elif defined( WIN32 )
395 if( !p_condvar->semaphore )
397 /* Increase our wait count */
398 p_condvar->i_waiting_threads++;
402 /* It is only possible to atomically release the mutex and
403 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
404 * SignalObjectAndWait(). */
405 p_condvar->SignalObjectAndWait( p_mutex->mutex,
411 LeaveCriticalSection( &p_mutex->csection );
412 WaitForSingleObject( p_condvar->event, INFINITE );
415 p_condvar->i_waiting_threads--;
417 else if( p_condvar->i_win9x_cv == 1 )
419 int i_waiting_threads;
421 /* Wait for the gate to be open */
422 WaitForSingleObject( p_condvar->event, INFINITE );
424 /* Increase our wait count */
425 p_condvar->i_waiting_threads++;
427 LeaveCriticalSection( &p_mutex->csection );
428 WaitForSingleObject( p_condvar->semaphore, INFINITE );
430 /* Decrement and test must be atomic */
431 EnterCriticalSection( &p_condvar->csection );
433 /* Decrease our wait count */
434 i_waiting_threads = --p_condvar->i_waiting_threads;
436 LeaveCriticalSection( &p_condvar->csection );
438 /* Reopen the gate if we were the last waiting thread */
439 if( !i_waiting_threads )
440 SetEvent( p_condvar->event );
444 int i_waiting_threads;
446 /* Increase our wait count */
447 p_condvar->i_waiting_threads++;
449 LeaveCriticalSection( &p_mutex->csection );
450 WaitForSingleObject( p_condvar->semaphore, INFINITE );
452 /* Decrement and test must be atomic */
453 EnterCriticalSection( &p_condvar->csection );
455 /* Decrease our wait count */
456 i_waiting_threads = --p_condvar->i_waiting_threads;
458 LeaveCriticalSection( &p_condvar->csection );
460 /* Signal that the last waiting thread just went through */
461 if( !i_waiting_threads )
462 SetEvent( p_condvar->event );
465 /* Reacquire the mutex before returning. */
466 vlc_mutex_lock( p_mutex );
470 #elif defined( HAVE_KERNEL_SCHEDULER_H )
471 if( p_condvar == NULL )
473 i_result = B_BAD_VALUE;
475 else if( p_mutex == NULL )
477 i_result = B_BAD_VALUE;
479 else if( p_condvar->init < 2000 )
481 i_result = B_NO_INIT;
484 /* The p_condvar->thread var is initialized before the unlock because
485 * it enables to identify when the thread is interrupted beetwen the
486 * unlock line and the suspend_thread line */
487 p_condvar->thread = find_thread( NULL );
488 vlc_mutex_unlock( p_mutex );
489 suspend_thread( p_condvar->thread );
490 p_condvar->thread = -1;
492 vlc_mutex_lock( p_mutex );
495 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
498 /* In debug mode, timeout */
500 struct timespec timeout;
502 gettimeofday( &now, NULL );
503 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
504 timeout.tv_nsec = now.tv_usec * 1000;
506 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
509 if( i_result == ETIMEDOUT )
511 msg_Dbg( p_condvar->p_this,
512 "thread %li: possible condition deadlock "
513 "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
514 psz_file, i_line, strerror(i_result) );
516 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
520 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
525 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
526 psz_error = strerror(i_result);
529 #elif defined( HAVE_CTHREADS_H )
530 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
537 msg_Err( p_condvar->p_this,
538 "thread %li: cond_wait failed at %s:%d (%d:%s)",
539 i_thread, psz_file, i_line, i_result, psz_error );
545 /*****************************************************************************
546 * vlc_cond_destroy: destroy a condition
547 *****************************************************************************/
548 #define vlc_cond_destroy( P_COND ) \
549 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
551 /*****************************************************************************
552 * vlc_threadvar_create: create a thread-local variable
553 *****************************************************************************/
554 #define vlc_threadvar_create( PTHIS, P_TLS ) \
555 __vlc_threadvar_create( PTHIS, P_TLS )
557 /*****************************************************************************
558 * vlc_threadvar_set: create: set the value of a thread-local variable
559 *****************************************************************************/
560 #define vlc_threadvar_set( P_TLS , P_VAL ) \
561 __vlc_threadvar_set( __FILE__, __LINE__, P_TLS, P_VAL )
563 static inline int __vlc_threadvar_set( char* psz_file, int line,
564 vlc_threadvar_t * p_tls, void *p_value )
568 #if defined( PTH_INIT_IN_PTH_H )
569 return pth_key_setdata( p_tls->handle, p_value );
570 #elif defined( ST_INIT_IN_ST_H )
571 return st_thread_setspecific( p_tls->handle, p_value );
572 #elif defined( HAVE_KERNEL_SCHEDULER_H )
575 #elif defined( UNDER_CE ) || defined( WIN32 )
576 i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
578 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
579 i_ret = pthread_setspecific( p_tls->handle, p_value );
581 #elif defined( HAVE_CTHREADS_H )
582 i_ret = cthread_setspecific( p_tls->handle, p_value );
588 /*****************************************************************************
589 * vlc_threadvar_get: create: get the value of a thread-local variable
590 *****************************************************************************/
591 #define vlc_threadvar_get( P_TLS ) \
592 __vlc_threadvar_get( __FILE__, __LINE__, P_TLS )
594 static inline void* __vlc_threadvar_get( char* psz_file, int line,
595 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__ )