]> git.sesse.net Git - vlc/blob - include/threads.h
1bc62cc4c77cdd4c49983bfe33d4412fbc5d50d0
[vlc] / include / threads.h
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 $
7  *
8  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9  *          Samuel Hocevar <sam@via.ecp.fr>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 #include <stdio.h>
27
28 #if defined(HAVE_PTHREAD_H)            /* pthreads (Linux & BSD for example) */
29 #include <pthread.h>
30
31 #elif defined(HAVE_CTHREADS_H)                                    /* GNUMach */
32 #include <cthreads.h>
33
34 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)   /* BeOS */
35 #undef MAX
36 #undef MIN
37 #include <kernel/OS.h>
38 #include <kernel/scheduler.h>
39 #include <byteorder.h>
40 #else
41 #error no threads available on your system !
42 #endif
43
44 /*****************************************************************************
45  * Constants
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  *****************************************************************************/
51
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 */
55
56 /* Creation status */
57 #define THREAD_CREATE       10                     /* thread is initializing */
58 #define THREAD_START        11                          /* thread has forked */
59 #define THREAD_READY        19                            /* thread is ready */
60
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 */
65
66 /* Error status */
67 #define THREAD_ERROR        30                           /* an error occured */
68 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
69
70 /*****************************************************************************
71  * Types definition
72  *****************************************************************************/
73
74 #if defined(HAVE_PTHREAD_H)
75
76 typedef pthread_t        vlc_thread_t;
77 typedef pthread_mutex_t  vlc_mutex_t;
78 typedef pthread_cond_t   vlc_cond_t;
79
80 #elif defined(HAVE_CTHREADS_H)
81
82 typedef cthread_t        vlc_thread_t;
83
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 {
88     spin_lock_t held;
89     spin_lock_t lock;
90     char *name;
91     struct cthread_queue queue;
92 } vlc_mutex_t;
93
94 typedef struct s_condition {
95     spin_lock_t lock;
96     struct cthread_queue queue;
97     char *name;
98     struct cond_imp *implications;
99 } vlc_cond_t;
100
101 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
102
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 */
106
107 typedef thread_id vlc_thread_t;
108
109 typedef struct
110 {
111     int32           init;
112     sem_id          lock;
113 } vlc_mutex_t;
114
115 typedef struct
116 {
117     int32           init;
118     thread_id       thread;
119 } vlc_cond_t;
120
121 #endif
122
123 typedef void *(*vlc_thread_func_t)(void *p_data);
124
125 /*****************************************************************************
126  * Prototypes
127  *****************************************************************************/
128
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 );
133
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 );
138
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 );
143
144 #if 0
145 static _inline__ int    vlc_cond_timedwait   ( vlc_cond_t * condvar, vlc_mutex_t * mutex,
146                               mtime_t absoute_timeout_time );
147 #endif
148
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,
154                                          void *p_data)
155 {
156 #if defined(HAVE_PTHREAD_H)
157     return pthread_create( p_thread, NULL, func, p_data );
158
159 #elif defined(HAVE_CTHREADS_H)
160     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
161     return 0;
162
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 );
167
168 #endif
169 }
170
171 /*****************************************************************************
172  * vlc_thread_exit: terminate a thread
173  *****************************************************************************/
174 static __inline__ void vlc_thread_exit( void )
175 {
176 #if defined(HAVE_PTHREAD_H)
177     pthread_exit( 0 );
178
179 #elif defined(HAVE_CTHREADS_H)
180     int result;
181     cthread_exit( &result );
182
183 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
184     exit_thread( 0 );
185
186 #endif
187 }
188
189 /*****************************************************************************
190  * vlc_thread_join: wait until a thread exits
191  *****************************************************************************/
192 static __inline__ void vlc_thread_join( vlc_thread_t thread )
193 {
194 #if defined(HAVE_PTHREAD_H)
195     pthread_join( thread, NULL );
196
197 #elif defined(HAVE_CTHREADS_H)
198     cthread_join( thread );
199
200 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
201     int32 exit_value;
202     wait_for_thread( thread, &exit_value );
203
204 #endif
205 }
206
207 /*****************************************************************************
208  * vlc_mutex_init: initialize a mutex
209  *****************************************************************************/
210 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
211 {
212 #if defined(HAVE_PTHREAD_H)
213     return pthread_mutex_init( p_mutex, NULL );
214
215 #elif defined(HAVE_CTHREADS_H)
216     mutex_init( p_mutex );
217     return 0;
218
219 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
220
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 )
224     {
225         return EALREADY;
226     }
227
228     p_mutex->lock = create_sem( 1, "BeMutex" );
229     if( p_mutex->lock < B_NO_ERROR )
230         return( -1 );
231     p_mutex->init = 9999;
232     return B_OK;
233
234 #endif
235 }
236
237 /*****************************************************************************
238  * vlc_mutex_lock: lock a mutex
239  *****************************************************************************/
240 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
241 {
242 #if defined(HAVE_PTHREAD_H)
243     return pthread_mutex_lock( p_mutex );
244
245 #elif defined(HAVE_CTHREADS_H)
246     mutex_lock( p_mutex );
247     return 0;
248
249 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
250     status_t err;
251
252     if( !p_mutex ) return B_BAD_VALUE;
253     if( p_mutex->init < 2000 ) return B_NO_INIT;
254
255     err = acquire_sem( p_mutex->lock );
256     return err;
257
258 #endif
259 }
260
261 /*****************************************************************************
262  * vlc_mutex_unlock: unlock a mutex
263  *****************************************************************************/
264 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
265 {
266 #if defined(HAVE_PTHREAD_H)
267     return pthread_mutex_unlock( p_mutex );
268
269 #elif defined(HAVE_CTHREADS_H)
270     mutex_unlock( p_mutex );
271     return 0;
272
273 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
274
275     if(! p_mutex) return B_BAD_VALUE;
276     if( p_mutex->init < 2000 ) return B_NO_INIT;
277
278     release_sem( p_mutex->lock );
279     return B_OK;
280
281 #endif
282 }
283
284 /*****************************************************************************
285  * vlc_mutex_destroy: destroy a mutex
286  *****************************************************************************/
287 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
288 {
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 );
294     p_mutex->init = 0;
295     return B_OK;
296 #endif    
297 }
298
299 /*****************************************************************************
300  * vlc_cond_init: initialize a condition
301  *****************************************************************************/
302 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
303 {
304 #if defined(HAVE_PTHREAD_H)
305     return pthread_cond_init( p_condvar, NULL );
306
307 #elif defined(HAVE_CTHREADS_H)
308     /* condition_init() */
309     spin_lock_init( &p_condvar->lock );
310     cthread_queue_init( &p_condvar->queue );
311     p_condvar->name = 0;
312     p_condvar->implications = 0;
313
314     return 0;
315
316 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
317     if( !p_condvar )
318         return B_BAD_VALUE;
319
320     if( p_condvar->init == 9999 )
321         return EALREADY;
322
323     p_condvar->thread = -1;
324     p_condvar->init = 9999;
325     return 0;
326
327 #endif
328 }
329
330 /*****************************************************************************
331  * vlc_cond_signal: start a thread on condition completion
332  *****************************************************************************/
333 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
334 {
335 #if defined(HAVE_PTHREAD_H)
336     return pthread_cond_signal( p_condvar );
337
338 #elif defined(HAVE_CTHREADS_H)
339     /* condition_signal() */
340     if ( p_condvar->queue.head || p_condvar->implications )
341     {
342         cond_signal( (condition_t)p_condvar );
343     }
344     return 0;
345
346 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
347     if( !p_condvar )
348         return B_BAD_VALUE;
349
350     if( p_condvar->init < 2000 )
351         return B_NO_INIT;
352     while( p_condvar->thread != -1 )
353     {
354         thread_info info;
355         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
356             return 0;
357
358         if( info.state != B_THREAD_SUSPENDED )
359         {
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.
364             snooze( 10000 );
365         }
366         else
367         {
368             // Ok, we have to wake up that thread
369             resume_thread( p_condvar->thread );
370             return 0;
371         }
372     }
373     return 0;
374
375 #endif
376 }
377
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 )
382 {
383 #if defined(HAVE_PTHREAD_H)
384     return pthread_cond_wait( p_condvar, p_mutex );
385
386 #elif defined(HAVE_CTHREADS_H)
387     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
388     return 0;
389
390 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
391     if( !p_condvar )
392         return B_BAD_VALUE;
393
394     if( !p_mutex )
395         return B_BAD_VALUE;
396
397     if( p_condvar->init < 2000 )
398         return B_NO_INIT;
399
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;
407
408     vlc_mutex_lock( p_mutex );
409     return 0;
410
411 #endif
412 }
413
414 /*****************************************************************************
415  * vlc_cond_destroy: destroy a condition
416  *****************************************************************************/
417 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
418 {
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)
422     p_condvar->init = 0;
423     return 0;
424 #endif    
425 }