]> git.sesse.net Git - vlc/blob - include/threads.h
a36c95c08dd889586dc0dbc441a037d298852e59
[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  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <stdio.h>
26
27 #if defined(HAVE_PTHREAD_H)            /* pthreads (Linux & BSD for example) */
28 #include <pthread.h>
29
30 #elif defined(HAVE_CTHREADS_H)                                    /* GNUMach */
31 #include <cthreads.h>
32
33 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)   /* BeOS */
34 #undef MAX
35 #undef MIN
36 #include <kernel/OS.h>
37 #include <kernel/scheduler.h>
38 #include <byteorder.h>
39 #else
40 #error no threads available on your system !
41 #endif
42
43 /*****************************************************************************
44  * Constants
45  *****************************************************************************
46  * These constants are used by all threads in *_CreateThread() and
47  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
48  * value is used as a shared flag to represent the status of the thread.
49  *****************************************************************************/
50
51 /* Void status - this value can be used to make sure no operation is currently
52  * in progress on the concerned thread in an array of recorded threads */
53 #define THREAD_NOP          0                            /* nothing happened */
54
55 /* Creation status */
56 #define THREAD_CREATE       10                     /* thread is initializing */
57 #define THREAD_START        11                          /* thread has forked */
58 #define THREAD_READY        19                            /* thread is ready */
59
60 /* Destructions status */
61 #define THREAD_DESTROY      20            /* destruction order has been sent */
62 #define THREAD_END          21        /* destruction order has been received */
63 #define THREAD_OVER         29             /* thread does not exist any more */
64
65 /* Error status */
66 #define THREAD_ERROR        30                           /* an error occured */
67 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
68
69 /*****************************************************************************
70  * Types definition
71  *****************************************************************************/
72
73 #if defined(HAVE_PTHREAD_H)
74
75 typedef pthread_t        vlc_thread_t;
76 typedef pthread_mutex_t  vlc_mutex_t;
77 typedef pthread_cond_t   vlc_cond_t;
78
79 #elif defined(HAVE_CTHREADS_H)
80
81 typedef cthread_t        vlc_thread_t;
82
83 /* those structs are the ones defined in /include/cthreads.h but we need
84  * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
85  * foo is a (mutex_t*) */
86 typedef struct s_mutex {
87     spin_lock_t held;
88     spin_lock_t lock;
89     char *name;
90     struct cthread_queue queue;
91 } vlc_mutex_t;
92
93 typedef struct s_condition {
94     spin_lock_t lock;
95     struct cthread_queue queue;
96     char *name;
97     struct cond_imp *implications;
98 } vlc_cond_t;
99
100 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
101
102 /* This is the BeOS implementation of the vlc thread, note that the mutex is
103  * not a real mutex and the cond_var is not like a pthread cond_var but it is
104  * enough for what wee need */
105
106 typedef thread_id vlc_thread_t;
107
108 typedef struct
109 {
110     int32           init;
111     sem_id          lock;
112 } vlc_mutex_t;
113
114 typedef struct
115 {
116     int32           init;
117     thread_id       thread;
118 } vlc_cond_t;
119
120 #endif
121
122 typedef void *(*vlc_thread_func_t)(void *p_data);
123
124 /*****************************************************************************
125  * Prototypes
126  *****************************************************************************/
127
128 static __inline__ int  vlc_thread_create( vlc_thread_t *p_thread, char *psz_name,
129                                           vlc_thread_func_t func, void *p_data );
130 static __inline__ void vlc_thread_exit  ( void );
131 static __inline__ void vlc_thread_join  ( vlc_thread_t thread );
132
133 static __inline__ int  vlc_mutex_init    ( vlc_mutex_t *p_mutex );
134 static __inline__ int  vlc_mutex_lock    ( vlc_mutex_t *p_mutex );
135 static __inline__ int  vlc_mutex_unlock  ( vlc_mutex_t *p_mutex );
136 static __inline__ int  vlc_mutex_destroy ( vlc_mutex_t *p_mutex );
137
138 static __inline__ int  vlc_cond_init    ( vlc_cond_t *p_condvar );
139 static __inline__ int  vlc_cond_signal  ( vlc_cond_t *p_condvar );
140 static __inline__ int  vlc_cond_wait    ( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex );
141 static __inline__ int vlc_cond_destroy  ( vlc_cond_t *p_condvar );
142
143 #if 0
144 static _inline__ int    vlc_cond_timedwait   ( vlc_cond_t * condvar, vlc_mutex_t * mutex,
145                               mtime_t absoute_timeout_time );
146 #endif
147
148 /*****************************************************************************
149  * vlc_thread_create: create a thread
150  *****************************************************************************/
151 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
152                                          char *psz_name, vlc_thread_func_t func,
153                                          void *p_data)
154 {
155 #if defined(HAVE_PTHREAD_H)
156     return pthread_create( p_thread, NULL, func, p_data );
157
158 #elif defined(HAVE_CTHREADS_H)
159     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
160     return 0;
161
162 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
163     *p_thread = spawn_thread( (thread_func)func, psz_name,
164                               B_NORMAL_PRIORITY, p_data );
165     return resume_thread( *p_thread );
166
167 #endif
168 }
169
170 /*****************************************************************************
171  * vlc_thread_exit: terminate a thread
172  *****************************************************************************/
173 static __inline__ void vlc_thread_exit( void )
174 {
175 #if defined(HAVE_PTHREAD_H)
176     pthread_exit( 0 );
177
178 #elif defined(HAVE_CTHREADS_H)
179     int result;
180     cthread_exit( &result );
181
182 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
183     exit_thread( 0 );
184
185 #endif
186 }
187
188 /*****************************************************************************
189  * vlc_thread_join: wait until a thread exits
190  *****************************************************************************/
191 static __inline__ void vlc_thread_join( vlc_thread_t thread )
192 {
193 #if defined(HAVE_PTHREAD_H)
194     pthread_join( thread, NULL );
195
196 #elif defined(HAVE_CTHREADS_H)
197     cthread_join( thread );
198
199 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
200     int32 exit_value;
201     wait_for_thread( thread, &exit_value );
202
203 #endif
204 }
205
206 /*****************************************************************************
207  * vlc_mutex_init: initialize a mutex
208  *****************************************************************************/
209 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
210 {
211 #if defined(HAVE_PTHREAD_H)
212     return pthread_mutex_init( p_mutex, NULL );
213
214 #elif defined(HAVE_CTHREADS_H)
215     mutex_init( p_mutex );
216     return 0;
217
218 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
219
220     /* check the arguments and whether it's already been initialized */
221     if( p_mutex == NULL ) return B_BAD_VALUE;
222     if( p_mutex->init == 9999 )
223     {
224         return EALREADY;
225     }
226
227     p_mutex->lock = create_sem( 1, "BeMutex" );
228     if( p_mutex->lock < B_NO_ERROR )
229         return( -1 );
230     p_mutex->init = 9999;
231     return B_OK;
232
233 #endif
234 }
235
236 /*****************************************************************************
237  * vlc_mutex_lock: lock a mutex
238  *****************************************************************************/
239 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
240 {
241 #if defined(HAVE_PTHREAD_H)
242     return pthread_mutex_lock( p_mutex );
243
244 #elif defined(HAVE_CTHREADS_H)
245     mutex_lock( p_mutex );
246     return 0;
247
248 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
249     status_t err;
250
251     if( !p_mutex ) return B_BAD_VALUE;
252     if( p_mutex->init < 2000 ) return B_NO_INIT;
253
254     err = acquire_sem( p_mutex->lock );
255     return err;
256
257 #endif
258 }
259
260 /*****************************************************************************
261  * vlc_mutex_unlock: unlock a mutex
262  *****************************************************************************/
263 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
264 {
265 #if defined(HAVE_PTHREAD_H)
266     return pthread_mutex_unlock( p_mutex );
267
268 #elif defined(HAVE_CTHREADS_H)
269     mutex_unlock( p_mutex );
270     return 0;
271
272 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
273
274     if(! p_mutex) return B_BAD_VALUE;
275     if( p_mutex->init < 2000 ) return B_NO_INIT;
276
277     release_sem( p_mutex->lock );
278     return B_OK;
279
280 #endif
281 }
282
283 /*****************************************************************************
284  * vlc_mutex_destroy: destroy a mutex
285  *****************************************************************************/
286 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
287 {
288 #if defined(HAVE_PTHREAD_H)    
289     return pthread_mutex_destroy( p_mutex );
290 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
291     if( p_mutex->init == 9999 )
292         delete_sem( p_mutex->lock );
293     p_mutex->init = 0;
294     return B_OK;
295 #endif    
296 }
297
298 /*****************************************************************************
299  * vlc_cond_init: initialize a condition
300  *****************************************************************************/
301 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
302 {
303 #if defined(HAVE_PTHREAD_H)
304     return pthread_cond_init( p_condvar, NULL );
305
306 #elif defined(HAVE_CTHREADS_H)
307     /* condition_init() */
308     spin_lock_init( &p_condvar->lock );
309     cthread_queue_init( &p_condvar->queue );
310     p_condvar->name = 0;
311     p_condvar->implications = 0;
312
313     return 0;
314
315 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
316     if( !p_condvar )
317         return B_BAD_VALUE;
318
319     if( p_condvar->init == 9999 )
320         return EALREADY;
321
322     p_condvar->thread = -1;
323     p_condvar->init = 9999;
324     return 0;
325
326 #endif
327 }
328
329 /*****************************************************************************
330  * vlc_cond_signal: start a thread on condition completion
331  *****************************************************************************/
332 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
333 {
334 #if defined(HAVE_PTHREAD_H)
335     return pthread_cond_signal( p_condvar );
336
337 #elif defined(HAVE_CTHREADS_H)
338     /* condition_signal() */
339     if ( p_condvar->queue.head || p_condvar->implications )
340     {
341         cond_signal( (condition_t)p_condvar );
342     }
343     return 0;
344
345 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
346     if( !p_condvar )
347         return B_BAD_VALUE;
348
349     if( p_condvar->init < 2000 )
350         return B_NO_INIT;
351     while( p_condvar->thread != -1 )
352     {
353         thread_info info;
354         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
355             return 0;
356
357         if( info.state != B_THREAD_SUSPENDED )
358         {
359             // The  waiting thread is not suspended so it could
360             // have been interrupted beetwen the unlock and the
361             // suspend_thread line. That is why we sleep a little
362             // before retesting p_condver->thread.
363             snooze( 10000 );
364         }
365         else
366         {
367             // Ok, we have to wake up that thread
368             resume_thread( p_condvar->thread );
369             return 0;
370         }
371     }
372     return 0;
373
374 #endif
375 }
376
377 /*****************************************************************************
378  * vlc_cond_wait: wait until condition completion
379  *****************************************************************************/
380 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
381 {
382 #if defined(HAVE_PTHREAD_H)
383     return pthread_cond_wait( p_condvar, p_mutex );
384
385 #elif defined(HAVE_CTHREADS_H)
386     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
387     return 0;
388
389 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
390     if( !p_condvar )
391         return B_BAD_VALUE;
392
393     if( !p_mutex )
394         return B_BAD_VALUE;
395
396     if( p_condvar->init < 2000 )
397         return B_NO_INIT;
398
399     // The p_condvar->thread var is initialized before the unlock because
400     // it enables to identify when the thread is interrupted beetwen the
401     // unlock line and the suspend_thread line
402     p_condvar->thread = find_thread( NULL );
403     vlc_mutex_unlock( p_mutex );
404     suspend_thread( p_condvar->thread );
405     p_condvar->thread = -1;
406
407     vlc_mutex_lock( p_mutex );
408     return 0;
409
410 #endif
411 }
412
413 /*****************************************************************************
414  * vlc_cond_destroy: destroy a condition
415  *****************************************************************************/
416 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
417 {
418 #if defined(HAVE_PTHREAD_H)
419     return pthread_cond_destroy( p_condvar );
420 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
421     p_condvar->init = 0;
422     return 0;
423 #endif    
424 }