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.16 2001/03/21 13:42:33 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(HAVE_PTHREAD_H) /* pthreads (Linux & BSD for example) */
31 #elif defined(HAVE_CTHREADS_H) /* GNUMach */
34 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H) /* BeOS */
37 #include <kernel/OS.h>
38 #include <kernel/scheduler.h>
39 #include <byteorder.h>
41 #error no threads available on your system !
44 /*****************************************************************************
46 *****************************************************************************
47 * These constants are used by all threads in *_CreateThread() and
48 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
49 * value is used as a shared flag to represent the status of the thread.
50 *****************************************************************************/
52 /* Void status - this value can be used to make sure no operation is currently
53 * in progress on the concerned thread in an array of recorded threads */
54 #define THREAD_NOP 0 /* nothing happened */
57 #define THREAD_CREATE 10 /* thread is initializing */
58 #define THREAD_START 11 /* thread has forked */
59 #define THREAD_READY 19 /* thread is ready */
61 /* Destructions status */
62 #define THREAD_DESTROY 20 /* destruction order has been sent */
63 #define THREAD_END 21 /* destruction order has been received */
64 #define THREAD_OVER 29 /* thread does not exist any more */
67 #define THREAD_ERROR 30 /* an error occured */
68 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
70 /*****************************************************************************
72 *****************************************************************************/
74 #if defined(HAVE_PTHREAD_H)
76 typedef pthread_t vlc_thread_t;
77 typedef pthread_mutex_t vlc_mutex_t;
78 typedef pthread_cond_t vlc_cond_t;
80 #elif defined(HAVE_CTHREADS_H)
82 typedef cthread_t vlc_thread_t;
84 /* those structs are the ones defined in /include/cthreads.h but we need
85 * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
86 * foo is a (mutex_t*) */
87 typedef struct s_mutex {
91 struct cthread_queue queue;
94 typedef struct s_condition {
96 struct cthread_queue queue;
98 struct cond_imp *implications;
101 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
103 /* This is the BeOS implementation of the vlc thread, note that the mutex is
104 * not a real mutex and the cond_var is not like a pthread cond_var but it is
105 * enough for what wee need */
107 typedef thread_id vlc_thread_t;
123 typedef void *(*vlc_thread_func_t)(void *p_data);
125 /*****************************************************************************
127 *****************************************************************************/
129 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread, char *psz_name,
130 vlc_thread_func_t func, void *p_data );
131 static __inline__ void vlc_thread_exit ( void );
132 static __inline__ void vlc_thread_join ( vlc_thread_t thread );
134 static __inline__ int vlc_mutex_init ( vlc_mutex_t *p_mutex );
135 static __inline__ int vlc_mutex_lock ( vlc_mutex_t *p_mutex );
136 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t *p_mutex );
137 static __inline__ int vlc_mutex_destroy ( vlc_mutex_t *p_mutex );
139 static __inline__ int vlc_cond_init ( vlc_cond_t *p_condvar );
140 static __inline__ int vlc_cond_signal ( vlc_cond_t *p_condvar );
141 static __inline__ int vlc_cond_wait ( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex );
142 static __inline__ int vlc_cond_destroy ( vlc_cond_t *p_condvar );
145 static _inline__ int vlc_cond_timedwait ( vlc_cond_t * condvar, vlc_mutex_t * mutex,
146 mtime_t absoute_timeout_time );
149 /*****************************************************************************
150 * vlc_thread_create: create a thread
151 *****************************************************************************/
152 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
153 char *psz_name, vlc_thread_func_t func,
156 #if defined(HAVE_PTHREAD_H)
157 return pthread_create( p_thread, NULL, func, p_data );
159 #elif defined(HAVE_CTHREADS_H)
160 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
163 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
164 *p_thread = spawn_thread( (thread_func)func, psz_name,
165 B_NORMAL_PRIORITY, p_data );
166 return resume_thread( *p_thread );
171 /*****************************************************************************
172 * vlc_thread_exit: terminate a thread
173 *****************************************************************************/
174 static __inline__ void vlc_thread_exit( void )
176 #if defined(HAVE_PTHREAD_H)
179 #elif defined(HAVE_CTHREADS_H)
181 cthread_exit( &result );
183 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
189 /*****************************************************************************
190 * vlc_thread_join: wait until a thread exits
191 *****************************************************************************/
192 static __inline__ void vlc_thread_join( vlc_thread_t thread )
194 #if defined(HAVE_PTHREAD_H)
195 pthread_join( thread, NULL );
197 #elif defined(HAVE_CTHREADS_H)
198 cthread_join( thread );
200 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
202 wait_for_thread( thread, &exit_value );
207 /*****************************************************************************
208 * vlc_mutex_init: initialize a mutex
209 *****************************************************************************/
210 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
212 #if defined(HAVE_PTHREAD_H)
213 return pthread_mutex_init( p_mutex, NULL );
215 #elif defined(HAVE_CTHREADS_H)
216 mutex_init( p_mutex );
219 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
221 /* check the arguments and whether it's already been initialized */
222 if( p_mutex == NULL ) return B_BAD_VALUE;
223 if( p_mutex->init == 9999 )
228 p_mutex->lock = create_sem( 1, "BeMutex" );
229 if( p_mutex->lock < B_NO_ERROR )
231 p_mutex->init = 9999;
237 /*****************************************************************************
238 * vlc_mutex_lock: lock a mutex
239 *****************************************************************************/
240 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
242 #if defined(HAVE_PTHREAD_H)
243 return pthread_mutex_lock( p_mutex );
245 #elif defined(HAVE_CTHREADS_H)
246 mutex_lock( p_mutex );
249 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
252 if( !p_mutex ) return B_BAD_VALUE;
253 if( p_mutex->init < 2000 ) return B_NO_INIT;
255 err = acquire_sem( p_mutex->lock );
261 /*****************************************************************************
262 * vlc_mutex_unlock: unlock a mutex
263 *****************************************************************************/
264 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
266 #if defined(HAVE_PTHREAD_H)
267 return pthread_mutex_unlock( p_mutex );
269 #elif defined(HAVE_CTHREADS_H)
270 mutex_unlock( p_mutex );
273 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
275 if(! p_mutex) return B_BAD_VALUE;
276 if( p_mutex->init < 2000 ) return B_NO_INIT;
278 release_sem( p_mutex->lock );
284 /*****************************************************************************
285 * vlc_mutex_destroy: destroy a mutex
286 *****************************************************************************/
287 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
289 #if defined(HAVE_PTHREAD_H)
290 return pthread_mutex_destroy( p_mutex );
291 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
292 if( p_mutex->init == 9999 )
293 delete_sem( p_mutex->lock );
299 /*****************************************************************************
300 * vlc_cond_init: initialize a condition
301 *****************************************************************************/
302 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
304 #if defined(HAVE_PTHREAD_H)
305 return pthread_cond_init( p_condvar, NULL );
307 #elif defined(HAVE_CTHREADS_H)
308 /* condition_init() */
309 spin_lock_init( &p_condvar->lock );
310 cthread_queue_init( &p_condvar->queue );
312 p_condvar->implications = 0;
316 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
320 if( p_condvar->init == 9999 )
323 p_condvar->thread = -1;
324 p_condvar->init = 9999;
330 /*****************************************************************************
331 * vlc_cond_signal: start a thread on condition completion
332 *****************************************************************************/
333 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
335 #if defined(HAVE_PTHREAD_H)
336 return pthread_cond_signal( p_condvar );
338 #elif defined(HAVE_CTHREADS_H)
339 /* condition_signal() */
340 if ( p_condvar->queue.head || p_condvar->implications )
342 cond_signal( (condition_t)p_condvar );
346 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
350 if( p_condvar->init < 2000 )
352 while( p_condvar->thread != -1 )
355 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
358 if( info.state != B_THREAD_SUSPENDED )
360 // The waiting thread is not suspended so it could
361 // have been interrupted beetwen the unlock and the
362 // suspend_thread line. That is why we sleep a little
363 // before retesting p_condver->thread.
368 // Ok, we have to wake up that thread
369 resume_thread( p_condvar->thread );
378 /*****************************************************************************
379 * vlc_cond_wait: wait until condition completion
380 *****************************************************************************/
381 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
383 #if defined(HAVE_PTHREAD_H)
384 return pthread_cond_wait( p_condvar, p_mutex );
386 #elif defined(HAVE_CTHREADS_H)
387 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
390 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
397 if( p_condvar->init < 2000 )
400 // The p_condvar->thread var is initialized before the unlock because
401 // it enables to identify when the thread is interrupted beetwen the
402 // unlock line and the suspend_thread line
403 p_condvar->thread = find_thread( NULL );
404 vlc_mutex_unlock( p_mutex );
405 suspend_thread( p_condvar->thread );
406 p_condvar->thread = -1;
408 vlc_mutex_lock( p_mutex );
414 /*****************************************************************************
415 * vlc_cond_destroy: destroy a condition
416 *****************************************************************************/
417 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
419 #if defined(HAVE_PTHREAD_H)
420 return pthread_cond_destroy( p_condvar );
421 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)