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.26 2001/11/14 00:01:36 jlj 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) */
37 /* This is not prototyped under Linux, though it exists. */
38 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
40 #elif defined( HAVE_CTHREADS_H ) /* GNUMach */
41 # include <cthreads.h>
43 #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
46 # include <kernel/OS.h>
47 # include <kernel/scheduler.h>
48 # include <byteorder.h>
50 #elif defined( WIN32 )
55 # error no threads available on your system !
59 /*****************************************************************************
61 *****************************************************************************
62 * These constants are used by all threads in *_CreateThread() and
63 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
64 * value is used as a shared flag to represent the status of the thread.
65 *****************************************************************************/
67 /* Void status - this value can be used to make sure no operation is currently
68 * in progress on the concerned thread in an array of recorded threads */
69 #define THREAD_NOP 0 /* nothing happened */
72 #define THREAD_CREATE 10 /* thread is initializing */
73 #define THREAD_START 11 /* thread has forked */
74 #define THREAD_READY 19 /* thread is ready */
76 /* Destructions status */
77 #define THREAD_DESTROY 20 /* destruction order has been sent */
78 #define THREAD_END 21 /* destruction order has been received */
79 #define THREAD_OVER 29 /* thread does not exist any more */
82 #define THREAD_ERROR 30 /* an error occured */
83 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
85 /*****************************************************************************
87 *****************************************************************************/
89 #if defined( PTH_INIT_IN_PTH_H )
90 typedef pth_t vlc_thread_t;
91 typedef pth_mutex_t vlc_mutex_t;
92 typedef pth_cond_t vlc_cond_t;
94 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
95 typedef pthread_t vlc_thread_t;
96 typedef pthread_mutex_t vlc_mutex_t;
97 typedef pthread_cond_t vlc_cond_t;
99 #elif defined( HAVE_CTHREADS_H )
100 typedef cthread_t vlc_thread_t;
102 /* Those structs are the ones defined in /include/cthreads.h but we need
103 * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
104 * foo is a (mutex_t*) */
105 typedef struct s_mutex {
109 struct cthread_queue queue;
112 typedef struct s_condition {
114 struct cthread_queue queue;
116 struct cond_imp *implications;
119 #elif defined( HAVE_KERNEL_SCHEDULER_H )
120 /* This is the BeOS implementation of the vlc threads, note that the mutex is
121 * not a real mutex and the cond_var is not like a pthread cond_var but it is
122 * enough for what wee need */
124 typedef thread_id vlc_thread_t;
138 #elif defined( WIN32 )
139 typedef HANDLE vlc_thread_t;
140 typedef CRITICAL_SECTION vlc_mutex_t;
144 int i_waiting_threads;
148 typedef unsigned (__stdcall *PTHREAD_START) (void *);
152 typedef void *(*vlc_thread_func_t)(void *p_data);
154 /*****************************************************************************
156 *****************************************************************************/
158 static __inline__ int vlc_threads_init ( void );
159 static __inline__ int vlc_threads_end ( void );
161 static __inline__ int vlc_mutex_init ( vlc_mutex_t * );
162 static __inline__ int vlc_mutex_lock ( vlc_mutex_t * );
163 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t * );
164 static __inline__ int vlc_mutex_destroy ( vlc_mutex_t * );
166 static __inline__ int vlc_cond_init ( vlc_cond_t * );
167 static __inline__ int vlc_cond_signal ( vlc_cond_t * );
168 static __inline__ int vlc_cond_wait ( vlc_cond_t *, vlc_mutex_t * );
169 static __inline__ int vlc_cond_destroy ( vlc_cond_t * );
171 static __inline__ int vlc_thread_create ( vlc_thread_t *, char *,
172 vlc_thread_func_t, void * );
173 static __inline__ void vlc_thread_exit ( void );
174 static __inline__ void vlc_thread_join ( vlc_thread_t );
177 static __inline__ int vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
182 /* Wrapper function for profiling */
183 static void * vlc_thread_wrapper ( void *p_wrapper );
185 typedef struct wrapper_s
187 /* Data lock access */
191 /* Data used to spawn the real thread */
192 vlc_thread_func_t func;
195 /* Profiling timer passed to the thread */
196 struct itimerval itimer;
203 struct timeval it_value;
204 struct timeval it_interval;
207 int setitimer(int kind, const struct itimerval* itnew,
208 struct itimerval* itold);
210 #define ITIMER_REAL 1
211 #define ITIMER_PROF 2
217 /*****************************************************************************
218 * vlc_threads_init: initialize threads system
219 *****************************************************************************/
220 static __inline__ int vlc_threads_init( void )
222 #if defined( PTH_INIT_IN_PTH_H )
225 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
228 #elif defined( HAVE_CTHREADS_H )
231 #elif defined( HAVE_KERNEL_SCHEDULER_H )
234 #elif defined( WIN32 )
240 /*****************************************************************************
241 * vlc_threads_end: stop threads system
242 *****************************************************************************/
243 static __inline__ int vlc_threads_end( void )
245 #if defined( PTH_INIT_IN_PTH_H )
248 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
251 #elif defined( HAVE_CTHREADS_H )
254 #elif defined( HAVE_KERNEL_SCHEDULER_H )
257 #elif defined( WIN32 )
263 /*****************************************************************************
264 * vlc_mutex_init: initialize a mutex
265 *****************************************************************************/
266 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
268 #if defined( PTH_INIT_IN_PTH_H )
269 return pth_mutex_init( p_mutex );
271 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
272 # if defined(DEBUG) && defined(SYS_LINUX)
273 /* Create error-checking mutex to detect threads problems more easily. */
274 pthread_mutexattr_t attr;
277 pthread_mutexattr_init( &attr );
278 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
279 i_result = pthread_mutex_init( p_mutex, &attr );
280 pthread_mutexattr_destroy( &attr );
284 return pthread_mutex_init( p_mutex, NULL );
286 #elif defined( HAVE_CTHREADS_H )
287 mutex_init( p_mutex );
290 #elif defined( HAVE_KERNEL_SCHEDULER_H )
292 /* check the arguments and whether it's already been initialized */
293 if( p_mutex == NULL )
298 if( p_mutex->init == 9999 )
303 p_mutex->lock = create_sem( 1, "BeMutex" );
304 if( p_mutex->lock < B_NO_ERROR )
309 p_mutex->init = 9999;
312 #elif defined( WIN32 )
313 InitializeCriticalSection( p_mutex );
319 /*****************************************************************************
320 * vlc_mutex_lock: lock a mutex
321 *****************************************************************************/
322 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
324 #if defined( PTH_INIT_IN_PTH_H )
325 return pth_mutex_acquire( p_mutex, TRUE, NULL );
327 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
328 return pthread_mutex_lock( p_mutex );
330 #elif defined( HAVE_CTHREADS_H )
331 mutex_lock( p_mutex );
334 #elif defined( HAVE_KERNEL_SCHEDULER_H )
342 if( p_mutex->init < 2000 )
347 err = acquire_sem( p_mutex->lock );
350 #elif defined( WIN32 )
351 EnterCriticalSection( p_mutex );
357 /*****************************************************************************
358 * vlc_mutex_unlock: unlock a mutex
359 *****************************************************************************/
360 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
362 #if defined( PTH_INIT_IN_PTH_H )
363 return pth_mutex_release( p_mutex );
365 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
366 return pthread_mutex_unlock( p_mutex );
368 #elif defined( HAVE_CTHREADS_H )
369 mutex_unlock( p_mutex );
372 #elif defined( HAVE_KERNEL_SCHEDULER_H )
378 if( p_mutex->init < 2000 )
383 release_sem( p_mutex->lock );
386 #elif defined( WIN32 )
387 LeaveCriticalSection( p_mutex );
393 /*****************************************************************************
394 * vlc_mutex_destroy: destroy a mutex
395 *****************************************************************************/
396 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
398 #if defined( PTH_INIT_IN_PTH_H )
401 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
402 return pthread_mutex_destroy( p_mutex );
404 #elif defined( HAVE_CTHREADS_H )
407 #elif defined( HAVE_KERNEL_SCHEDULER_H )
408 if( p_mutex->init == 9999 )
410 delete_sem( p_mutex->lock );
416 #elif defined( WIN32 )
417 DeleteCriticalSection( p_mutex );
423 /*****************************************************************************
424 * vlc_cond_init: initialize a condition
425 *****************************************************************************/
426 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
428 #if defined( PTH_INIT_IN_PTH_H )
429 return pth_cond_init( p_condvar );
431 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
432 return pthread_cond_init( p_condvar, NULL );
434 #elif defined( HAVE_CTHREADS_H )
435 /* condition_init() */
436 spin_lock_init( &p_condvar->lock );
437 cthread_queue_init( &p_condvar->queue );
439 p_condvar->implications = 0;
443 #elif defined( HAVE_KERNEL_SCHEDULER_H )
449 if( p_condvar->init == 9999 )
454 p_condvar->thread = -1;
455 p_condvar->init = 9999;
458 #elif defined( WIN32 )
459 /* initialise counter */
460 p_condvar->i_waiting_threads = 0;
462 /* Create an auto-reset event. */
463 p_condvar->signal = CreateEvent( NULL, /* no security */
464 FALSE, /* auto-reset event */
465 FALSE, /* non-signaled initially */
466 NULL ); /* unnamed */
468 return( !p_condvar->signal );
473 /*****************************************************************************
474 * vlc_cond_signal: start a thread on condition completion
475 *****************************************************************************/
476 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
478 #if defined( PTH_INIT_IN_PTH_H )
479 return pth_cond_notify( p_condvar, FALSE );
481 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
482 return pthread_cond_signal( p_condvar );
484 #elif defined( HAVE_CTHREADS_H )
485 /* condition_signal() */
486 if ( p_condvar->queue.head || p_condvar->implications )
488 cond_signal( (condition_t)p_condvar );
492 #elif defined( HAVE_KERNEL_SCHEDULER_H )
498 if( p_condvar->init < 2000 )
503 while( p_condvar->thread != -1 )
506 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
511 if( info.state != B_THREAD_SUSPENDED )
513 /* The waiting thread is not suspended so it could
514 * have been interrupted beetwen the unlock and the
515 * suspend_thread line. That is why we sleep a little
516 * before retesting p_condver->thread. */
521 /* Ok, we have to wake up that thread */
522 resume_thread( p_condvar->thread );
528 #elif defined( WIN32 )
529 /* Release one waiting thread if one is available. */
530 /* For this trick to work properly, the vlc_cond_signal must be surrounded
531 * by a mutex. This will prevent another thread from stealing the signal */
532 int i_waiting_threads = p_condvar->i_waiting_threads;
533 while( p_condvar->i_waiting_threads
534 && p_condvar->i_waiting_threads == i_waiting_threads )
536 PulseEvent( p_condvar->signal );
537 Sleep( 0 ); /* deschedule the current thread */
544 /*****************************************************************************
545 * vlc_cond_broadcast: start all threads waiting on condition completion
546 *****************************************************************************/
548 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
549 * Only works with pthreads, you need to adapt it for others
550 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
552 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
554 #if defined( PTH_INIT_IN_PTH_H )
555 return pth_cond_notify( p_condvar, FALSE );
557 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
558 return pthread_cond_broadcast( p_condvar );
560 #elif defined( HAVE_CTHREADS_H )
561 /* condition_signal() */
562 if ( p_condvar->queue.head || p_condvar->implications )
564 cond_signal( (condition_t)p_condvar );
568 #elif defined( HAVE_KERNEL_SCHEDULER_H )
574 if( p_condvar->init < 2000 )
579 while( p_condvar->thread != -1 )
582 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
587 if( info.state != B_THREAD_SUSPENDED )
589 /* The waiting thread is not suspended so it could
590 * have been interrupted beetwen the unlock and the
591 * suspend_thread line. That is why we sleep a little
592 * before retesting p_condver->thread. */
597 /* Ok, we have to wake up that thread */
598 resume_thread( p_condvar->thread );
604 #elif defined( WIN32 )
605 /* Release all waiting threads. */
606 /* For this trick to work properly, the vlc_cond_signal must be surrounded
607 * by a mutex. This will prevent another thread from stealing the signal */
608 while( p_condvar->i_waiting_threads )
610 PulseEvent( p_condvar->signal );
611 Sleep( 0 ); /* deschedule the current thread */
618 /*****************************************************************************
619 * vlc_cond_wait: wait until condition completion
620 *****************************************************************************/
621 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
623 #if defined( PTH_INIT_IN_PTH_H )
624 return pth_cond_await( p_condvar, p_mutex, NULL );
626 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
627 return pthread_cond_wait( p_condvar, p_mutex );
629 #elif defined( HAVE_CTHREADS_H )
630 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
633 #elif defined( HAVE_KERNEL_SCHEDULER_H )
644 if( p_condvar->init < 2000 )
649 /* The p_condvar->thread var is initialized before the unlock because
650 * it enables to identify when the thread is interrupted beetwen the
651 * unlock line and the suspend_thread line */
652 p_condvar->thread = find_thread( NULL );
653 vlc_mutex_unlock( p_mutex );
654 suspend_thread( p_condvar->thread );
655 p_condvar->thread = -1;
657 vlc_mutex_lock( p_mutex );
660 #elif defined( WIN32 )
661 /* The ideal would be to use a function which atomically releases the
662 * mutex and initiate the waiting.
663 * Unfortunately only the SignalObjectAndWait function does this and it's
664 * only supported on WinNT/2K, furthermore it cannot take multiple
665 * events as parameters.
667 * The solution we use should however fulfill all our needs (even though
668 * it is not a correct pthreads implementation)
672 p_condvar->i_waiting_threads ++;
674 /* Release the mutex */
675 vlc_mutex_unlock( p_mutex );
677 i_result = WaitForSingleObject( p_condvar->signal, INFINITE);
679 /* maybe we should protect this with a mutex ? */
680 p_condvar->i_waiting_threads --;
682 /* Reacquire the mutex before returning. */
683 vlc_mutex_lock( p_mutex );
685 return( i_result == WAIT_FAILED );
690 /*****************************************************************************
691 * vlc_cond_destroy: destroy a condition
692 *****************************************************************************/
693 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
695 #if defined( PTH_INIT_IN_PTH_H )
698 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
699 return pthread_cond_destroy( p_condvar );
701 #elif defined( HAVE_CTHREADS_H )
704 #elif defined( HAVE_KERNEL_SCHEDULER_H )
708 #elif defined( WIN32 )
709 return( !CloseHandle( p_condvar->signal ) );
714 /*****************************************************************************
715 * vlc_thread_create: create a thread
716 *****************************************************************************/
717 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
719 vlc_thread_func_t func,
727 /* Initialize the wrapper structure */
729 wrapper.p_data = p_data;
730 getitimer( ITIMER_PROF, &wrapper.itimer );
731 vlc_mutex_init( &wrapper.lock );
732 vlc_cond_init( &wrapper.wait );
733 vlc_mutex_lock( &wrapper.lock );
735 /* Alter user-passed data so that we call the wrapper instead
736 * of the real function */
738 func = vlc_thread_wrapper;
741 #if defined( PTH_INIT_IN_PTH_H )
742 *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
743 i_ret = ( p_thread == NULL );
745 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
746 i_ret = pthread_create( p_thread, NULL, func, p_data );
748 #elif defined( HAVE_CTHREADS_H )
749 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
752 #elif defined( HAVE_KERNEL_SCHEDULER_H )
753 *p_thread = spawn_thread( (thread_func)func, psz_name,
754 B_NORMAL_PRIORITY, p_data );
755 i_ret = resume_thread( *p_thread );
757 #elif defined( WIN32 )
760 /* This method is not recommended when using the MSVCRT C library,
761 * so we'll have to use _beginthreadex instead */
762 *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func,
763 p_data, 0, &threadID);
766 /* When using the MSVCRT C library you have to use the _beginthreadex
767 * function instead of CreateThread, otherwise you'll end up with memory
768 * leaks and the signal function not working */
769 *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func,
770 p_data, 0, &threadID);
772 i_ret = ( *p_thread ? 0 : 1 );
779 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
782 vlc_mutex_unlock( &wrapper.lock );
783 vlc_mutex_destroy( &wrapper.lock );
784 vlc_cond_destroy( &wrapper.wait );
790 /*****************************************************************************
791 * vlc_thread_exit: terminate a thread
792 *****************************************************************************/
793 static __inline__ void vlc_thread_exit( void )
795 #if defined( PTH_INIT_IN_PTH_H )
798 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
801 #elif defined( HAVE_CTHREADS_H )
803 cthread_exit( &result );
805 #elif defined( HAVE_KERNEL_SCHEDULER_H )
808 #elif defined( WIN32 )
812 /* For now we don't close the thread handles (because of race conditions).
813 * Need to be looked at. */
819 /*****************************************************************************
820 * vlc_thread_join: wait until a thread exits
821 *****************************************************************************/
822 static __inline__ void vlc_thread_join( vlc_thread_t thread )
824 #if defined( PTH_INIT_IN_PTH_H )
825 pth_join( thread, NULL );
827 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
828 pthread_join( thread, NULL );
830 #elif defined( HAVE_CTHREADS_H )
831 cthread_join( thread );
833 #elif defined( HAVE_KERNEL_SCHEDULER_H )
835 wait_for_thread( thread, &exit_value );
837 #elif defined( WIN32 )
838 WaitForSingleObject( thread, INFINITE);
844 static void *vlc_thread_wrapper( void *p_wrapper )
846 /* Put user data in thread-local variables */
847 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
848 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
850 /* Set the profile timer value */
851 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
853 /* Tell the calling thread that we don't need its data anymore */
854 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
855 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
856 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
858 /* Call the real function */
859 return func( p_data );