]> git.sesse.net Git - vlc/blob - src/misc/threads.c
* include/vlc_common.h:
[vlc] / 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.42 2003/10/25 00:49:14 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         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( PTHREAD_COND_T_IN_PTHREAD_H )
128 #elif defined( HAVE_CTHREADS_H )
129 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
161     pthread_mutex_unlock( &once_mutex );
162 #elif defined( HAVE_CTHREADS_H )
163     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
164 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
201     pthread_mutex_lock( &once_mutex );
202     i_initializations--;
203     pthread_mutex_unlock( &once_mutex );
204
205 #elif defined( HAVE_CTHREADS_H )
206     i_initializations--;
207
208 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
253 #   if defined(DEBUG) && defined(SYS_LINUX)
254     {
255         /* Create error-checking mutex to detect problems more easily. */
256         pthread_mutexattr_t attr;
257         int                 i_result;
258
259         pthread_mutexattr_init( &attr );
260         pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
261         i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
262         pthread_mutexattr_destroy( &attr );
263         return( i_result );
264     }
265 #   endif
266     return pthread_mutex_init( &p_mutex->mutex, NULL );
267
268 #elif defined( HAVE_CTHREADS_H )
269     mutex_init( p_mutex );
270     return 0;
271
272 #elif defined( HAVE_KERNEL_SCHEDULER_H )
273     /* check the arguments and whether it's already been initialized */
274     if( p_mutex == NULL )
275     {
276         return B_BAD_VALUE;
277     }
278
279     if( p_mutex->init == 9999 )
280     {
281         return EALREADY;
282     }
283
284     p_mutex->lock = create_sem( 1, "BeMutex" );
285     if( p_mutex->lock < B_NO_ERROR )
286     {
287         return( -1 );
288     }
289
290     p_mutex->init = 9999;
291     return B_OK;
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( PTHREAD_COND_T_IN_PTHREAD_H )
328     i_result = pthread_mutex_destroy( &p_mutex->mutex );
329     if ( i_result )
330     {
331         i_thread = (int)pthread_self();
332         psz_error = strerror(i_result);
333     }
334
335 #elif defined( HAVE_CTHREADS_H )
336     return 0;
337
338 #elif defined( HAVE_KERNEL_SCHEDULER_H )
339     if( p_mutex->init == 9999 )
340     {
341         delete_sem( p_mutex->lock );
342     }
343
344     p_mutex->init = 0;
345     return B_OK;
346 #endif
347
348     if( i_result )
349     {
350         msg_Err( p_mutex->p_this,
351                  "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
352                  i_thread, psz_file, i_line, i_result, psz_error );
353     }
354     return i_result;
355 }
356
357 /*****************************************************************************
358  * vlc_cond_init: initialize a condition
359  *****************************************************************************/
360 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
361 {
362     p_condvar->p_this = p_this;
363
364 #if defined( PTH_INIT_IN_PTH_H )
365     return ( pth_cond_init( &p_condvar->cond ) == FALSE );
366
367 #elif defined( ST_INIT_IN_ST_H )
368     p_condvar->cond = st_cond_new();
369     return ( p_condvar->cond == NULL ) ? errno : 0;
370
371 #elif defined( UNDER_CE )
372     /* Initialize counter */
373     p_condvar->i_waiting_threads = 0;
374
375     /* Create an auto-reset event. */
376     p_condvar->event = CreateEvent( NULL,   /* no security */
377                                     FALSE,  /* auto-reset event */
378                                     FALSE,  /* start non-signaled */
379                                     NULL ); /* unnamed */
380     return !p_condvar->event;
381
382 #elif defined( WIN32 )
383     /* Initialize counter */
384     p_condvar->i_waiting_threads = 0;
385
386     /* Misc init */
387     p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
388     p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
389
390     if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
391         || p_condvar->i_win9x_cv == 0 )
392     {
393         /* Create an auto-reset event. */
394         p_condvar->event = CreateEvent( NULL,   /* no security */
395                                         FALSE,  /* auto-reset event */
396                                         FALSE,  /* start non-signaled */
397                                         NULL ); /* unnamed */
398
399         p_condvar->semaphore = NULL;
400         return !p_condvar->event;
401     }
402     else
403     {
404         p_condvar->semaphore = CreateSemaphore( NULL,       /* no security */
405                                                 0,          /* initial count */
406                                                 0x7fffffff, /* max count */
407                                                 NULL );     /* unnamed */
408
409         if( p_condvar->i_win9x_cv == 1 )
410             /* Create a manual-reset event initially signaled. */
411             p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
412         else
413             /* Create a auto-reset event. */
414             p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
415
416         InitializeCriticalSection( &p_condvar->csection );
417
418         return !p_condvar->semaphore || !p_condvar->event;
419     }
420
421 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
422     return pthread_cond_init( &p_condvar->cond, NULL );
423
424 #elif defined( HAVE_CTHREADS_H )
425     /* condition_init() */
426     spin_lock_init( &p_condvar->lock );
427     cthread_queue_init( &p_condvar->queue );
428     p_condvar->name = 0;
429     p_condvar->implications = 0;
430
431     return 0;
432
433 #elif defined( HAVE_KERNEL_SCHEDULER_H )
434     if( !p_condvar )
435     {
436         return B_BAD_VALUE;
437     }
438
439     if( p_condvar->init == 9999 )
440     {
441         return EALREADY;
442     }
443
444     p_condvar->thread = -1;
445     p_condvar->init = 9999;
446     return 0;
447 #endif
448 }
449
450 /*****************************************************************************
451  * vlc_cond_destroy: destroy a condition, inner version
452  *****************************************************************************/
453 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
454 {
455     int i_result;
456     /* In case of error : */
457     int i_thread = -1;
458     const char * psz_error = "";
459
460 #if defined( PTH_INIT_IN_PTH_H )
461     return 0;
462
463 #elif defined( ST_INIT_IN_ST_H )
464     i_result = st_cond_destroy( p_condvar->cond );
465
466 #elif defined( UNDER_CE )
467     i_result = !CloseHandle( p_condvar->event );
468
469 #elif defined( WIN32 )
470     if( !p_condvar->semaphore )
471         i_result = !CloseHandle( p_condvar->event );
472     else
473         i_result = !CloseHandle( p_condvar->event )
474           || !CloseHandle( p_condvar->semaphore );
475
476 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
477     i_result = pthread_cond_destroy( &p_condvar->cond );
478     if ( i_result )
479     {
480         i_thread = (int)pthread_self();
481         psz_error = strerror(i_result);
482     }
483
484 #elif defined( HAVE_CTHREADS_H )
485     return 0;
486
487 #elif defined( HAVE_KERNEL_SCHEDULER_H )
488     p_condvar->init = 0;
489     return 0;
490 #endif
491
492     if( i_result )
493     {
494         msg_Err( p_condvar->p_this,
495                  "thread %d: cond_destroy failed at %s:%d (%d:%s)",
496                  i_thread, psz_file, i_line, i_result, psz_error );
497     }
498     return i_result;
499 }
500
501 /*****************************************************************************
502  * vlc_thread_create: create a thread, inner version
503  *****************************************************************************
504  * Note that i_priority is only taken into account on platforms supporting
505  * userland real-time priority threads.
506  *****************************************************************************/
507 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
508                          char *psz_name, void * ( *func ) ( void * ),
509                          int i_priority, vlc_bool_t b_wait )
510 {
511     int i_ret;
512     void *p_data = (void *)p_this;
513
514     vlc_mutex_lock( &p_this->object_lock );
515
516 #if defined( PTH_INIT_IN_PTH_H )
517     p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
518     i_ret = p_this->thread_id == NULL;
519
520 #elif defined( ST_INIT_IN_ST_H )
521     p_this->thread_id = st_thread_create( func, p_data, 1, 0 );
522     i_ret = 0;
523
524 #elif defined( WIN32 ) || defined( UNDER_CE )
525     {
526         unsigned threadID;
527         /* When using the MSVCRT C library you have to use the _beginthreadex
528          * function instead of CreateThread, otherwise you'll end up with
529          * memory leaks and the signal functions not working (see Microsoft
530          * Knowledge Base, article 104641) */
531         p_this->thread_id =
532 #if defined( UNDER_CE )
533                 (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
534                                       p_data, 0, &threadID );
535 #else
536                 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
537                                         p_data, 0, &threadID );
538 #endif
539     }
540
541     if ( p_this->thread_id && i_priority )
542     {
543         if ( !SetThreadPriority(p_this->thread_id, i_priority) )
544         {
545             msg_Warn( p_this, "couldn't set a faster priority" );
546             i_priority = 0;
547         }
548     }
549
550     i_ret = ( p_this->thread_id ? 0 : 1 );
551
552 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
553     i_ret = pthread_create( &p_this->thread_id, NULL, func, p_data );
554
555 #ifdef SYS_DARWIN
556     if ( i_priority )
557     {
558         int i_error, i_policy;
559         struct sched_param param;
560         memset( &param, 0, sizeof(struct sched_param) );
561         if ( i_priority < 0 )
562         {
563             param.sched_priority = (-1) * i_priority;
564             i_policy = SCHED_OTHER;
565         }
566         else
567         {
568             param.sched_priority = i_priority;
569             i_policy = SCHED_RR;
570         }
571         if ( (i_error = pthread_setschedparam( p_this->thread_id,
572                                                i_policy, &param )) )
573         {
574             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
575                       psz_file, i_line, strerror(i_error) );
576             i_priority = 0;
577         }
578     }
579 #endif
580
581 #elif defined( HAVE_CTHREADS_H )
582     p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
583     i_ret = 0;
584
585 #elif defined( HAVE_KERNEL_SCHEDULER_H )
586     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
587                                       i_priority, p_data );
588     i_ret = resume_thread( p_this->thread_id );
589
590 #endif
591
592     if( i_ret == 0 )
593     {
594         if( b_wait )
595         {
596             msg_Dbg( p_this, "waiting for thread completion" );
597             vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
598         }
599
600         p_this->b_thread = 1;
601
602         msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
603                  (int)p_this->thread_id, psz_name, i_priority,
604                  psz_file, i_line );
605
606         vlc_mutex_unlock( &p_this->object_lock );
607     }
608     else
609     {
610 #ifdef HAVE_STRERROR
611         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
612                          psz_name, psz_file, i_line, strerror(i_ret) );
613 #else
614         msg_Err( p_this, "%s thread could not be created at %s:%d",
615                          psz_name, psz_file, i_line );
616 #endif
617         vlc_mutex_unlock( &p_this->object_lock );
618     }
619
620     return i_ret;
621 }
622
623 /*****************************************************************************
624  * vlc_thread_set_priority: set the priority of the current thread when we
625  * couldn't set it in vlc_thread_create (for instance for the main thread)
626  *****************************************************************************/
627 int __vlc_thread_set_priority( vlc_object_t *p_this, char * psz_file,
628                                int i_line, int i_priority )
629 {
630 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
631 #elif defined( WIN32 ) || defined( UNDER_CE )
632     if ( !SetThreadPriority(GetCurrentThread(), i_priority) )
633     {
634         msg_Warn( p_this, "couldn't set a faster priority" );
635         return 1;
636     }
637
638 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
639
640 #ifdef SYS_DARWIN
641     if ( i_priority )
642     {
643         int i_error, i_policy;
644         struct sched_param param;
645         memset( &param, 0, sizeof(struct sched_param) );
646         if ( i_priority < 0 )
647         {
648             param.sched_priority = (-1) * i_priority;
649             i_policy = SCHED_OTHER;
650         }
651         else
652         {
653             param.sched_priority = i_priority;
654             i_policy = SCHED_RR;
655         }
656         if ( (i_error = pthread_setschedparam( pthread_self(),
657                                                i_policy, &param )) )
658         {
659             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",     
660                       psz_file, i_line, strerror(i_error) );
661             i_priority = 0;
662         }
663     }
664 #endif
665
666 #endif
667
668     return 0;
669 }
670
671 /*****************************************************************************
672  * vlc_thread_ready: tell the parent thread we were successfully spawned
673  *****************************************************************************/
674 void __vlc_thread_ready( vlc_object_t *p_this )
675 {
676     vlc_mutex_lock( &p_this->object_lock );
677     vlc_cond_signal( &p_this->object_wait );
678     vlc_mutex_unlock( &p_this->object_lock );
679 }
680
681 /*****************************************************************************
682  * vlc_thread_join: wait until a thread exits, inner version
683  *****************************************************************************/
684 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
685 {
686     int i_ret = 0;
687
688 #if defined( PTH_INIT_IN_PTH_H )
689     i_ret = ( pth_join( p_this->thread_id, NULL ) == FALSE );
690
691 #elif defined( ST_INIT_IN_ST_H )
692     i_ret = st_thread_join( p_this->thread_id, NULL );
693
694 #elif defined( UNDER_CE )
695     WaitForSingleObject( p_this->thread_id, INFINITE );
696
697 #elif defined( WIN32 )
698     WaitForSingleObject( p_this->thread_id, INFINITE );
699
700 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
701     i_ret = pthread_join( p_this->thread_id, NULL );
702
703 #elif defined( HAVE_CTHREADS_H )
704     cthread_join( p_this->thread_id );
705     i_ret = 1;
706
707 #elif defined( HAVE_KERNEL_SCHEDULER_H )
708     int32_t exit_value;
709     wait_for_thread( p_this->thread_id, &exit_value );
710
711 #endif
712
713     if( i_ret )
714     {
715 #ifdef HAVE_STRERROR
716         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
717                          (int)p_this->thread_id, psz_file, i_line,
718                          strerror(i_ret) );
719 #else
720         msg_Err( p_this, "thread_join(%d) failed at %s:%d",
721                          (int)p_this->thread_id, psz_file, i_line );
722 #endif
723     }
724     else
725     {
726         msg_Dbg( p_this, "thread %d joined (%s:%d)",
727                          (int)p_this->thread_id, psz_file, i_line );
728     }
729
730     p_this->b_thread = 0;
731 }
732