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.23 2001/08/14 04:52:39 sam 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_CTHREADS_H )
393 #elif defined( HAVE_KERNEL_SCHEDULER_H )
394 if( p_mutex
->init
== 9999 )
396 delete_sem( p_mutex
->lock
);
402 #elif defined( WIN32 )
403 DeleteCriticalSection( p_mutex
);
409 /*****************************************************************************
410 * vlc_cond_init: initialize a condition
411 *****************************************************************************/
412 static __inline__
int vlc_cond_init( vlc_cond_t
*p_condvar
)
414 #if defined( PTH_INIT_IN_PTH_H )
415 return pth_cond_init( p_condvar
);
417 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
418 return pthread_cond_init( p_condvar
, NULL
);
420 #elif defined( HAVE_CTHREADS_H )
421 /* condition_init() */
422 spin_lock_init( &p_condvar
->lock
);
423 cthread_queue_init( &p_condvar
->queue
);
425 p_condvar
->implications
= 0;
429 #elif defined( HAVE_KERNEL_SCHEDULER_H )
435 if( p_condvar
->init
== 9999 )
440 p_condvar
->thread
= -1;
441 p_condvar
->init
= 9999;
444 #elif defined( WIN32 )
445 /* initialise counter */
446 p_condvar
->i_waiting_threads
= 0;
448 /* Create an auto-reset event. */
449 p_condvar
->signal
= CreateEvent( NULL
, /* no security */
450 FALSE
, /* auto-reset event */
451 FALSE
, /* non-signaled initially */
452 NULL
); /* unnamed */
454 return( !p_condvar
->signal
);
459 /*****************************************************************************
460 * vlc_cond_signal: start a thread on condition completion
461 *****************************************************************************/
462 static __inline__
int vlc_cond_signal( vlc_cond_t
*p_condvar
)
464 #if defined( PTH_INIT_IN_PTH_H )
465 return pth_cond_notify( p_condvar
, FALSE
);
467 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
468 return pthread_cond_signal( p_condvar
);
470 #elif defined( HAVE_CTHREADS_H )
471 /* condition_signal() */
472 if ( p_condvar
->queue
.head
|| p_condvar
->implications
)
474 cond_signal( (condition_t
)p_condvar
);
478 #elif defined( HAVE_KERNEL_SCHEDULER_H )
484 if( p_condvar
->init
< 2000 )
489 while( p_condvar
->thread
!= -1 )
492 if( get_thread_info(p_condvar
->thread
, &info
) == B_BAD_VALUE
)
497 if( info
.state
!= B_THREAD_SUSPENDED
)
499 /* The waiting thread is not suspended so it could
500 * have been interrupted beetwen the unlock and the
501 * suspend_thread line. That is why we sleep a little
502 * before retesting p_condver->thread. */
507 /* Ok, we have to wake up that thread */
508 resume_thread( p_condvar
->thread
);
514 #elif defined( WIN32 )
515 /* Release one waiting thread if one is available. */
516 /* For this trick to work properly, the vlc_cond_signal must be surrounded
517 * by a mutex. This will prevent another thread from stealing the signal */
518 int i_waiting_threads
= p_condvar
->i_waiting_threads
;
519 while( p_condvar
->i_waiting_threads
520 && p_condvar
->i_waiting_threads
== i_waiting_threads
)
522 PulseEvent( p_condvar
->signal
);
523 Sleep( 1 ); /* deschedule the current thread */
530 /*****************************************************************************
531 * vlc_cond_broadcast: start all threads waiting on condition completion
532 *****************************************************************************/
534 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
535 * Only works with pthreads, you need to adapt it for others
536 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
538 static __inline__
int vlc_cond_broadcast( vlc_cond_t
*p_condvar
)
540 #if defined( PTH_INIT_IN_PTH_H )
541 return pth_cond_notify( p_condvar
, FALSE
);
543 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
544 return pthread_cond_broadcast( p_condvar
);
546 #elif defined( HAVE_CTHREADS_H )
547 /* condition_signal() */
548 if ( p_condvar
->queue
.head
|| p_condvar
->implications
)
550 cond_signal( (condition_t
)p_condvar
);
554 #elif defined( HAVE_KERNEL_SCHEDULER_H )
560 if( p_condvar
->init
< 2000 )
565 while( p_condvar
->thread
!= -1 )
568 if( get_thread_info(p_condvar
->thread
, &info
) == B_BAD_VALUE
)
573 if( info
.state
!= B_THREAD_SUSPENDED
)
575 /* The waiting thread is not suspended so it could
576 * have been interrupted beetwen the unlock and the
577 * suspend_thread line. That is why we sleep a little
578 * before retesting p_condver->thread. */
583 /* Ok, we have to wake up that thread */
584 resume_thread( p_condvar
->thread
);
590 #elif defined( WIN32 )
591 /* Release all waiting threads. */
592 /* For this trick to work properly, the vlc_cond_signal must be surrounded
593 * by a mutex. This will prevent another thread from stealing the signal */
594 while( p_condvar
->i_waiting_threads
)
596 PulseEvent( p_condvar
->signal
);
597 Sleep( 1 ); /* deschedule the current thread */
604 /*****************************************************************************
605 * vlc_cond_wait: wait until condition completion
606 *****************************************************************************/
607 static __inline__
int vlc_cond_wait( vlc_cond_t
*p_condvar
, vlc_mutex_t
*p_mutex
)
609 #if defined( PTH_INIT_IN_PTH_H )
610 return pth_cond_await( p_condvar
, p_mutex
, NULL
);
612 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
613 return pthread_cond_wait( p_condvar
, p_mutex
);
615 #elif defined( HAVE_CTHREADS_H )
616 condition_wait( (condition_t
)p_condvar
, (mutex_t
)p_mutex
);
619 #elif defined( HAVE_KERNEL_SCHEDULER_H )
630 if( p_condvar
->init
< 2000 )
635 /* The p_condvar->thread var is initialized before the unlock because
636 * it enables to identify when the thread is interrupted beetwen the
637 * unlock line and the suspend_thread line */
638 p_condvar
->thread
= find_thread( NULL
);
639 vlc_mutex_unlock( p_mutex
);
640 suspend_thread( p_condvar
->thread
);
641 p_condvar
->thread
= -1;
643 vlc_mutex_lock( p_mutex
);
646 #elif defined( WIN32 )
647 /* The ideal would be to use a function which atomically releases the
648 * mutex and initiate the waiting.
649 * Unfortunately only the SignalObjectAndWait function does this and it's
650 * only supported on WinNT/2K, furthermore it cannot take multiple
651 * events as parameters.
653 * The solution we use should however fulfill all our needs (even though
654 * it is not a correct pthreads implementation)
658 p_condvar
->i_waiting_threads
++;
660 /* Release the mutex */
661 vlc_mutex_unlock( p_mutex
);
663 i_result
= WaitForSingleObject( p_condvar
->signal
, INFINITE
);
665 /* maybe we should protect this with a mutex ? */
666 p_condvar
->i_waiting_threads
--;
668 /* Reacquire the mutex before returning. */
669 vlc_mutex_lock( p_mutex
);
671 return( i_result
== WAIT_FAILED
);
676 /*****************************************************************************
677 * vlc_cond_destroy: destroy a condition
678 *****************************************************************************/
679 static __inline__
int vlc_cond_destroy( vlc_cond_t
*p_condvar
)
681 #if defined( PTH_INIT_IN_PTH_H )
684 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
685 return pthread_cond_destroy( p_condvar
);
687 #elif defined( HAVE_CTHREADS_H )
690 #elif defined( HAVE_KERNEL_SCHEDULER_H )
694 #elif defined( WIN32 )
695 return( !CloseHandle( p_condvar
->signal
) );
700 /*****************************************************************************
701 * vlc_thread_create: create a thread
702 *****************************************************************************/
703 static __inline__
int vlc_thread_create( vlc_thread_t
*p_thread
,
705 vlc_thread_func_t func
,
713 /* Initialize the wrapper structure */
715 wrapper
.p_data
= p_data
;
716 getitimer( ITIMER_PROF
, &wrapper
.itimer
);
717 vlc_mutex_init( &wrapper
.lock
);
718 vlc_cond_init( &wrapper
.wait
);
719 vlc_mutex_lock( &wrapper
.lock
);
721 /* Alter user-passed data so that we call the wrapper instead
722 * of the real function */
724 func
= vlc_thread_wrapper
;
727 #if defined( PTH_INIT_IN_PTH_H )
728 *p_thread
= pth_spawn( PTH_ATTR_DEFAULT
, func
, p_data
);
729 i_ret
= ( p_thread
== NULL
);
731 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
732 i_ret
= pthread_create( p_thread
, NULL
, func
, p_data
);
734 #elif defined( HAVE_CTHREADS_H )
735 *p_thread
= cthread_fork( (cthread_fn_t
)func
, (any_t
)p_data
);
738 #elif defined( HAVE_KERNEL_SCHEDULER_H )
739 *p_thread
= spawn_thread( (thread_func
)func
, psz_name
,
740 B_NORMAL_PRIORITY
, p_data
);
741 i_ret
= resume_thread( *p_thread
);
743 #elif defined( WIN32 )
746 /* This method is not recommended when using the MSVCRT C library,
747 * so we'll have to use _beginthreadex instead */
748 *p_thread
= CreateThread(0, 0, (LPTHREAD_START_ROUTINE
) func
,
749 p_data
, 0, &threadID
);
752 /* When using the MSVCRT C library you have to use the _beginthreadex
753 * function instead of CreateThread, otherwise you'll end up with memory
754 * leaks and the signal function not working */
755 *p_thread
= (HANDLE
)_beginthreadex(NULL
, 0, (PTHREAD_START
) func
,
756 p_data
, 0, &threadID
);
758 i_ret
= ( *p_thread
? 0 : 1 );
765 vlc_cond_wait( &wrapper
.wait
, &wrapper
.lock
);
768 vlc_mutex_unlock( &wrapper
.lock
);
769 vlc_mutex_destroy( &wrapper
.lock
);
770 vlc_cond_destroy( &wrapper
.wait
);
776 /*****************************************************************************
777 * vlc_thread_exit: terminate a thread
778 *****************************************************************************/
779 static __inline__
void vlc_thread_exit( void )
781 #if defined( PTH_INIT_IN_PTH_H )
784 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
787 #elif defined( HAVE_CTHREADS_H )
789 cthread_exit( &result
);
791 #elif defined( HAVE_KERNEL_SCHEDULER_H )
794 #elif defined( WIN32 )
798 /* For now we don't close the thread handles (because of race conditions).
799 * Need to be looked at. */
805 /*****************************************************************************
806 * vlc_thread_join: wait until a thread exits
807 *****************************************************************************/
808 static __inline__
void vlc_thread_join( vlc_thread_t thread
)
810 #if defined( PTH_INIT_IN_PTH_H )
811 pth_join( thread
, NULL
);
813 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
814 pthread_join( thread
, NULL
);
816 #elif defined( HAVE_CTHREADS_H )
817 cthread_join( thread
);
819 #elif defined( HAVE_KERNEL_SCHEDULER_H )
821 wait_for_thread( thread
, &exit_value
);
823 #elif defined( WIN32 )
824 WaitForSingleObject( thread
, INFINITE
);
830 static void *vlc_thread_wrapper( void *p_wrapper
)
832 /* Put user data in thread-local variables */
833 void * p_data
= ((wrapper_t
*)p_wrapper
)->p_data
;
834 vlc_thread_func_t func
= ((wrapper_t
*)p_wrapper
)->func
;
836 /* Set the profile timer value */
837 setitimer( ITIMER_PROF
, &((wrapper_t
*)p_wrapper
)->itimer
, NULL
);
839 /* Tell the calling thread that we don't need its data anymore */
840 vlc_mutex_lock( &((wrapper_t
*)p_wrapper
)->lock
);
841 vlc_cond_signal( &((wrapper_t
*)p_wrapper
)->wait
);
842 vlc_mutex_unlock( &((wrapper_t
*)p_wrapper
)->lock
);
844 /* Call the real function */
845 return func( p_data
);