]> git.sesse.net Git - vlc/blob - include/threads.h
* dvdcss_readv optimisations for Win32. We now send only one read command
[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.21 2001/07/25 08:41:21 gbazin 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 )
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 CRITICAL_SECTION vlc_mutex_t;
139
140 typedef struct
141 {
142     int             i_waiting_threads;
143     HANDLE          signal;
144 } vlc_cond_t;
145
146 typedef unsigned (__stdcall *PTHREAD_START) (void *);
147
148 #endif
149
150 typedef void *(*vlc_thread_func_t)(void *p_data);
151
152 /*****************************************************************************
153  * Prototypes
154  *****************************************************************************/
155
156 static __inline__ int  vlc_threads_init  ( void );
157 static __inline__ int  vlc_threads_end   ( void );
158
159 static __inline__ int  vlc_mutex_init    ( vlc_mutex_t * );
160 static __inline__ int  vlc_mutex_lock    ( vlc_mutex_t * );
161 static __inline__ int  vlc_mutex_unlock  ( vlc_mutex_t * );
162 static __inline__ int  vlc_mutex_destroy ( vlc_mutex_t * );
163
164 static __inline__ int  vlc_cond_init     ( vlc_cond_t * );
165 static __inline__ int  vlc_cond_signal   ( vlc_cond_t * );
166 static __inline__ int  vlc_cond_wait     ( vlc_cond_t *, vlc_mutex_t * );
167 static __inline__ int  vlc_cond_destroy  ( vlc_cond_t * );
168
169 static __inline__ int  vlc_thread_create ( vlc_thread_t *, char *,
170                                            vlc_thread_func_t, void * );
171 static __inline__ void vlc_thread_exit   ( void );
172 static __inline__ void vlc_thread_join   ( vlc_thread_t );
173
174 #if 0
175 static __inline__ int  vlc_cond_timedwait( vlc_cond_t *, vlc_mutex_t *,
176                                            mtime_t );
177 #endif
178
179 #ifdef PROFILING
180 /* Wrapper function for profiling */
181 static void *      vlc_thread_wrapper ( void *p_wrapper );
182
183 typedef struct wrapper_s
184 {
185     /* Data lock access */
186     vlc_mutex_t lock;
187     vlc_cond_t  wait;
188
189     /* Data used to spawn the real thread */
190     vlc_thread_func_t func;
191     void *p_data;
192
193     /* Profiling timer passed to the thread */
194     struct itimerval itimer;
195
196 } wrapper_t;
197
198 #ifdef WIN32
199 struct itimerval
200 {
201     struct timeval it_value;
202     struct timeval it_interval;
203 };
204
205 int setitimer(int kind, const struct itimerval* itnew,
206               struct itimerval* itold);
207
208 #define ITIMER_REAL 1
209 #define ITIMER_PROF 2
210
211 #endif /* WIN32 */
212
213 #endif /* PROFILING */
214
215 /*****************************************************************************
216  * vlc_threads_init: initialize threads system
217  *****************************************************************************/
218 static __inline__ int vlc_threads_init( void )
219 {
220 #if defined( PTH_INIT_IN_PTH_H )
221     return pth_init();
222
223 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
224     return 0;
225
226 #elif defined( HAVE_CTHREADS_H )
227     return 0;
228
229 #elif defined( HAVE_KERNEL_SCHEDULER_H )
230     return 0;
231
232 #elif defined( WIN32 )
233     return 0;
234
235 #endif
236 }
237
238 /*****************************************************************************
239  * vlc_threads_end: stop threads system
240  *****************************************************************************/
241 static __inline__ int vlc_threads_end( void )
242 {
243 #if defined( PTH_INIT_IN_PTH_H )
244     return pth_kill();
245
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247     return 0;
248
249 #elif defined( HAVE_CTHREADS_H )
250     return 0;
251
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
253     return 0;
254
255 #elif defined( WIN32 )
256     return 0;
257
258 #endif
259 }
260
261 /*****************************************************************************
262  * vlc_mutex_init: initialize a mutex
263  *****************************************************************************/
264 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
265 {
266 #if defined( PTH_INIT_IN_PTH_H )
267     return pth_mutex_init( p_mutex );
268
269 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
270     return pthread_mutex_init( p_mutex, NULL );
271
272 #elif defined( HAVE_CTHREADS_H )
273     mutex_init( p_mutex );
274     return 0;
275
276 #elif defined( HAVE_KERNEL_SCHEDULER_H )
277
278     /* check the arguments and whether it's already been initialized */
279     if( p_mutex == NULL )
280     {
281         return B_BAD_VALUE;
282     }
283
284     if( p_mutex->init == 9999 )
285     {
286         return EALREADY;
287     }
288
289     p_mutex->lock = create_sem( 1, "BeMutex" );
290     if( p_mutex->lock < B_NO_ERROR )
291     {
292         return( -1 );
293     }
294
295     p_mutex->init = 9999;
296     return B_OK;
297
298 #elif defined( WIN32 )
299     InitializeCriticalSection( p_mutex );
300     return 0;
301
302 #endif
303 }
304
305 /*****************************************************************************
306  * vlc_mutex_lock: lock a mutex
307  *****************************************************************************/
308 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
309 {
310 #if defined( PTH_INIT_IN_PTH_H )
311     return pth_mutex_acquire( p_mutex, TRUE, NULL );
312
313 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
314     return pthread_mutex_lock( p_mutex );
315
316 #elif defined( HAVE_CTHREADS_H )
317     mutex_lock( p_mutex );
318     return 0;
319
320 #elif defined( HAVE_KERNEL_SCHEDULER_H )
321     status_t err;
322
323     if( !p_mutex )
324     {
325         return B_BAD_VALUE;
326     }
327
328     if( p_mutex->init < 2000 )
329     {
330         return B_NO_INIT;
331     }
332
333     err = acquire_sem( p_mutex->lock );
334     return err;
335
336 #elif defined( WIN32 )
337     EnterCriticalSection( p_mutex );
338     return 0;
339
340 #endif
341 }
342
343 /*****************************************************************************
344  * vlc_mutex_unlock: unlock a mutex
345  *****************************************************************************/
346 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
347 {
348 #if defined( PTH_INIT_IN_PTH_H )
349     return pth_mutex_release( p_mutex );
350
351 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
352     return pthread_mutex_unlock( p_mutex );
353
354 #elif defined( HAVE_CTHREADS_H )
355     mutex_unlock( p_mutex );
356     return 0;
357
358 #elif defined( HAVE_KERNEL_SCHEDULER_H )
359     if( !p_mutex)
360     {
361         return B_BAD_VALUE;
362     }
363
364     if( p_mutex->init < 2000 )
365     {
366         return B_NO_INIT;
367     }
368
369     release_sem( p_mutex->lock );
370     return B_OK;
371
372 #elif defined( WIN32 )
373     LeaveCriticalSection( p_mutex );
374     return 0;
375
376 #endif
377 }
378
379 /*****************************************************************************
380  * vlc_mutex_destroy: destroy a mutex
381  *****************************************************************************/
382 static __inline__ int vlc_mutex_destroy( vlc_mutex_t *p_mutex )
383 {
384 #if defined( PTH_INIT_IN_PTH_H )
385     return 0;
386
387 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
388     return pthread_mutex_destroy( p_mutex );
389
390 #elif defined( HAVE_KERNEL_SCHEDULER_H )
391     if( p_mutex->init == 9999 )
392     {
393         delete_sem( p_mutex->lock );
394     }
395
396     p_mutex->init = 0;
397     return B_OK;
398
399 #elif defined( WIN32 )
400     DeleteCriticalSection( p_mutex );
401     return 0;
402
403 #endif    
404 }
405
406 /*****************************************************************************
407  * vlc_cond_init: initialize a condition
408  *****************************************************************************/
409 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
410 {
411 #if defined( PTH_INIT_IN_PTH_H )
412     return pth_cond_init( p_condvar );
413
414 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
415     return pthread_cond_init( p_condvar, NULL );
416
417 #elif defined( HAVE_CTHREADS_H )
418     /* condition_init() */
419     spin_lock_init( &p_condvar->lock );
420     cthread_queue_init( &p_condvar->queue );
421     p_condvar->name = 0;
422     p_condvar->implications = 0;
423
424     return 0;
425
426 #elif defined( HAVE_KERNEL_SCHEDULER_H )
427     if( !p_condvar )
428     {
429         return B_BAD_VALUE;
430     }
431
432     if( p_condvar->init == 9999 )
433     {
434         return EALREADY;
435     }
436
437     p_condvar->thread = -1;
438     p_condvar->init = 9999;
439     return 0;
440
441 #elif defined( WIN32 )
442     /* initialise counter */
443     p_condvar->i_waiting_threads = 0;
444
445     /* Create an auto-reset event. */
446     p_condvar->signal = CreateEvent( NULL, /* no security */
447                                      FALSE,  /* auto-reset event */
448                                      FALSE,  /* non-signaled initially */
449                                      NULL ); /* unnamed */
450
451     return( !p_condvar->signal );
452     
453 #endif
454 }
455
456 /*****************************************************************************
457  * vlc_cond_signal: start a thread on condition completion
458  *****************************************************************************/
459 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
460 {
461 #if defined( PTH_INIT_IN_PTH_H )
462     return pth_cond_notify( p_condvar, FALSE );
463
464 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
465     return pthread_cond_signal( p_condvar );
466
467 #elif defined( HAVE_CTHREADS_H )
468     /* condition_signal() */
469     if ( p_condvar->queue.head || p_condvar->implications )
470     {
471         cond_signal( (condition_t)p_condvar );
472     }
473     return 0;
474
475 #elif defined( HAVE_KERNEL_SCHEDULER_H )
476     if( !p_condvar )
477     {
478         return B_BAD_VALUE;
479     }
480
481     if( p_condvar->init < 2000 )
482     {
483         return B_NO_INIT;
484     }
485
486     while( p_condvar->thread != -1 )
487     {
488         thread_info info;
489         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
490         {
491             return 0;
492         }
493
494         if( info.state != B_THREAD_SUSPENDED )
495         {
496             /* The  waiting thread is not suspended so it could
497              * have been interrupted beetwen the unlock and the
498              * suspend_thread line. That is why we sleep a little
499              * before retesting p_condver->thread. */
500             snooze( 10000 );
501         }
502         else
503         {
504             /* Ok, we have to wake up that thread */
505             resume_thread( p_condvar->thread );
506             return 0;
507         }
508     }
509     return 0;
510
511 #elif defined( WIN32 )
512     /* Release one waiting thread if one is available. */
513     /* For this trick to work properly, the vlc_cond_signal must be surrounded
514      * by a mutex. This will prevent another thread from stealing the signal */
515     while( p_condvar->i_waiting_threads )
516     {
517         PulseEvent( p_condvar->signal );
518         Sleep( 0 ); /* deschedule the current thread */
519     }
520     return 0;
521
522 #endif
523 }
524
525 /*****************************************************************************
526  * vlc_cond_broadcast: start all threads waiting on condition completion
527  *****************************************************************************/
528 /*
529  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
530  * Only works with pthreads, you need to adapt it for others
531  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
532  */
533 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
534 {
535 #if defined( PTH_INIT_IN_PTH_H )
536     return pth_cond_notify( p_condvar, FALSE );
537
538 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
539     return pthread_cond_broadcast( p_condvar );
540
541 #elif defined( HAVE_CTHREADS_H )
542     /* condition_signal() */
543     if ( p_condvar->queue.head || p_condvar->implications )
544     {
545         cond_signal( (condition_t)p_condvar );
546     }
547     return 0;
548
549 #elif defined( HAVE_KERNEL_SCHEDULER_H )
550     if( !p_condvar )
551     {
552         return B_BAD_VALUE;
553     }
554
555     if( p_condvar->init < 2000 )
556     {
557         return B_NO_INIT;
558     }
559
560     while( p_condvar->thread != -1 )
561     {
562         thread_info info;
563         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
564         {
565             return 0;
566         }
567
568         if( info.state != B_THREAD_SUSPENDED )
569         {
570             /* The  waiting thread is not suspended so it could
571              * have been interrupted beetwen the unlock and the
572              * suspend_thread line. That is why we sleep a little
573              * before retesting p_condver->thread. */
574             snooze( 10000 );
575         }
576         else
577         {
578             /* Ok, we have to wake up that thread */
579             resume_thread( p_condvar->thread );
580             return 0;
581         }
582     }
583     return 0;
584
585 #elif defined( WIN32 )
586     /* Release all waiting threads. */
587     /* For this trick to work properly, the vlc_cond_signal must be surrounded
588      * by a mutex. This will prevent another thread from stealing the signal */
589     while( p_condvar->i_waiting_threads )
590     {
591         PulseEvent( p_condvar->signal );
592         Sleep( 0 ); /* deschedule the current thread */
593     }
594     return 0;
595
596 #endif
597 }
598
599 /*****************************************************************************
600  * vlc_cond_wait: wait until condition completion
601  *****************************************************************************/
602 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
603 {
604 #if defined( PTH_INIT_IN_PTH_H )
605     return pth_cond_await( p_condvar, p_mutex, NULL );
606
607 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
608     return pthread_cond_wait( p_condvar, p_mutex );
609
610 #elif defined( HAVE_CTHREADS_H )
611     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
612     return 0;
613
614 #elif defined( HAVE_KERNEL_SCHEDULER_H )
615     if( !p_condvar )
616     {
617         return B_BAD_VALUE;
618     }
619
620     if( !p_mutex )
621     {
622         return B_BAD_VALUE;
623     }
624
625     if( p_condvar->init < 2000 )
626     {
627         return B_NO_INIT;
628     }
629
630     /* The p_condvar->thread var is initialized before the unlock because
631      * it enables to identify when the thread is interrupted beetwen the
632      * unlock line and the suspend_thread line */
633     p_condvar->thread = find_thread( NULL );
634     vlc_mutex_unlock( p_mutex );
635     suspend_thread( p_condvar->thread );
636     p_condvar->thread = -1;
637
638     vlc_mutex_lock( p_mutex );
639     return 0;
640
641 #elif defined( WIN32 )
642     /* The ideal would be to use a function which atomically releases the
643      * mutex and initiate the waiting.
644      * Unfortunately only the SignalObjectAndWait function does this and it's
645      * only supported on WinNT/2K, furthermore it cannot take multiple
646      * events as parameters.
647      *
648      * The solution we use should however fulfill all our needs (even though
649      * it is not a correct pthreads implementation)
650      */
651     int i_result;
652
653     p_condvar->i_waiting_threads ++;
654
655     /* Release the mutex */
656     vlc_mutex_unlock( p_mutex );
657
658     i_result = WaitForSingleObject( p_condvar->signal, INFINITE); 
659
660     /* maybe we should protect this with a mutex ? */
661     p_condvar->i_waiting_threads --;
662
663     /* Reacquire the mutex before returning. */
664     vlc_mutex_lock( p_mutex );
665
666     return( i_result == WAIT_FAILED );
667
668 #endif
669 }
670
671 /*****************************************************************************
672  * vlc_cond_destroy: destroy a condition
673  *****************************************************************************/
674 static __inline__ int vlc_cond_destroy( vlc_cond_t *p_condvar )
675 {
676 #if defined( PTH_INIT_IN_PTH_H )
677     return 0;
678
679 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
680     return pthread_cond_destroy( p_condvar );
681
682 #elif defined( HAVE_KERNEL_SCHEDULER_H )
683     p_condvar->init = 0;
684     return 0;
685
686 #elif defined( WIN32 )
687     return( !CloseHandle( p_condvar->signal ) );
688
689 #endif    
690 }
691
692 /*****************************************************************************
693  * vlc_thread_create: create a thread
694  *****************************************************************************/
695 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
696                                          char *psz_name,
697                                          vlc_thread_func_t func,
698                                          void *p_data )
699 {
700     int i_ret;
701
702 #ifdef PROFILING
703     wrapper_t wrapper;
704
705     /* Initialize the wrapper structure */
706     wrapper.func = func;
707     wrapper.p_data = p_data;
708     getitimer( ITIMER_PROF, &wrapper.itimer );
709     vlc_mutex_init( &wrapper.lock );
710     vlc_cond_init( &wrapper.wait );
711     vlc_mutex_lock( &wrapper.lock );
712
713     /* Alter user-passed data so that we call the wrapper instead
714      * of the real function */
715     p_data = &wrapper;
716     func = vlc_thread_wrapper;
717 #endif
718
719 #if defined( PTH_INIT_IN_PTH_H )
720     *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
721     i_ret = ( p_thread == NULL );
722
723 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
724     i_ret = pthread_create( p_thread, NULL, func, p_data );
725
726 #elif defined( HAVE_CTHREADS_H )
727     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
728     i_ret = 0;
729
730 #elif defined( HAVE_KERNEL_SCHEDULER_H )
731     *p_thread = spawn_thread( (thread_func)func, psz_name,
732                               B_NORMAL_PRIORITY, p_data );
733     i_ret = resume_thread( *p_thread );
734
735 #elif defined( WIN32 )
736 #if 0
737     DWORD threadID;
738     /* This method is not recommended when using the MSVCRT C library,
739      * so we'll have to use _beginthreadex instead */
740     *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, 
741                              p_data, 0, &threadID);
742 #endif
743     unsigned threadID;
744     /* When using the MSVCRT C library you have to use the _beginthreadex
745      * function instead of CreateThread, otherwise you'll end up with memory
746      * leaks and the signal function not working */
747     *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func, 
748                              p_data, 0, &threadID);
749     
750     i_ret = ( *p_thread ? 0 : 1 );
751
752 #endif
753
754 #ifdef PROFILING
755     if( i_ret == 0 )
756     {
757         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
758     }
759
760     vlc_mutex_unlock( &wrapper.lock );
761     vlc_mutex_destroy( &wrapper.lock );
762     vlc_cond_destroy( &wrapper.wait );
763 #endif
764
765     return i_ret;
766 }
767
768 /*****************************************************************************
769  * vlc_thread_exit: terminate a thread
770  *****************************************************************************/
771 static __inline__ void vlc_thread_exit( void )
772 {
773 #if defined( PTH_INIT_IN_PTH_H )
774     pth_exit( 0 );
775
776 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
777     pthread_exit( 0 );
778
779 #elif defined( HAVE_CTHREADS_H )
780     int result;
781     cthread_exit( &result );
782
783 #elif defined( HAVE_KERNEL_SCHEDULER_H )
784     exit_thread( 0 );
785
786 #elif defined( WIN32 )
787 #if 0
788     ExitThread( 0 );
789 #endif
790     /* For now we don't close the thread handles (because of race conditions).
791      * Need to be looked at. */
792     _endthreadex(0);
793
794 #endif
795 }
796
797 /*****************************************************************************
798  * vlc_thread_join: wait until a thread exits
799  *****************************************************************************/
800 static __inline__ void vlc_thread_join( vlc_thread_t thread )
801 {
802 #if defined( PTH_INIT_IN_PTH_H )
803     pth_join( thread, NULL );
804
805 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
806     pthread_join( thread, NULL );
807
808 #elif defined( HAVE_CTHREADS_H )
809     cthread_join( thread );
810
811 #elif defined( HAVE_KERNEL_SCHEDULER_H )
812     int32 exit_value;
813     wait_for_thread( thread, &exit_value );
814
815 #elif defined( WIN32 )
816     WaitForSingleObject( thread, INFINITE);
817
818 #endif
819 }
820
821 #ifdef PROFILING
822 static void *vlc_thread_wrapper( void *p_wrapper )
823 {
824     /* Put user data in thread-local variables */
825     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
826     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
827
828     /* Set the profile timer value */
829     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
830
831     /* Tell the calling thread that we don't need its data anymore */
832     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
833     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
834     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
835
836     /* Call the real function */
837     return func( p_data );
838 }
839 #endif