]> git.sesse.net Git - vlc/blob - include/threads.h
* Added gprof profiling support with --enable-profiling.
[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.19 2001/06/14 20:21:04 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 #ifdef PROFILING
29 #   include <sys/time.h>
30 #endif
31
32 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
33 #   include <pth.h>
34
35 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
36 #   include <pthread.h>
37
38 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
39 #   include <cthreads.h>
40
41 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
42 #   undef MAX
43 #   undef MIN
44 #   include <kernel/OS.h>
45 #   include <kernel/scheduler.h>
46 #   include <byteorder.h>
47
48 #elif defined( WIN32 )                        /* Win32 with MinGW32 compiler */
49 #   include <windows.h>
50 #   include <process.h>
51
52 #else
53 #   error no threads available on your system !
54
55 #endif
56
57 /*****************************************************************************
58  * Constants
59  *****************************************************************************
60  * These constants are used by all threads in *_CreateThread() and
61  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
62  * value is used as a shared flag to represent the status of the thread.
63  *****************************************************************************/
64
65 /* Void status - this value can be used to make sure no operation is currently
66  * in progress on the concerned thread in an array of recorded threads */
67 #define THREAD_NOP          0                            /* nothing happened */
68
69 /* Creation status */
70 #define THREAD_CREATE       10                     /* thread is initializing */
71 #define THREAD_START        11                          /* thread has forked */
72 #define THREAD_READY        19                            /* thread is ready */
73
74 /* Destructions status */
75 #define THREAD_DESTROY      20            /* destruction order has been sent */
76 #define THREAD_END          21        /* destruction order has been received */
77 #define THREAD_OVER         29             /* thread does not exist any more */
78
79 /* Error status */
80 #define THREAD_ERROR        30                           /* an error occured */
81 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
82
83 /*****************************************************************************
84  * Types definition
85  *****************************************************************************/
86
87 #if defined( PTH_INIT_IN_PTH_H )
88 typedef pth_t            vlc_thread_t;
89 typedef pth_mutex_t      vlc_mutex_t;
90 typedef pth_cond_t       vlc_cond_t;
91
92 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
93 typedef pthread_t        vlc_thread_t;
94 typedef pthread_mutex_t  vlc_mutex_t;
95 typedef pthread_cond_t   vlc_cond_t;
96
97 #elif defined( HAVE_CTHREADS_H )
98 typedef cthread_t        vlc_thread_t;
99
100 /* Those structs are the ones defined in /include/cthreads.h but we need
101  * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
102  * foo is a (mutex_t*) */
103 typedef struct s_mutex {
104     spin_lock_t held;
105     spin_lock_t lock;
106     char *name;
107     struct cthread_queue queue;
108 } vlc_mutex_t;
109
110 typedef struct s_condition {
111     spin_lock_t lock;
112     struct cthread_queue queue;
113     char *name;
114     struct cond_imp *implications;
115 } vlc_cond_t;
116
117 #elif defined( HAVE_KERNEL_SCHEDULER_H )
118 /* This is the BeOS implementation of the vlc threads, note that the mutex is
119  * not a real mutex and the cond_var is not like a pthread cond_var but it is
120  * enough for what wee need */
121
122 typedef thread_id vlc_thread_t;
123
124 typedef struct
125 {
126     int32           init;
127     sem_id          lock;
128 } vlc_mutex_t;
129
130 typedef struct
131 {
132     int32           init;
133     thread_id       thread;
134 } vlc_cond_t;
135
136 #elif defined( WIN32 )
137 typedef HANDLE      vlc_thread_t;
138 typedef HANDLE      vlc_mutex_t;
139 typedef HANDLE      vlc_cond_t; 
140 typedef unsigned (__stdcall *PTHREAD_START) (void *);
141
142 #endif
143
144 typedef void *(*vlc_thread_func_t)(void *p_data);
145
146 /*****************************************************************************
147  * Prototypes
148  *****************************************************************************/
149
150 static __inline__ int  vlc_threads_init  ( void );
151 static __inline__ int  vlc_threads_end   ( void );
152
153 static __inline__ int  vlc_mutex_init    ( vlc_mutex_t * );
154 static __inline__ int  vlc_mutex_lock    ( vlc_mutex_t * );
155 static __inline__ int  vlc_mutex_unlock  ( vlc_mutex_t * );
156 static __inline__ int  vlc_mutex_destroy ( vlc_mutex_t * );
157
158 static __inline__ int  vlc_cond_init     ( vlc_cond_t * );
159 static __inline__ int  vlc_cond_signal   ( vlc_cond_t * );
160 static __inline__ int  vlc_cond_wait     ( vlc_cond_t *, vlc_mutex_t * );
161 static __inline__ int  vlc_cond_destroy  ( vlc_cond_t * );
162
163 static __inline__ int  vlc_thread_create ( vlc_thread_t *, char *,
164                                            vlc_thread_func_t, void * );
165 static __inline__ void vlc_thread_exit   ( void );
166 static __inline__ void vlc_thread_join   ( vlc_thread_t );
167
168 #if 0
169 static __inline__ int  vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
170                                            mtime_t );
171 #endif
172
173 #ifdef PROFILING
174 /* Wrapper function for profiling */
175 static void *      vlc_thread_wrapper ( void *p_wrapper );
176
177 typedef struct wrapper_s
178 {
179     /* Data lock access */
180     vlc_mutex_t lock;
181     vlc_cond_t  wait;
182
183     /* Data used to spawn the real thread */
184     vlc_thread_func_t func;
185     void *p_data;
186
187     /* Profiling timer passed to the thread */
188     struct itimerval itimer;
189
190 } wrapper_t;
191 #endif
192
193 /*****************************************************************************
194  * vlc_threads_init: initialize threads system
195  *****************************************************************************/
196 static __inline__ int vlc_threads_init( void )
197 {
198 #if defined( PTH_INIT_IN_PTH_H )
199     return pth_init();
200
201 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
202     return 0;
203
204 #elif defined( HAVE_CTHREADS_H )
205     return 0;
206
207 #elif defined( HAVE_KERNEL_SCHEDULER_H )
208     return 0;
209
210 #elif defined( WIN32 )
211     return 0;
212
213 #endif
214 }
215
216 /*****************************************************************************
217  * vlc_threads_end: stop threads system
218  *****************************************************************************/
219 static __inline__ int vlc_threads_end( void )
220 {
221 #if defined( PTH_INIT_IN_PTH_H )
222     return pth_kill();
223
224 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
225     return 0;
226
227 #elif defined( HAVE_CTHREADS_H )
228     return 0;
229
230 #elif defined( HAVE_KERNEL_SCHEDULER_H )
231     return 0;
232
233 #elif defined( WIN32 )
234     return 0;
235
236 #endif
237 }
238
239 /*****************************************************************************
240  * vlc_mutex_init: initialize a mutex
241  *****************************************************************************/
242 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
243 {
244 #if defined( PTH_INIT_IN_PTH_H )
245     return pth_mutex_init( p_mutex );
246
247 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
248     return pthread_mutex_init( p_mutex, NULL );
249
250 #elif defined( HAVE_CTHREADS_H )
251     mutex_init( p_mutex );
252     return 0;
253
254 #elif defined( HAVE_KERNEL_SCHEDULER_H )
255
256     /* check the arguments and whether it's already been initialized */
257     if( p_mutex == NULL )
258     {
259         return B_BAD_VALUE;
260     }
261
262     if( p_mutex->init == 9999 )
263     {
264         return EALREADY;
265     }
266
267     p_mutex->lock = create_sem( 1, "BeMutex" );
268     if( p_mutex->lock < B_NO_ERROR )
269     {
270         return( -1 );
271     }
272
273     p_mutex->init = 9999;
274     return B_OK;
275
276 #elif defined( WIN32 )
277     *p_mutex = CreateMutex(0,FALSE,0);
278     return (*p_mutex?0:1);
279
280 #endif
281 }
282
283 /*****************************************************************************
284  * vlc_mutex_lock: lock a mutex
285  *****************************************************************************/
286 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
287 {
288 #if defined( PTH_INIT_IN_PTH_H )
289     return pth_mutex_acquire( p_mutex, TRUE, NULL );
290
291 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
292     return pthread_mutex_lock( p_mutex );
293
294 #elif defined( HAVE_CTHREADS_H )
295     mutex_lock( p_mutex );
296     return 0;
297
298 #elif defined( HAVE_KERNEL_SCHEDULER_H )
299     status_t err;
300
301     if( !p_mutex )
302     {
303         return B_BAD_VALUE;
304     }
305
306     if( p_mutex->init < 2000 )
307     {
308         return B_NO_INIT;
309     }
310
311     err = acquire_sem( p_mutex->lock );
312     return err;
313
314 #elif defined( WIN32 )
315     WaitForSingleObject( *p_mutex, INFINITE );
316     return 0;
317
318 #endif
319 }
320
321 /*****************************************************************************
322  * vlc_mutex_unlock: unlock a mutex
323  *****************************************************************************/
324 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
325 {
326 #if defined( PTH_INIT_IN_PTH_H )
327     return pth_mutex_release( p_mutex );
328
329 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
330     return pthread_mutex_unlock( p_mutex );
331
332 #elif defined( HAVE_CTHREADS_H )
333     mutex_unlock( p_mutex );
334     return 0;
335
336 #elif defined( HAVE_KERNEL_SCHEDULER_H )
337     if( !p_mutex)
338     {
339         return B_BAD_VALUE;
340     }
341
342     if( p_mutex->init < 2000 )
343     {
344         return B_NO_INIT;
345     }
346
347     release_sem( p_mutex->lock );
348     return B_OK;
349
350 #elif defined( WIN32 )
351     ReleaseMutex( *p_mutex );
352     return 0;
353
354 #endif
355 }
356
357 /*****************************************************************************
358  * vlc_mutex_destroy: destroy a mutex
359  *****************************************************************************/
360 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
361 {
362 #if defined( PTH_INIT_IN_PTH_H )
363     return 0;
364
365 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
366     return pthread_mutex_destroy( p_mutex );
367
368 #elif defined( HAVE_KERNEL_SCHEDULER_H )
369     if( p_mutex->init == 9999 )
370     {
371         delete_sem( p_mutex->lock );
372     }
373
374     p_mutex->init = 0;
375     return B_OK;
376
377 #elif defined( WIN32 )
378     CloseHandle(*p_mutex);
379     return 0;
380
381 #endif    
382 }
383
384 /*****************************************************************************
385  * vlc_cond_init: initialize a condition
386  *****************************************************************************/
387 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
388 {
389 #if defined( PTH_INIT_IN_PTH_H )
390     return pth_cond_init( p_condvar );
391
392 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
393     return pthread_cond_init( p_condvar, NULL );
394
395 #elif defined( HAVE_CTHREADS_H )
396     /* condition_init() */
397     spin_lock_init( &p_condvar->lock );
398     cthread_queue_init( &p_condvar->queue );
399     p_condvar->name = 0;
400     p_condvar->implications = 0;
401
402     return 0;
403
404 #elif defined( HAVE_KERNEL_SCHEDULER_H )
405     if( !p_condvar )
406     {
407         return B_BAD_VALUE;
408     }
409
410     if( p_condvar->init == 9999 )
411     {
412         return EALREADY;
413     }
414
415     p_condvar->thread = -1;
416     p_condvar->init = 9999;
417     return 0;
418
419 #elif defined( WIN32 )
420     /* Create an auto-reset event. */
421     *p_condvar = CreateEvent( NULL,   /* no security */
422                               FALSE,  /* auto-reset event */
423                               FALSE,  /* non-signaled initially */
424                               NULL ); /* unnamed */
425
426     return( *p_condvar ? 0 : 1 );
427     
428 #endif
429 }
430
431 /*****************************************************************************
432  * vlc_cond_signal: start a thread on condition completion
433  *****************************************************************************/
434 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
435 {
436 #if defined( PTH_INIT_IN_PTH_H )
437     return pth_cond_notify( p_condvar, FALSE );
438
439 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
440     return pthread_cond_signal( p_condvar );
441
442 #elif defined( HAVE_CTHREADS_H )
443     /* condition_signal() */
444     if ( p_condvar->queue.head || p_condvar->implications )
445     {
446         cond_signal( (condition_t)p_condvar );
447     }
448     return 0;
449
450 #elif defined( HAVE_KERNEL_SCHEDULER_H )
451     if( !p_condvar )
452     {
453         return B_BAD_VALUE;
454     }
455
456     if( p_condvar->init < 2000 )
457     {
458         return B_NO_INIT;
459     }
460
461     while( p_condvar->thread != -1 )
462     {
463         thread_info info;
464         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
465         {
466             return 0;
467         }
468
469         if( info.state != B_THREAD_SUSPENDED )
470         {
471             /* The  waiting thread is not suspended so it could
472              * have been interrupted beetwen the unlock and the
473              * suspend_thread line. That is why we sleep a little
474              * before retesting p_condver->thread. */
475             snooze( 10000 );
476         }
477         else
478         {
479             /* Ok, we have to wake up that thread */
480             resume_thread( p_condvar->thread );
481             return 0;
482         }
483     }
484     return 0;
485
486 #elif defined( WIN32 )
487     /* Try to release one waiting thread. */
488     PulseEvent ( *p_condvar );
489     return 0;
490
491 #endif
492 }
493
494 /*****************************************************************************
495  * vlc_cond_wait: wait until condition completion
496  *****************************************************************************/
497 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
498 {
499 #if defined( PTH_INIT_IN_PTH_H )
500     return pth_cond_await( p_condvar, p_mutex, NULL );
501
502 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
503     return pthread_cond_wait( p_condvar, p_mutex );
504
505 #elif defined( HAVE_CTHREADS_H )
506     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
507     return 0;
508
509 #elif defined( HAVE_KERNEL_SCHEDULER_H )
510     if( !p_condvar )
511     {
512         return B_BAD_VALUE;
513     }
514
515     if( !p_mutex )
516     {
517         return B_BAD_VALUE;
518     }
519
520     if( p_condvar->init < 2000 )
521     {
522         return B_NO_INIT;
523     }
524
525     /* The p_condvar->thread var is initialized before the unlock because
526      * it enables to identify when the thread is interrupted beetwen the
527      * unlock line and the suspend_thread line */
528     p_condvar->thread = find_thread( NULL );
529     vlc_mutex_unlock( p_mutex );
530     suspend_thread( p_condvar->thread );
531     p_condvar->thread = -1;
532
533     vlc_mutex_lock( p_mutex );
534     return 0;
535
536 #elif defined( WIN32 )
537     /* Release the <external_mutex> here and wait for the event
538      * to become signaled, due to <pthread_cond_signal> being
539      * called. */
540     vlc_mutex_unlock( p_mutex );
541
542     WaitForSingleObject( *p_condvar, INFINITE );
543
544     /* Reacquire the mutex before returning. */
545     vlc_mutex_lock( p_mutex );
546     return 0;
547
548 #endif
549 }
550
551 /*****************************************************************************
552  * vlc_cond_destroy: destroy a condition
553  *****************************************************************************/
554 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
555 {
556 #if defined( PTH_INIT_IN_PTH_H )
557     return 0;
558
559 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
560     return pthread_cond_destroy( p_condvar );
561
562 #elif defined( HAVE_KERNEL_SCHEDULER_H )
563     p_condvar->init = 0;
564     return 0;
565
566 #elif defined( WIN32 )
567     CloseHandle( *p_condvar );
568     return 0;
569
570 #endif    
571 }
572
573 /*****************************************************************************
574  * vlc_thread_create: create a thread
575  *****************************************************************************/
576 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
577                                          char *psz_name, vlc_thread_func_t func,
578                                          void *p_data )
579 {
580     int i_ret;
581
582 #ifdef PROFILING
583     wrapper_t wrapper;
584
585     /* Initialize the wrapper structure */
586     wrapper.func = func;
587     wrapper.p_data = p_data;
588     getitimer( ITIMER_PROF, &wrapper.itimer );
589     vlc_mutex_init( &wrapper.lock );
590     vlc_cond_init( &wrapper.wait );
591     vlc_mutex_lock( &wrapper.lock );
592
593     /* Alter user-passed data so that we call the wrapper instead
594      * of the real function */
595     p_data = &wrapper;
596     func = vlc_thread_wrapper;
597 #endif
598
599 #if defined( PTH_INIT_IN_PTH_H )
600     *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
601     i_ret = ( p_thread == NULL );
602
603 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
604     i_ret = pthread_create( p_thread, NULL, func, p_data );
605
606 #elif defined( HAVE_CTHREADS_H )
607     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
608     i_ret = 0;
609
610 #elif defined( HAVE_KERNEL_SCHEDULER_H )
611     *p_thread = spawn_thread( (thread_func)func, psz_name,
612                               B_NORMAL_PRIORITY, p_data );
613     i_ret = resume_thread( *p_thread );
614
615 #elif defined( WIN32 )
616 #if 0
617     DWORD threadID;
618     /* This method is not recommended when using the MSVCRT C library,
619      * so we'll have to use _beginthreadex instead */
620     *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, 
621                              p_data, 0, &threadID);
622 #endif
623     unsigned threadID;
624     /* When using the MSVCRT C library you have to use the _beginthreadex
625      * function instead of CreateThread, otherwise you'll end up with memory
626      * leaks and the signal function not working */
627     *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func, 
628                              p_data, 0, &threadID);
629     
630     i_ret = ( *p_thread ? 0 : 1 );
631
632 #endif
633
634 #ifdef PROFILING
635     if( i_ret == 0 )
636     {
637         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
638     }
639
640     vlc_mutex_unlock( &wrapper.lock );
641     vlc_mutex_destroy( &wrapper.lock );
642     vlc_cond_destroy( &wrapper.wait );
643 #endif
644
645     return i_ret;
646 }
647
648 /*****************************************************************************
649  * vlc_thread_exit: terminate a thread
650  *****************************************************************************/
651 static __inline__ void vlc_thread_exit( void )
652 {
653 #if defined( PTH_INIT_IN_PTH_H )
654     pth_exit( 0 );
655
656 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
657     pthread_exit( 0 );
658
659 #elif defined( HAVE_CTHREADS_H )
660     int result;
661     cthread_exit( &result );
662
663 #elif defined( HAVE_KERNEL_SCHEDULER_H )
664     exit_thread( 0 );
665
666 #elif defined( WIN32 )
667 #if 0
668     ExitThread( 0 );
669 #endif
670     /* For now we don't close the thread handles (because of race conditions).
671      * Need to be looked at. */
672     _endthreadex(0);
673
674 #endif
675 }
676
677 /*****************************************************************************
678  * vlc_thread_join: wait until a thread exits
679  *****************************************************************************/
680 static __inline__ void vlc_thread_join( vlc_thread_t thread )
681 {
682 #if defined( PTH_INIT_IN_PTH_H )
683     pth_join( thread, NULL );
684
685 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
686     pthread_join( thread, NULL );
687
688 #elif defined( HAVE_CTHREADS_H )
689     cthread_join( thread );
690
691 #elif defined( HAVE_KERNEL_SCHEDULER_H )
692     int32 exit_value;
693     wait_for_thread( thread, &exit_value );
694
695 #elif defined( WIN32 )
696     WaitForSingleObject( thread, INFINITE);
697
698 #endif
699 }
700
701 #ifdef PROFILING
702 static void *vlc_thread_wrapper( void *p_wrapper )
703 {
704     /* Put user data in thread-local variables */
705     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
706     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
707
708     /* Set the profile timer value */
709     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
710
711     /* Tell the calling thread that we don't need its data anymore */
712     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
713     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
714     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
715
716     /* Call the real function */
717     return func( p_data );
718 }
719 #endif
720