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