]> git.sesse.net Git - vlc/blob - include/threads.h
7984ca93c98a8ee2d4150d1f081648c9cb54ba3f
[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.18 2001/06/14 01:49:44 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( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
29 #   include <pth.h>
30
31 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
32 #   include <pthread.h>
33
34 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
35 #   include <cthreads.h>
36
37 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
38 #   undef MAX
39 #   undef MIN
40 #   include <kernel/OS.h>
41 #   include <kernel/scheduler.h>
42 #   include <byteorder.h>
43
44 #elif defined( WIN32 )                        /* Win32 with MinGW32 compiler */
45 #   include <windows.h>
46 #   include <process.h>
47
48 #else
49 #   error no threads available on your system !
50
51 #endif
52
53 /*****************************************************************************
54  * Constants
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  *****************************************************************************/
60
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 */
64
65 /* Creation status */
66 #define THREAD_CREATE       10                     /* thread is initializing */
67 #define THREAD_START        11                          /* thread has forked */
68 #define THREAD_READY        19                            /* thread is ready */
69
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 */
74
75 /* Error status */
76 #define THREAD_ERROR        30                           /* an error occured */
77 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
78
79 /*****************************************************************************
80  * Types definition
81  *****************************************************************************/
82
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;
87
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;
92
93 #elif defined( HAVE_CTHREADS_H )
94 typedef cthread_t        vlc_thread_t;
95
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 {
100     spin_lock_t held;
101     spin_lock_t lock;
102     char *name;
103     struct cthread_queue queue;
104 } vlc_mutex_t;
105
106 typedef struct s_condition {
107     spin_lock_t lock;
108     struct cthread_queue queue;
109     char *name;
110     struct cond_imp *implications;
111 } vlc_cond_t;
112
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 */
117
118 typedef thread_id vlc_thread_t;
119
120 typedef struct
121 {
122     int32           init;
123     sem_id          lock;
124 } vlc_mutex_t;
125
126 typedef struct
127 {
128     int32           init;
129     thread_id       thread;
130 } vlc_cond_t;
131
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 *);
137
138 #endif
139
140 typedef void *(*vlc_thread_func_t)(void *p_data);
141
142 /*****************************************************************************
143  * Prototypes
144  *****************************************************************************/
145
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 );
150
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 * );
155
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 * );
160
161 #if 0
162 static __inline__ int  vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
163                                            mtime_t );
164 #endif
165
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,
171                                          void *p_data )
172 {
173 #if defined( PTH_INIT_IN_PTH_H )
174     *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
175     return ( p_thread == NULL );
176
177 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
178     return pthread_create( p_thread, NULL, func, p_data );
179
180 #elif defined( HAVE_CTHREADS_H )
181     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
182     return 0;
183
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 );
188
189 #elif defined( WIN32 )
190 #if 0
191     DWORD threadID;
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);
196 #endif
197     unsigned 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);
203     
204     return( *p_thread ? 0 : 1 );
205
206 #endif
207 }
208
209 /*****************************************************************************
210  * vlc_thread_exit: terminate a thread
211  *****************************************************************************/
212 static __inline__ void vlc_thread_exit( void )
213 {
214 #if defined( PTH_INIT_IN_PTH_H )
215     pth_exit( 0 );
216
217 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
218     pthread_exit( 0 );
219
220 #elif defined( HAVE_CTHREADS_H )
221     int result;
222     cthread_exit( &result );
223
224 #elif defined( HAVE_KERNEL_SCHEDULER_H )
225     exit_thread( 0 );
226
227 #elif defined( WIN32 )
228 #if 0
229     ExitThread( 0 );
230 #endif
231     /* For now we don't close the thread handles (because of race conditions).
232      * Need to be looked at. */
233     _endthreadex(0);
234
235 #endif
236 }
237
238 /*****************************************************************************
239  * vlc_thread_join: wait until a thread exits
240  *****************************************************************************/
241 static __inline__ void vlc_thread_join( vlc_thread_t thread )
242 {
243 #if defined( PTH_INIT_IN_PTH_H )
244     pth_join( thread, NULL );
245
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247     pthread_join( thread, NULL );
248
249 #elif defined( HAVE_CTHREADS_H )
250     cthread_join( thread );
251
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
253     int32 exit_value;
254     wait_for_thread( thread, &exit_value );
255
256 #elif defined( WIN32 )
257     WaitForSingleObject( thread, INFINITE);
258
259 #endif
260 }
261
262 /*****************************************************************************
263  * vlc_mutex_init: initialize a mutex
264  *****************************************************************************/
265 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
266 {
267 #if defined( PTH_INIT_IN_PTH_H )
268     return pth_mutex_init( p_mutex );
269
270 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
271     return pthread_mutex_init( p_mutex, NULL );
272
273 #elif defined( HAVE_CTHREADS_H )
274     mutex_init( p_mutex );
275     return 0;
276
277 #elif defined( HAVE_KERNEL_SCHEDULER_H )
278
279     /* check the arguments and whether it's already been initialized */
280     if( p_mutex == NULL )
281     {
282         return B_BAD_VALUE;
283     }
284
285     if( p_mutex->init == 9999 )
286     {
287         return EALREADY;
288     }
289
290     p_mutex->lock = create_sem( 1, "BeMutex" );
291     if( p_mutex->lock < B_NO_ERROR )
292     {
293         return( -1 );
294     }
295
296     p_mutex->init = 9999;
297     return B_OK;
298
299 #elif defined( WIN32 )
300     *p_mutex = CreateMutex(0,FALSE,0);
301     return (*p_mutex?0:1);
302
303 #endif
304 }
305
306 /*****************************************************************************
307  * vlc_mutex_lock: lock a mutex
308  *****************************************************************************/
309 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
310 {
311 #if defined( PTH_INIT_IN_PTH_H )
312     return pth_mutex_acquire( p_mutex, TRUE, NULL );
313
314 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
315     return pthread_mutex_lock( p_mutex );
316
317 #elif defined( HAVE_CTHREADS_H )
318     mutex_lock( p_mutex );
319     return 0;
320
321 #elif defined( HAVE_KERNEL_SCHEDULER_H )
322     status_t err;
323
324     if( !p_mutex )
325     {
326         return B_BAD_VALUE;
327     }
328
329     if( p_mutex->init < 2000 )
330     {
331         return B_NO_INIT;
332     }
333
334     err = acquire_sem( p_mutex->lock );
335     return err;
336
337 #elif defined( WIN32 )
338     WaitForSingleObject( *p_mutex, INFINITE );
339     return 0;
340
341 #endif
342 }
343
344 /*****************************************************************************
345  * vlc_mutex_unlock: unlock a mutex
346  *****************************************************************************/
347 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
348 {
349 #if defined( PTH_INIT_IN_PTH_H )
350     return pth_mutex_release( p_mutex );
351
352 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
353     return pthread_mutex_unlock( p_mutex );
354
355 #elif defined( HAVE_CTHREADS_H )
356     mutex_unlock( p_mutex );
357     return 0;
358
359 #elif defined( HAVE_KERNEL_SCHEDULER_H )
360     if( !p_mutex)
361     {
362         return B_BAD_VALUE;
363     }
364
365     if( p_mutex->init < 2000 )
366     {
367         return B_NO_INIT;
368     }
369
370     release_sem( p_mutex->lock );
371     return B_OK;
372
373 #elif defined( WIN32 )
374     ReleaseMutex( *p_mutex );
375     return 0;
376
377 #endif
378 }
379
380 /*****************************************************************************
381  * vlc_mutex_destroy: destroy a mutex
382  *****************************************************************************/
383 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
384 {
385 #if defined( PTH_INIT_IN_PTH_H )
386     return 0;
387
388 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
389     return pthread_mutex_destroy( p_mutex );
390
391 #elif defined( HAVE_KERNEL_SCHEDULER_H )
392     if( p_mutex->init == 9999 )
393     {
394         delete_sem( p_mutex->lock );
395     }
396
397     p_mutex->init = 0;
398     return B_OK;
399
400 #elif defined( WIN32 )
401     CloseHandle(*p_mutex);
402     return 0;
403
404 #endif    
405 }
406
407 /*****************************************************************************
408  * vlc_cond_init: initialize a condition
409  *****************************************************************************/
410 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
411 {
412 #if defined( PTH_INIT_IN_PTH_H )
413     return pth_cond_init( p_condvar );
414
415 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
416     return pthread_cond_init( p_condvar, NULL );
417
418 #elif defined( HAVE_CTHREADS_H )
419     /* condition_init() */
420     spin_lock_init( &p_condvar->lock );
421     cthread_queue_init( &p_condvar->queue );
422     p_condvar->name = 0;
423     p_condvar->implications = 0;
424
425     return 0;
426
427 #elif defined( HAVE_KERNEL_SCHEDULER_H )
428     if( !p_condvar )
429     {
430         return B_BAD_VALUE;
431     }
432
433     if( p_condvar->init == 9999 )
434     {
435         return EALREADY;
436     }
437
438     p_condvar->thread = -1;
439     p_condvar->init = 9999;
440     return 0;
441
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 */
448
449     return( *p_condvar ? 0 : 1 );
450     
451 #endif
452 }
453
454 /*****************************************************************************
455  * vlc_cond_signal: start a thread on condition completion
456  *****************************************************************************/
457 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
458 {
459 #if defined( PTH_INIT_IN_PTH_H )
460     return pth_cond_notify( p_condvar, FALSE );
461
462 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
463     return pthread_cond_signal( p_condvar );
464
465 #elif defined( HAVE_CTHREADS_H )
466     /* condition_signal() */
467     if ( p_condvar->queue.head || p_condvar->implications )
468     {
469         cond_signal( (condition_t)p_condvar );
470     }
471     return 0;
472
473 #elif defined( HAVE_KERNEL_SCHEDULER_H )
474     if( !p_condvar )
475     {
476         return B_BAD_VALUE;
477     }
478
479     if( p_condvar->init < 2000 )
480     {
481         return B_NO_INIT;
482     }
483
484     while( p_condvar->thread != -1 )
485     {
486         thread_info info;
487         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
488         {
489             return 0;
490         }
491
492         if( info.state != B_THREAD_SUSPENDED )
493         {
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. */
498             snooze( 10000 );
499         }
500         else
501         {
502             /* Ok, we have to wake up that thread */
503             resume_thread( p_condvar->thread );
504             return 0;
505         }
506     }
507     return 0;
508
509 #elif defined( WIN32 )
510     /* Try to release one waiting thread. */
511     PulseEvent ( *p_condvar );
512     return 0;
513
514 #endif
515 }
516
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 )
521 {
522 #if defined( PTH_INIT_IN_PTH_H )
523     return pth_cond_await( p_condvar, p_mutex, NULL );
524
525 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
526     return pthread_cond_wait( p_condvar, p_mutex );
527
528 #elif defined( HAVE_CTHREADS_H )
529     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
530     return 0;
531
532 #elif defined( HAVE_KERNEL_SCHEDULER_H )
533     if( !p_condvar )
534     {
535         return B_BAD_VALUE;
536     }
537
538     if( !p_mutex )
539     {
540         return B_BAD_VALUE;
541     }
542
543     if( p_condvar->init < 2000 )
544     {
545         return B_NO_INIT;
546     }
547
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;
555
556     vlc_mutex_lock( p_mutex );
557     return 0;
558
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
562      * called. */
563     vlc_mutex_unlock( p_mutex );
564
565     WaitForSingleObject( *p_condvar, INFINITE );
566
567     /* Reacquire the mutex before returning. */
568     vlc_mutex_lock( p_mutex );
569     return 0;
570
571 #endif
572 }
573
574 /*****************************************************************************
575  * vlc_cond_destroy: destroy a condition
576  *****************************************************************************/
577 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
578 {
579 #if defined( PTH_INIT_IN_PTH_H )
580     return 0;
581
582 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
583     return pthread_cond_destroy( p_condvar );
584
585 #elif defined( HAVE_KERNEL_SCHEDULER_H )
586     p_condvar->init = 0;
587     return 0;
588
589 #elif defined( WIN32 )
590     CloseHandle( *p_condvar );
591     return 0;
592
593 #endif    
594 }
595