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 *****************************************************************************/
87 __vlc_threads_error( vlc_object_t *p_this )
89 msg_Err( p_this, "Error detected. Put a breakpoint in '%s' to debug.",
93 /*****************************************************************************
94 * vlc_threads_init: initialize threads system
95 *****************************************************************************
96 * This function requires lazy initialization of a global lock in order to
97 * keep the library really thread-safe. Some architectures don't support this
98 * and thus do not guarantee the complete reentrancy.
99 *****************************************************************************/
100 int __vlc_threads_init( vlc_object_t *p_this )
102 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_this;
103 int i_ret = VLC_SUCCESS;
105 /* If we have lazy mutex initialization, use it. Otherwise, we just
106 * hope nothing wrong happens. */
107 #if defined( PTH_INIT_IN_PTH_H )
108 #elif defined( ST_INIT_IN_ST_H )
109 #elif defined( UNDER_CE )
110 #elif defined( WIN32 )
111 if( IsDebuggerPresent() )
113 /* SignalObjectAndWait() is problematic under a debugger */
114 b_fast_mutex = VLC_TRUE;
117 #elif defined( HAVE_KERNEL_SCHEDULER_H )
118 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
119 pthread_mutex_lock( &once_mutex );
120 #elif defined( HAVE_CTHREADS_H )
123 if( i_status == VLC_THREADS_UNINITIALIZED )
125 i_status = VLC_THREADS_PENDING;
127 /* We should be safe now. Do all the initialization stuff we want. */
128 p_libvlc_global->b_ready = VLC_FALSE;
130 #if defined( PTH_INIT_IN_PTH_H )
131 i_ret = ( pth_init() == FALSE );
133 #elif defined( ST_INIT_IN_ST_H )
136 #elif defined( UNDER_CE )
137 /* Nothing to initialize */
139 #elif defined( WIN32 )
140 /* Dynamically get the address of SignalObjectAndWait */
141 if( GetVersion() < 0x80000000 )
145 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
146 hInstLib = LoadLibrary( "kernel32" );
149 pf_SignalObjectAndWait =
150 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
151 "SignalObjectAndWait" );
155 #elif defined( HAVE_KERNEL_SCHEDULER_H )
156 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
157 #elif defined( HAVE_CTHREADS_H )
160 p_root = vlc_object_create( p_libvlc_global, VLC_OBJECT_GLOBAL );
166 i_status = VLC_THREADS_ERROR;
171 i_status = VLC_THREADS_READY;
174 vlc_threadvar_create( p_root, &msg_context_global_key );
178 /* Just increment the initialization count */
182 /* If we have lazy mutex initialization support, unlock the mutex;
183 * otherwize, do a naive wait loop. */
184 #if defined( PTH_INIT_IN_PTH_H )
185 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
186 #elif defined( ST_INIT_IN_ST_H )
187 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
188 #elif defined( UNDER_CE )
189 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
190 #elif defined( WIN32 )
191 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
192 #elif defined( HAVE_KERNEL_SCHEDULER_H )
193 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
194 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
195 pthread_mutex_unlock( &once_mutex );
196 #elif defined( HAVE_CTHREADS_H )
197 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
200 if( i_status != VLC_THREADS_READY )
208 /*****************************************************************************
209 * vlc_threads_end: stop threads system
210 *****************************************************************************
211 * FIXME: This function is far from being threadsafe.
212 *****************************************************************************/
213 int __vlc_threads_end( vlc_object_t *p_this )
216 #if defined( PTH_INIT_IN_PTH_H )
217 #elif defined( ST_INIT_IN_ST_H )
218 #elif defined( UNDER_CE )
219 #elif defined( WIN32 )
220 #elif defined( HAVE_KERNEL_SCHEDULER_H )
221 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
222 pthread_mutex_lock( &once_mutex );
223 #elif defined( HAVE_CTHREADS_H )
226 if( i_initializations == 0 )
230 if( i_initializations == 0 )
232 i_status = VLC_THREADS_UNINITIALIZED;
233 vlc_object_destroy( p_root );
236 #if defined( PTH_INIT_IN_PTH_H )
237 if( i_initializations == 0 )
239 return ( pth_kill() == FALSE );
242 #elif defined( ST_INIT_IN_ST_H )
243 #elif defined( UNDER_CE )
244 #elif defined( WIN32 )
245 #elif defined( HAVE_KERNEL_SCHEDULER_H )
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247 pthread_mutex_unlock( &once_mutex );
248 #elif defined( HAVE_CTHREADS_H )
253 /*****************************************************************************
254 * vlc_mutex_init: initialize a mutex
255 *****************************************************************************/
256 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
259 p_mutex->p_this = p_this;
261 #if defined( PTH_INIT_IN_PTH_H )
262 return ( pth_mutex_init( &p_mutex->mutex ) == FALSE );
264 #elif defined( ST_INIT_IN_ST_H )
265 p_mutex->mutex = st_mutex_new();
266 return ( p_mutex->mutex == NULL ) ? errno : 0;
268 #elif defined( UNDER_CE )
269 InitializeCriticalSection( &p_mutex->csection );
272 #elif defined( WIN32 )
273 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
274 * function and have a 100% correct vlc_cond_wait() implementation.
275 * As this function is not available on Win9x, we can use the faster
276 * CriticalSections */
277 if( pf_SignalObjectAndWait && !b_fast_mutex )
279 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
280 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
281 return ( p_mutex->mutex != NULL ? 0 : 1 );
285 p_mutex->mutex = NULL;
286 InitializeCriticalSection( &p_mutex->csection );
290 #elif defined( HAVE_KERNEL_SCHEDULER_H )
291 /* check the arguments and whether it's already been initialized */
292 if( p_mutex == NULL )
297 if( p_mutex->init == 9999 )
302 p_mutex->lock = create_sem( 1, "BeMutex" );
303 if( p_mutex->lock < B_NO_ERROR )
308 p_mutex->init = 9999;
311 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
314 /* Create error-checking mutex to detect problems more easily. */
315 pthread_mutexattr_t attr;
318 pthread_mutexattr_init( &attr );
319 # if defined(SYS_LINUX)
320 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
322 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
325 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
326 pthread_mutexattr_destroy( &attr );
330 return pthread_mutex_init( &p_mutex->mutex, NULL );
332 #elif defined( HAVE_CTHREADS_H )
333 mutex_init( p_mutex );
339 /*****************************************************************************
340 * vlc_mutex_init: initialize a recursive mutex (Do not use)
341 *****************************************************************************/
342 int __vlc_mutex_init_recursive( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
345 /* Create mutex returns a recursive mutex */
346 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
347 return ( p_mutex->mutex != NULL ? 0 : 1 );
348 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
349 pthread_mutexattr_t attr;
352 pthread_mutexattr_init( &attr );
354 /* Create error-checking mutex to detect problems more easily. */
355 # if defined(SYS_LINUX)
356 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
358 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
361 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
362 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
363 pthread_mutexattr_destroy( &attr );
366 msg_Err(p_this, "no recursive mutex found. Falling back to regular mutex.\n"
368 return __vlc_mutex_init( p_this, p_mutex );
373 /*****************************************************************************
374 * vlc_mutex_destroy: destroy a mutex, inner version
375 *****************************************************************************/
376 int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
379 /* In case of error : */
382 #if defined( PTH_INIT_IN_PTH_H )
385 #elif defined( ST_INIT_IN_ST_H )
386 i_result = st_mutex_destroy( p_mutex->mutex );
388 #elif defined( UNDER_CE )
389 DeleteCriticalSection( &p_mutex->csection );
392 #elif defined( WIN32 )
395 CloseHandle( p_mutex->mutex );
399 DeleteCriticalSection( &p_mutex->csection );
403 #elif defined( HAVE_KERNEL_SCHEDULER_H )
404 if( p_mutex->init == 9999 )
406 delete_sem( p_mutex->lock );
412 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
413 i_result = pthread_mutex_destroy( &p_mutex->mutex );
416 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
420 #elif defined( HAVE_CTHREADS_H )
427 msg_Err( p_mutex->p_this,
428 "thread %d: mutex_destroy failed at %s:%d (%d:%m)",
429 i_thread, psz_file, i_line, i_result );
430 vlc_threads_error( p_mutex->p_this );
435 /*****************************************************************************
436 * vlc_cond_init: initialize a condition
437 *****************************************************************************/
438 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
440 p_condvar->p_this = p_this;
442 #if defined( PTH_INIT_IN_PTH_H )
443 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
445 #elif defined( ST_INIT_IN_ST_H )
446 p_condvar->cond = st_cond_new();
447 return ( p_condvar->cond == NULL ) ? errno : 0;
449 #elif defined( UNDER_CE )
450 /* Initialize counter */
451 p_condvar->i_waiting_threads = 0;
453 /* Create an auto-reset event. */
454 p_condvar->event = CreateEvent( NULL, /* no security */
455 FALSE, /* auto-reset event */
456 FALSE, /* start non-signaled */
457 NULL ); /* unnamed */
458 return !p_condvar->event;
460 #elif defined( WIN32 )
461 /* Initialize counter */
462 p_condvar->i_waiting_threads = 0;
465 p_condvar->i_win9x_cv = i_win9x_cv;
466 p_condvar->SignalObjectAndWait = pf_SignalObjectAndWait;
468 if( (p_condvar->SignalObjectAndWait && !b_fast_mutex)
469 || p_condvar->i_win9x_cv == 0 )
471 /* Create an auto-reset event. */
472 p_condvar->event = CreateEvent( NULL, /* no security */
473 FALSE, /* auto-reset event */
474 FALSE, /* start non-signaled */
475 NULL ); /* unnamed */
477 p_condvar->semaphore = NULL;
478 return !p_condvar->event;
482 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
483 0, /* initial count */
484 0x7fffffff, /* max count */
485 NULL ); /* unnamed */
487 if( p_condvar->i_win9x_cv == 1 )
488 /* Create a manual-reset event initially signaled. */
489 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
491 /* Create a auto-reset event. */
492 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
494 InitializeCriticalSection( &p_condvar->csection );
496 return !p_condvar->semaphore || !p_condvar->event;
499 #elif defined( HAVE_KERNEL_SCHEDULER_H )
505 if( p_condvar->init == 9999 )
510 p_condvar->thread = -1;
511 p_condvar->init = 9999;
514 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
515 pthread_condattr_t attr;
518 ret = pthread_condattr_init (&attr);
522 # if !defined (_POSIX_CLOCK_SELECTION)
523 /* Fairly outdated POSIX support (that was defined in 2001) */
524 # define _POSIX_CLOCK_SELECTION (-1)
526 # if (_POSIX_CLOCK_SELECTION >= 0)
527 /* NOTE: This must be the same clock as the one in mtime.c */
528 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
531 ret = pthread_cond_init (&p_condvar->cond, &attr);
532 pthread_condattr_destroy (&attr);
535 #elif defined( HAVE_CTHREADS_H )
536 /* condition_init() */
537 spin_lock_init( &p_condvar->lock );
538 cthread_queue_init( &p_condvar->queue );
540 p_condvar->implications = 0;
547 /*****************************************************************************
548 * vlc_cond_destroy: destroy a condition, inner version
549 *****************************************************************************/
550 int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
553 /* In case of error : */
556 #if defined( PTH_INIT_IN_PTH_H )
559 #elif defined( ST_INIT_IN_ST_H )
560 i_result = st_cond_destroy( p_condvar->cond );
562 #elif defined( UNDER_CE )
563 i_result = !CloseHandle( p_condvar->event );
565 #elif defined( WIN32 )
566 if( !p_condvar->semaphore )
567 i_result = !CloseHandle( p_condvar->event );
569 i_result = !CloseHandle( p_condvar->event )
570 || !CloseHandle( p_condvar->semaphore );
572 if( p_condvar->semaphore != NULL )
573 DeleteCriticalSection( &p_condvar->csection );
575 #elif defined( HAVE_KERNEL_SCHEDULER_H )
579 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
580 i_result = pthread_cond_destroy( &p_condvar->cond );
583 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
587 #elif defined( HAVE_CTHREADS_H )
594 msg_Err( p_condvar->p_this,
595 "thread %d: cond_destroy failed at %s:%d (%d:%m)",
596 i_thread, psz_file, i_line, i_result );
597 vlc_threads_error( p_condvar->p_this );
602 /*****************************************************************************
603 * vlc_tls_create: create a thread-local variable
604 *****************************************************************************/
605 int __vlc_threadvar_create( vlc_object_t *p_this, vlc_threadvar_t *p_tls )
609 #if defined( PTH_INIT_IN_PTH_H )
610 i_ret = pth_key_create( &p_tls->handle, NULL );
611 #elif defined( HAVE_KERNEL_SCHEDULER_H )
612 msg_Err( p_this, "TLS not implemented" );
614 #elif defined( ST_INIT_IN_ST_H )
615 i_ret = st_key_create( &p_tls->handle, NULL );
616 #elif defined( UNDER_CE ) || defined( WIN32 )
617 #elif defined( WIN32 )
618 p_tls->handle = TlsAlloc();
619 i_ret = !( p_tls->handle == 0xFFFFFFFF );
621 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
622 i_ret = pthread_key_create( &p_tls->handle, NULL );
623 #elif defined( HAVE_CTHREADS_H )
624 i_ret = cthread_keycreate( &p_tls-handle );
629 /*****************************************************************************
630 * vlc_thread_create: create a thread, inner version
631 *****************************************************************************
632 * Note that i_priority is only taken into account on platforms supporting
633 * userland real-time priority threads.
634 *****************************************************************************/
635 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
636 const char *psz_name, void * ( *func ) ( void * ),
637 int i_priority, vlc_bool_t b_wait )
640 void *p_data = (void *)p_this;
641 vlc_object_internals_t *p_priv = p_this->p_internals;
643 vlc_mutex_lock( &p_this->object_lock );
645 #if defined( PTH_INIT_IN_PTH_H )
646 p_priv->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
647 i_ret = p_priv->thread_id == NULL;
649 #elif defined( ST_INIT_IN_ST_H )
650 p_priv->thread_id = st_thread_create( func, p_data, 1, 0 );
653 #elif defined( WIN32 ) || defined( UNDER_CE )
655 /* When using the MSVCRT C library you have to use the _beginthreadex
656 * function instead of CreateThread, otherwise you'll end up with
657 * memory leaks and the signal functions not working (see Microsoft
658 * Knowledge Base, article 104641) */
659 #if defined( UNDER_CE )
661 HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
662 (LPVOID)p_data, CREATE_SUSPENDED,
666 uintptr_t hThread = _beginthreadex( NULL, 0,
667 (LPTHREAD_START_ROUTINE)func,
668 (void*)p_data, CREATE_SUSPENDED,
671 p_priv->thread_id.id = (DWORD)threadId;
672 p_priv->thread_id.hThread = (HANDLE)hThread;
673 ResumeThread((HANDLE)hThread);
676 i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
678 if( i_ret && i_priority )
680 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
682 msg_Warn( p_this, "couldn't set a faster priority" );
687 #elif defined( HAVE_KERNEL_SCHEDULER_H )
688 p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
689 i_priority, p_data );
690 i_ret = resume_thread( p_priv->thread_id );
692 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
693 i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
696 if( config_GetInt( p_this, "rt-priority" ) )
699 int i_error, i_policy;
700 struct sched_param param;
702 memset( ¶m, 0, sizeof(struct sched_param) );
703 if( config_GetType( p_this, "rt-offset" ) )
705 i_priority += config_GetInt( p_this, "rt-offset" );
707 if( i_priority <= 0 )
709 param.sched_priority = (-1) * i_priority;
710 i_policy = SCHED_OTHER;
714 param.sched_priority = i_priority;
717 if( (i_error = pthread_setschedparam( p_priv->thread_id,
718 i_policy, ¶m )) )
721 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
733 #elif defined( HAVE_CTHREADS_H )
734 p_priv->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
743 msg_Dbg( p_this, "waiting for thread completion" );
744 vlc_object_wait( p_this );
747 p_priv->b_thread = VLC_TRUE;
749 #if defined( WIN32 ) || defined( UNDER_CE )
750 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
751 (unsigned int)p_priv->thread_id.id, psz_name,
752 i_priority, psz_file, i_line );
754 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
755 (unsigned int)p_priv->thread_id, psz_name, i_priority,
760 vlc_mutex_unlock( &p_this->object_lock );
765 msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
766 psz_name, psz_file, i_line );
767 vlc_threads_error( p_this );
768 vlc_mutex_unlock( &p_this->object_lock );
774 /*****************************************************************************
775 * vlc_thread_set_priority: set the priority of the current thread when we
776 * couldn't set it in vlc_thread_create (for instance for the main thread)
777 *****************************************************************************/
778 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
779 int i_line, int i_priority )
781 vlc_object_internals_t *p_priv = p_this->p_internals;
782 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
783 #elif defined( WIN32 ) || defined( UNDER_CE )
784 if( !p_priv->thread_id.hThread )
785 p_priv->thread_id.hThread = GetCurrentThread();
786 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
788 msg_Warn( p_this, "couldn't set a faster priority" );
792 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
794 if( config_GetInt( p_this, "rt-priority" ) > 0 )
797 int i_error, i_policy;
798 struct sched_param param;
800 memset( ¶m, 0, sizeof(struct sched_param) );
801 if( config_GetType( p_this, "rt-offset" ) )
803 i_priority += config_GetInt( p_this, "rt-offset" );
805 if( i_priority <= 0 )
807 param.sched_priority = (-1) * i_priority;
808 i_policy = SCHED_OTHER;
812 param.sched_priority = i_priority;
815 if( !p_priv->thread_id )
816 p_priv->thread_id = pthread_self();
817 if( (i_error = pthread_setschedparam( p_priv->thread_id,
818 i_policy, ¶m )) )
821 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
831 /*****************************************************************************
832 * vlc_thread_ready: tell the parent thread we were successfully spawned
833 *****************************************************************************/
834 void __vlc_thread_ready( vlc_object_t *p_this )
836 vlc_object_signal( p_this );
839 /*****************************************************************************
840 * vlc_thread_join: wait until a thread exits, inner version
841 *****************************************************************************/
842 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
844 vlc_object_internals_t *p_priv = p_this->p_internals;
846 #if defined( UNDER_CE ) || defined( WIN32 )
848 BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
849 FILETIME*, FILETIME* );
850 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
851 int64_t real_time, kernel_time, user_time;
855 ** object will close its thread handle when destroyed, duplicate it here
856 ** to be on the safe side
858 if( ! DuplicateHandle(GetCurrentProcess(),
859 p_priv->thread_id.hThread,
864 DUPLICATE_SAME_ACCESS) )
866 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
867 (unsigned int)p_priv->thread_id.id,
868 psz_file, i_line, GetLastError() );
869 vlc_threads_error( p_this );
870 p_priv->b_thread = VLC_FALSE;
874 WaitForSingleObject( hThread, INFINITE );
876 msg_Dbg( p_this, "thread %u joined (%s:%d)",
877 (unsigned int)p_priv->thread_id.id,
879 #if defined( UNDER_CE )
880 hmodule = GetModuleHandle( _T("COREDLL") );
882 hmodule = GetModuleHandle( _T("KERNEL32") );
884 OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
885 FILETIME*, FILETIME* ))
886 GetProcAddress( hmodule, _T("GetThreadTimes") );
888 if( OurGetThreadTimes &&
889 OurGetThreadTimes( hThread,
890 &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
893 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
894 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
898 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
899 kernel_ft.dwLowDateTime) / 10;
902 ((((int64_t)user_ft.dwHighDateTime)<<32)|
903 user_ft.dwLowDateTime) / 10;
905 msg_Dbg( p_this, "thread times: "
906 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
907 real_time/60/1000000,
908 (double)((real_time%(60*1000000))/1000000.0),
909 kernel_time/60/1000000,
910 (double)((kernel_time%(60*1000000))/1000000.0),
911 user_time/60/1000000,
912 (double)((user_time%(60*1000000))/1000000.0) );
914 CloseHandle( hThread );
916 #else /* !defined(WIN32) */
920 #if defined( PTH_INIT_IN_PTH_H )
921 i_ret = ( pth_join( p_priv->thread_id, NULL ) == FALSE );
923 #elif defined( ST_INIT_IN_ST_H )
924 i_ret = st_thread_join( p_priv->thread_id, NULL );
926 #elif defined( HAVE_KERNEL_SCHEDULER_H )
928 i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
930 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
931 i_ret = pthread_join( p_priv->thread_id, NULL );
933 #elif defined( HAVE_CTHREADS_H )
934 cthread_join( p_priv->thread_id );
942 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%m)",
943 (unsigned int)p_priv->thread_id, psz_file, i_line );
944 vlc_threads_error( p_this );
948 msg_Dbg( p_this, "thread %u joined (%s:%d)",
949 (unsigned int)p_priv->thread_id, psz_file, i_line );
954 p_priv->b_thread = VLC_FALSE;