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.20 2001/07/18 14:21:00 massiot 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 ) /* Win32 with MinGW32 compiler */
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 HANDLE vlc_mutex_t;
139 typedef HANDLE vlc_cond_t;
140 typedef unsigned (__stdcall *PTHREAD_START) (void *);
144 typedef void *(*vlc_thread_func_t)(void *p_data);
146 /*****************************************************************************
148 *****************************************************************************/
150 static __inline__ int vlc_threads_init ( void );
151 static __inline__ int vlc_threads_end ( void );
153 static __inline__ int vlc_mutex_init ( vlc_mutex_t * );
154 static __inline__ int vlc_mutex_lock ( vlc_mutex_t * );
155 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t * );
156 static __inline__ int vlc_mutex_destroy ( vlc_mutex_t * );
158 static __inline__ int vlc_cond_init ( vlc_cond_t * );
159 static __inline__ int vlc_cond_signal ( vlc_cond_t * );
160 static __inline__ int vlc_cond_wait ( vlc_cond_t *, vlc_mutex_t * );
161 static __inline__ int vlc_cond_destroy ( vlc_cond_t * );
163 static __inline__ int vlc_thread_create ( vlc_thread_t *, char *,
164 vlc_thread_func_t, void * );
165 static __inline__ void vlc_thread_exit ( void );
166 static __inline__ void vlc_thread_join ( vlc_thread_t );
169 static __inline__ int vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
174 /* Wrapper function for profiling */
175 static void * vlc_thread_wrapper ( void *p_wrapper );
177 typedef struct wrapper_s
179 /* Data lock access */
183 /* Data used to spawn the real thread */
184 vlc_thread_func_t func;
187 /* Profiling timer passed to the thread */
188 struct itimerval itimer;
193 /*****************************************************************************
194 * vlc_threads_init: initialize threads system
195 *****************************************************************************/
196 static __inline__ int vlc_threads_init( void )
198 #if defined( PTH_INIT_IN_PTH_H )
201 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
204 #elif defined( HAVE_CTHREADS_H )
207 #elif defined( HAVE_KERNEL_SCHEDULER_H )
210 #elif defined( WIN32 )
216 /*****************************************************************************
217 * vlc_threads_end: stop threads system
218 *****************************************************************************/
219 static __inline__ int vlc_threads_end( void )
221 #if defined( PTH_INIT_IN_PTH_H )
224 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
227 #elif defined( HAVE_CTHREADS_H )
230 #elif defined( HAVE_KERNEL_SCHEDULER_H )
233 #elif defined( WIN32 )
239 /*****************************************************************************
240 * vlc_mutex_init: initialize a mutex
241 *****************************************************************************/
242 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
244 #if defined( PTH_INIT_IN_PTH_H )
245 return pth_mutex_init( p_mutex );
247 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
248 return pthread_mutex_init( p_mutex, NULL );
250 #elif defined( HAVE_CTHREADS_H )
251 mutex_init( p_mutex );
254 #elif defined( HAVE_KERNEL_SCHEDULER_H )
256 /* check the arguments and whether it's already been initialized */
257 if( p_mutex == NULL )
262 if( p_mutex->init == 9999 )
267 p_mutex->lock = create_sem( 1, "BeMutex" );
268 if( p_mutex->lock < B_NO_ERROR )
273 p_mutex->init = 9999;
276 #elif defined( WIN32 )
277 *p_mutex = CreateMutex(0,FALSE,0);
278 return (*p_mutex?0:1);
283 /*****************************************************************************
284 * vlc_mutex_lock: lock a mutex
285 *****************************************************************************/
286 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
288 #if defined( PTH_INIT_IN_PTH_H )
289 return pth_mutex_acquire( p_mutex, TRUE, NULL );
291 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
292 return pthread_mutex_lock( p_mutex );
294 #elif defined( HAVE_CTHREADS_H )
295 mutex_lock( p_mutex );
298 #elif defined( HAVE_KERNEL_SCHEDULER_H )
306 if( p_mutex->init < 2000 )
311 err = acquire_sem( p_mutex->lock );
314 #elif defined( WIN32 )
315 WaitForSingleObject( *p_mutex, INFINITE );
321 /*****************************************************************************
322 * vlc_mutex_unlock: unlock a mutex
323 *****************************************************************************/
324 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
326 #if defined( PTH_INIT_IN_PTH_H )
327 return pth_mutex_release( p_mutex );
329 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
330 return pthread_mutex_unlock( p_mutex );
332 #elif defined( HAVE_CTHREADS_H )
333 mutex_unlock( p_mutex );
336 #elif defined( HAVE_KERNEL_SCHEDULER_H )
342 if( p_mutex->init < 2000 )
347 release_sem( p_mutex->lock );
350 #elif defined( WIN32 )
351 ReleaseMutex( *p_mutex );
357 /*****************************************************************************
358 * vlc_mutex_destroy: destroy a mutex
359 *****************************************************************************/
360 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
362 #if defined( PTH_INIT_IN_PTH_H )
365 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
366 return pthread_mutex_destroy( p_mutex );
368 #elif defined( HAVE_KERNEL_SCHEDULER_H )
369 if( p_mutex->init == 9999 )
371 delete_sem( p_mutex->lock );
377 #elif defined( WIN32 )
378 CloseHandle(*p_mutex);
384 /*****************************************************************************
385 * vlc_cond_init: initialize a condition
386 *****************************************************************************/
387 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
389 #if defined( PTH_INIT_IN_PTH_H )
390 return pth_cond_init( p_condvar );
392 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
393 return pthread_cond_init( p_condvar, NULL );
395 #elif defined( HAVE_CTHREADS_H )
396 /* condition_init() */
397 spin_lock_init( &p_condvar->lock );
398 cthread_queue_init( &p_condvar->queue );
400 p_condvar->implications = 0;
404 #elif defined( HAVE_KERNEL_SCHEDULER_H )
410 if( p_condvar->init == 9999 )
415 p_condvar->thread = -1;
416 p_condvar->init = 9999;
419 #elif defined( WIN32 )
420 /* Create an auto-reset event. */
421 *p_condvar = CreateEvent( NULL, /* no security */
422 FALSE, /* auto-reset event */
423 FALSE, /* non-signaled initially */
424 NULL ); /* unnamed */
426 return( *p_condvar ? 0 : 1 );
431 /*****************************************************************************
432 * vlc_cond_signal: start a thread on condition completion
433 *****************************************************************************/
434 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
436 #if defined( PTH_INIT_IN_PTH_H )
437 return pth_cond_notify( p_condvar, FALSE );
439 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
440 return pthread_cond_signal( p_condvar );
442 #elif defined( HAVE_CTHREADS_H )
443 /* condition_signal() */
444 if ( p_condvar->queue.head || p_condvar->implications )
446 cond_signal( (condition_t)p_condvar );
450 #elif defined( HAVE_KERNEL_SCHEDULER_H )
456 if( p_condvar->init < 2000 )
461 while( p_condvar->thread != -1 )
464 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
469 if( info.state != B_THREAD_SUSPENDED )
471 /* The waiting thread is not suspended so it could
472 * have been interrupted beetwen the unlock and the
473 * suspend_thread line. That is why we sleep a little
474 * before retesting p_condver->thread. */
479 /* Ok, we have to wake up that thread */
480 resume_thread( p_condvar->thread );
486 #elif defined( WIN32 )
487 /* Try to release one waiting thread. */
488 PulseEvent ( *p_condvar );
494 /*****************************************************************************
495 * vlc_cond_broadcast: start all threads waiting on condition completion
496 *****************************************************************************/
498 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
499 * Only works with pthreads, you need to adapt it for others
500 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
502 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
504 #if defined( PTH_INIT_IN_PTH_H )
505 return pth_cond_notify( p_condvar, FALSE );
507 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
508 return pthread_cond_broadcast( p_condvar );
510 #elif defined( HAVE_CTHREADS_H )
511 /* condition_signal() */
512 if ( p_condvar->queue.head || p_condvar->implications )
514 cond_signal( (condition_t)p_condvar );
518 #elif defined( HAVE_KERNEL_SCHEDULER_H )
524 if( p_condvar->init < 2000 )
529 while( p_condvar->thread != -1 )
532 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
537 if( info.state != B_THREAD_SUSPENDED )
539 /* The waiting thread is not suspended so it could
540 * have been interrupted beetwen the unlock and the
541 * suspend_thread line. That is why we sleep a little
542 * before retesting p_condver->thread. */
547 /* Ok, we have to wake up that thread */
548 resume_thread( p_condvar->thread );
554 #elif defined( WIN32 )
555 /* Try to release one waiting thread. */
556 PulseEvent ( *p_condvar );
562 /*****************************************************************************
563 * vlc_cond_wait: wait until condition completion
564 *****************************************************************************/
565 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
567 #if defined( PTH_INIT_IN_PTH_H )
568 return pth_cond_await( p_condvar, p_mutex, NULL );
570 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
571 return pthread_cond_wait( p_condvar, p_mutex );
573 #elif defined( HAVE_CTHREADS_H )
574 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
577 #elif defined( HAVE_KERNEL_SCHEDULER_H )
588 if( p_condvar->init < 2000 )
593 /* The p_condvar->thread var is initialized before the unlock because
594 * it enables to identify when the thread is interrupted beetwen the
595 * unlock line and the suspend_thread line */
596 p_condvar->thread = find_thread( NULL );
597 vlc_mutex_unlock( p_mutex );
598 suspend_thread( p_condvar->thread );
599 p_condvar->thread = -1;
601 vlc_mutex_lock( p_mutex );
604 #elif defined( WIN32 )
605 /* Release the <external_mutex> here and wait for the event
606 * to become signaled, due to <pthread_cond_signal> being
608 vlc_mutex_unlock( p_mutex );
610 WaitForSingleObject( *p_condvar, INFINITE );
612 /* Reacquire the mutex before returning. */
613 vlc_mutex_lock( p_mutex );
619 /*****************************************************************************
620 * vlc_cond_destroy: destroy a condition
621 *****************************************************************************/
622 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
624 #if defined( PTH_INIT_IN_PTH_H )
627 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
628 return pthread_cond_destroy( p_condvar );
630 #elif defined( HAVE_KERNEL_SCHEDULER_H )
634 #elif defined( WIN32 )
635 CloseHandle( *p_condvar );
641 /*****************************************************************************
642 * vlc_thread_create: create a thread
643 *****************************************************************************/
644 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
645 char *psz_name, vlc_thread_func_t func,
653 /* Initialize the wrapper structure */
655 wrapper.p_data = p_data;
656 getitimer( ITIMER_PROF, &wrapper.itimer );
657 vlc_mutex_init( &wrapper.lock );
658 vlc_cond_init( &wrapper.wait );
659 vlc_mutex_lock( &wrapper.lock );
661 /* Alter user-passed data so that we call the wrapper instead
662 * of the real function */
664 func = vlc_thread_wrapper;
667 #if defined( PTH_INIT_IN_PTH_H )
668 *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
669 i_ret = ( p_thread == NULL );
671 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
672 i_ret = pthread_create( p_thread, NULL, func, p_data );
674 #elif defined( HAVE_CTHREADS_H )
675 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
678 #elif defined( HAVE_KERNEL_SCHEDULER_H )
679 *p_thread = spawn_thread( (thread_func)func, psz_name,
680 B_NORMAL_PRIORITY, p_data );
681 i_ret = resume_thread( *p_thread );
683 #elif defined( WIN32 )
686 /* This method is not recommended when using the MSVCRT C library,
687 * so we'll have to use _beginthreadex instead */
688 *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func,
689 p_data, 0, &threadID);
692 /* When using the MSVCRT C library you have to use the _beginthreadex
693 * function instead of CreateThread, otherwise you'll end up with memory
694 * leaks and the signal function not working */
695 *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func,
696 p_data, 0, &threadID);
698 i_ret = ( *p_thread ? 0 : 1 );
705 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
708 vlc_mutex_unlock( &wrapper.lock );
709 vlc_mutex_destroy( &wrapper.lock );
710 vlc_cond_destroy( &wrapper.wait );
716 /*****************************************************************************
717 * vlc_thread_exit: terminate a thread
718 *****************************************************************************/
719 static __inline__ void vlc_thread_exit( void )
721 #if defined( PTH_INIT_IN_PTH_H )
724 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
727 #elif defined( HAVE_CTHREADS_H )
729 cthread_exit( &result );
731 #elif defined( HAVE_KERNEL_SCHEDULER_H )
734 #elif defined( WIN32 )
738 /* For now we don't close the thread handles (because of race conditions).
739 * Need to be looked at. */
745 /*****************************************************************************
746 * vlc_thread_join: wait until a thread exits
747 *****************************************************************************/
748 static __inline__ void vlc_thread_join( vlc_thread_t thread )
750 #if defined( PTH_INIT_IN_PTH_H )
751 pth_join( thread, NULL );
753 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
754 pthread_join( thread, NULL );
756 #elif defined( HAVE_CTHREADS_H )
757 cthread_join( thread );
759 #elif defined( HAVE_KERNEL_SCHEDULER_H )
761 wait_for_thread( thread, &exit_value );
763 #elif defined( WIN32 )
764 WaitForSingleObject( thread, INFINITE);
770 static void *vlc_thread_wrapper( void *p_wrapper )
772 /* Put user data in thread-local variables */
773 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
774 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
776 /* Set the profile timer value */
777 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
779 /* Tell the calling thread that we don't need its data anymore */
780 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
781 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
782 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
784 /* Call the real function */
785 return func( p_data );