]> git.sesse.net Git - vlc/blob - include/threads.h
* Coding style fixes here and there.
[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.17 2001/04/28 03:36:25 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(PTHREAD_COND_T_IN_PTHREAD_H)      /* pthreads (like Linux & BSD) */
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
41 #elif defined(WIN32)                          /* Win32 with MinGW32 compiler */
42 #include <windows.h>
43 #include <process.h>
44
45 #else
46 #error no threads available on your system !
47 #endif
48
49 /*****************************************************************************
50  * Constants
51  *****************************************************************************
52  * These constants are used by all threads in *_CreateThread() and
53  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
54  * value is used as a shared flag to represent the status of the thread.
55  *****************************************************************************/
56
57 /* Void status - this value can be used to make sure no operation is currently
58  * in progress on the concerned thread in an array of recorded threads */
59 #define THREAD_NOP          0                            /* nothing happened */
60
61 /* Creation status */
62 #define THREAD_CREATE       10                     /* thread is initializing */
63 #define THREAD_START        11                          /* thread has forked */
64 #define THREAD_READY        19                            /* thread is ready */
65
66 /* Destructions status */
67 #define THREAD_DESTROY      20            /* destruction order has been sent */
68 #define THREAD_END          21        /* destruction order has been received */
69 #define THREAD_OVER         29             /* thread does not exist any more */
70
71 /* Error status */
72 #define THREAD_ERROR        30                           /* an error occured */
73 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
74
75 /*****************************************************************************
76  * Types definition
77  *****************************************************************************/
78
79 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
80 typedef pthread_t        vlc_thread_t;
81 typedef pthread_mutex_t  vlc_mutex_t;
82 typedef pthread_cond_t   vlc_cond_t;
83
84 #elif defined(HAVE_CTHREADS_H)
85 typedef cthread_t        vlc_thread_t;
86
87 /* Those structs are the ones defined in /include/cthreads.h but we need
88  * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
89  * foo is a (mutex_t*) */
90 typedef struct s_mutex {
91     spin_lock_t held;
92     spin_lock_t lock;
93     char *name;
94     struct cthread_queue queue;
95 } vlc_mutex_t;
96
97 typedef struct s_condition {
98     spin_lock_t lock;
99     struct cthread_queue queue;
100     char *name;
101     struct cond_imp *implications;
102 } vlc_cond_t;
103
104 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
105 /* This is the BeOS implementation of the vlc threads, note that the mutex is
106  * not a real mutex and the cond_var is not like a pthread cond_var but it is
107  * enough for what wee need */
108
109 typedef thread_id vlc_thread_t;
110
111 typedef struct
112 {
113     int32           init;
114     sem_id          lock;
115 } vlc_mutex_t;
116
117 typedef struct
118 {
119     int32           init;
120     thread_id       thread;
121 } vlc_cond_t;
122
123 #elif defined(WIN32)
124 typedef HANDLE      vlc_thread_t;
125 typedef HANDLE      vlc_mutex_t;
126 typedef HANDLE      vlc_cond_t; 
127 typedef unsigned (__stdcall *PTHREAD_START) (void *);
128
129 #endif
130
131 typedef void *(*vlc_thread_func_t)(void *p_data);
132
133 /*****************************************************************************
134  * Prototypes
135  *****************************************************************************/
136
137 static __inline__ int  vlc_thread_create ( vlc_thread_t *, char *,
138                                            vlc_thread_func_t, void * );
139 static __inline__ void vlc_thread_exit   ( void );
140 static __inline__ void vlc_thread_join   ( vlc_thread_t );
141
142 static __inline__ int  vlc_mutex_init    ( vlc_mutex_t * );
143 static __inline__ int  vlc_mutex_lock    ( vlc_mutex_t * );
144 static __inline__ int  vlc_mutex_unlock  ( vlc_mutex_t * );
145 static __inline__ int  vlc_mutex_destroy ( vlc_mutex_t * );
146
147 static __inline__ int  vlc_cond_init     ( vlc_cond_t * );
148 static __inline__ int  vlc_cond_signal   ( vlc_cond_t * );
149 static __inline__ int  vlc_cond_wait     ( vlc_cond_t *, vlc_mutex_t * );
150 static __inline__ int  vlc_cond_destroy  ( vlc_cond_t * );
151
152 #if 0
153 static __inline__ int  vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
154                                            mtime_t );
155 #endif
156
157 /*****************************************************************************
158  * vlc_thread_create: create a thread
159  *****************************************************************************/
160 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
161                                          char *psz_name, vlc_thread_func_t func,
162                                          void *p_data)
163 {
164 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
165     return pthread_create( p_thread, NULL, func, p_data );
166
167 #elif defined(HAVE_CTHREADS_H)
168     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
169     return 0;
170
171 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
172     *p_thread = spawn_thread( (thread_func)func, psz_name,
173                               B_NORMAL_PRIORITY, p_data );
174     return resume_thread( *p_thread );
175
176 #elif defined(WIN32)
177 #if 0
178     DWORD threadID;
179     /* This method is not recommended when using the MSVCRT C library,
180      * so we'll have to use _beginthreadex instead */
181     *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, 
182                              p_data, 0, &threadID);
183 #endif
184     unsigned threadID;
185     /* When using the MSVCRT C library you have to use the _beginthreadex
186      * function instead of CreateThread, otherwise you'll end up with memory
187      * leaks and the signal function not working */
188     *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func, 
189                              p_data, 0, &threadID);
190     
191     return( *p_thread ? 0 : 1 );
192
193 #endif
194 }
195
196 /*****************************************************************************
197  * vlc_thread_exit: terminate a thread
198  *****************************************************************************/
199 static __inline__ void vlc_thread_exit( void )
200 {
201 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
202     pthread_exit( 0 );
203
204 #elif defined(HAVE_CTHREADS_H)
205     int result;
206     cthread_exit( &result );
207
208 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
209     exit_thread( 0 );
210
211 #elif defined(WIN32)
212 #if 0
213     ExitThread( 0 );
214 #endif
215     /* For now we don't close the thread handles (because of race conditions).
216      * Need to be looked at. */
217     _endthreadex(0);
218
219 #endif
220 }
221
222 /*****************************************************************************
223  * vlc_thread_join: wait until a thread exits
224  *****************************************************************************/
225 static __inline__ void vlc_thread_join( vlc_thread_t thread )
226 {
227 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
228     pthread_join( thread, NULL );
229
230 #elif defined(HAVE_CTHREADS_H)
231     cthread_join( thread );
232
233 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
234     int32 exit_value;
235     wait_for_thread( thread, &exit_value );
236
237 #elif defined(WIN32)
238     WaitForSingleObject( thread, INFINITE);
239
240 #endif
241 }
242
243 /*****************************************************************************
244  * vlc_mutex_init: initialize a mutex
245  *****************************************************************************/
246 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
247 {
248 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
249     return pthread_mutex_init( p_mutex, NULL );
250
251 #elif defined(HAVE_CTHREADS_H)
252     mutex_init( p_mutex );
253     return 0;
254
255 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
256
257     /* check the arguments and whether it's already been initialized */
258     if( p_mutex == NULL )
259     {
260         return B_BAD_VALUE;
261     }
262
263     if( p_mutex->init == 9999 )
264     {
265         return EALREADY;
266     }
267
268     p_mutex->lock = create_sem( 1, "BeMutex" );
269     if( p_mutex->lock < B_NO_ERROR )
270     {
271         return( -1 );
272     }
273
274     p_mutex->init = 9999;
275     return B_OK;
276
277 #elif defined(WIN32)
278     *p_mutex = CreateMutex(0,FALSE,0);
279     return (*p_mutex?0:1);
280
281 #endif
282 }
283
284 /*****************************************************************************
285  * vlc_mutex_lock: lock a mutex
286  *****************************************************************************/
287 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
288 {
289 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
290     return pthread_mutex_lock( p_mutex );
291
292 #elif defined(HAVE_CTHREADS_H)
293     mutex_lock( p_mutex );
294     return 0;
295
296 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
297     status_t err;
298
299     if( !p_mutex )
300     {
301         return B_BAD_VALUE;
302     }
303
304     if( p_mutex->init < 2000 )
305     {
306         return B_NO_INIT;
307     }
308
309     err = acquire_sem( p_mutex->lock );
310     return err;
311
312 #elif defined(WIN32)
313     WaitForSingleObject( *p_mutex, INFINITE );
314     return 0;
315
316 #endif
317 }
318
319 /*****************************************************************************
320  * vlc_mutex_unlock: unlock a mutex
321  *****************************************************************************/
322 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
323 {
324 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
325     return pthread_mutex_unlock( p_mutex );
326
327 #elif defined(HAVE_CTHREADS_H)
328     mutex_unlock( p_mutex );
329     return 0;
330
331 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
332     if( !p_mutex)
333     {
334         return B_BAD_VALUE;
335     }
336
337     if( p_mutex->init < 2000 )
338     {
339         return B_NO_INIT;
340     }
341
342     release_sem( p_mutex->lock );
343     return B_OK;
344
345 #elif defined(WIN32)
346     ReleaseMutex( *p_mutex );
347     return 0;
348
349 #endif
350 }
351
352 /*****************************************************************************
353  * vlc_mutex_destroy: destroy a mutex
354  *****************************************************************************/
355 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
356 {
357 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)    
358     return pthread_mutex_destroy( p_mutex );
359
360 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
361     if( p_mutex->init == 9999 )
362     {
363         delete_sem( p_mutex->lock );
364     }
365
366     p_mutex->init = 0;
367     return B_OK;
368
369 #elif defined(WIN32)
370     CloseHandle(*p_mutex);
371     return 0;
372
373 #endif    
374 }
375
376 /*****************************************************************************
377  * vlc_cond_init: initialize a condition
378  *****************************************************************************/
379 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
380 {
381 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
382     return pthread_cond_init( p_condvar, NULL );
383
384 #elif defined(HAVE_CTHREADS_H)
385     /* condition_init() */
386     spin_lock_init( &p_condvar->lock );
387     cthread_queue_init( &p_condvar->queue );
388     p_condvar->name = 0;
389     p_condvar->implications = 0;
390
391     return 0;
392
393 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
394     if( !p_condvar )
395     {
396         return B_BAD_VALUE;
397     }
398
399     if( p_condvar->init == 9999 )
400     {
401         return EALREADY;
402     }
403
404     p_condvar->thread = -1;
405     p_condvar->init = 9999;
406     return 0;
407
408 #elif defined(WIN32)
409     /* Create an auto-reset event. */
410     *p_condvar = CreateEvent( NULL,   /* no security */
411                               FALSE,  /* auto-reset event */
412                               FALSE,  /* non-signaled initially */
413                               NULL ); /* unnamed */
414
415     return( *p_condvar ? 0 : 1 );
416     
417 #endif
418 }
419
420 /*****************************************************************************
421  * vlc_cond_signal: start a thread on condition completion
422  *****************************************************************************/
423 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
424 {
425 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
426     return pthread_cond_signal( p_condvar );
427
428 #elif defined(HAVE_CTHREADS_H)
429     /* condition_signal() */
430     if ( p_condvar->queue.head || p_condvar->implications )
431     {
432         cond_signal( (condition_t)p_condvar );
433     }
434     return 0;
435
436 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
437     if( !p_condvar )
438     {
439         return B_BAD_VALUE;
440     }
441
442     if( p_condvar->init < 2000 )
443     {
444         return B_NO_INIT;
445     }
446
447     while( p_condvar->thread != -1 )
448     {
449         thread_info info;
450         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
451         {
452             return 0;
453         }
454
455         if( info.state != B_THREAD_SUSPENDED )
456         {
457             /* The  waiting thread is not suspended so it could
458              * have been interrupted beetwen the unlock and the
459              * suspend_thread line. That is why we sleep a little
460              * before retesting p_condver->thread. */
461             snooze( 10000 );
462         }
463         else
464         {
465             /* Ok, we have to wake up that thread */
466             resume_thread( p_condvar->thread );
467             return 0;
468         }
469     }
470     return 0;
471
472 #elif defined(WIN32)
473     /* Try to release one waiting thread. */
474     PulseEvent ( *p_condvar );
475     return 0;
476
477 #endif
478 }
479
480 /*****************************************************************************
481  * vlc_cond_wait: wait until condition completion
482  *****************************************************************************/
483 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
484 {
485 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
486     return pthread_cond_wait( p_condvar, p_mutex );
487
488 #elif defined(HAVE_CTHREADS_H)
489     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
490     return 0;
491
492 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
493     if( !p_condvar )
494     {
495         return B_BAD_VALUE;
496     }
497
498     if( !p_mutex )
499     {
500         return B_BAD_VALUE;
501     }
502
503     if( p_condvar->init < 2000 )
504     {
505         return B_NO_INIT;
506     }
507
508     /* The p_condvar->thread var is initialized before the unlock because
509      * it enables to identify when the thread is interrupted beetwen the
510      * unlock line and the suspend_thread line */
511     p_condvar->thread = find_thread( NULL );
512     vlc_mutex_unlock( p_mutex );
513     suspend_thread( p_condvar->thread );
514     p_condvar->thread = -1;
515
516     vlc_mutex_lock( p_mutex );
517     return 0;
518
519 #elif defined(WIN32)
520     /* Release the <external_mutex> here and wait for the event
521      * to become signaled, due to <pthread_cond_signal> being
522      * called. */
523     vlc_mutex_unlock( p_mutex );
524
525     WaitForSingleObject( *p_condvar, INFINITE );
526
527     /* Reacquire the mutex before returning. */
528     vlc_mutex_lock( p_mutex );
529     return 0;
530
531 #endif
532 }
533
534 /*****************************************************************************
535  * vlc_cond_destroy: destroy a condition
536  *****************************************************************************/
537 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
538 {
539 #if defined(PTHREAD_COND_T_IN_PTHREAD_H)
540     return pthread_cond_destroy( p_condvar );
541
542 #elif defined(HAVE_KERNEL_SCHEDULER_H) && defined(HAVE_KERNEL_OS_H)
543     p_condvar->init = 0;
544     return 0;
545
546 #elif defined(WIN32)
547     CloseHandle( *p_condvar );
548     return 0;
549
550 #endif    
551 }
552