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