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