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