1 /*****************************************************************************
2 * threads.h : threads implementation for the VideoLAN client
3 * This header provides a portable threads implementation.
4 *****************************************************************************
5 * Copyright (C) 1999, 2000 VideoLAN
6 * $Id: vlc_threads.h,v 1.6 2002/07/29 19:05:47 gbazin Exp $
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
29 #if defined(GPROF) || defined(DEBUG)
30 # include <sys/time.h>
33 #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
36 #elif defined( ST_INIT_IN_ST_H ) /* State threads */
39 #elif defined( WIN32 ) /* Win32 API */
42 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) /* pthreads (like Linux & BSD) */
45 /* Needed for pthread_cond_timedwait */
48 /* This is not prototyped under Linux, though it exists. */
49 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
51 #elif defined( HAVE_CTHREADS_H ) /* GNUMach */
52 # include <cthreads.h>
54 #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
55 # include <kernel/OS.h>
56 # include <kernel/scheduler.h>
57 # include <byteorder.h>
60 # error no threads available on your system !
64 /*****************************************************************************
66 *****************************************************************************
67 * These constants are used by all threads in *_CreateThread() and
68 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
69 * value is used as a shared flag to represent the status of the thread.
70 *****************************************************************************/
72 /* Void status - this value can be used to make sure no operation is currently
73 * in progress on the concerned thread in an array of recorded threads */
74 #define THREAD_NOP 0 /* nothing happened */
77 #define THREAD_CREATE 10 /* thread is initializing */
78 #define THREAD_START 11 /* thread has forked */
79 #define THREAD_READY 19 /* thread is ready */
81 /* Destructions status */
82 #define THREAD_DESTROY 20 /* destruction order has been sent */
83 #define THREAD_END 21 /* destruction order has been received */
84 #define THREAD_OVER 29 /* thread does not exist any more */
87 #define THREAD_ERROR 30 /* an error occured */
88 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
90 /*****************************************************************************
92 *****************************************************************************/
94 #if defined( PTH_INIT_IN_PTH_H )
95 typedef pth_t vlc_thread_t;
96 typedef pth_mutex_t vlc_mutex_t;
97 typedef pth_cond_t vlc_cond_t;
99 #elif defined( ST_INIT_IN_ST_H )
100 typedef st_thread_t * vlc_thread_t;
101 typedef st_mutex_t * vlc_mutex_t;
102 typedef st_cond_t * vlc_cond_t;
104 #elif defined( WIN32 )
105 typedef HANDLE vlc_thread_t;
106 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
107 typedef unsigned (__stdcall *PTHREAD_START) (void *);
111 /* WinNT/2K/XP implementation */
113 /* Win95/98/ME implementation */
114 CRITICAL_SECTION csection;
119 volatile int i_waiting_threads;
120 /* WinNT/2K/XP implementation */
122 SIGNALOBJECTANDWAIT SignalObjectAndWait;
123 /* Win95/98/ME implementation */
125 CRITICAL_SECTION csection;
129 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
130 typedef pthread_t vlc_thread_t;
131 typedef pthread_mutex_t vlc_mutex_t;
132 typedef pthread_cond_t vlc_cond_t;
134 #elif defined( HAVE_CTHREADS_H )
135 typedef cthread_t vlc_thread_t;
137 /* Those structs are the ones defined in /include/cthreads.h but we need
138 * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
139 * foo is a (mutex_t*) */
145 struct cthread_queue queue;
151 struct cthread_queue queue;
153 struct cond_imp *implications;
156 #elif defined( HAVE_KERNEL_SCHEDULER_H )
157 /* This is the BeOS implementation of the vlc threads, note that the mutex is
158 * not a real mutex and the cond_var is not like a pthread cond_var but it is
159 * enough for what wee need */
161 typedef thread_id vlc_thread_t;
177 /*****************************************************************************
178 * Function definitions
179 *****************************************************************************/
180 VLC_EXPORT( int, __vlc_threads_init, ( vlc_object_t * ) );
181 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
182 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
183 VLC_EXPORT( int, __vlc_mutex_destroy, ( char *, int, vlc_mutex_t * ) );
184 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
185 VLC_EXPORT( int, __vlc_cond_destroy, ( char *, int, vlc_cond_t * ) );
186 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, char *, int, char *, void * ( * ) ( void * ), vlc_bool_t ) );
187 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
188 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, char *, int ) );
190 /*****************************************************************************
191 * vlc_threads_init: initialize threads system
192 *****************************************************************************/
193 #define vlc_threads_init( P_THIS ) \
194 __vlc_threads_init( CAST_TO_VLC_OBJECT(P_THIS) )
196 /*****************************************************************************
197 * vlc_threads_end: deinitialize threads system
198 *****************************************************************************/
199 #define vlc_threads_end( P_THIS ) \
200 __vlc_threads_end( CAST_TO_VLC_OBJECT(P_THIS) )
202 /*****************************************************************************
203 * vlc_mutex_init: initialize a mutex
204 *****************************************************************************/
205 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
206 __vlc_mutex_init( CAST_TO_VLC_OBJECT(P_THIS), P_MUTEX )
208 /*****************************************************************************
209 * vlc_mutex_lock: lock a mutex
210 *****************************************************************************/
212 # define vlc_mutex_lock( P_MUTEX ) \
213 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
215 # define vlc_mutex_lock( P_MUTEX ) \
216 __vlc_mutex_lock( "(unknown)", 0, P_MUTEX )
219 static inline int __vlc_mutex_lock( char * psz_file, int i_line,
220 vlc_mutex_t *p_mutex )
222 #if defined( PTH_INIT_IN_PTH_H )
223 return pth_mutex_acquire( p_mutex, TRUE, NULL );
225 #elif defined( ST_INIT_IN_ST_H )
226 return st_mutex_lock( *p_mutex );
228 #elif defined( WIN32 )
231 WaitForSingleObject( p_mutex->mutex, INFINITE );
235 EnterCriticalSection( &p_mutex->csection );
239 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
240 int i_return = pthread_mutex_lock( p_mutex );
243 // msg_Err( "thread %d: mutex_lock failed at %s:%d (%s)",
244 // pthread_self(), psz_file, i_line, strerror(i_return) );
248 #elif defined( HAVE_CTHREADS_H )
249 mutex_lock( p_mutex );
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
260 if( p_mutex->init < 2000 )
265 err = acquire_sem( p_mutex->lock );
271 /*****************************************************************************
272 * vlc_mutex_unlock: unlock a mutex
273 *****************************************************************************/
275 # define vlc_mutex_unlock( P_MUTEX ) \
276 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
278 # define vlc_mutex_unlock( P_MUTEX ) \
279 __vlc_mutex_unlock( "(unknown)", 0, P_MUTEX )
282 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
283 vlc_mutex_t *p_mutex )
285 #if defined( PTH_INIT_IN_PTH_H )
286 return pth_mutex_release( p_mutex );
288 #elif defined( ST_INIT_IN_ST_H )
289 return st_mutex_unlock( *p_mutex );
291 #elif defined( WIN32 )
294 ReleaseMutex( p_mutex->mutex );
298 LeaveCriticalSection( &p_mutex->csection );
302 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
303 int i_return = pthread_mutex_unlock( p_mutex );
306 // msg_Err( "thread %d: mutex_unlock failed at %s:%d (%s)",
307 // pthread_self(), psz_file, i_line, strerror(i_return) );
311 #elif defined( HAVE_CTHREADS_H )
312 mutex_unlock( p_mutex );
315 #elif defined( HAVE_KERNEL_SCHEDULER_H )
321 if( p_mutex->init < 2000 )
326 release_sem( p_mutex->lock );
332 /*****************************************************************************
333 * vlc_mutex_destroy: destroy a mutex
334 *****************************************************************************/
336 # define vlc_mutex_destroy( P_MUTEX ) \
337 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
339 # define vlc_mutex_destroy( P_MUTEX ) \
340 __vlc_mutex_destroy( "(unknown)", 0, P_MUTEX )
343 /*****************************************************************************
344 * vlc_cond_init: initialize a condition
345 *****************************************************************************/
346 #define vlc_cond_init( P_THIS, P_COND ) \
347 __vlc_cond_init( CAST_TO_VLC_OBJECT(P_THIS), P_COND )
349 /*****************************************************************************
350 * vlc_cond_signal: start a thread on condition completion
351 *****************************************************************************/
352 static inline int vlc_cond_signal( vlc_cond_t *p_condvar )
354 #if defined( PTH_INIT_IN_PTH_H )
355 return pth_cond_notify( p_condvar, FALSE );
357 #elif defined( ST_INIT_IN_ST_H )
358 return st_cond_signal( *p_condvar );
360 #elif defined( WIN32 )
361 /* Release one waiting thread if one is available. */
362 /* For this trick to work properly, the vlc_cond_signal must be surrounded
363 * by a mutex. This will prevent another thread from stealing the signal */
364 if( !p_condvar->semaphore )
366 PulseEvent( p_condvar->event );
368 else if( p_condvar->i_win9x_cv == 1 )
370 /* Wait for the gate to be open */
371 WaitForSingleObject( p_condvar->event, INFINITE );
373 if( p_condvar->i_waiting_threads )
375 /* Using a semaphore exposes us to a race condition. It is
376 * possible for another thread to start waiting on the semaphore
377 * just after we signaled it and thus steal the signal.
378 * We have to prevent new threads from entering the cond_wait(). */
379 ResetEvent( p_condvar->event );
381 /* A semaphore is used here because Win9x doesn't have
382 * SignalObjectAndWait() and thus a race condition exists
383 * during the time we release the mutex and the time we start
384 * waiting on the event (more precisely, the signal can sometimes
385 * be missed by the waiting thread if we use PulseEvent()). */
386 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
391 if( p_condvar->i_waiting_threads )
393 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
395 /* Wait for the last thread to be awakened */
396 WaitForSingleObject( p_condvar->event, INFINITE );
401 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
402 return pthread_cond_signal( p_condvar );
404 #elif defined( HAVE_CTHREADS_H )
405 /* condition_signal() */
406 if ( p_condvar->queue.head || p_condvar->implications )
408 cond_signal( (condition_t)p_condvar );
412 #elif defined( HAVE_KERNEL_SCHEDULER_H )
418 if( p_condvar->init < 2000 )
423 while( p_condvar->thread != -1 )
426 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
431 if( info.state != B_THREAD_SUSPENDED )
433 /* The waiting thread is not suspended so it could
434 * have been interrupted beetwen the unlock and the
435 * suspend_thread line. That is why we sleep a little
436 * before retesting p_condver->thread. */
441 /* Ok, we have to wake up that thread */
442 resume_thread( p_condvar->thread );
451 /*****************************************************************************
452 * vlc_cond_broadcast: start all threads waiting on condition completion
453 *****************************************************************************/
455 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
456 * Only works with pthreads, you need to adapt it for others
457 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
459 static inline int vlc_cond_broadcast( vlc_cond_t *p_condvar )
461 #if defined( PTH_INIT_IN_PTH_H )
462 return pth_cond_notify( p_condvar, FALSE );
464 #elif defined( ST_INIT_IN_ST_H )
465 return st_cond_broadcast( p_condvar );
467 #elif defined( WIN32 )
468 /* Release all waiting threads. */
471 if( !p_condvar->semaphore )
472 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
473 PulseEvent( p_condvar->event );
474 else if( p_condvar->i_win9x_cv == 1 )
476 /* Wait for the gate to be open */
477 WaitForSingleObject( p_condvar->event, INFINITE );
479 if( p_condvar->i_waiting_threads )
482 ResetEvent( p_condvar->event );
484 ReleaseSemaphore( p_condvar->semaphore,
485 p_condvar->i_waiting_threads, 0 );
490 if( p_condvar->i_waiting_threads )
492 ReleaseSemaphore( p_condvar->semaphore,
493 p_condvar->i_waiting_threads, 0 );
494 /* Wait for the last thread to be awakened */
495 WaitForSingleObject( p_condvar->event, INFINITE );
501 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
502 return pthread_cond_broadcast( p_condvar );
504 #elif defined( HAVE_CTHREADS_H )
505 /* condition_signal() */
506 if ( p_condvar->queue.head || p_condvar->implications )
508 cond_signal( (condition_t)p_condvar );
512 #elif defined( HAVE_KERNEL_SCHEDULER_H )
518 if( p_condvar->init < 2000 )
523 while( p_condvar->thread != -1 )
526 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
531 if( info.state != B_THREAD_SUSPENDED )
533 /* The waiting thread is not suspended so it could
534 * have been interrupted beetwen the unlock and the
535 * suspend_thread line. That is why we sleep a little
536 * before retesting p_condver->thread. */
541 /* Ok, we have to wake up that thread */
542 resume_thread( p_condvar->thread );
551 /*****************************************************************************
552 * vlc_cond_wait: wait until condition completion
553 *****************************************************************************/
555 # define vlc_cond_wait( P_COND, P_MUTEX ) \
556 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
558 # define vlc_cond_wait( P_COND, P_MUTEX ) \
559 __vlc_cond_wait( "(unknown)", 0, P_COND, P_MUTEX )
562 static inline int __vlc_cond_wait( char * psz_file, int i_line,
563 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
565 #if defined( PTH_INIT_IN_PTH_H )
566 return pth_cond_await( p_condvar, p_mutex, NULL );
568 #elif defined( ST_INIT_IN_ST_H )
571 st_mutex_unlock( *p_mutex );
572 i_ret = st_cond_wait( *p_condvar );
573 st_mutex_lock( *p_mutex );
577 #elif defined( WIN32 )
578 if( !p_condvar->semaphore )
580 /* Increase our wait count */
581 p_condvar->i_waiting_threads++;
583 if( p_condvar->SignalObjectAndWait )
584 /* It is only possible to atomically release the mutex and initiate the
585 * waiting on WinNT/2K/XP. Win9x doesn't have SignalObjectAndWait(). */
586 p_condvar->SignalObjectAndWait( p_mutex->mutex,
591 LeaveCriticalSection( &p_mutex->csection );
592 WaitForSingleObject( p_condvar->event, INFINITE );
595 else if( p_condvar->i_win9x_cv == 1 )
597 int i_waiting_threads;
599 /* Wait for the gate to be open */
600 WaitForSingleObject( p_condvar->event, INFINITE );
602 /* Increase our wait count */
603 p_condvar->i_waiting_threads++;
605 LeaveCriticalSection( &p_mutex->csection );
606 WaitForSingleObject( p_condvar->semaphore, INFINITE );
608 /* Decrement and test must be atomic */
609 EnterCriticalSection( &p_condvar->csection );
611 /* Decrease our wait count */
612 i_waiting_threads = --p_condvar->i_waiting_threads;
614 LeaveCriticalSection( &p_condvar->csection );
616 /* Reopen the gate if we were the last waiting thread */
617 if( !i_waiting_threads )
618 SetEvent( p_condvar->event );
622 int i_waiting_threads;
624 /* Increase our wait count */
625 p_condvar->i_waiting_threads++;
627 LeaveCriticalSection( &p_mutex->csection );
628 WaitForSingleObject( p_condvar->semaphore, INFINITE );
630 /* Decrement and test must be atomic */
631 EnterCriticalSection( &p_condvar->csection );
633 /* Decrease our wait count */
634 i_waiting_threads = --p_condvar->i_waiting_threads;
636 LeaveCriticalSection( &p_condvar->csection );
638 /* Signal that the last waiting thread just went through */
639 if( !i_waiting_threads )
640 SetEvent( p_condvar->event );
643 /* Reacquire the mutex before returning. */
644 vlc_mutex_lock( p_mutex );
648 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
651 /* In debug mode, timeout */
653 struct timespec timeout;
658 gettimeofday( &now, NULL );
659 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
660 timeout.tv_nsec = now.tv_usec * 1000;
662 i_result = pthread_cond_timedwait( p_condvar, p_mutex, &timeout );
664 if( i_result == ETIMEDOUT )
666 //X msg_Warn( "thread %d: possible deadlock detected "
667 //X "in cond_wait at %s:%d (%s)", pthread_self(),
668 //X psz_file, i_line, strerror(i_result) );
674 //X msg_Err( "thread %d: cond_wait failed at %s:%d (%s)",
675 //X pthread_self(), psz_file, i_line, strerror(i_result) );
680 return pthread_cond_wait( p_condvar, p_mutex );
683 #elif defined( HAVE_CTHREADS_H )
684 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
687 #elif defined( HAVE_KERNEL_SCHEDULER_H )
698 if( p_condvar->init < 2000 )
703 /* The p_condvar->thread var is initialized before the unlock because
704 * it enables to identify when the thread is interrupted beetwen the
705 * unlock line and the suspend_thread line */
706 p_condvar->thread = find_thread( NULL );
707 vlc_mutex_unlock( p_mutex );
708 suspend_thread( p_condvar->thread );
709 p_condvar->thread = -1;
711 vlc_mutex_lock( p_mutex );
717 /*****************************************************************************
718 * vlc_cond_destroy: destroy a condition
719 *****************************************************************************/
721 # define vlc_cond_destroy( P_COND ) \
722 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
724 # define vlc_cond_destroy( P_COND ) \
725 __vlc_cond_destroy( "(unknown)", 0, P_COND )
728 /*****************************************************************************
729 * vlc_thread_create: create a thread
730 *****************************************************************************/
731 # define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, WAIT ) \
732 __vlc_thread_create( CAST_TO_VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, WAIT )
734 /*****************************************************************************
735 * vlc_thread_ready: tell the parent thread we were successfully spawned
736 *****************************************************************************/
737 # define vlc_thread_ready( P_THIS ) \
738 __vlc_thread_ready( CAST_TO_VLC_OBJECT(P_THIS) )
740 /*****************************************************************************
741 * vlc_thread_join: wait until a thread exits
742 *****************************************************************************/
744 # define vlc_thread_join( P_THIS ) \
745 __vlc_thread_join( CAST_TO_VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
747 # define vlc_thread_join( P_THIS ) \
748 __vlc_thread_join( CAST_TO_VLC_OBJECT(P_THIS), "(unknown)", 0 )