1 /*****************************************************************************
2 * threads.c : threads implementation for the VideoLAN client
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
35 #define VLC_THREADS_UNINITIALIZED 0
36 #define VLC_THREADS_PENDING 1
37 #define VLC_THREADS_ERROR 2
38 #define VLC_THREADS_READY 3
40 /*****************************************************************************
41 * Global mutex for lazy initialization of the threads system
42 *****************************************************************************/
43 static volatile unsigned i_initializations = 0;
44 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
45 static vlc_object_t *p_root;
47 #if defined( PTH_INIT_IN_PTH_H )
48 #elif defined( ST_INIT_IN_ST_H )
49 #elif defined( UNDER_CE )
50 #elif defined( WIN32 )
52 /* following is only available on NT/2000/XP and above */
53 static SIGNALOBJECTANDWAIT pf_SignalObjectAndWait = NULL;
56 ** On Windows NT/2K/XP we use a slow mutex implementation but which
57 ** allows us to correctly implement condition variables.
58 ** You can also use the faster Win9x implementation but you might
59 ** experience problems with it.
61 static vlc_bool_t b_fast_mutex = VLC_FALSE;
63 ** On Windows 9x/Me you can use a fast but incorrect condition variables
64 ** implementation (more precisely there is a possibility for a race
65 ** condition to happen).
66 ** However it is possible to use slower alternatives which are more robust.
67 ** Currently you can choose between implementation 0 (which is the
68 ** fastest but slightly incorrect), 1 (default) and 2.
70 static int i_win9x_cv = 1;
72 #elif defined( HAVE_KERNEL_SCHEDULER_H )
73 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
74 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
75 #elif defined( HAVE_CTHREADS_H )
78 vlc_threadvar_t msg_context_global_key;
80 /*****************************************************************************
81 * vlc_threads_error: Report an error from the threading mecanism
82 *****************************************************************************
83 * This is especially useful to debug those errors, as this is a nice symbol
84 * on which you can break.
85 *****************************************************************************/
86 void vlc_threads_error( vlc_object_t *p_this )
88 msg_Err( p_this, "Error detected. Put a breakpoint in '%s' to debug.",
92 /*****************************************************************************
93 * vlc_threads_init: initialize threads system
94 *****************************************************************************
95 * This function requires lazy initialization of a global lock in order to
96 * keep the library really thread-safe. Some architectures don't support this
97 * and thus do not guarantee the complete reentrancy.
98 *****************************************************************************/
99 int __vlc_threads_init( vlc_object_t *p_this )
101 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_this;
102 int i_ret = VLC_SUCCESS;
104 /* If we have lazy mutex initialization, use it. Otherwise, we just
105 * hope nothing wrong happens. */
106 #if defined( PTH_INIT_IN_PTH_H )
107 #elif defined( ST_INIT_IN_ST_H )
108 #elif defined( UNDER_CE )
109 #elif defined( WIN32 )
110 if( IsDebuggerPresent() )
112 /* SignalObjectAndWait() is problematic under a debugger */
113 b_fast_mutex = VLC_TRUE;
116 #elif defined( HAVE_KERNEL_SCHEDULER_H )
117 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
118 pthread_mutex_lock( &once_mutex );
119 #elif defined( HAVE_CTHREADS_H )
122 if( i_status == VLC_THREADS_UNINITIALIZED )
124 i_status = VLC_THREADS_PENDING;
126 /* We should be safe now. Do all the initialization stuff we want. */
127 p_libvlc_global->b_ready = VLC_FALSE;
129 #if defined( PTH_INIT_IN_PTH_H )
130 i_ret = ( pth_init() == FALSE );
132 #elif defined( ST_INIT_IN_ST_H )
135 #elif defined( UNDER_CE )
136 /* Nothing to initialize */
138 #elif defined( WIN32 )
139 /* Dynamically get the address of SignalObjectAndWait */
140 if( GetVersion() < 0x80000000 )
144 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
145 hInstLib = LoadLibrary( "kernel32" );
148 pf_SignalObjectAndWait =
149 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
150 "SignalObjectAndWait" );
154 #elif defined( HAVE_KERNEL_SCHEDULER_H )
155 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
156 #elif defined( HAVE_CTHREADS_H )
159 p_root = vlc_object_create( p_libvlc_global, VLC_OBJECT_GLOBAL );
165 i_status = VLC_THREADS_ERROR;
170 i_status = VLC_THREADS_READY;
173 vlc_threadvar_create( p_root, &msg_context_global_key );
177 /* Just increment the initialization count */
181 /* If we have lazy mutex initialization support, unlock the mutex;
182 * otherwize, do a naive wait loop. */
183 #if defined( PTH_INIT_IN_PTH_H )
184 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
185 #elif defined( ST_INIT_IN_ST_H )
186 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
187 #elif defined( UNDER_CE )
188 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
189 #elif defined( WIN32 )
190 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
191 #elif defined( HAVE_KERNEL_SCHEDULER_H )
192 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
193 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
194 pthread_mutex_unlock( &once_mutex );
195 #elif defined( HAVE_CTHREADS_H )
196 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
199 if( i_status != VLC_THREADS_READY )
207 /*****************************************************************************
208 * vlc_threads_end: stop threads system
209 *****************************************************************************
210 * FIXME: This function is far from being threadsafe.
211 *****************************************************************************/
212 int __vlc_threads_end( vlc_object_t *p_this )
215 #if defined( PTH_INIT_IN_PTH_H )
216 #elif defined( ST_INIT_IN_ST_H )
217 #elif defined( UNDER_CE )
218 #elif defined( WIN32 )
219 #elif defined( HAVE_KERNEL_SCHEDULER_H )
220 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
221 pthread_mutex_lock( &once_mutex );
222 #elif defined( HAVE_CTHREADS_H )
225 if( i_initializations == 0 )
229 if( i_initializations == 0 )
231 i_status = VLC_THREADS_UNINITIALIZED;
232 vlc_object_destroy( p_root );
235 #if defined( PTH_INIT_IN_PTH_H )
236 if( i_initializations == 0 )
238 return ( pth_kill() == FALSE );
241 #elif defined( ST_INIT_IN_ST_H )
242 #elif defined( UNDER_CE )
243 #elif defined( WIN32 )
244 #elif defined( HAVE_KERNEL_SCHEDULER_H )
245 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
246 pthread_mutex_unlock( &once_mutex );
247 #elif defined( HAVE_CTHREADS_H )
252 /*****************************************************************************
253 * vlc_mutex_init: initialize a mutex
254 *****************************************************************************/
255 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
258 p_mutex->p_this = p_this;
260 #if defined( PTH_INIT_IN_PTH_H )
261 return ( pth_mutex_init( &p_mutex->mutex ) == FALSE );
263 #elif defined( ST_INIT_IN_ST_H )
264 p_mutex->mutex = st_mutex_new();
265 return ( p_mutex->mutex == NULL ) ? errno : 0;
267 #elif defined( UNDER_CE )
268 InitializeCriticalSection( &p_mutex->csection );
271 #elif defined( WIN32 )
272 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
273 * function and have a 100% correct vlc_cond_wait() implementation.
274 * As this function is not available on Win9x, we can use the faster
275 * CriticalSections */
276 if( pf_SignalObjectAndWait && !b_fast_mutex )
278 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
279 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
280 return ( p_mutex->mutex != NULL ? 0 : 1 );
284 p_mutex->mutex = NULL;
285 InitializeCriticalSection( &p_mutex->csection );
289 #elif defined( HAVE_KERNEL_SCHEDULER_H )
290 /* check the arguments and whether it's already been initialized */
291 if( p_mutex == NULL )
296 if( p_mutex->init == 9999 )
301 p_mutex->lock = create_sem( 1, "BeMutex" );
302 if( p_mutex->lock < B_NO_ERROR )
307 p_mutex->init = 9999;
310 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
313 /* Create error-checking mutex to detect problems more easily. */
314 pthread_mutexattr_t attr;
317 pthread_mutexattr_init( &attr );
318 # if defined(SYS_LINUX)
319 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
321 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
324 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
325 pthread_mutexattr_destroy( &attr );
329 return pthread_mutex_init( &p_mutex->mutex, NULL );
331 #elif defined( HAVE_CTHREADS_H )
332 mutex_init( p_mutex );
338 /*****************************************************************************
339 * vlc_mutex_init: initialize a recursive mutex (Do not use)
340 *****************************************************************************/
341 int __vlc_mutex_init_recursive( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
344 /* Create mutex returns a recursive mutex */
345 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
346 return ( p_mutex->mutex != NULL ? 0 : 1 );
347 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
348 pthread_mutexattr_t attr;
351 pthread_mutexattr_init( &attr );
353 /* Create error-checking mutex to detect problems more easily. */
354 # if defined(SYS_LINUX)
355 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
357 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
360 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
361 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
362 pthread_mutexattr_destroy( &attr );
365 msg_Err(p_this, "no recursive mutex found. Falling back to regular mutex.\n"
367 return __vlc_mutex_init( p_this, p_mutex );
372 /*****************************************************************************
373 * vlc_mutex_destroy: destroy a mutex, inner version
374 *****************************************************************************/
375 int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
378 /* In case of error : */
381 #if defined( PTH_INIT_IN_PTH_H )
384 #elif defined( ST_INIT_IN_ST_H )
385 i_result = st_mutex_destroy( p_mutex->mutex );
387 #elif defined( UNDER_CE )
388 DeleteCriticalSection( &p_mutex->csection );
391 #elif defined( WIN32 )
394 CloseHandle( p_mutex->mutex );
398 DeleteCriticalSection( &p_mutex->csection );
402 #elif defined( HAVE_KERNEL_SCHEDULER_H )
403 if( p_mutex->init == 9999 )
405 delete_sem( p_mutex->lock );
411 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
412 i_result = pthread_mutex_destroy( &p_mutex->mutex );
415 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
419 #elif defined( HAVE_CTHREADS_H )
426 msg_Err( p_mutex->p_this,
427 "thread %d: mutex_destroy failed at %s:%d (%d:%m)",
428 i_thread, psz_file, i_line, i_result );
429 vlc_threads_error( p_mutex->p_this );
434 /*****************************************************************************
435 * vlc_cond_init: initialize a condition
436 *****************************************************************************/
437 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
439 p_condvar->p_this = p_this;
441 #if defined( PTH_INIT_IN_PTH_H )
442 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
444 #elif defined( ST_INIT_IN_ST_H )
445 p_condvar->cond = st_cond_new();
446 return ( p_condvar->cond == NULL ) ? errno : 0;
448 #elif defined( UNDER_CE )
449 /* Initialize counter */
450 p_condvar->i_waiting_threads = 0;
452 /* Create an auto-reset event. */
453 p_condvar->event = CreateEvent( NULL, /* no security */
454 FALSE, /* auto-reset event */
455 FALSE, /* start non-signaled */
456 NULL ); /* unnamed */
457 return !p_condvar->event;
459 #elif defined( WIN32 )
460 /* Initialize counter */
461 p_condvar->i_waiting_threads = 0;
464 p_condvar->i_win9x_cv = i_win9x_cv;
465 p_condvar->SignalObjectAndWait = pf_SignalObjectAndWait;
467 if( (p_condvar->SignalObjectAndWait && !b_fast_mutex)
468 || p_condvar->i_win9x_cv == 0 )
470 /* Create an auto-reset event. */
471 p_condvar->event = CreateEvent( NULL, /* no security */
472 FALSE, /* auto-reset event */
473 FALSE, /* start non-signaled */
474 NULL ); /* unnamed */
476 p_condvar->semaphore = NULL;
477 return !p_condvar->event;
481 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
482 0, /* initial count */
483 0x7fffffff, /* max count */
484 NULL ); /* unnamed */
486 if( p_condvar->i_win9x_cv == 1 )
487 /* Create a manual-reset event initially signaled. */
488 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
490 /* Create a auto-reset event. */
491 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
493 InitializeCriticalSection( &p_condvar->csection );
495 return !p_condvar->semaphore || !p_condvar->event;
498 #elif defined( HAVE_KERNEL_SCHEDULER_H )
504 if( p_condvar->init == 9999 )
509 p_condvar->thread = -1;
510 p_condvar->init = 9999;
513 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
514 pthread_condattr_t attr;
517 ret = pthread_condattr_init (&attr);
521 # if !defined (_POSIX_CLOCK_SELECTION)
522 /* Fairly outdated POSIX support (that was defined in 2001) */
523 # define _POSIX_CLOCK_SELECTION (-1)
525 # if (_POSIX_CLOCK_SELECTION >= 0)
526 /* NOTE: This must be the same clock as the one in mtime.c */
527 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
530 ret = pthread_cond_init (&p_condvar->cond, &attr);
531 pthread_condattr_destroy (&attr);
534 #elif defined( HAVE_CTHREADS_H )
535 /* condition_init() */
536 spin_lock_init( &p_condvar->lock );
537 cthread_queue_init( &p_condvar->queue );
539 p_condvar->implications = 0;
546 /*****************************************************************************
547 * vlc_cond_destroy: destroy a condition, inner version
548 *****************************************************************************/
549 int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
552 /* In case of error : */
555 #if defined( PTH_INIT_IN_PTH_H )
558 #elif defined( ST_INIT_IN_ST_H )
559 i_result = st_cond_destroy( p_condvar->cond );
561 #elif defined( UNDER_CE )
562 i_result = !CloseHandle( p_condvar->event );
564 #elif defined( WIN32 )
565 if( !p_condvar->semaphore )
566 i_result = !CloseHandle( p_condvar->event );
568 i_result = !CloseHandle( p_condvar->event )
569 || !CloseHandle( p_condvar->semaphore );
571 if( p_condvar->semaphore != NULL )
572 DeleteCriticalSection( &p_condvar->csection );
574 #elif defined( HAVE_KERNEL_SCHEDULER_H )
578 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
579 i_result = pthread_cond_destroy( &p_condvar->cond );
582 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
586 #elif defined( HAVE_CTHREADS_H )
593 msg_Err( p_condvar->p_this,
594 "thread %d: cond_destroy failed at %s:%d (%d:%m)",
595 i_thread, psz_file, i_line, i_result );
596 vlc_threads_error( p_condvar->p_this );
601 /*****************************************************************************
602 * vlc_tls_create: create a thread-local variable
603 *****************************************************************************/
604 int __vlc_threadvar_create( vlc_object_t *p_this, vlc_threadvar_t *p_tls )
608 #if defined( PTH_INIT_IN_PTH_H )
609 i_ret = pth_key_create( &p_tls->handle, NULL );
610 #elif defined( HAVE_KERNEL_SCHEDULER_H )
611 msg_Err( p_this, "TLS not implemented" );
613 #elif defined( ST_INIT_IN_ST_H )
614 i_ret = st_key_create( &p_tls->handle, NULL );
615 #elif defined( UNDER_CE ) || defined( WIN32 )
616 #elif defined( WIN32 )
617 p_tls->handle = TlsAlloc();
618 i_ret = !( p_tls->handle == 0xFFFFFFFF );
620 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
621 i_ret = pthread_key_create( &p_tls->handle, NULL );
622 #elif defined( HAVE_CTHREADS_H )
623 i_ret = cthread_keycreate( &p_tls-handle );
628 /*****************************************************************************
629 * vlc_thread_create: create a thread, inner version
630 *****************************************************************************
631 * Note that i_priority is only taken into account on platforms supporting
632 * userland real-time priority threads.
633 *****************************************************************************/
634 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
635 const char *psz_name, void * ( *func ) ( void * ),
636 int i_priority, vlc_bool_t b_wait )
639 void *p_data = (void *)p_this;
640 vlc_object_internals_t *p_priv = p_this->p_internals;
642 vlc_mutex_lock( &p_this->object_lock );
644 #if defined( PTH_INIT_IN_PTH_H )
645 p_priv->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
646 i_ret = p_priv->thread_id == NULL;
648 #elif defined( ST_INIT_IN_ST_H )
649 p_priv->thread_id = st_thread_create( func, p_data, 1, 0 );
652 #elif defined( WIN32 ) || defined( UNDER_CE )
654 /* When using the MSVCRT C library you have to use the _beginthreadex
655 * function instead of CreateThread, otherwise you'll end up with
656 * memory leaks and the signal functions not working (see Microsoft
657 * Knowledge Base, article 104641) */
658 #if defined( UNDER_CE )
660 HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
661 (LPVOID)p_data, CREATE_SUSPENDED,
665 uintptr_t hThread = _beginthreadex( NULL, 0,
666 (LPTHREAD_START_ROUTINE)func,
667 (void*)p_data, CREATE_SUSPENDED,
670 p_priv->thread_id.id = (DWORD)threadId;
671 p_priv->thread_id.hThread = (HANDLE)hThread;
672 ResumeThread((HANDLE)hThread);
675 i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
677 if( i_ret && i_priority )
679 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
681 msg_Warn( p_this, "couldn't set a faster priority" );
686 #elif defined( HAVE_KERNEL_SCHEDULER_H )
687 p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
688 i_priority, p_data );
689 i_ret = resume_thread( p_priv->thread_id );
691 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
692 i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
695 if( config_GetInt( p_this, "rt-priority" ) )
698 int i_error, i_policy;
699 struct sched_param param;
701 memset( ¶m, 0, sizeof(struct sched_param) );
702 if( config_GetType( p_this, "rt-offset" ) )
704 i_priority += config_GetInt( p_this, "rt-offset" );
706 if( i_priority <= 0 )
708 param.sched_priority = (-1) * i_priority;
709 i_policy = SCHED_OTHER;
713 param.sched_priority = i_priority;
716 if( (i_error = pthread_setschedparam( p_priv->thread_id,
717 i_policy, ¶m )) )
720 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
732 #elif defined( HAVE_CTHREADS_H )
733 p_priv->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
742 msg_Dbg( p_this, "waiting for thread completion" );
743 vlc_object_wait( p_this );
746 p_priv->b_thread = VLC_TRUE;
748 #if defined( WIN32 ) || defined( UNDER_CE )
749 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
750 (unsigned int)p_priv->thread_id.id, psz_name,
751 i_priority, psz_file, i_line );
753 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
754 (unsigned int)p_priv->thread_id, psz_name, i_priority,
759 vlc_mutex_unlock( &p_this->object_lock );
764 msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
765 psz_name, psz_file, i_line );
766 vlc_threads_error( p_this );
767 vlc_mutex_unlock( &p_this->object_lock );
773 /*****************************************************************************
774 * vlc_thread_set_priority: set the priority of the current thread when we
775 * couldn't set it in vlc_thread_create (for instance for the main thread)
776 *****************************************************************************/
777 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
778 int i_line, int i_priority )
780 vlc_object_internals_t *p_priv = p_this->p_internals;
781 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
782 #elif defined( WIN32 ) || defined( UNDER_CE )
783 if( !p_priv->thread_id.hThread )
784 p_priv->thread_id.hThread = GetCurrentThread();
785 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
787 msg_Warn( p_this, "couldn't set a faster priority" );
791 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
793 if( config_GetInt( p_this, "rt-priority" ) > 0 )
796 int i_error, i_policy;
797 struct sched_param param;
799 memset( ¶m, 0, sizeof(struct sched_param) );
800 if( config_GetType( p_this, "rt-offset" ) )
802 i_priority += config_GetInt( p_this, "rt-offset" );
804 if( i_priority <= 0 )
806 param.sched_priority = (-1) * i_priority;
807 i_policy = SCHED_OTHER;
811 param.sched_priority = i_priority;
814 if( !p_priv->thread_id )
815 p_priv->thread_id = pthread_self();
816 if( (i_error = pthread_setschedparam( p_priv->thread_id,
817 i_policy, ¶m )) )
820 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
830 /*****************************************************************************
831 * vlc_thread_ready: tell the parent thread we were successfully spawned
832 *****************************************************************************/
833 void __vlc_thread_ready( vlc_object_t *p_this )
835 vlc_object_signal( p_this );
838 /*****************************************************************************
839 * vlc_thread_join: wait until a thread exits, inner version
840 *****************************************************************************/
841 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
843 vlc_object_internals_t *p_priv = p_this->p_internals;
845 #if defined( UNDER_CE ) || defined( WIN32 )
847 BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
848 FILETIME*, FILETIME* );
849 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
850 int64_t real_time, kernel_time, user_time;
854 ** object will close its thread handle when destroyed, duplicate it here
855 ** to be on the safe side
857 if( ! DuplicateHandle(GetCurrentProcess(),
858 p_priv->thread_id.hThread,
863 DUPLICATE_SAME_ACCESS) )
865 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
866 (unsigned int)p_priv->thread_id.id,
867 psz_file, i_line, GetLastError() );
868 vlc_threads_error( p_this );
869 p_priv->b_thread = VLC_FALSE;
873 WaitForSingleObject( hThread, INFINITE );
875 msg_Dbg( p_this, "thread %u joined (%s:%d)",
876 (unsigned int)p_priv->thread_id.id,
878 #if defined( UNDER_CE )
879 hmodule = GetModuleHandle( _T("COREDLL") );
881 hmodule = GetModuleHandle( _T("KERNEL32") );
883 OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
884 FILETIME*, FILETIME* ))
885 GetProcAddress( hmodule, _T("GetThreadTimes") );
887 if( OurGetThreadTimes &&
888 OurGetThreadTimes( hThread,
889 &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
892 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
893 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
897 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
898 kernel_ft.dwLowDateTime) / 10;
901 ((((int64_t)user_ft.dwHighDateTime)<<32)|
902 user_ft.dwLowDateTime) / 10;
904 msg_Dbg( p_this, "thread times: "
905 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
906 real_time/60/1000000,
907 (double)((real_time%(60*1000000))/1000000.0),
908 kernel_time/60/1000000,
909 (double)((kernel_time%(60*1000000))/1000000.0),
910 user_time/60/1000000,
911 (double)((user_time%(60*1000000))/1000000.0) );
913 CloseHandle( hThread );
915 #else /* !defined(WIN32) */
919 #if defined( PTH_INIT_IN_PTH_H )
920 i_ret = ( pth_join( p_priv->thread_id, NULL ) == FALSE );
922 #elif defined( ST_INIT_IN_ST_H )
923 i_ret = st_thread_join( p_priv->thread_id, NULL );
925 #elif defined( HAVE_KERNEL_SCHEDULER_H )
927 i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
929 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
930 i_ret = pthread_join( p_priv->thread_id, NULL );
932 #elif defined( HAVE_CTHREADS_H )
933 cthread_join( p_priv->thread_id );
941 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%m)",
942 (unsigned int)p_priv->thread_id, psz_file, i_line );
943 vlc_threads_error( p_this );
947 msg_Dbg( p_this, "thread %u joined (%s:%d)",
948 (unsigned int)p_priv->thread_id, psz_file, i_line );
953 p_priv->b_thread = VLC_FALSE;