]> git.sesse.net Git - vlc/blob - include/threads.h
a43129630d11f00cec17758f339c355d0f331430
[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.20 2001/07/18 14:21:00 massiot 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_broadcast: start all threads waiting on condition completion
496  *****************************************************************************/
497 /*
498  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
499  * Only works with pthreads, you need to adapt it for others
500  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
501  */
502 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
503 {
504 #if defined( PTH_INIT_IN_PTH_H )
505     return pth_cond_notify( p_condvar, FALSE );
506
507 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
508     return pthread_cond_broadcast( p_condvar );
509
510 #elif defined( HAVE_CTHREADS_H )
511     /* condition_signal() */
512     if ( p_condvar->queue.head || p_condvar->implications )
513     {
514         cond_signal( (condition_t)p_condvar );
515     }
516     return 0;
517
518 #elif defined( HAVE_KERNEL_SCHEDULER_H )
519     if( !p_condvar )
520     {
521         return B_BAD_VALUE;
522     }
523
524     if( p_condvar->init < 2000 )
525     {
526         return B_NO_INIT;
527     }
528
529     while( p_condvar->thread != -1 )
530     {
531         thread_info info;
532         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
533         {
534             return 0;
535         }
536
537         if( info.state != B_THREAD_SUSPENDED )
538         {
539             /* The  waiting thread is not suspended so it could
540              * have been interrupted beetwen the unlock and the
541              * suspend_thread line. That is why we sleep a little
542              * before retesting p_condver->thread. */
543             snooze( 10000 );
544         }
545         else
546         {
547             /* Ok, we have to wake up that thread */
548             resume_thread( p_condvar->thread );
549             return 0;
550         }
551     }
552     return 0;
553
554 #elif defined( WIN32 )
555     /* Try to release one waiting thread. */
556     PulseEvent ( *p_condvar );
557     return 0;
558
559 #endif
560 }
561
562 /*****************************************************************************
563  * vlc_cond_wait: wait until condition completion
564  *****************************************************************************/
565 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
566 {
567 #if defined( PTH_INIT_IN_PTH_H )
568     return pth_cond_await( p_condvar, p_mutex, NULL );
569
570 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
571     return pthread_cond_wait( p_condvar, p_mutex );
572
573 #elif defined( HAVE_CTHREADS_H )
574     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
575     return 0;
576
577 #elif defined( HAVE_KERNEL_SCHEDULER_H )
578     if( !p_condvar )
579     {
580         return B_BAD_VALUE;
581     }
582
583     if( !p_mutex )
584     {
585         return B_BAD_VALUE;
586     }
587
588     if( p_condvar->init < 2000 )
589     {
590         return B_NO_INIT;
591     }
592
593     /* The p_condvar->thread var is initialized before the unlock because
594      * it enables to identify when the thread is interrupted beetwen the
595      * unlock line and the suspend_thread line */
596     p_condvar->thread = find_thread( NULL );
597     vlc_mutex_unlock( p_mutex );
598     suspend_thread( p_condvar->thread );
599     p_condvar->thread = -1;
600
601     vlc_mutex_lock( p_mutex );
602     return 0;
603
604 #elif defined( WIN32 )
605     /* Release the <external_mutex> here and wait for the event
606      * to become signaled, due to <pthread_cond_signal> being
607      * called. */
608     vlc_mutex_unlock( p_mutex );
609
610     WaitForSingleObject( *p_condvar, INFINITE );
611
612     /* Reacquire the mutex before returning. */
613     vlc_mutex_lock( p_mutex );
614     return 0;
615
616 #endif
617 }
618
619 /*****************************************************************************
620  * vlc_cond_destroy: destroy a condition
621  *****************************************************************************/
622 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
623 {
624 #if defined( PTH_INIT_IN_PTH_H )
625     return 0;
626
627 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
628     return pthread_cond_destroy( p_condvar );
629
630 #elif defined( HAVE_KERNEL_SCHEDULER_H )
631     p_condvar->init = 0;
632     return 0;
633
634 #elif defined( WIN32 )
635     CloseHandle( *p_condvar );
636     return 0;
637
638 #endif    
639 }
640
641 /*****************************************************************************
642  * vlc_thread_create: create a thread
643  *****************************************************************************/
644 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
645                                          char *psz_name, vlc_thread_func_t func,
646                                          void *p_data )
647 {
648     int i_ret;
649
650 #ifdef PROFILING
651     wrapper_t wrapper;
652
653     /* Initialize the wrapper structure */
654     wrapper.func = func;
655     wrapper.p_data = p_data;
656     getitimer( ITIMER_PROF, &wrapper.itimer );
657     vlc_mutex_init( &wrapper.lock );
658     vlc_cond_init( &wrapper.wait );
659     vlc_mutex_lock( &wrapper.lock );
660
661     /* Alter user-passed data so that we call the wrapper instead
662      * of the real function */
663     p_data = &wrapper;
664     func = vlc_thread_wrapper;
665 #endif
666
667 #if defined( PTH_INIT_IN_PTH_H )
668     *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
669     i_ret = ( p_thread == NULL );
670
671 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
672     i_ret = pthread_create( p_thread, NULL, func, p_data );
673
674 #elif defined( HAVE_CTHREADS_H )
675     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
676     i_ret = 0;
677
678 #elif defined( HAVE_KERNEL_SCHEDULER_H )
679     *p_thread = spawn_thread( (thread_func)func, psz_name,
680                               B_NORMAL_PRIORITY, p_data );
681     i_ret = resume_thread( *p_thread );
682
683 #elif defined( WIN32 )
684 #if 0
685     DWORD threadID;
686     /* This method is not recommended when using the MSVCRT C library,
687      * so we'll have to use _beginthreadex instead */
688     *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, 
689                              p_data, 0, &threadID);
690 #endif
691     unsigned threadID;
692     /* When using the MSVCRT C library you have to use the _beginthreadex
693      * function instead of CreateThread, otherwise you'll end up with memory
694      * leaks and the signal function not working */
695     *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func, 
696                              p_data, 0, &threadID);
697     
698     i_ret = ( *p_thread ? 0 : 1 );
699
700 #endif
701
702 #ifdef PROFILING
703     if( i_ret == 0 )
704     {
705         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
706     }
707
708     vlc_mutex_unlock( &wrapper.lock );
709     vlc_mutex_destroy( &wrapper.lock );
710     vlc_cond_destroy( &wrapper.wait );
711 #endif
712
713     return i_ret;
714 }
715
716 /*****************************************************************************
717  * vlc_thread_exit: terminate a thread
718  *****************************************************************************/
719 static __inline__ void vlc_thread_exit( void )
720 {
721 #if defined( PTH_INIT_IN_PTH_H )
722     pth_exit( 0 );
723
724 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
725     pthread_exit( 0 );
726
727 #elif defined( HAVE_CTHREADS_H )
728     int result;
729     cthread_exit( &result );
730
731 #elif defined( HAVE_KERNEL_SCHEDULER_H )
732     exit_thread( 0 );
733
734 #elif defined( WIN32 )
735 #if 0
736     ExitThread( 0 );
737 #endif
738     /* For now we don't close the thread handles (because of race conditions).
739      * Need to be looked at. */
740     _endthreadex(0);
741
742 #endif
743 }
744
745 /*****************************************************************************
746  * vlc_thread_join: wait until a thread exits
747  *****************************************************************************/
748 static __inline__ void vlc_thread_join( vlc_thread_t thread )
749 {
750 #if defined( PTH_INIT_IN_PTH_H )
751     pth_join( thread, NULL );
752
753 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
754     pthread_join( thread, NULL );
755
756 #elif defined( HAVE_CTHREADS_H )
757     cthread_join( thread );
758
759 #elif defined( HAVE_KERNEL_SCHEDULER_H )
760     int32 exit_value;
761     wait_for_thread( thread, &exit_value );
762
763 #elif defined( WIN32 )
764     WaitForSingleObject( thread, INFINITE);
765
766 #endif
767 }
768
769 #ifdef PROFILING
770 static void *vlc_thread_wrapper( void *p_wrapper )
771 {
772     /* Put user data in thread-local variables */
773     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
774     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
775
776     /* Set the profile timer value */
777     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
778
779     /* Tell the calling thread that we don't need its data anymore */
780     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
781     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
782     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
783
784     /* Call the real function */
785     return func( p_data );
786 }
787 #endif
788