78dc219dcc48690f25c1d4d7afa6adb8bbf18cc7
[vlc.git] / src / misc / threads.c
1 /*****************************************************************************
2  * threads.c : threads implementation for the VideoLAN client
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
5  * $Id: threads.c,v 1.36 2003/02/10 16:53:34 sam Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
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 <vlc/vlc.h>
27
28 #include <stdlib.h>
29
30 #define VLC_THREADS_UNINITIALIZED  0
31 #define VLC_THREADS_PENDING        1
32 #define VLC_THREADS_ERROR          2
33 #define VLC_THREADS_READY          3
34
35 /*****************************************************************************
36  * Global mutex for lazy initialization of the threads system
37  *****************************************************************************/
38 static volatile int i_initializations = 0;
39
40 #if defined( PTH_INIT_IN_PTH_H )
41 #elif defined( ST_INIT_IN_ST_H )
42 #elif defined( UNDER_CE )
43 #elif defined( WIN32 )
44 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
45     static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
46 #elif defined( HAVE_CTHREADS_H )
47 #elif defined( HAVE_KERNEL_SCHEDULER_H )
48 #endif
49
50 /*****************************************************************************
51  * Global variable for named mutexes
52  *****************************************************************************/
53 typedef struct vlc_namedmutex_t vlc_namedmutex_t;
54 struct vlc_namedmutex_t
55 {
56     vlc_mutex_t lock;
57
58     char *psz_name;
59     int i_usage;
60     vlc_namedmutex_t *p_next;
61 };
62
63 /*****************************************************************************
64  * vlc_threads_init: initialize threads system
65  *****************************************************************************
66  * This function requires lazy initialization of a global lock in order to
67  * keep the library really thread-safe. Some architectures don't support this
68  * and thus do not guarantee the complete reentrancy.
69  *****************************************************************************/
70 int __vlc_threads_init( vlc_object_t *p_this )
71 {
72     static volatile int i_status = VLC_THREADS_UNINITIALIZED;
73
74     libvlc_t *p_libvlc = (libvlc_t *)p_this;
75     int i_ret = VLC_SUCCESS;
76
77     /* If we have lazy mutex initialization, use it. Otherwise, we just
78      * hope nothing wrong happens. */
79 #if defined( PTH_INIT_IN_PTH_H )
80 #elif defined( ST_INIT_IN_ST_H )
81 #elif defined( UNDER_CE )
82 #elif defined( WIN32 )
83     HINSTANCE hInstLib;
84 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
85     pthread_mutex_lock( &once_mutex );
86 #elif defined( HAVE_CTHREADS_H )
87 #elif defined( HAVE_KERNEL_SCHEDULER_H )
88 #endif
89
90     if( i_status == VLC_THREADS_UNINITIALIZED )
91     {
92         i_status = VLC_THREADS_PENDING;
93
94         /* We should be safe now. Do all the initialization stuff we want. */
95         vlc_object_create( p_libvlc, VLC_OBJECT_ROOT );
96         p_libvlc->b_ready = VLC_FALSE;
97
98 #if defined( PTH_INIT_IN_PTH_H )
99         i_ret = pth_init();
100
101 #elif defined( ST_INIT_IN_ST_H )
102         i_ret = st_init();
103
104 #elif defined( UNDER_CE )
105         /* Nothing to initialize */
106
107 #elif defined( WIN32 )
108         /* Dynamically get the address of SignalObjectAndWait */
109         if( GetVersion() < 0x80000000 )
110         {
111             /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
112             hInstLib = LoadLibrary( "kernel32" );
113             if( hInstLib )
114             {
115                 p_libvlc->SignalObjectAndWait =
116                     (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
117                                                      "SignalObjectAndWait" );
118             }
119         }
120         else
121         {
122             p_libvlc->SignalObjectAndWait = NULL;
123         }
124
125         p_libvlc->b_fast_mutex = 0;
126         p_libvlc->i_win9x_cv = 0;
127
128 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
129 #elif defined( HAVE_CTHREADS_H )
130 #elif defined( HAVE_KERNEL_SCHEDULER_H )
131 #endif
132
133         if( i_ret )
134         {
135             i_status = VLC_THREADS_ERROR;
136         }
137         else
138         {
139             i_initializations++;
140             i_status = VLC_THREADS_READY;
141         }
142     }
143     else
144     {
145         /* Just increment the initialization count */
146         i_initializations++;
147     }
148
149     /* If we have lazy mutex initialization support, unlock the mutex;
150      * otherwize, do a naive wait loop. */
151 #if defined( PTH_INIT_IN_PTH_H )
152     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
153 #elif defined( ST_INIT_IN_ST_H )
154     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
155 #elif defined( UNDER_CE )
156     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
157 #elif defined( WIN32 )
158     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
159 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
160     pthread_mutex_unlock( &once_mutex );
161 #elif defined( HAVE_CTHREADS_H )
162     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
163 #elif defined( HAVE_KERNEL_SCHEDULER_H )
164     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
165 #endif
166
167     if( i_status != VLC_THREADS_READY )
168     {
169         return VLC_ETHREAD;
170     }
171
172     return i_ret;
173 }
174
175 /*****************************************************************************
176  * vlc_threads_end: stop threads system
177  *****************************************************************************
178  * FIXME: This function is far from being threadsafe. We should undo exactly
179  * what we did above in vlc_threads_init.
180  *****************************************************************************/
181 int __vlc_threads_end( vlc_object_t *p_this )
182 {
183 #if defined( PTH_INIT_IN_PTH_H )
184     i_initializations--;
185     if( i_initializations == 0 )
186     {
187         return pth_kill();
188     }
189
190 #elif defined( ST_INIT_IN_ST_H )
191     i_initializations--;
192
193 #elif defined( UNDER_CE )
194     i_initializations--;
195
196 #elif defined( WIN32 )
197     i_initializations--;
198
199 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
200     pthread_mutex_lock( &once_mutex );
201     i_initializations--;
202     pthread_mutex_unlock( &once_mutex );
203
204 #elif defined( HAVE_CTHREADS_H )
205     i_initializations--;
206
207 #elif defined( HAVE_KERNEL_SCHEDULER_H )
208     i_initializations--;
209
210 #endif
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Prototype for GPROF wrapper
216  *****************************************************************************/
217 #ifdef GPROF
218 typedef void *(*vlc_thread_func_t)(void *p_data);
219
220 /* Wrapper function for profiling */
221 static void *      vlc_thread_wrapper ( void *p_wrapper );
222
223 #   if defined( WIN32 ) && !defined( UNDER_CE )
224
225 #       define ITIMER_REAL 1
226 #       define ITIMER_PROF 2
227
228 struct itimerval
229 {
230     struct timeval it_value;
231     struct timeval it_interval;
232 };
233
234 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
235 #   endif /* WIN32 && !UNDER_CE */
236
237 typedef struct wrapper_t
238 {
239     /* Data lock access */
240     vlc_mutex_t lock;
241     vlc_cond_t  wait;
242
243     /* Data used to spawn the real thread */
244     vlc_thread_func_t func;
245     void *p_data;
246
247     /* Profiling timer passed to the thread */
248     struct itimerval itimer;
249
250 } wrapper_t;
251
252 #endif /* GPROF */
253
254 /*****************************************************************************
255  * vlc_mutex_init: initialize a mutex
256  *****************************************************************************/
257 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
258 {
259     p_mutex->p_this = p_this;
260
261 #if defined( PTH_INIT_IN_PTH_H )
262     return pth_mutex_init( &p_mutex->mutex );
263
264 #elif defined( ST_INIT_IN_ST_H )
265     p_mutex->mutex = st_mutex_new();
266     return ( p_mutex->mutex == NULL ) ? errno : 0;
267
268 #elif defined( UNDER_CE )
269     InitializeCriticalSection( &p_mutex->csection );
270     return 0;
271
272 #elif defined( WIN32 )
273     /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
274      * function and have a 100% correct vlc_cond_wait() implementation.
275      * As this function is not available on Win9x, we can use the faster
276      * CriticalSections */
277     if( p_this->p_libvlc->SignalObjectAndWait &&
278         !p_this->p_libvlc->b_fast_mutex )
279     {
280         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
281         p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
282         return ( p_mutex->mutex != NULL ? 0 : 1 );
283     }
284     else
285     {
286         p_mutex->mutex = NULL;
287         InitializeCriticalSection( &p_mutex->csection );
288         return 0;
289     }
290
291 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
292 #   if defined(DEBUG) && defined(SYS_LINUX)
293     {
294         /* Create error-checking mutex to detect problems more easily. */
295         pthread_mutexattr_t attr;
296         int                 i_result;
297
298         pthread_mutexattr_init( &attr );
299         pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
300         i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
301         pthread_mutexattr_destroy( &attr );
302         return( i_result );
303     }
304 #   endif
305     return pthread_mutex_init( &p_mutex->mutex, NULL );
306
307 #elif defined( HAVE_CTHREADS_H )
308     mutex_init( p_mutex );
309     return 0;
310
311 #elif defined( HAVE_KERNEL_SCHEDULER_H )
312     /* check the arguments and whether it's already been initialized */
313     if( p_mutex == NULL )
314     {
315         return B_BAD_VALUE;
316     }
317
318     if( p_mutex->init == 9999 )
319     {
320         return EALREADY;
321     }
322
323     p_mutex->lock = create_sem( 1, "BeMutex" );
324     if( p_mutex->lock < B_NO_ERROR )
325     {
326         return( -1 );
327     }
328
329     p_mutex->init = 9999;
330     return B_OK;
331
332 #endif
333 }
334
335 /*****************************************************************************
336  * vlc_mutex_destroy: destroy a mutex, inner version
337  *****************************************************************************/
338 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
339 {
340     int i_result;
341     /* In case of error : */
342     int i_thread = -1;
343     const char * psz_error = "";
344
345 #if defined( PTH_INIT_IN_PTH_H )
346     return 0;
347
348 #elif defined( ST_INIT_IN_ST_H )
349     i_result = st_mutex_destroy( p_mutex->mutex );
350
351 #elif defined( UNDER_CE )
352     DeleteCriticalSection( &p_mutex->csection );
353     return 0;
354
355 #elif defined( WIN32 )
356     if( p_mutex->mutex )
357     {
358         CloseHandle( p_mutex->mutex );
359     }
360     else
361     {
362         DeleteCriticalSection( &p_mutex->csection );
363     }
364     return 0;
365
366 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
367     i_result = pthread_mutex_destroy( &p_mutex->mutex );
368     if ( i_result )
369     {
370         i_thread = (int)pthread_self();
371         psz_error = strerror(i_result);
372     }
373
374 #elif defined( HAVE_CTHREADS_H )
375     return 0;
376
377 #elif defined( HAVE_KERNEL_SCHEDULER_H )
378     if( p_mutex->init == 9999 )
379     {
380         delete_sem( p_mutex->lock );
381     }
382
383     p_mutex->init = 0;
384     return B_OK;
385 #endif
386
387     if( i_result )
388     {
389         msg_Err( p_mutex->p_this,
390                  "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
391                  i_thread, psz_file, i_line, i_result, psz_error );
392     }
393     return i_result;
394 }
395
396 /*****************************************************************************
397  * vlc_cond_init: initialize a condition
398  *****************************************************************************/
399 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
400 {
401     p_condvar->p_this = p_this;
402
403 #if defined( PTH_INIT_IN_PTH_H )
404     return pth_cond_init( &p_condvar->cond );
405
406 #elif defined( ST_INIT_IN_ST_H )
407     p_condvar->cond = st_cond_new();
408     return ( p_condvar->cond == NULL ) ? errno : 0;
409
410 #elif defined( UNDER_CE )
411     /* Initialize counter */
412     p_condvar->i_waiting_threads = 0;
413
414     /* Create an auto-reset event. */
415     p_condvar->event = CreateEvent( NULL,   /* no security */
416                                     FALSE,  /* auto-reset event */
417                                     FALSE,  /* start non-signaled */
418                                     NULL ); /* unnamed */
419     return !p_condvar->event;
420
421 #elif defined( WIN32 )
422     /* Initialize counter */
423     p_condvar->i_waiting_threads = 0;
424
425     /* Misc init */
426     p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
427     p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
428
429     if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
430         || p_condvar->i_win9x_cv == 0 )
431     {
432         /* Create an auto-reset event. */
433         p_condvar->event = CreateEvent( NULL,   /* no security */
434                                         FALSE,  /* auto-reset event */
435                                         FALSE,  /* start non-signaled */
436                                         NULL ); /* unnamed */
437
438         p_condvar->semaphore = NULL;
439         return !p_condvar->event;
440     }
441     else
442     {
443         p_condvar->semaphore = CreateSemaphore( NULL,       /* no security */
444                                                 0,          /* initial count */
445                                                 0x7fffffff, /* max count */
446                                                 NULL );     /* unnamed */
447
448         if( p_condvar->i_win9x_cv == 1 )
449             /* Create a manual-reset event initially signaled. */
450             p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
451         else
452             /* Create a auto-reset event. */
453             p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
454
455         InitializeCriticalSection( &p_condvar->csection );
456
457         return !p_condvar->semaphore || !p_condvar->event;
458     }
459
460 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
461     return pthread_cond_init( &p_condvar->cond, NULL );
462
463 #elif defined( HAVE_CTHREADS_H )
464     /* condition_init() */
465     spin_lock_init( &p_condvar->lock );
466     cthread_queue_init( &p_condvar->queue );
467     p_condvar->name = 0;
468     p_condvar->implications = 0;
469
470     return 0;
471
472 #elif defined( HAVE_KERNEL_SCHEDULER_H )
473     if( !p_condvar )
474     {
475         return B_BAD_VALUE;
476     }
477
478     if( p_condvar->init == 9999 )
479     {
480         return EALREADY;
481     }
482
483     p_condvar->thread = -1;
484     p_condvar->init = 9999;
485     return 0;
486 #endif
487 }
488
489 /*****************************************************************************
490  * vlc_cond_destroy: destroy a condition, inner version
491  *****************************************************************************/
492 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
493 {
494     int i_result;
495     /* In case of error : */
496     int i_thread = -1;
497     const char * psz_error = "";
498
499 #if defined( PTH_INIT_IN_PTH_H )
500     return 0;
501
502 #elif defined( ST_INIT_IN_ST_H )
503     i_result = st_cond_destroy( p_condvar->cond );
504
505 #elif defined( UNDER_CE )
506     i_result = !CloseHandle( p_condvar->event );
507
508 #elif defined( WIN32 )
509     if( !p_condvar->semaphore )
510         i_result = !CloseHandle( p_condvar->event );
511     else
512         i_result = !CloseHandle( p_condvar->event )
513           || !CloseHandle( p_condvar->semaphore );
514
515 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
516     i_result = pthread_cond_destroy( &p_condvar->cond );
517     if ( i_result )
518     {
519         i_thread = (int)pthread_self();
520         psz_error = strerror(i_result);
521     }
522
523 #elif defined( HAVE_CTHREADS_H )
524     return 0;
525
526 #elif defined( HAVE_KERNEL_SCHEDULER_H )
527     p_condvar->init = 0;
528     return 0;
529 #endif
530
531     if( i_result )
532     {
533         msg_Err( p_condvar->p_this,
534                  "thread %d: cond_destroy failed at %s:%d (%d:%s)",
535                  i_thread, psz_file, i_line, i_result, psz_error );
536     }
537     return i_result;
538 }
539
540 /*****************************************************************************
541  * vlc_thread_create: create a thread, inner version
542  *****************************************************************************
543  * Note that i_priority is only taken into account on platforms supporting
544  * userland real-time priority threads.
545  *****************************************************************************/
546 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
547                          char *psz_name, void * ( *func ) ( void * ),
548                          int i_priority, vlc_bool_t b_wait )
549 {
550     int i_ret;
551     void *p_data;
552
553     vlc_mutex_lock( &p_this->object_lock );
554
555 #ifdef GPROF
556     wrapper_t wrapper;
557
558     /* Initialize the wrapper structure */
559     wrapper.func = func;
560     wrapper.p_data = (void *)p_this;
561     getitimer( ITIMER_PROF, &wrapper.itimer );
562     vlc_mutex_init( p_this, &wrapper.lock );
563     vlc_cond_init( p_this, &wrapper.wait );
564     vlc_mutex_lock( &wrapper.lock );
565
566     /* Alter user-passed data so that we call the wrapper instead
567      * of the real function */
568     p_data = &wrapper;
569     func = vlc_thread_wrapper;
570 #else
571     p_data = (void *)p_this;
572 #endif
573
574 #if defined( PTH_INIT_IN_PTH_H )
575     p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
576     i_ret = 0;
577
578 #elif defined( ST_INIT_IN_ST_H )
579     p_this->thread_id = st_thread_create( func, p_data, 1, 0 );
580     i_ret = 0;
581
582 #elif defined( WIN32 ) || defined( UNDER_CE )
583     {
584         unsigned threadID;
585         /* When using the MSVCRT C library you have to use the _beginthreadex
586          * function instead of CreateThread, otherwise you'll end up with
587          * memory leaks and the signal functions not working (see Microsoft
588          * Knowledge Base, article 104641) */
589         p_this->thread_id =
590 #if defined( UNDER_CE )
591                 (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
592                                       p_data, 0, &threadID );
593 #else
594                 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
595                                         p_data, 0, &threadID );
596 #endif
597     }
598
599     if ( p_this->thread_id && i_priority )
600     {
601         if ( !SetThreadPriority(p_this->thread_id, i_priority) )
602         {
603             msg_Warn( p_this, "couldn't set a faster priority" );
604             i_priority = 0;
605         }
606     }
607
608     i_ret = ( p_this->thread_id ? 0 : 1 );
609
610 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
611     i_ret = pthread_create( &p_this->thread_id, NULL, func, p_data );
612
613     if ( i_priority )
614     {
615         int i_error;
616         struct sched_param param;
617         memset( &param, 0, sizeof(struct sched_param) );
618         param.sched_priority = i_priority;
619         if ( (i_error = pthread_setschedparam( p_this->thread_id,
620                                                SCHED_RR, &param )) )
621         {
622             msg_Warn( p_this, "couldn't go to real-time priority (%s:%d): %s",
623                       psz_file, i_line, strerror(i_error) );
624             i_priority = 0;
625         }
626
627     }
628
629 #elif defined( HAVE_CTHREADS_H )
630     p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
631     i_ret = 0;
632
633 #elif defined( HAVE_KERNEL_SCHEDULER_H )
634     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
635                                       i_priority, p_data );
636     i_ret = resume_thread( p_this->thread_id );
637
638 #endif
639
640 #ifdef GPROF
641     if( i_ret == 0 )
642     {
643         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
644     }
645
646     vlc_mutex_unlock( &wrapper.lock );
647     vlc_mutex_destroy( &wrapper.lock );
648     vlc_cond_destroy( &wrapper.wait );
649 #endif
650
651     if( i_ret == 0 )
652     {
653         if( b_wait )
654         {
655             msg_Dbg( p_this, "waiting for thread completion" );
656             vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
657         }
658
659         p_this->b_thread = 1;
660
661         msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
662                  (int)p_this->thread_id, psz_name, i_priority,
663                  psz_file, i_line );
664
665         vlc_mutex_unlock( &p_this->object_lock );
666     }
667     else
668     {
669 #ifdef HAVE_STRERROR
670         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
671                          psz_name, psz_file, i_line, strerror(i_ret) );
672 #else
673         msg_Err( p_this, "%s thread could not be created at %s:%d",
674                          psz_name, psz_file, i_line );
675 #endif
676         vlc_mutex_unlock( &p_this->object_lock );
677     }
678
679     return i_ret;
680 }
681
682 /*****************************************************************************
683  * vlc_thread_set_priority: set the priority of the current thread when we
684  * couldn't set it in vlc_thread_create (for instance for the main thread)
685  *****************************************************************************/
686 int __vlc_thread_set_priority( vlc_object_t *p_this, char * psz_file,
687                                int i_line, int i_priority )
688 {
689 #if defined( WIN32 ) || defined( UNDER_CE )
690     if ( !SetThreadPriority(GetCurrentThread(), i_priority) )
691     {
692         msg_Warn( p_this, "couldn't set a faster priority" );
693         return 1;
694     }
695
696 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
697     if ( i_priority )
698     {
699         int i_error;
700         struct sched_param param;
701         memset( &param, 0, sizeof(struct sched_param) );
702         param.sched_priority = i_priority;
703         if ( (i_error = pthread_setschedparam( pthread_self(),
704                                                SCHED_RR, &param )) )
705         {
706             msg_Warn( p_this, "couldn't go to real-time priority (%s:%d): %s",
707                       psz_file, i_line, strerror(i_error) );
708             i_priority = 0;
709         }
710     }
711
712 #endif
713
714     return 0;
715 }
716
717 /*****************************************************************************
718  * vlc_thread_ready: tell the parent thread we were successfully spawned
719  *****************************************************************************/
720 void __vlc_thread_ready( vlc_object_t *p_this )
721 {
722     vlc_mutex_lock( &p_this->object_lock );
723     vlc_cond_signal( &p_this->object_wait );
724     vlc_mutex_unlock( &p_this->object_lock );
725 }
726
727 /*****************************************************************************
728  * vlc_thread_join: wait until a thread exits, inner version
729  *****************************************************************************/
730 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
731 {
732     int i_ret = 0;
733
734 #if defined( PTH_INIT_IN_PTH_H )
735     i_ret = pth_join( p_this->thread_id, NULL );
736
737 #elif defined( ST_INIT_IN_ST_H )
738     i_ret = st_thread_join( p_this->thread_id, NULL );
739
740 #elif defined( UNDER_CE )
741     WaitForSingleObject( p_this->thread_id, INFINITE );
742
743 #elif defined( WIN32 )
744     WaitForSingleObject( p_this->thread_id, INFINITE );
745
746 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
747     i_ret = pthread_join( p_this->thread_id, NULL );
748
749 #elif defined( HAVE_CTHREADS_H )
750     cthread_join( p_this->thread_id );
751     i_ret = 1;
752
753 #elif defined( HAVE_KERNEL_SCHEDULER_H )
754     int32 exit_value;
755     wait_for_thread( p_this->thread_id, &exit_value );
756
757 #endif
758
759     if( i_ret )
760     {
761 #ifdef HAVE_STRERROR
762         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
763                          (int)p_this->thread_id, psz_file, i_line,
764                          strerror(i_ret) );
765 #else
766         msg_Err( p_this, "thread_join(%d) failed at %s:%d",
767                          (int)p_this->thread_id, psz_file, i_line );
768 #endif
769     }
770     else
771     {
772         msg_Dbg( p_this, "thread %d joined (%s:%d)",
773                          (int)p_this->thread_id, psz_file, i_line );
774     }
775
776     p_this->b_thread = 0;
777 }
778
779 /*****************************************************************************
780  * vlc_thread_wrapper: wrapper around thread functions used when profiling.
781  *****************************************************************************/
782 #ifdef GPROF
783 static void *vlc_thread_wrapper( void *p_wrapper )
784 {
785     /* Put user data in thread-local variables */
786     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
787     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
788
789     /* Set the profile timer value */
790     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
791
792     /* Tell the calling thread that we don't need its data anymore */
793     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
794     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
795     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
796
797     /* Call the real function */
798     return func( p_data );
799 }
800 #endif