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_init: initialize threads system
82 *****************************************************************************
83 * This function requires lazy initialization of a global lock in order to
84 * keep the library really thread-safe. Some architectures don't support this
85 * and thus do not guarantee the complete reentrancy.
86 *****************************************************************************/
87 int __vlc_threads_init( vlc_object_t *p_this )
89 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_this;
90 int i_ret = VLC_SUCCESS;
92 /* If we have lazy mutex initialization, use it. Otherwise, we just
93 * hope nothing wrong happens. */
94 #if defined( PTH_INIT_IN_PTH_H )
95 #elif defined( ST_INIT_IN_ST_H )
96 #elif defined( UNDER_CE )
97 #elif defined( WIN32 )
98 if( IsDebuggerPresent() )
100 /* SignalObjectAndWait() is problematic under a debugger */
101 b_fast_mutex = VLC_TRUE;
104 #elif defined( HAVE_KERNEL_SCHEDULER_H )
105 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
106 pthread_mutex_lock( &once_mutex );
107 #elif defined( HAVE_CTHREADS_H )
110 if( i_status == VLC_THREADS_UNINITIALIZED )
112 i_status = VLC_THREADS_PENDING;
114 /* We should be safe now. Do all the initialization stuff we want. */
115 p_libvlc_global->b_ready = VLC_FALSE;
117 #if defined( PTH_INIT_IN_PTH_H )
118 i_ret = ( pth_init() == FALSE );
120 #elif defined( ST_INIT_IN_ST_H )
123 #elif defined( UNDER_CE )
124 /* Nothing to initialize */
126 #elif defined( WIN32 )
127 /* Dynamically get the address of SignalObjectAndWait */
128 if( GetVersion() < 0x80000000 )
132 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
133 hInstLib = LoadLibrary( "kernel32" );
136 pf_SignalObjectAndWait =
137 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
138 "SignalObjectAndWait" );
142 #elif defined( HAVE_KERNEL_SCHEDULER_H )
143 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
144 #elif defined( HAVE_CTHREADS_H )
147 p_root = vlc_object_create( p_libvlc_global, VLC_OBJECT_GLOBAL );
153 i_status = VLC_THREADS_ERROR;
158 i_status = VLC_THREADS_READY;
161 vlc_threadvar_create( p_root, &msg_context_global_key );
165 /* Just increment the initialization count */
169 /* If we have lazy mutex initialization support, unlock the mutex;
170 * otherwize, do a naive wait loop. */
171 #if defined( PTH_INIT_IN_PTH_H )
172 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
173 #elif defined( ST_INIT_IN_ST_H )
174 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
175 #elif defined( UNDER_CE )
176 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
177 #elif defined( WIN32 )
178 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
179 #elif defined( HAVE_KERNEL_SCHEDULER_H )
180 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
181 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
182 pthread_mutex_unlock( &once_mutex );
183 #elif defined( HAVE_CTHREADS_H )
184 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
187 if( i_status != VLC_THREADS_READY )
195 /*****************************************************************************
196 * vlc_threads_end: stop threads system
197 *****************************************************************************
198 * FIXME: This function is far from being threadsafe.
199 *****************************************************************************/
200 int __vlc_threads_end( vlc_object_t *p_this )
203 #if defined( PTH_INIT_IN_PTH_H )
204 #elif defined( ST_INIT_IN_ST_H )
205 #elif defined( UNDER_CE )
206 #elif defined( WIN32 )
207 #elif defined( HAVE_KERNEL_SCHEDULER_H )
208 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
209 pthread_mutex_lock( &once_mutex );
210 #elif defined( HAVE_CTHREADS_H )
213 if( i_initializations == 0 )
217 if( i_initializations == 0 )
219 i_status = VLC_THREADS_UNINITIALIZED;
220 vlc_object_destroy( p_root );
223 #if defined( PTH_INIT_IN_PTH_H )
224 if( i_initializations == 0 )
226 return ( pth_kill() == FALSE );
229 #elif defined( ST_INIT_IN_ST_H )
230 #elif defined( UNDER_CE )
231 #elif defined( WIN32 )
232 #elif defined( HAVE_KERNEL_SCHEDULER_H )
233 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
234 pthread_mutex_unlock( &once_mutex );
235 #elif defined( HAVE_CTHREADS_H )
240 /*****************************************************************************
241 * vlc_mutex_init: initialize a mutex
242 *****************************************************************************/
243 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
246 p_mutex->p_this = p_this;
248 #if defined( PTH_INIT_IN_PTH_H )
249 return ( pth_mutex_init( &p_mutex->mutex ) == FALSE );
251 #elif defined( ST_INIT_IN_ST_H )
252 p_mutex->mutex = st_mutex_new();
253 return ( p_mutex->mutex == NULL ) ? errno : 0;
255 #elif defined( UNDER_CE )
256 InitializeCriticalSection( &p_mutex->csection );
259 #elif defined( WIN32 )
260 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
261 * function and have a 100% correct vlc_cond_wait() implementation.
262 * As this function is not available on Win9x, we can use the faster
263 * CriticalSections */
264 if( pf_SignalObjectAndWait && !b_fast_mutex )
266 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
267 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
268 return ( p_mutex->mutex != NULL ? 0 : 1 );
272 p_mutex->mutex = NULL;
273 InitializeCriticalSection( &p_mutex->csection );
277 #elif defined( HAVE_KERNEL_SCHEDULER_H )
278 /* check the arguments and whether it's already been initialized */
279 if( p_mutex == NULL )
284 if( p_mutex->init == 9999 )
289 p_mutex->lock = create_sem( 1, "BeMutex" );
290 if( p_mutex->lock < B_NO_ERROR )
295 p_mutex->init = 9999;
298 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
300 # if defined(SYS_LINUX)
302 /* Create error-checking mutex to detect problems more easily. */
303 pthread_mutexattr_t attr;
306 pthread_mutexattr_init( &attr );
307 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
308 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
309 pthread_mutexattr_destroy( &attr );
314 /* Create error-checking mutex to detect problems more easily. */
315 pthread_mutexattr_t attr;
318 pthread_mutexattr_init( &attr );
319 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
320 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
321 pthread_mutexattr_destroy( &attr );
326 return pthread_mutex_init( &p_mutex->mutex, NULL );
328 #elif defined( HAVE_CTHREADS_H )
329 mutex_init( p_mutex );
335 /*****************************************************************************
336 * vlc_mutex_destroy: destroy a mutex, inner version
337 *****************************************************************************/
338 int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
341 /* In case of error : */
343 const char * psz_error = "";
345 #if defined( PTH_INIT_IN_PTH_H )
348 #elif defined( ST_INIT_IN_ST_H )
349 i_result = st_mutex_destroy( p_mutex->mutex );
351 #elif defined( UNDER_CE )
352 DeleteCriticalSection( &p_mutex->csection );
355 #elif defined( WIN32 )
358 CloseHandle( p_mutex->mutex );
362 DeleteCriticalSection( &p_mutex->csection );
366 #elif defined( HAVE_KERNEL_SCHEDULER_H )
367 if( p_mutex->init == 9999 )
369 delete_sem( p_mutex->lock );
375 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
376 i_result = pthread_mutex_destroy( &p_mutex->mutex );
379 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
380 psz_error = strerror(i_result);
383 #elif defined( HAVE_CTHREADS_H )
390 msg_Err( p_mutex->p_this,
391 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
392 i_thread, psz_file, i_line, i_result, psz_error );
397 /*****************************************************************************
398 * vlc_cond_init: initialize a condition
399 *****************************************************************************/
400 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
402 p_condvar->p_this = p_this;
404 #if defined( PTH_INIT_IN_PTH_H )
405 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
407 #elif defined( ST_INIT_IN_ST_H )
408 p_condvar->cond = st_cond_new();
409 return ( p_condvar->cond == NULL ) ? errno : 0;
411 #elif defined( UNDER_CE )
412 /* Initialize counter */
413 p_condvar->i_waiting_threads = 0;
415 /* Create an auto-reset event. */
416 p_condvar->event = CreateEvent( NULL, /* no security */
417 FALSE, /* auto-reset event */
418 FALSE, /* start non-signaled */
419 NULL ); /* unnamed */
420 return !p_condvar->event;
422 #elif defined( WIN32 )
423 /* Initialize counter */
424 p_condvar->i_waiting_threads = 0;
427 p_condvar->i_win9x_cv = i_win9x_cv;
428 p_condvar->SignalObjectAndWait = pf_SignalObjectAndWait;
430 if( (p_condvar->SignalObjectAndWait && !b_fast_mutex)
431 || p_condvar->i_win9x_cv == 0 )
433 /* Create an auto-reset event. */
434 p_condvar->event = CreateEvent( NULL, /* no security */
435 FALSE, /* auto-reset event */
436 FALSE, /* start non-signaled */
437 NULL ); /* unnamed */
439 p_condvar->semaphore = NULL;
440 return !p_condvar->event;
444 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
445 0, /* initial count */
446 0x7fffffff, /* max count */
447 NULL ); /* unnamed */
449 if( p_condvar->i_win9x_cv == 1 )
450 /* Create a manual-reset event initially signaled. */
451 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
453 /* Create a auto-reset event. */
454 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
456 InitializeCriticalSection( &p_condvar->csection );
458 return !p_condvar->semaphore || !p_condvar->event;
461 #elif defined( HAVE_KERNEL_SCHEDULER_H )
467 if( p_condvar->init == 9999 )
472 p_condvar->thread = -1;
473 p_condvar->init = 9999;
476 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
477 pthread_condattr_t attr;
480 ret = pthread_condattr_init (&attr);
484 # if defined (_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
485 /* This must be the same clock as the one in mtime.c */
486 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
489 ret = pthread_cond_init (&p_condvar->cond, &attr);
490 pthread_condattr_destroy (&attr);
493 #elif defined( HAVE_CTHREADS_H )
494 /* condition_init() */
495 spin_lock_init( &p_condvar->lock );
496 cthread_queue_init( &p_condvar->queue );
498 p_condvar->implications = 0;
505 /*****************************************************************************
506 * vlc_cond_destroy: destroy a condition, inner version
507 *****************************************************************************/
508 int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
511 /* In case of error : */
513 const char * psz_error = "";
515 #if defined( PTH_INIT_IN_PTH_H )
518 #elif defined( ST_INIT_IN_ST_H )
519 i_result = st_cond_destroy( p_condvar->cond );
521 #elif defined( UNDER_CE )
522 i_result = !CloseHandle( p_condvar->event );
524 #elif defined( WIN32 )
525 if( !p_condvar->semaphore )
526 i_result = !CloseHandle( p_condvar->event );
528 i_result = !CloseHandle( p_condvar->event )
529 || !CloseHandle( p_condvar->semaphore );
531 if( p_condvar->semaphore != NULL )
532 DeleteCriticalSection( &p_condvar->csection );
534 #elif defined( HAVE_KERNEL_SCHEDULER_H )
538 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
539 i_result = pthread_cond_destroy( &p_condvar->cond );
542 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
543 psz_error = strerror(i_result);
546 #elif defined( HAVE_CTHREADS_H )
553 msg_Err( p_condvar->p_this,
554 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
555 i_thread, psz_file, i_line, i_result, psz_error );
560 /*****************************************************************************
561 * vlc_tls_create: create a thread-local variable
562 *****************************************************************************/
563 int __vlc_threadvar_create( vlc_object_t *p_this, vlc_threadvar_t *p_tls )
567 #if defined( PTH_INIT_IN_PTH_H )
568 i_ret = pth_key_create( &p_tls->handle, NULL );
569 #elif defined( HAVE_KERNEL_SCHEDULER_H )
570 msg_Err( p_this, "TLS not implemented" );
572 #elif defined( ST_INIT_IN_ST_H )
573 i_ret = st_key_create( &p_tls->handle, NULL );
574 #elif defined( UNDER_CE ) || defined( WIN32 )
575 #elif defined( WIN32 )
576 p_tls->handle = TlsAlloc();
577 i_ret = !( p_tls->handle == 0xFFFFFFFF );
579 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
580 i_ret = pthread_key_create( &p_tls->handle, NULL );
581 #elif defined( HAVE_CTHREADS_H )
582 i_ret = cthread_keycreate( &p_tls-handle );
587 /*****************************************************************************
588 * vlc_thread_create: create a thread, inner version
589 *****************************************************************************
590 * Note that i_priority is only taken into account on platforms supporting
591 * userland real-time priority threads.
592 *****************************************************************************/
593 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
594 const char *psz_name, void * ( *func ) ( void * ),
595 int i_priority, vlc_bool_t b_wait )
598 void *p_data = (void *)p_this;
599 vlc_object_internals_t *p_priv = p_this->p_internals;
601 vlc_mutex_lock( &p_this->object_lock );
603 #if defined( PTH_INIT_IN_PTH_H )
604 p_priv->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
605 i_ret = p_priv->thread_id == NULL;
607 #elif defined( ST_INIT_IN_ST_H )
608 p_priv->thread_id = st_thread_create( func, p_data, 1, 0 );
611 #elif defined( WIN32 ) || defined( UNDER_CE )
613 /* When using the MSVCRT C library you have to use the _beginthreadex
614 * function instead of CreateThread, otherwise you'll end up with
615 * memory leaks and the signal functions not working (see Microsoft
616 * Knowledge Base, article 104641) */
617 #if defined( UNDER_CE )
619 HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
620 (LPVOID)p_data, CREATE_SUSPENDED,
624 uintptr_t hThread = _beginthreadex( NULL, 0,
625 (LPTHREAD_START_ROUTINE)func,
626 (void*)p_data, CREATE_SUSPENDED,
629 p_priv->thread_id.id = (DWORD)threadId;
630 p_priv->thread_id.hThread = (HANDLE)hThread;
631 ResumeThread((HANDLE)hThread);
634 i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
636 if( i_ret && i_priority )
638 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
640 msg_Warn( p_this, "couldn't set a faster priority" );
645 #elif defined( HAVE_KERNEL_SCHEDULER_H )
646 p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
647 i_priority, p_data );
648 i_ret = resume_thread( p_priv->thread_id );
650 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
651 i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
654 if( config_GetInt( p_this, "rt-priority" ) )
657 int i_error, i_policy;
658 struct sched_param param;
660 memset( ¶m, 0, sizeof(struct sched_param) );
661 if( config_GetType( p_this, "rt-offset" ) )
663 i_priority += config_GetInt( p_this, "rt-offset" );
665 if( i_priority <= 0 )
667 param.sched_priority = (-1) * i_priority;
668 i_policy = SCHED_OTHER;
672 param.sched_priority = i_priority;
675 if( (i_error = pthread_setschedparam( p_priv->thread_id,
676 i_policy, ¶m )) )
678 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
679 psz_file, i_line, strerror(i_error) );
690 #elif defined( HAVE_CTHREADS_H )
691 p_priv->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
700 msg_Dbg( p_this, "waiting for thread completion" );
701 vlc_object_wait( p_this );
704 p_priv->b_thread = VLC_TRUE;
706 #if defined( WIN32 ) || defined( UNDER_CE )
707 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
708 (unsigned int)p_priv->thread_id.id, psz_name,
709 i_priority, psz_file, i_line );
711 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
712 (unsigned int)p_priv->thread_id, psz_name, i_priority,
717 vlc_mutex_unlock( &p_this->object_lock );
721 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
722 psz_name, psz_file, i_line, strerror(i_ret) );
723 vlc_mutex_unlock( &p_this->object_lock );
729 /*****************************************************************************
730 * vlc_thread_set_priority: set the priority of the current thread when we
731 * couldn't set it in vlc_thread_create (for instance for the main thread)
732 *****************************************************************************/
733 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
734 int i_line, int i_priority )
736 vlc_object_internals_t *p_priv = p_this->p_internals;
737 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
738 #elif defined( WIN32 ) || defined( UNDER_CE )
739 if( !p_priv->thread_id.hThread )
740 p_priv->thread_id.hThread = GetCurrentThread();
741 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
743 msg_Warn( p_this, "couldn't set a faster priority" );
747 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
749 if( config_GetInt( p_this, "rt-priority" ) > 0 )
752 int i_error, i_policy;
753 struct sched_param param;
755 memset( ¶m, 0, sizeof(struct sched_param) );
756 if( config_GetType( p_this, "rt-offset" ) )
758 i_priority += config_GetInt( p_this, "rt-offset" );
760 if( i_priority <= 0 )
762 param.sched_priority = (-1) * i_priority;
763 i_policy = SCHED_OTHER;
767 param.sched_priority = i_priority;
770 if( !p_priv->thread_id )
771 p_priv->thread_id = pthread_self();
772 if( (i_error = pthread_setschedparam( p_priv->thread_id,
773 i_policy, ¶m )) )
775 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
776 psz_file, i_line, strerror(i_error) );
785 /*****************************************************************************
786 * vlc_thread_ready: tell the parent thread we were successfully spawned
787 *****************************************************************************/
788 void __vlc_thread_ready( vlc_object_t *p_this )
790 vlc_object_signal( p_this );
793 /*****************************************************************************
794 * vlc_thread_join: wait until a thread exits, inner version
795 *****************************************************************************/
796 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
798 vlc_object_internals_t *p_priv = p_this->p_internals;
800 #if defined( UNDER_CE ) || defined( WIN32 )
802 BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
803 FILETIME*, FILETIME* );
804 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
805 int64_t real_time, kernel_time, user_time;
809 ** object will close its thread handle when destroyed, duplicate it here
810 ** to be on the safe side
812 if( ! DuplicateHandle(GetCurrentProcess(),
813 p_priv->thread_id.hThread,
818 DUPLICATE_SAME_ACCESS) )
820 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
821 (unsigned int)p_priv->thread_id.id,
822 psz_file, i_line, GetLastError() );
823 p_priv->b_thread = VLC_FALSE;
827 WaitForSingleObject( hThread, INFINITE );
829 msg_Dbg( p_this, "thread %u joined (%s:%d)",
830 (unsigned int)p_priv->thread_id.id,
832 #if defined( UNDER_CE )
833 hmodule = GetModuleHandle( _T("COREDLL") );
835 hmodule = GetModuleHandle( _T("KERNEL32") );
837 OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
838 FILETIME*, FILETIME* ))
839 GetProcAddress( hmodule, _T("GetThreadTimes") );
841 if( OurGetThreadTimes &&
842 OurGetThreadTimes( hThread,
843 &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
846 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
847 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
851 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
852 kernel_ft.dwLowDateTime) / 10;
855 ((((int64_t)user_ft.dwHighDateTime)<<32)|
856 user_ft.dwLowDateTime) / 10;
858 msg_Dbg( p_this, "thread times: "
859 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
860 real_time/60/1000000,
861 (double)((real_time%(60*1000000))/1000000.0),
862 kernel_time/60/1000000,
863 (double)((kernel_time%(60*1000000))/1000000.0),
864 user_time/60/1000000,
865 (double)((user_time%(60*1000000))/1000000.0) );
867 CloseHandle( hThread );
869 #else /* !defined(WIN32) */
873 #if defined( PTH_INIT_IN_PTH_H )
874 i_ret = ( pth_join( p_priv->thread_id, NULL ) == FALSE );
876 #elif defined( ST_INIT_IN_ST_H )
877 i_ret = st_thread_join( p_priv->thread_id, NULL );
879 #elif defined( HAVE_KERNEL_SCHEDULER_H )
881 i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
883 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
884 i_ret = pthread_join( p_priv->thread_id, NULL );
886 #elif defined( HAVE_CTHREADS_H )
887 cthread_join( p_priv->thread_id );
894 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
895 (unsigned int)p_priv->thread_id, psz_file, i_line,
900 msg_Dbg( p_this, "thread %u joined (%s:%d)",
901 (unsigned int)p_priv->thread_id, psz_file, i_line );
906 p_priv->b_thread = VLC_FALSE;