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