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: threads.h,v 1.21 2001/07/25 08:41:21 gbazin Exp $
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
29 # include <sys/time.h>
32 #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
35 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) /* pthreads (like Linux & BSD) */
38 #elif defined( HAVE_CTHREADS_H ) /* GNUMach */
39 # include <cthreads.h>
41 #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
44 # include <kernel/OS.h>
45 # include <kernel/scheduler.h>
46 # include <byteorder.h>
48 #elif defined( WIN32 )
53 # error no threads available on your system !
57 /*****************************************************************************
59 *****************************************************************************
60 * These constants are used by all threads in *_CreateThread() and
61 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
62 * value is used as a shared flag to represent the status of the thread.
63 *****************************************************************************/
65 /* Void status - this value can be used to make sure no operation is currently
66 * in progress on the concerned thread in an array of recorded threads */
67 #define THREAD_NOP 0 /* nothing happened */
70 #define THREAD_CREATE 10 /* thread is initializing */
71 #define THREAD_START 11 /* thread has forked */
72 #define THREAD_READY 19 /* thread is ready */
74 /* Destructions status */
75 #define THREAD_DESTROY 20 /* destruction order has been sent */
76 #define THREAD_END 21 /* destruction order has been received */
77 #define THREAD_OVER 29 /* thread does not exist any more */
80 #define THREAD_ERROR 30 /* an error occured */
81 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
83 /*****************************************************************************
85 *****************************************************************************/
87 #if defined( PTH_INIT_IN_PTH_H )
88 typedef pth_t vlc_thread_t;
89 typedef pth_mutex_t vlc_mutex_t;
90 typedef pth_cond_t vlc_cond_t;
92 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
93 typedef pthread_t vlc_thread_t;
94 typedef pthread_mutex_t vlc_mutex_t;
95 typedef pthread_cond_t vlc_cond_t;
97 #elif defined( HAVE_CTHREADS_H )
98 typedef cthread_t vlc_thread_t;
100 /* Those structs are the ones defined in /include/cthreads.h but we need
101 * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
102 * foo is a (mutex_t*) */
103 typedef struct s_mutex {
107 struct cthread_queue queue;
110 typedef struct s_condition {
112 struct cthread_queue queue;
114 struct cond_imp *implications;
117 #elif defined( HAVE_KERNEL_SCHEDULER_H )
118 /* This is the BeOS implementation of the vlc threads, note that the mutex is
119 * not a real mutex and the cond_var is not like a pthread cond_var but it is
120 * enough for what wee need */
122 typedef thread_id vlc_thread_t;
136 #elif defined( WIN32 )
137 typedef HANDLE vlc_thread_t;
138 typedef CRITICAL_SECTION vlc_mutex_t;
142 int i_waiting_threads;
146 typedef unsigned (__stdcall *PTHREAD_START) (void *);
150 typedef void *(*vlc_thread_func_t)(void *p_data);
152 /*****************************************************************************
154 *****************************************************************************/
156 static __inline__ int vlc_threads_init ( void );
157 static __inline__ int vlc_threads_end ( void );
159 static __inline__ int vlc_mutex_init ( vlc_mutex_t * );
160 static __inline__ int vlc_mutex_lock ( vlc_mutex_t * );
161 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t * );
162 static __inline__ int vlc_mutex_destroy ( vlc_mutex_t * );
164 static __inline__ int vlc_cond_init ( vlc_cond_t * );
165 static __inline__ int vlc_cond_signal ( vlc_cond_t * );
166 static __inline__ int vlc_cond_wait ( vlc_cond_t *, vlc_mutex_t * );
167 static __inline__ int vlc_cond_destroy ( vlc_cond_t * );
169 static __inline__ int vlc_thread_create ( vlc_thread_t *, char *,
170 vlc_thread_func_t, void * );
171 static __inline__ void vlc_thread_exit ( void );
172 static __inline__ void vlc_thread_join ( vlc_thread_t );
175 static __inline__ int vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
180 /* Wrapper function for profiling */
181 static void * vlc_thread_wrapper ( void *p_wrapper );
183 typedef struct wrapper_s
185 /* Data lock access */
189 /* Data used to spawn the real thread */
190 vlc_thread_func_t func;
193 /* Profiling timer passed to the thread */
194 struct itimerval itimer;
201 struct timeval it_value;
202 struct timeval it_interval;
205 int setitimer(int kind, const struct itimerval* itnew,
206 struct itimerval* itold);
208 #define ITIMER_REAL 1
209 #define ITIMER_PROF 2
213 #endif /* PROFILING */
215 /*****************************************************************************
216 * vlc_threads_init: initialize threads system
217 *****************************************************************************/
218 static __inline__ int vlc_threads_init( void )
220 #if defined( PTH_INIT_IN_PTH_H )
223 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
226 #elif defined( HAVE_CTHREADS_H )
229 #elif defined( HAVE_KERNEL_SCHEDULER_H )
232 #elif defined( WIN32 )
238 /*****************************************************************************
239 * vlc_threads_end: stop threads system
240 *****************************************************************************/
241 static __inline__ int vlc_threads_end( void )
243 #if defined( PTH_INIT_IN_PTH_H )
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
249 #elif defined( HAVE_CTHREADS_H )
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
255 #elif defined( WIN32 )
261 /*****************************************************************************
262 * vlc_mutex_init: initialize a mutex
263 *****************************************************************************/
264 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
266 #if defined( PTH_INIT_IN_PTH_H )
267 return pth_mutex_init( p_mutex );
269 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
270 return pthread_mutex_init( p_mutex, NULL );
272 #elif defined( HAVE_CTHREADS_H )
273 mutex_init( p_mutex );
276 #elif defined( HAVE_KERNEL_SCHEDULER_H )
278 /* check the arguments and whether it's already been initialized */
279 if( p_mutex == NULL )
284 if( p_mutex->init == 9999 )
289 p_mutex->lock = create_sem( 1, "BeMutex" );
290 if( p_mutex->lock < B_NO_ERROR )
295 p_mutex->init = 9999;
298 #elif defined( WIN32 )
299 InitializeCriticalSection( p_mutex );
305 /*****************************************************************************
306 * vlc_mutex_lock: lock a mutex
307 *****************************************************************************/
308 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
310 #if defined( PTH_INIT_IN_PTH_H )
311 return pth_mutex_acquire( p_mutex, TRUE, NULL );
313 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
314 return pthread_mutex_lock( p_mutex );
316 #elif defined( HAVE_CTHREADS_H )
317 mutex_lock( p_mutex );
320 #elif defined( HAVE_KERNEL_SCHEDULER_H )
328 if( p_mutex->init < 2000 )
333 err = acquire_sem( p_mutex->lock );
336 #elif defined( WIN32 )
337 EnterCriticalSection( p_mutex );
343 /*****************************************************************************
344 * vlc_mutex_unlock: unlock a mutex
345 *****************************************************************************/
346 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
348 #if defined( PTH_INIT_IN_PTH_H )
349 return pth_mutex_release( p_mutex );
351 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
352 return pthread_mutex_unlock( p_mutex );
354 #elif defined( HAVE_CTHREADS_H )
355 mutex_unlock( p_mutex );
358 #elif defined( HAVE_KERNEL_SCHEDULER_H )
364 if( p_mutex->init < 2000 )
369 release_sem( p_mutex->lock );
372 #elif defined( WIN32 )
373 LeaveCriticalSection( p_mutex );
379 /*****************************************************************************
380 * vlc_mutex_destroy: destroy a mutex
381 *****************************************************************************/
382 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
384 #if defined( PTH_INIT_IN_PTH_H )
387 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
388 return pthread_mutex_destroy( p_mutex );
390 #elif defined( HAVE_KERNEL_SCHEDULER_H )
391 if( p_mutex->init == 9999 )
393 delete_sem( p_mutex->lock );
399 #elif defined( WIN32 )
400 DeleteCriticalSection( p_mutex );
406 /*****************************************************************************
407 * vlc_cond_init: initialize a condition
408 *****************************************************************************/
409 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
411 #if defined( PTH_INIT_IN_PTH_H )
412 return pth_cond_init( p_condvar );
414 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
415 return pthread_cond_init( p_condvar, NULL );
417 #elif defined( HAVE_CTHREADS_H )
418 /* condition_init() */
419 spin_lock_init( &p_condvar->lock );
420 cthread_queue_init( &p_condvar->queue );
422 p_condvar->implications = 0;
426 #elif defined( HAVE_KERNEL_SCHEDULER_H )
432 if( p_condvar->init == 9999 )
437 p_condvar->thread = -1;
438 p_condvar->init = 9999;
441 #elif defined( WIN32 )
442 /* initialise counter */
443 p_condvar->i_waiting_threads = 0;
445 /* Create an auto-reset event. */
446 p_condvar->signal = CreateEvent( NULL, /* no security */
447 FALSE, /* auto-reset event */
448 FALSE, /* non-signaled initially */
449 NULL ); /* unnamed */
451 return( !p_condvar->signal );
456 /*****************************************************************************
457 * vlc_cond_signal: start a thread on condition completion
458 *****************************************************************************/
459 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
461 #if defined( PTH_INIT_IN_PTH_H )
462 return pth_cond_notify( p_condvar, FALSE );
464 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
465 return pthread_cond_signal( p_condvar );
467 #elif defined( HAVE_CTHREADS_H )
468 /* condition_signal() */
469 if ( p_condvar->queue.head || p_condvar->implications )
471 cond_signal( (condition_t)p_condvar );
475 #elif defined( HAVE_KERNEL_SCHEDULER_H )
481 if( p_condvar->init < 2000 )
486 while( p_condvar->thread != -1 )
489 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
494 if( info.state != B_THREAD_SUSPENDED )
496 /* The waiting thread is not suspended so it could
497 * have been interrupted beetwen the unlock and the
498 * suspend_thread line. That is why we sleep a little
499 * before retesting p_condver->thread. */
504 /* Ok, we have to wake up that thread */
505 resume_thread( p_condvar->thread );
511 #elif defined( WIN32 )
512 /* Release one waiting thread if one is available. */
513 /* For this trick to work properly, the vlc_cond_signal must be surrounded
514 * by a mutex. This will prevent another thread from stealing the signal */
515 while( p_condvar->i_waiting_threads )
517 PulseEvent( p_condvar->signal );
518 Sleep( 0 ); /* deschedule the current thread */
525 /*****************************************************************************
526 * vlc_cond_broadcast: start all threads waiting on condition completion
527 *****************************************************************************/
529 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
530 * Only works with pthreads, you need to adapt it for others
531 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
533 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
535 #if defined( PTH_INIT_IN_PTH_H )
536 return pth_cond_notify( p_condvar, FALSE );
538 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
539 return pthread_cond_broadcast( p_condvar );
541 #elif defined( HAVE_CTHREADS_H )
542 /* condition_signal() */
543 if ( p_condvar->queue.head || p_condvar->implications )
545 cond_signal( (condition_t)p_condvar );
549 #elif defined( HAVE_KERNEL_SCHEDULER_H )
555 if( p_condvar->init < 2000 )
560 while( p_condvar->thread != -1 )
563 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
568 if( info.state != B_THREAD_SUSPENDED )
570 /* The waiting thread is not suspended so it could
571 * have been interrupted beetwen the unlock and the
572 * suspend_thread line. That is why we sleep a little
573 * before retesting p_condver->thread. */
578 /* Ok, we have to wake up that thread */
579 resume_thread( p_condvar->thread );
585 #elif defined( WIN32 )
586 /* Release all waiting threads. */
587 /* For this trick to work properly, the vlc_cond_signal must be surrounded
588 * by a mutex. This will prevent another thread from stealing the signal */
589 while( p_condvar->i_waiting_threads )
591 PulseEvent( p_condvar->signal );
592 Sleep( 0 ); /* deschedule the current thread */
599 /*****************************************************************************
600 * vlc_cond_wait: wait until condition completion
601 *****************************************************************************/
602 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
604 #if defined( PTH_INIT_IN_PTH_H )
605 return pth_cond_await( p_condvar, p_mutex, NULL );
607 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
608 return pthread_cond_wait( p_condvar, p_mutex );
610 #elif defined( HAVE_CTHREADS_H )
611 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
614 #elif defined( HAVE_KERNEL_SCHEDULER_H )
625 if( p_condvar->init < 2000 )
630 /* The p_condvar->thread var is initialized before the unlock because
631 * it enables to identify when the thread is interrupted beetwen the
632 * unlock line and the suspend_thread line */
633 p_condvar->thread = find_thread( NULL );
634 vlc_mutex_unlock( p_mutex );
635 suspend_thread( p_condvar->thread );
636 p_condvar->thread = -1;
638 vlc_mutex_lock( p_mutex );
641 #elif defined( WIN32 )
642 /* The ideal would be to use a function which atomically releases the
643 * mutex and initiate the waiting.
644 * Unfortunately only the SignalObjectAndWait function does this and it's
645 * only supported on WinNT/2K, furthermore it cannot take multiple
646 * events as parameters.
648 * The solution we use should however fulfill all our needs (even though
649 * it is not a correct pthreads implementation)
653 p_condvar->i_waiting_threads ++;
655 /* Release the mutex */
656 vlc_mutex_unlock( p_mutex );
658 i_result = WaitForSingleObject( p_condvar->signal, INFINITE);
660 /* maybe we should protect this with a mutex ? */
661 p_condvar->i_waiting_threads --;
663 /* Reacquire the mutex before returning. */
664 vlc_mutex_lock( p_mutex );
666 return( i_result == WAIT_FAILED );
671 /*****************************************************************************
672 * vlc_cond_destroy: destroy a condition
673 *****************************************************************************/
674 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
676 #if defined( PTH_INIT_IN_PTH_H )
679 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
680 return pthread_cond_destroy( p_condvar );
682 #elif defined( HAVE_KERNEL_SCHEDULER_H )
686 #elif defined( WIN32 )
687 return( !CloseHandle( p_condvar->signal ) );
692 /*****************************************************************************
693 * vlc_thread_create: create a thread
694 *****************************************************************************/
695 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
697 vlc_thread_func_t func,
705 /* Initialize the wrapper structure */
707 wrapper.p_data = p_data;
708 getitimer( ITIMER_PROF, &wrapper.itimer );
709 vlc_mutex_init( &wrapper.lock );
710 vlc_cond_init( &wrapper.wait );
711 vlc_mutex_lock( &wrapper.lock );
713 /* Alter user-passed data so that we call the wrapper instead
714 * of the real function */
716 func = vlc_thread_wrapper;
719 #if defined( PTH_INIT_IN_PTH_H )
720 *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
721 i_ret = ( p_thread == NULL );
723 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
724 i_ret = pthread_create( p_thread, NULL, func, p_data );
726 #elif defined( HAVE_CTHREADS_H )
727 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
730 #elif defined( HAVE_KERNEL_SCHEDULER_H )
731 *p_thread = spawn_thread( (thread_func)func, psz_name,
732 B_NORMAL_PRIORITY, p_data );
733 i_ret = resume_thread( *p_thread );
735 #elif defined( WIN32 )
738 /* This method is not recommended when using the MSVCRT C library,
739 * so we'll have to use _beginthreadex instead */
740 *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func,
741 p_data, 0, &threadID);
744 /* When using the MSVCRT C library you have to use the _beginthreadex
745 * function instead of CreateThread, otherwise you'll end up with memory
746 * leaks and the signal function not working */
747 *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func,
748 p_data, 0, &threadID);
750 i_ret = ( *p_thread ? 0 : 1 );
757 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
760 vlc_mutex_unlock( &wrapper.lock );
761 vlc_mutex_destroy( &wrapper.lock );
762 vlc_cond_destroy( &wrapper.wait );
768 /*****************************************************************************
769 * vlc_thread_exit: terminate a thread
770 *****************************************************************************/
771 static __inline__ void vlc_thread_exit( void )
773 #if defined( PTH_INIT_IN_PTH_H )
776 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
779 #elif defined( HAVE_CTHREADS_H )
781 cthread_exit( &result );
783 #elif defined( HAVE_KERNEL_SCHEDULER_H )
786 #elif defined( WIN32 )
790 /* For now we don't close the thread handles (because of race conditions).
791 * Need to be looked at. */
797 /*****************************************************************************
798 * vlc_thread_join: wait until a thread exits
799 *****************************************************************************/
800 static __inline__ void vlc_thread_join( vlc_thread_t thread )
802 #if defined( PTH_INIT_IN_PTH_H )
803 pth_join( thread, NULL );
805 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
806 pthread_join( thread, NULL );
808 #elif defined( HAVE_CTHREADS_H )
809 cthread_join( thread );
811 #elif defined( HAVE_KERNEL_SCHEDULER_H )
813 wait_for_thread( thread, &exit_value );
815 #elif defined( WIN32 )
816 WaitForSingleObject( thread, INFINITE);
822 static void *vlc_thread_wrapper( void *p_wrapper )
824 /* Put user data in thread-local variables */
825 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
826 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
828 /* Set the profile timer value */
829 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
831 /* Tell the calling thread that we don't need its data anymore */
832 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
833 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
834 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
836 /* Call the real function */
837 return func( p_data );