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 *****************************************************************************/
32 #define VLC_THREADS_UNINITIALIZED 0
33 #define VLC_THREADS_PENDING 1
34 #define VLC_THREADS_ERROR 2
35 #define VLC_THREADS_READY 3
37 /*****************************************************************************
38 * Global mutex for lazy initialization of the threads system
39 *****************************************************************************/
40 static volatile unsigned i_initializations = 0;
41 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
42 static vlc_object_t *p_root;
44 #if defined( PTH_INIT_IN_PTH_H )
45 #elif defined( ST_INIT_IN_ST_H )
46 #elif defined( UNDER_CE )
47 #elif defined( WIN32 )
49 /* following is only available on NT/2000/XP and above */
50 static SIGNALOBJECTANDWAIT pf_SignalObjectAndWait = NULL;
53 ** On Windows NT/2K/XP we use a slow mutex implementation but which
54 ** allows us to correctly implement condition variables.
55 ** You can also use the faster Win9x implementation but you might
56 ** experience problems with it.
58 static vlc_bool_t b_fast_mutex = VLC_FALSE;
60 ** On Windows 9x/Me you can use a fast but incorrect condition variables
61 ** implementation (more precisely there is a possibility for a race
62 ** condition to happen).
63 ** However it is possible to use slower alternatives which are more robust.
64 ** Currently you can choose between implementation 0 (which is the
65 ** fastest but slightly incorrect), 1 (default) and 2.
67 static int i_win9x_cv = 1;
69 #elif defined( HAVE_KERNEL_SCHEDULER_H )
70 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
71 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
72 #elif defined( HAVE_CTHREADS_H )
75 vlc_threadvar_t msg_context_global_key;
77 /*****************************************************************************
78 * vlc_threads_init: initialize threads system
79 *****************************************************************************
80 * This function requires lazy initialization of a global lock in order to
81 * keep the library really thread-safe. Some architectures don't support this
82 * and thus do not guarantee the complete reentrancy.
83 *****************************************************************************/
84 int __vlc_threads_init( vlc_object_t *p_this )
86 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_this;
87 int i_ret = VLC_SUCCESS;
89 /* If we have lazy mutex initialization, use it. Otherwise, we just
90 * hope nothing wrong happens. */
91 #if defined( PTH_INIT_IN_PTH_H )
92 #elif defined( ST_INIT_IN_ST_H )
93 #elif defined( UNDER_CE )
94 #elif defined( WIN32 )
95 if( IsDebuggerPresent() )
97 /* SignalObjectAndWait() is problematic under a debugger */
98 b_fast_mutex = VLC_TRUE;
101 #elif defined( HAVE_KERNEL_SCHEDULER_H )
102 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
103 pthread_mutex_lock( &once_mutex );
104 #elif defined( HAVE_CTHREADS_H )
107 if( i_status == VLC_THREADS_UNINITIALIZED )
109 i_status = VLC_THREADS_PENDING;
111 /* We should be safe now. Do all the initialization stuff we want. */
112 p_libvlc_global->b_ready = VLC_FALSE;
114 #if defined( PTH_INIT_IN_PTH_H )
115 i_ret = ( pth_init() == FALSE );
117 #elif defined( ST_INIT_IN_ST_H )
120 #elif defined( UNDER_CE )
121 /* Nothing to initialize */
123 #elif defined( WIN32 )
124 /* Dynamically get the address of SignalObjectAndWait */
125 if( GetVersion() < 0x80000000 )
129 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
130 hInstLib = LoadLibrary( "kernel32" );
133 pf_SignalObjectAndWait =
134 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
135 "SignalObjectAndWait" );
139 #elif defined( HAVE_KERNEL_SCHEDULER_H )
140 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
141 #elif defined( HAVE_CTHREADS_H )
144 p_root = vlc_object_create( p_libvlc_global, VLC_OBJECT_GLOBAL );
150 i_status = VLC_THREADS_ERROR;
155 i_status = VLC_THREADS_READY;
158 vlc_threadvar_create( p_root, &msg_context_global_key );
162 /* Just increment the initialization count */
166 /* If we have lazy mutex initialization support, unlock the mutex;
167 * otherwize, do a naive wait loop. */
168 #if defined( PTH_INIT_IN_PTH_H )
169 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
170 #elif defined( ST_INIT_IN_ST_H )
171 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
172 #elif defined( UNDER_CE )
173 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
174 #elif defined( WIN32 )
175 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
176 #elif defined( HAVE_KERNEL_SCHEDULER_H )
177 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
178 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
179 pthread_mutex_unlock( &once_mutex );
180 #elif defined( HAVE_CTHREADS_H )
181 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
184 if( i_status != VLC_THREADS_READY )
192 /*****************************************************************************
193 * vlc_threads_end: stop threads system
194 *****************************************************************************
195 * FIXME: This function is far from being threadsafe.
196 *****************************************************************************/
197 int __vlc_threads_end( vlc_object_t *p_this )
200 #if defined( PTH_INIT_IN_PTH_H )
201 #elif defined( ST_INIT_IN_ST_H )
202 #elif defined( UNDER_CE )
203 #elif defined( WIN32 )
204 #elif defined( HAVE_KERNEL_SCHEDULER_H )
205 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
206 pthread_mutex_lock( &once_mutex );
207 #elif defined( HAVE_CTHREADS_H )
210 if( i_initializations == 0 )
214 if( i_initializations == 0 )
216 i_status = VLC_THREADS_UNINITIALIZED;
217 vlc_object_destroy( p_root );
220 #if defined( PTH_INIT_IN_PTH_H )
221 if( i_initializations == 0 )
223 return ( pth_kill() == FALSE );
226 #elif defined( ST_INIT_IN_ST_H )
227 #elif defined( UNDER_CE )
228 #elif defined( WIN32 )
229 #elif defined( HAVE_KERNEL_SCHEDULER_H )
230 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
231 pthread_mutex_unlock( &once_mutex );
232 #elif defined( HAVE_CTHREADS_H )
237 /*****************************************************************************
238 * vlc_mutex_init: initialize a mutex
239 *****************************************************************************/
240 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
243 p_mutex->p_this = p_this;
245 #if defined( PTH_INIT_IN_PTH_H )
246 return ( pth_mutex_init( &p_mutex->mutex ) == FALSE );
248 #elif defined( ST_INIT_IN_ST_H )
249 p_mutex->mutex = st_mutex_new();
250 return ( p_mutex->mutex == NULL ) ? errno : 0;
252 #elif defined( UNDER_CE )
253 InitializeCriticalSection( &p_mutex->csection );
256 #elif defined( WIN32 )
257 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
258 * function and have a 100% correct vlc_cond_wait() implementation.
259 * As this function is not available on Win9x, we can use the faster
260 * CriticalSections */
261 if( pf_SignalObjectAndWait && !b_fast_mutex )
263 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
264 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
265 return ( p_mutex->mutex != NULL ? 0 : 1 );
269 p_mutex->mutex = NULL;
270 InitializeCriticalSection( &p_mutex->csection );
274 #elif defined( HAVE_KERNEL_SCHEDULER_H )
275 /* check the arguments and whether it's already been initialized */
276 if( p_mutex == NULL )
281 if( p_mutex->init == 9999 )
286 p_mutex->lock = create_sem( 1, "BeMutex" );
287 if( p_mutex->lock < B_NO_ERROR )
292 p_mutex->init = 9999;
295 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
296 # if defined(DEBUG) && defined(SYS_LINUX)
298 /* Create error-checking mutex to detect problems more easily. */
299 pthread_mutexattr_t attr;
302 pthread_mutexattr_init( &attr );
303 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
304 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
305 pthread_mutexattr_destroy( &attr );
309 return pthread_mutex_init( &p_mutex->mutex, NULL );
311 #elif defined( HAVE_CTHREADS_H )
312 mutex_init( p_mutex );
318 /*****************************************************************************
319 * vlc_mutex_destroy: destroy a mutex, inner version
320 *****************************************************************************/
321 int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
324 /* In case of error : */
326 const char * psz_error = "";
328 #if defined( PTH_INIT_IN_PTH_H )
331 #elif defined( ST_INIT_IN_ST_H )
332 i_result = st_mutex_destroy( p_mutex->mutex );
334 #elif defined( UNDER_CE )
335 DeleteCriticalSection( &p_mutex->csection );
338 #elif defined( WIN32 )
341 CloseHandle( p_mutex->mutex );
345 DeleteCriticalSection( &p_mutex->csection );
349 #elif defined( HAVE_KERNEL_SCHEDULER_H )
350 if( p_mutex->init == 9999 )
352 delete_sem( p_mutex->lock );
358 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
359 i_result = pthread_mutex_destroy( &p_mutex->mutex );
362 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
363 psz_error = strerror(i_result);
366 #elif defined( HAVE_CTHREADS_H )
373 msg_Err( p_mutex->p_this,
374 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
375 i_thread, psz_file, i_line, i_result, psz_error );
380 /*****************************************************************************
381 * vlc_cond_init: initialize a condition
382 *****************************************************************************/
383 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
385 p_condvar->p_this = p_this;
387 #if defined( PTH_INIT_IN_PTH_H )
388 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
390 #elif defined( ST_INIT_IN_ST_H )
391 p_condvar->cond = st_cond_new();
392 return ( p_condvar->cond == NULL ) ? errno : 0;
394 #elif defined( UNDER_CE )
395 /* Initialize counter */
396 p_condvar->i_waiting_threads = 0;
398 /* Create an auto-reset event. */
399 p_condvar->event = CreateEvent( NULL, /* no security */
400 FALSE, /* auto-reset event */
401 FALSE, /* start non-signaled */
402 NULL ); /* unnamed */
403 return !p_condvar->event;
405 #elif defined( WIN32 )
406 /* Initialize counter */
407 p_condvar->i_waiting_threads = 0;
410 p_condvar->i_win9x_cv = i_win9x_cv;
411 p_condvar->SignalObjectAndWait = pf_SignalObjectAndWait;
413 if( (p_condvar->SignalObjectAndWait && !b_fast_mutex)
414 || p_condvar->i_win9x_cv == 0 )
416 /* Create an auto-reset event. */
417 p_condvar->event = CreateEvent( NULL, /* no security */
418 FALSE, /* auto-reset event */
419 FALSE, /* start non-signaled */
420 NULL ); /* unnamed */
422 p_condvar->semaphore = NULL;
423 return !p_condvar->event;
427 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
428 0, /* initial count */
429 0x7fffffff, /* max count */
430 NULL ); /* unnamed */
432 if( p_condvar->i_win9x_cv == 1 )
433 /* Create a manual-reset event initially signaled. */
434 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
436 /* Create a auto-reset event. */
437 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
439 InitializeCriticalSection( &p_condvar->csection );
441 return !p_condvar->semaphore || !p_condvar->event;
444 #elif defined( HAVE_KERNEL_SCHEDULER_H )
450 if( p_condvar->init == 9999 )
455 p_condvar->thread = -1;
456 p_condvar->init = 9999;
459 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
460 pthread_condattr_t attr;
463 ret = pthread_condattr_init (&attr);
467 # if defined (_POSIX_CLOCK_MONOTONIC) && (_POSIX_CLOCK_MONOTONIC >= 0)
468 /* This must be the same clock as the one in mtime.c */
469 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
472 ret = pthread_cond_init (&p_condvar->cond, &attr);
473 pthread_condattr_destroy (&attr);
476 #elif defined( HAVE_CTHREADS_H )
477 /* condition_init() */
478 spin_lock_init( &p_condvar->lock );
479 cthread_queue_init( &p_condvar->queue );
481 p_condvar->implications = 0;
488 /*****************************************************************************
489 * vlc_cond_destroy: destroy a condition, inner version
490 *****************************************************************************/
491 int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
494 /* In case of error : */
496 const char * psz_error = "";
498 #if defined( PTH_INIT_IN_PTH_H )
501 #elif defined( ST_INIT_IN_ST_H )
502 i_result = st_cond_destroy( p_condvar->cond );
504 #elif defined( UNDER_CE )
505 i_result = !CloseHandle( p_condvar->event );
507 #elif defined( WIN32 )
508 if( !p_condvar->semaphore )
509 i_result = !CloseHandle( p_condvar->event );
511 i_result = !CloseHandle( p_condvar->event )
512 || !CloseHandle( p_condvar->semaphore );
514 if( p_condvar->semaphore != NULL )
515 DeleteCriticalSection( &p_condvar->csection );
517 #elif defined( HAVE_KERNEL_SCHEDULER_H )
521 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
522 i_result = pthread_cond_destroy( &p_condvar->cond );
525 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
526 psz_error = strerror(i_result);
529 #elif defined( HAVE_CTHREADS_H )
536 msg_Err( p_condvar->p_this,
537 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
538 i_thread, psz_file, i_line, i_result, psz_error );
543 /*****************************************************************************
544 * vlc_tls_create: create a thread-local variable
545 *****************************************************************************/
546 int __vlc_threadvar_create( vlc_object_t *p_this, vlc_threadvar_t *p_tls )
550 #if defined( PTH_INIT_IN_PTH_H )
551 i_ret = pth_key_create( &p_tls->handle, NULL );
552 #elif defined( HAVE_KERNEL_SCHEDULER_H )
553 msg_Err( p_this, "TLS not implemented" );
555 #elif defined( ST_INIT_IN_ST_H )
556 i_ret = st_key_create( &p_tls->handle, NULL );
557 #elif defined( UNDER_CE ) || defined( WIN32 )
558 #elif defined( WIN32 )
559 p_tls->handle = TlsAlloc();
560 i_ret = !( p_tls->handle == 0xFFFFFFFF );
562 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
563 i_ret = pthread_key_create( &p_tls->handle, NULL );
564 #elif defined( HAVE_CTHREADS_H )
565 i_ret = cthread_keycreate( &p_tls-handle );
570 /*****************************************************************************
571 * vlc_thread_create: create a thread, inner version
572 *****************************************************************************
573 * Note that i_priority is only taken into account on platforms supporting
574 * userland real-time priority threads.
575 *****************************************************************************/
576 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
577 const char *psz_name, void * ( *func ) ( void * ),
578 int i_priority, vlc_bool_t b_wait )
581 void *p_data = (void *)p_this;
582 vlc_object_internals_t *p_priv = p_this->p_internals;
584 vlc_mutex_lock( &p_this->object_lock );
586 #if defined( PTH_INIT_IN_PTH_H )
587 p_priv->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
588 i_ret = p_priv->thread_id == NULL;
590 #elif defined( ST_INIT_IN_ST_H )
591 p_priv->thread_id = st_thread_create( func, p_data, 1, 0 );
594 #elif defined( WIN32 ) || defined( UNDER_CE )
596 /* When using the MSVCRT C library you have to use the _beginthreadex
597 * function instead of CreateThread, otherwise you'll end up with
598 * memory leaks and the signal functions not working (see Microsoft
599 * Knowledge Base, article 104641) */
600 #if defined( UNDER_CE )
602 HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
603 (LPVOID)p_data, CREATE_SUSPENDED,
607 uintptr_t hThread = _beginthreadex( NULL, 0,
608 (LPTHREAD_START_ROUTINE)func,
609 (void*)p_data, CREATE_SUSPENDED,
612 p_priv->thread_id.id = (DWORD)threadId;
613 p_priv->thread_id.hThread = (HANDLE)hThread;
614 ResumeThread((HANDLE)hThread);
617 i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
619 if( i_ret && i_priority )
621 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
623 msg_Warn( p_this, "couldn't set a faster priority" );
628 #elif defined( HAVE_KERNEL_SCHEDULER_H )
629 p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
630 i_priority, p_data );
631 i_ret = resume_thread( p_priv->thread_id );
633 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
634 i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
637 if( config_GetInt( p_this, "rt-priority" ) )
640 int i_error, i_policy;
641 struct sched_param param;
643 memset( ¶m, 0, sizeof(struct sched_param) );
644 if( config_GetType( p_this, "rt-offset" ) )
646 i_priority += config_GetInt( p_this, "rt-offset" );
648 if( i_priority <= 0 )
650 param.sched_priority = (-1) * i_priority;
651 i_policy = SCHED_OTHER;
655 param.sched_priority = i_priority;
658 if( (i_error = pthread_setschedparam( p_priv->thread_id,
659 i_policy, ¶m )) )
661 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
662 psz_file, i_line, strerror(i_error) );
673 #elif defined( HAVE_CTHREADS_H )
674 p_priv->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
683 msg_Dbg( p_this, "waiting for thread completion" );
684 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
687 p_priv->b_thread = VLC_TRUE;
689 #if defined( WIN32 ) || defined( UNDER_CE )
690 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
691 (unsigned int)p_priv->thread_id.id, psz_name,
692 i_priority, psz_file, i_line );
694 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
695 (unsigned int)p_priv->thread_id, psz_name, i_priority,
700 vlc_mutex_unlock( &p_this->object_lock );
704 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
705 psz_name, psz_file, i_line, strerror(i_ret) );
706 vlc_mutex_unlock( &p_this->object_lock );
712 /*****************************************************************************
713 * vlc_thread_set_priority: set the priority of the current thread when we
714 * couldn't set it in vlc_thread_create (for instance for the main thread)
715 *****************************************************************************/
716 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
717 int i_line, int i_priority )
719 vlc_object_internals_t *p_priv = p_this->p_internals;
720 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
721 #elif defined( WIN32 ) || defined( UNDER_CE )
722 if( !p_priv->thread_id.hThread )
723 p_priv->thread_id.hThread = GetCurrentThread();
724 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
726 msg_Warn( p_this, "couldn't set a faster priority" );
730 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
732 if( config_GetInt( p_this, "rt-priority" ) > 0 )
735 int i_error, i_policy;
736 struct sched_param param;
738 memset( ¶m, 0, sizeof(struct sched_param) );
739 if( config_GetType( p_this, "rt-offset" ) )
741 i_priority += config_GetInt( p_this, "rt-offset" );
743 if( i_priority <= 0 )
745 param.sched_priority = (-1) * i_priority;
746 i_policy = SCHED_OTHER;
750 param.sched_priority = i_priority;
753 if( !p_priv->thread_id )
754 p_priv->thread_id = pthread_self();
755 if( (i_error = pthread_setschedparam( p_priv->thread_id,
756 i_policy, ¶m )) )
758 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
759 psz_file, i_line, strerror(i_error) );
768 /*****************************************************************************
769 * vlc_thread_ready: tell the parent thread we were successfully spawned
770 *****************************************************************************/
771 void __vlc_thread_ready( vlc_object_t *p_this )
773 vlc_mutex_lock( &p_this->object_lock );
774 vlc_cond_signal( &p_this->object_wait );
775 vlc_mutex_unlock( &p_this->object_lock );
778 /*****************************************************************************
779 * vlc_thread_join: wait until a thread exits, inner version
780 *****************************************************************************/
781 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
783 vlc_object_internals_t *p_priv = p_this->p_internals;
785 #if defined( UNDER_CE ) || defined( WIN32 )
787 BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
788 FILETIME*, FILETIME* );
789 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
790 int64_t real_time, kernel_time, user_time;
794 ** object will close its thread handle when destroyed, duplicate it here
795 ** to be on the safe side
797 if( ! DuplicateHandle(GetCurrentProcess(),
798 p_priv->thread_id.hThread,
803 DUPLICATE_SAME_ACCESS) )
805 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
806 (unsigned int)p_priv->thread_id.id,
807 psz_file, i_line, GetLastError() );
808 p_priv->b_thread = VLC_FALSE;
812 WaitForSingleObject( hThread, INFINITE );
814 msg_Dbg( p_this, "thread %u joined (%s:%d)",
815 (unsigned int)p_priv->thread_id.id,
817 #if defined( UNDER_CE )
818 hmodule = GetModuleHandle( _T("COREDLL") );
820 hmodule = GetModuleHandle( _T("KERNEL32") );
822 OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
823 FILETIME*, FILETIME* ))
824 GetProcAddress( hmodule, _T("GetThreadTimes") );
826 if( OurGetThreadTimes &&
827 OurGetThreadTimes( hThread,
828 &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
831 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
832 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
836 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
837 kernel_ft.dwLowDateTime) / 10;
840 ((((int64_t)user_ft.dwHighDateTime)<<32)|
841 user_ft.dwLowDateTime) / 10;
843 msg_Dbg( p_this, "thread times: "
844 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
845 real_time/60/1000000,
846 (double)((real_time%(60*1000000))/1000000.0),
847 kernel_time/60/1000000,
848 (double)((kernel_time%(60*1000000))/1000000.0),
849 user_time/60/1000000,
850 (double)((user_time%(60*1000000))/1000000.0) );
852 CloseHandle( hThread );
854 #else /* !defined(WIN32) */
858 #if defined( PTH_INIT_IN_PTH_H )
859 i_ret = ( pth_join( p_priv->thread_id, NULL ) == FALSE );
861 #elif defined( ST_INIT_IN_ST_H )
862 i_ret = st_thread_join( p_priv->thread_id, NULL );
864 #elif defined( HAVE_KERNEL_SCHEDULER_H )
866 i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
868 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
869 i_ret = pthread_join( p_priv->thread_id, NULL );
871 #elif defined( HAVE_CTHREADS_H )
872 cthread_join( p_priv->thread_id );
879 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
880 (unsigned int)p_priv->thread_id, psz_file, i_line,
885 msg_Dbg( p_this, "thread %u joined (%s:%d)",
886 (unsigned int)p_priv->thread_id, psz_file, i_line );
891 p_priv->b_thread = VLC_FALSE;