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