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.18 2001/06/14 01:49:44 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 *****************************************************************************/
28 #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
31 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) /* pthreads (like Linux & BSD) */
34 #elif defined( HAVE_CTHREADS_H ) /* GNUMach */
35 # include <cthreads.h>
37 #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
40 # include <kernel/OS.h>
41 # include <kernel/scheduler.h>
42 # include <byteorder.h>
44 #elif defined( WIN32 ) /* Win32 with MinGW32 compiler */
49 # error no threads available on your system !
53 /*****************************************************************************
55 *****************************************************************************
56 * These constants are used by all threads in *_CreateThread() and
57 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
58 * value is used as a shared flag to represent the status of the thread.
59 *****************************************************************************/
61 /* Void status - this value can be used to make sure no operation is currently
62 * in progress on the concerned thread in an array of recorded threads */
63 #define THREAD_NOP 0 /* nothing happened */
66 #define THREAD_CREATE 10 /* thread is initializing */
67 #define THREAD_START 11 /* thread has forked */
68 #define THREAD_READY 19 /* thread is ready */
70 /* Destructions status */
71 #define THREAD_DESTROY 20 /* destruction order has been sent */
72 #define THREAD_END 21 /* destruction order has been received */
73 #define THREAD_OVER 29 /* thread does not exist any more */
76 #define THREAD_ERROR 30 /* an error occured */
77 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
79 /*****************************************************************************
81 *****************************************************************************/
83 #if defined( PTH_INIT_IN_PTH_H )
84 typedef pth_t vlc_thread_t;
85 typedef pth_mutex_t vlc_mutex_t;
86 typedef pth_cond_t vlc_cond_t;
88 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
89 typedef pthread_t vlc_thread_t;
90 typedef pthread_mutex_t vlc_mutex_t;
91 typedef pthread_cond_t vlc_cond_t;
93 #elif defined( HAVE_CTHREADS_H )
94 typedef cthread_t vlc_thread_t;
96 /* Those structs are the ones defined in /include/cthreads.h but we need
97 * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
98 * foo is a (mutex_t*) */
99 typedef struct s_mutex {
103 struct cthread_queue queue;
106 typedef struct s_condition {
108 struct cthread_queue queue;
110 struct cond_imp *implications;
113 #elif defined( HAVE_KERNEL_SCHEDULER_H )
114 /* This is the BeOS implementation of the vlc threads, note that the mutex is
115 * not a real mutex and the cond_var is not like a pthread cond_var but it is
116 * enough for what wee need */
118 typedef thread_id vlc_thread_t;
132 #elif defined( WIN32 )
133 typedef HANDLE vlc_thread_t;
134 typedef HANDLE vlc_mutex_t;
135 typedef HANDLE vlc_cond_t;
136 typedef unsigned (__stdcall *PTHREAD_START) (void *);
140 typedef void *(*vlc_thread_func_t)(void *p_data);
142 /*****************************************************************************
144 *****************************************************************************/
146 static __inline__ int vlc_thread_create ( vlc_thread_t *, char *,
147 vlc_thread_func_t, void * );
148 static __inline__ void vlc_thread_exit ( void );
149 static __inline__ void vlc_thread_join ( vlc_thread_t );
151 static __inline__ int vlc_mutex_init ( vlc_mutex_t * );
152 static __inline__ int vlc_mutex_lock ( vlc_mutex_t * );
153 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t * );
154 static __inline__ int vlc_mutex_destroy ( vlc_mutex_t * );
156 static __inline__ int vlc_cond_init ( vlc_cond_t * );
157 static __inline__ int vlc_cond_signal ( vlc_cond_t * );
158 static __inline__ int vlc_cond_wait ( vlc_cond_t *, vlc_mutex_t * );
159 static __inline__ int vlc_cond_destroy ( vlc_cond_t * );
162 static __inline__ int vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
166 /*****************************************************************************
167 * vlc_thread_create: create a thread
168 *****************************************************************************/
169 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
170 char *psz_name, vlc_thread_func_t func,
173 #if defined( PTH_INIT_IN_PTH_H )
174 *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
175 return ( p_thread == NULL );
177 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
178 return pthread_create( p_thread, NULL, func, p_data );
180 #elif defined( HAVE_CTHREADS_H )
181 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
184 #elif defined( HAVE_KERNEL_SCHEDULER_H )
185 *p_thread = spawn_thread( (thread_func)func, psz_name,
186 B_NORMAL_PRIORITY, p_data );
187 return resume_thread( *p_thread );
189 #elif defined( WIN32 )
192 /* This method is not recommended when using the MSVCRT C library,
193 * so we'll have to use _beginthreadex instead */
194 *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func,
195 p_data, 0, &threadID);
198 /* When using the MSVCRT C library you have to use the _beginthreadex
199 * function instead of CreateThread, otherwise you'll end up with memory
200 * leaks and the signal function not working */
201 *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func,
202 p_data, 0, &threadID);
204 return( *p_thread ? 0 : 1 );
209 /*****************************************************************************
210 * vlc_thread_exit: terminate a thread
211 *****************************************************************************/
212 static __inline__ void vlc_thread_exit( void )
214 #if defined( PTH_INIT_IN_PTH_H )
217 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
220 #elif defined( HAVE_CTHREADS_H )
222 cthread_exit( &result );
224 #elif defined( HAVE_KERNEL_SCHEDULER_H )
227 #elif defined( WIN32 )
231 /* For now we don't close the thread handles (because of race conditions).
232 * Need to be looked at. */
238 /*****************************************************************************
239 * vlc_thread_join: wait until a thread exits
240 *****************************************************************************/
241 static __inline__ void vlc_thread_join( vlc_thread_t thread )
243 #if defined( PTH_INIT_IN_PTH_H )
244 pth_join( thread, NULL );
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247 pthread_join( thread, NULL );
249 #elif defined( HAVE_CTHREADS_H )
250 cthread_join( thread );
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
254 wait_for_thread( thread, &exit_value );
256 #elif defined( WIN32 )
257 WaitForSingleObject( thread, INFINITE);
262 /*****************************************************************************
263 * vlc_mutex_init: initialize a mutex
264 *****************************************************************************/
265 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
267 #if defined( PTH_INIT_IN_PTH_H )
268 return pth_mutex_init( p_mutex );
270 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
271 return pthread_mutex_init( p_mutex, NULL );
273 #elif defined( HAVE_CTHREADS_H )
274 mutex_init( p_mutex );
277 #elif defined( HAVE_KERNEL_SCHEDULER_H )
279 /* check the arguments and whether it's already been initialized */
280 if( p_mutex == NULL )
285 if( p_mutex->init == 9999 )
290 p_mutex->lock = create_sem( 1, "BeMutex" );
291 if( p_mutex->lock < B_NO_ERROR )
296 p_mutex->init = 9999;
299 #elif defined( WIN32 )
300 *p_mutex = CreateMutex(0,FALSE,0);
301 return (*p_mutex?0:1);
306 /*****************************************************************************
307 * vlc_mutex_lock: lock a mutex
308 *****************************************************************************/
309 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
311 #if defined( PTH_INIT_IN_PTH_H )
312 return pth_mutex_acquire( p_mutex, TRUE, NULL );
314 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
315 return pthread_mutex_lock( p_mutex );
317 #elif defined( HAVE_CTHREADS_H )
318 mutex_lock( p_mutex );
321 #elif defined( HAVE_KERNEL_SCHEDULER_H )
329 if( p_mutex->init < 2000 )
334 err = acquire_sem( p_mutex->lock );
337 #elif defined( WIN32 )
338 WaitForSingleObject( *p_mutex, INFINITE );
344 /*****************************************************************************
345 * vlc_mutex_unlock: unlock a mutex
346 *****************************************************************************/
347 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
349 #if defined( PTH_INIT_IN_PTH_H )
350 return pth_mutex_release( p_mutex );
352 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
353 return pthread_mutex_unlock( p_mutex );
355 #elif defined( HAVE_CTHREADS_H )
356 mutex_unlock( p_mutex );
359 #elif defined( HAVE_KERNEL_SCHEDULER_H )
365 if( p_mutex->init < 2000 )
370 release_sem( p_mutex->lock );
373 #elif defined( WIN32 )
374 ReleaseMutex( *p_mutex );
380 /*****************************************************************************
381 * vlc_mutex_destroy: destroy a mutex
382 *****************************************************************************/
383 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
385 #if defined( PTH_INIT_IN_PTH_H )
388 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
389 return pthread_mutex_destroy( p_mutex );
391 #elif defined( HAVE_KERNEL_SCHEDULER_H )
392 if( p_mutex->init == 9999 )
394 delete_sem( p_mutex->lock );
400 #elif defined( WIN32 )
401 CloseHandle(*p_mutex);
407 /*****************************************************************************
408 * vlc_cond_init: initialize a condition
409 *****************************************************************************/
410 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
412 #if defined( PTH_INIT_IN_PTH_H )
413 return pth_cond_init( p_condvar );
415 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
416 return pthread_cond_init( p_condvar, NULL );
418 #elif defined( HAVE_CTHREADS_H )
419 /* condition_init() */
420 spin_lock_init( &p_condvar->lock );
421 cthread_queue_init( &p_condvar->queue );
423 p_condvar->implications = 0;
427 #elif defined( HAVE_KERNEL_SCHEDULER_H )
433 if( p_condvar->init == 9999 )
438 p_condvar->thread = -1;
439 p_condvar->init = 9999;
442 #elif defined( WIN32 )
443 /* Create an auto-reset event. */
444 *p_condvar = CreateEvent( NULL, /* no security */
445 FALSE, /* auto-reset event */
446 FALSE, /* non-signaled initially */
447 NULL ); /* unnamed */
449 return( *p_condvar ? 0 : 1 );
454 /*****************************************************************************
455 * vlc_cond_signal: start a thread on condition completion
456 *****************************************************************************/
457 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
459 #if defined( PTH_INIT_IN_PTH_H )
460 return pth_cond_notify( p_condvar, FALSE );
462 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
463 return pthread_cond_signal( p_condvar );
465 #elif defined( HAVE_CTHREADS_H )
466 /* condition_signal() */
467 if ( p_condvar->queue.head || p_condvar->implications )
469 cond_signal( (condition_t)p_condvar );
473 #elif defined( HAVE_KERNEL_SCHEDULER_H )
479 if( p_condvar->init < 2000 )
484 while( p_condvar->thread != -1 )
487 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
492 if( info.state != B_THREAD_SUSPENDED )
494 /* The waiting thread is not suspended so it could
495 * have been interrupted beetwen the unlock and the
496 * suspend_thread line. That is why we sleep a little
497 * before retesting p_condver->thread. */
502 /* Ok, we have to wake up that thread */
503 resume_thread( p_condvar->thread );
509 #elif defined( WIN32 )
510 /* Try to release one waiting thread. */
511 PulseEvent ( *p_condvar );
517 /*****************************************************************************
518 * vlc_cond_wait: wait until condition completion
519 *****************************************************************************/
520 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
522 #if defined( PTH_INIT_IN_PTH_H )
523 return pth_cond_await( p_condvar, p_mutex, NULL );
525 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
526 return pthread_cond_wait( p_condvar, p_mutex );
528 #elif defined( HAVE_CTHREADS_H )
529 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
532 #elif defined( HAVE_KERNEL_SCHEDULER_H )
543 if( p_condvar->init < 2000 )
548 /* The p_condvar->thread var is initialized before the unlock because
549 * it enables to identify when the thread is interrupted beetwen the
550 * unlock line and the suspend_thread line */
551 p_condvar->thread = find_thread( NULL );
552 vlc_mutex_unlock( p_mutex );
553 suspend_thread( p_condvar->thread );
554 p_condvar->thread = -1;
556 vlc_mutex_lock( p_mutex );
559 #elif defined( WIN32 )
560 /* Release the <external_mutex> here and wait for the event
561 * to become signaled, due to <pthread_cond_signal> being
563 vlc_mutex_unlock( p_mutex );
565 WaitForSingleObject( *p_condvar, INFINITE );
567 /* Reacquire the mutex before returning. */
568 vlc_mutex_lock( p_mutex );
574 /*****************************************************************************
575 * vlc_cond_destroy: destroy a condition
576 *****************************************************************************/
577 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
579 #if defined( PTH_INIT_IN_PTH_H )
582 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
583 return pthread_cond_destroy( p_condvar );
585 #elif defined( HAVE_KERNEL_SCHEDULER_H )
589 #elif defined( WIN32 )
590 CloseHandle( *p_condvar );