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_threads_init, ( vlc_object_t * ) );
39 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
40 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
41 VLC_EXPORT( int, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
42 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
43 VLC_EXPORT( int, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
44 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_object_t *, vlc_threadvar_t * ) );
45 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
46 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
47 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
48 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
51 /*****************************************************************************
52 * vlc_threads_init: initialize threads system
53 *****************************************************************************/
54 #define vlc_threads_init( P_THIS ) \
55 __vlc_threads_init( VLC_OBJECT(P_THIS) )
57 /*****************************************************************************
58 * vlc_threads_end: deinitialize threads system
59 *****************************************************************************/
60 #define vlc_threads_end( P_THIS ) \
61 __vlc_threads_end( VLC_OBJECT(P_THIS) )
63 /*****************************************************************************
64 * vlc_mutex_init: initialize a mutex
65 *****************************************************************************/
66 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
67 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
69 /*****************************************************************************
70 * vlc_mutex_lock: lock a mutex
71 *****************************************************************************/
72 #define vlc_mutex_lock( P_MUTEX ) \
73 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
76 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
78 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
81 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
82 vlc_mutex_t * p_mutex )
85 /* In case of error : */
86 unsigned long int i_thread = 0;
87 const char * psz_error = "";
89 #if defined( PTH_INIT_IN_PTH_H )
90 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
92 #elif defined( ST_INIT_IN_ST_H )
93 i_result = st_mutex_lock( p_mutex->mutex );
95 #elif defined( UNDER_CE )
96 EnterCriticalSection( &p_mutex->csection );
99 #elif defined( WIN32 )
102 WaitForSingleObject( p_mutex->mutex, INFINITE );
106 EnterCriticalSection( &p_mutex->csection );
110 #elif defined( HAVE_KERNEL_SCHEDULER_H )
111 if( p_mutex == NULL )
113 i_result = B_BAD_VALUE;
115 else if( p_mutex->init < 2000 )
117 i_result = B_NO_INIT;
121 i_result = acquire_sem( p_mutex->lock );
124 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
125 i_result = pthread_mutex_lock( &p_mutex->mutex );
128 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
129 psz_error = strerror(i_result);
132 #elif defined( HAVE_CTHREADS_H )
133 mutex_lock( p_mutex->mutex );
140 msg_Err( p_mutex->p_this,
141 "thread %li: mutex_lock failed at %s:%d (%d:%s)",
142 i_thread, psz_file, i_line, i_result, psz_error );
147 /*****************************************************************************
148 * vlc_mutex_unlock: unlock a mutex
149 *****************************************************************************/
150 #define vlc_mutex_unlock( P_MUTEX ) \
151 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
153 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
154 vlc_mutex_t *p_mutex )
157 /* In case of error : */
158 unsigned long int i_thread = 0;
159 const char * psz_error = "";
161 #if defined( PTH_INIT_IN_PTH_H )
162 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
164 #elif defined( ST_INIT_IN_ST_H )
165 i_result = st_mutex_unlock( p_mutex->mutex );
167 #elif defined( UNDER_CE )
168 LeaveCriticalSection( &p_mutex->csection );
171 #elif defined( WIN32 )
174 ReleaseMutex( p_mutex->mutex );
178 LeaveCriticalSection( &p_mutex->csection );
182 #elif defined( HAVE_KERNEL_SCHEDULER_H )
183 if( p_mutex == NULL )
185 i_result = B_BAD_VALUE;
187 else if( p_mutex->init < 2000 )
189 i_result = B_NO_INIT;
193 release_sem( p_mutex->lock );
197 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
198 i_result = pthread_mutex_unlock( &p_mutex->mutex );
201 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
202 psz_error = strerror(i_result);
205 #elif defined( HAVE_CTHREADS_H )
206 mutex_unlock( p_mutex );
213 msg_Err( p_mutex->p_this,
214 "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
215 i_thread, psz_file, i_line, i_result, psz_error );
221 /*****************************************************************************
222 * vlc_mutex_destroy: destroy a mutex
223 *****************************************************************************/
224 #define vlc_mutex_destroy( P_MUTEX ) \
225 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
227 /*****************************************************************************
228 * vlc_cond_init: initialize a condition
229 *****************************************************************************/
230 #define vlc_cond_init( P_THIS, P_COND ) \
231 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
233 /*****************************************************************************
234 * vlc_cond_signal: start a thread on condition completion
235 *****************************************************************************/
236 #define vlc_cond_signal( P_COND ) \
237 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
239 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
240 vlc_cond_t *p_condvar )
243 /* In case of error : */
244 unsigned long int i_thread = 0;
245 const char * psz_error = "";
247 #if defined( PTH_INIT_IN_PTH_H )
248 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
250 #elif defined( ST_INIT_IN_ST_H )
251 i_result = st_cond_signal( p_condvar->cond );
253 #elif defined( UNDER_CE )
254 PulseEvent( p_condvar->event );
257 #elif defined( WIN32 )
258 /* Release one waiting thread if one is available. */
259 /* For this trick to work properly, the vlc_cond_signal must be surrounded
260 * by a mutex. This will prevent another thread from stealing the signal */
261 if( !p_condvar->semaphore )
263 PulseEvent( p_condvar->event );
265 else if( p_condvar->i_win9x_cv == 1 )
267 /* Wait for the gate to be open */
268 WaitForSingleObject( p_condvar->event, INFINITE );
270 if( p_condvar->i_waiting_threads )
272 /* Using a semaphore exposes us to a race condition. It is
273 * possible for another thread to start waiting on the semaphore
274 * just after we signaled it and thus steal the signal.
275 * We have to prevent new threads from entering the cond_wait(). */
276 ResetEvent( p_condvar->event );
278 /* A semaphore is used here because Win9x doesn't have
279 * SignalObjectAndWait() and thus a race condition exists
280 * during the time we release the mutex and the time we start
281 * waiting on the event (more precisely, the signal can sometimes
282 * be missed by the waiting thread if we use PulseEvent()). */
283 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
288 if( p_condvar->i_waiting_threads )
290 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
292 /* Wait for the last thread to be awakened */
293 WaitForSingleObject( p_condvar->event, INFINITE );
298 #elif defined( HAVE_KERNEL_SCHEDULER_H )
299 if( p_condvar == NULL )
301 i_result = B_BAD_VALUE;
303 else if( p_condvar->init < 2000 )
305 i_result = B_NO_INIT;
309 while( p_condvar->thread != -1 )
312 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
317 if( info.state != B_THREAD_SUSPENDED )
319 /* The waiting thread is not suspended so it could
320 * have been interrupted beetwen the unlock and the
321 * suspend_thread line. That is why we sleep a little
322 * before retesting p_condver->thread. */
327 /* Ok, we have to wake up that thread */
328 resume_thread( p_condvar->thread );
335 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
336 i_result = pthread_cond_signal( &p_condvar->cond );
339 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
340 psz_error = strerror(i_result);
343 #elif defined( HAVE_CTHREADS_H )
344 /* condition_signal() */
345 if ( p_condvar->queue.head || p_condvar->implications )
347 cond_signal( (condition_t)p_condvar );
355 msg_Err( p_condvar->p_this,
356 "thread %li: cond_signal failed at %s:%d (%d:%s)",
357 i_thread, psz_file, i_line, i_result, psz_error );
363 /*****************************************************************************
364 * vlc_cond_wait: wait until condition completion
365 *****************************************************************************/
366 #define vlc_cond_wait( P_COND, P_MUTEX ) \
367 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
369 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
370 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
373 /* In case of error : */
374 unsigned long int i_thread = 0;
375 const char * psz_error = "";
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 */
503 struct timespec timeout;
505 gettimeofday( &now, NULL );
506 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
507 timeout.tv_nsec = now.tv_usec * 1000;
509 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
512 if( i_result == ETIMEDOUT )
514 msg_Dbg( p_condvar->p_this,
515 "thread %li: possible condition deadlock "
516 "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
517 psz_file, i_line, strerror(i_result) );
519 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
523 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
528 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
529 psz_error = strerror(i_result);
532 #elif defined( HAVE_CTHREADS_H )
533 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
540 msg_Err( p_condvar->p_this,
541 "thread %li: cond_wait failed at %s:%d (%d:%s)",
542 i_thread, psz_file, i_line, i_result, psz_error );
548 /*****************************************************************************
549 * vlc_cond_destroy: destroy a condition
550 *****************************************************************************/
551 #define vlc_cond_destroy( P_COND ) \
552 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
554 /*****************************************************************************
555 * vlc_threadvar_create: create a thread-local variable
556 *****************************************************************************/
557 #define vlc_threadvar_create( PTHIS, P_TLS ) \
558 __vlc_threadvar_create( PTHIS, P_TLS )
560 /*****************************************************************************
561 * vlc_threadvar_set: create: set the value of a thread-local variable
562 *****************************************************************************/
563 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
567 #if defined( PTH_INIT_IN_PTH_H )
568 return pth_key_setdata( p_tls->handle, p_value );
569 #elif defined( ST_INIT_IN_ST_H )
570 return st_thread_setspecific( p_tls->handle, p_value );
571 #elif defined( HAVE_KERNEL_SCHEDULER_H )
574 #elif defined( UNDER_CE ) || defined( WIN32 )
575 i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
577 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
578 i_ret = pthread_setspecific( p_tls->handle, p_value );
580 #elif defined( HAVE_CTHREADS_H )
581 i_ret = cthread_setspecific( p_tls->handle, p_value );
587 /*****************************************************************************
588 * vlc_threadvar_get: create: get the value of a thread-local variable
589 *****************************************************************************/
590 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
594 #if defined( PTH_INIT_IN_PTH_H )
595 p_ret = pth_key_getdata( p_handle->key );
596 #elif defined( ST_INIT_IN_ST_H )
597 p_ret = st_thread_getspecific( p_handle->key );
598 #elif defined( HAVE_KERNEL_SCHEDULER_H )
600 #elif defined( UNDER_CE ) || defined( WIN32 )
601 p_ret = TlsGetValue( p_tls->handle );
603 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
604 p_ret = pthread_getspecific( p_tls->handle );
606 #elif defined( HAVE_CTHREADS_H )
607 if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
616 /*****************************************************************************
617 * vlc_thread_create: create a thread
618 *****************************************************************************/
619 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
620 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
622 /*****************************************************************************
623 * vlc_thread_set_priority: set the priority of the calling thread
624 *****************************************************************************/
625 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
626 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
628 /*****************************************************************************
629 * vlc_thread_ready: tell the parent thread we were successfully spawned
630 *****************************************************************************/
631 #define vlc_thread_ready( P_THIS ) \
632 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
634 /*****************************************************************************
635 * vlc_thread_join: wait until a thread exits
636 *****************************************************************************/
637 #define vlc_thread_join( P_THIS ) \
638 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )