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 );
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_destroy: destroy a mutex, inner version
340 *****************************************************************************/
341 int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
344 /* In case of error : */
347 #if defined( PTH_INIT_IN_PTH_H )
350 #elif defined( ST_INIT_IN_ST_H )
351 i_result = st_mutex_destroy( p_mutex->mutex );
353 #elif defined( UNDER_CE )
354 DeleteCriticalSection( &p_mutex->csection );
357 #elif defined( WIN32 )
360 CloseHandle( p_mutex->mutex );
364 DeleteCriticalSection( &p_mutex->csection );
368 #elif defined( HAVE_KERNEL_SCHEDULER_H )
369 if( p_mutex->init == 9999 )
371 delete_sem( p_mutex->lock );
377 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
378 i_result = pthread_mutex_destroy( &p_mutex->mutex );
381 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
385 #elif defined( HAVE_CTHREADS_H )
392 msg_Err( p_mutex->p_this,
393 "thread %d: mutex_destroy failed at %s:%d (%d:%m)",
394 i_thread, psz_file, i_line, i_result );
395 vlc_threads_error( p_mutex->p_this );
400 /*****************************************************************************
401 * vlc_cond_init: initialize a condition
402 *****************************************************************************/
403 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
405 p_condvar->p_this = p_this;
407 #if defined( PTH_INIT_IN_PTH_H )
408 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
410 #elif defined( ST_INIT_IN_ST_H )
411 p_condvar->cond = st_cond_new();
412 return ( p_condvar->cond == NULL ) ? errno : 0;
414 #elif defined( UNDER_CE )
415 /* Initialize counter */
416 p_condvar->i_waiting_threads = 0;
418 /* Create an auto-reset event. */
419 p_condvar->event = CreateEvent( NULL, /* no security */
420 FALSE, /* auto-reset event */
421 FALSE, /* start non-signaled */
422 NULL ); /* unnamed */
423 return !p_condvar->event;
425 #elif defined( WIN32 )
426 /* Initialize counter */
427 p_condvar->i_waiting_threads = 0;
430 p_condvar->i_win9x_cv = i_win9x_cv;
431 p_condvar->SignalObjectAndWait = pf_SignalObjectAndWait;
433 if( (p_condvar->SignalObjectAndWait && !b_fast_mutex)
434 || p_condvar->i_win9x_cv == 0 )
436 /* Create an auto-reset event. */
437 p_condvar->event = CreateEvent( NULL, /* no security */
438 FALSE, /* auto-reset event */
439 FALSE, /* start non-signaled */
440 NULL ); /* unnamed */
442 p_condvar->semaphore = NULL;
443 return !p_condvar->event;
447 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
448 0, /* initial count */
449 0x7fffffff, /* max count */
450 NULL ); /* unnamed */
452 if( p_condvar->i_win9x_cv == 1 )
453 /* Create a manual-reset event initially signaled. */
454 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
456 /* Create a auto-reset event. */
457 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
459 InitializeCriticalSection( &p_condvar->csection );
461 return !p_condvar->semaphore || !p_condvar->event;
464 #elif defined( HAVE_KERNEL_SCHEDULER_H )
470 if( p_condvar->init == 9999 )
475 p_condvar->thread = -1;
476 p_condvar->init = 9999;
479 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
480 pthread_condattr_t attr;
483 ret = pthread_condattr_init (&attr);
487 # if defined (_POSIX_CLOCK_SELECTION) && (_POSIX_CLOCK_SELECTION >= 0)
488 /* NOTE: This must be the same clock as the one in mtime.c */
489 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
492 ret = pthread_cond_init (&p_condvar->cond, &attr);
493 pthread_condattr_destroy (&attr);
496 #elif defined( HAVE_CTHREADS_H )
497 /* condition_init() */
498 spin_lock_init( &p_condvar->lock );
499 cthread_queue_init( &p_condvar->queue );
501 p_condvar->implications = 0;
508 /*****************************************************************************
509 * vlc_cond_destroy: destroy a condition, inner version
510 *****************************************************************************/
511 int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
514 /* In case of error : */
517 #if defined( PTH_INIT_IN_PTH_H )
520 #elif defined( ST_INIT_IN_ST_H )
521 i_result = st_cond_destroy( p_condvar->cond );
523 #elif defined( UNDER_CE )
524 i_result = !CloseHandle( p_condvar->event );
526 #elif defined( WIN32 )
527 if( !p_condvar->semaphore )
528 i_result = !CloseHandle( p_condvar->event );
530 i_result = !CloseHandle( p_condvar->event )
531 || !CloseHandle( p_condvar->semaphore );
533 if( p_condvar->semaphore != NULL )
534 DeleteCriticalSection( &p_condvar->csection );
536 #elif defined( HAVE_KERNEL_SCHEDULER_H )
540 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
541 i_result = pthread_cond_destroy( &p_condvar->cond );
544 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
548 #elif defined( HAVE_CTHREADS_H )
555 msg_Err( p_condvar->p_this,
556 "thread %d: cond_destroy failed at %s:%d (%d:%m)",
557 i_thread, psz_file, i_line, i_result );
558 vlc_threads_error( p_condvar->p_this );
563 /*****************************************************************************
564 * vlc_tls_create: create a thread-local variable
565 *****************************************************************************/
566 int __vlc_threadvar_create( vlc_object_t *p_this, vlc_threadvar_t *p_tls )
570 #if defined( PTH_INIT_IN_PTH_H )
571 i_ret = pth_key_create( &p_tls->handle, NULL );
572 #elif defined( HAVE_KERNEL_SCHEDULER_H )
573 msg_Err( p_this, "TLS not implemented" );
575 #elif defined( ST_INIT_IN_ST_H )
576 i_ret = st_key_create( &p_tls->handle, NULL );
577 #elif defined( UNDER_CE ) || defined( WIN32 )
578 #elif defined( WIN32 )
579 p_tls->handle = TlsAlloc();
580 i_ret = !( p_tls->handle == 0xFFFFFFFF );
582 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
583 i_ret = pthread_key_create( &p_tls->handle, NULL );
584 #elif defined( HAVE_CTHREADS_H )
585 i_ret = cthread_keycreate( &p_tls-handle );
590 /*****************************************************************************
591 * vlc_thread_create: create a thread, inner version
592 *****************************************************************************
593 * Note that i_priority is only taken into account on platforms supporting
594 * userland real-time priority threads.
595 *****************************************************************************/
596 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
597 const char *psz_name, void * ( *func ) ( void * ),
598 int i_priority, vlc_bool_t b_wait )
601 void *p_data = (void *)p_this;
602 vlc_object_internals_t *p_priv = p_this->p_internals;
604 vlc_mutex_lock( &p_this->object_lock );
606 #if defined( PTH_INIT_IN_PTH_H )
607 p_priv->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
608 i_ret = p_priv->thread_id == NULL;
610 #elif defined( ST_INIT_IN_ST_H )
611 p_priv->thread_id = st_thread_create( func, p_data, 1, 0 );
614 #elif defined( WIN32 ) || defined( UNDER_CE )
616 /* When using the MSVCRT C library you have to use the _beginthreadex
617 * function instead of CreateThread, otherwise you'll end up with
618 * memory leaks and the signal functions not working (see Microsoft
619 * Knowledge Base, article 104641) */
620 #if defined( UNDER_CE )
622 HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
623 (LPVOID)p_data, CREATE_SUSPENDED,
627 uintptr_t hThread = _beginthreadex( NULL, 0,
628 (LPTHREAD_START_ROUTINE)func,
629 (void*)p_data, CREATE_SUSPENDED,
632 p_priv->thread_id.id = (DWORD)threadId;
633 p_priv->thread_id.hThread = (HANDLE)hThread;
634 ResumeThread((HANDLE)hThread);
637 i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
639 if( i_ret && i_priority )
641 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
643 msg_Warn( p_this, "couldn't set a faster priority" );
648 #elif defined( HAVE_KERNEL_SCHEDULER_H )
649 p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
650 i_priority, p_data );
651 i_ret = resume_thread( p_priv->thread_id );
653 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
654 i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
657 if( config_GetInt( p_this, "rt-priority" ) )
660 int i_error, i_policy;
661 struct sched_param param;
663 memset( ¶m, 0, sizeof(struct sched_param) );
664 if( config_GetType( p_this, "rt-offset" ) )
666 i_priority += config_GetInt( p_this, "rt-offset" );
668 if( i_priority <= 0 )
670 param.sched_priority = (-1) * i_priority;
671 i_policy = SCHED_OTHER;
675 param.sched_priority = i_priority;
678 if( (i_error = pthread_setschedparam( p_priv->thread_id,
679 i_policy, ¶m )) )
682 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
694 #elif defined( HAVE_CTHREADS_H )
695 p_priv->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
704 msg_Dbg( p_this, "waiting for thread completion" );
705 vlc_object_wait( p_this );
708 p_priv->b_thread = VLC_TRUE;
710 #if defined( WIN32 ) || defined( UNDER_CE )
711 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
712 (unsigned int)p_priv->thread_id.id, psz_name,
713 i_priority, psz_file, i_line );
715 msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
716 (unsigned int)p_priv->thread_id, psz_name, i_priority,
721 vlc_mutex_unlock( &p_this->object_lock );
726 msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
727 psz_name, psz_file, i_line );
728 vlc_threads_error( p_this );
729 vlc_mutex_unlock( &p_this->object_lock );
735 /*****************************************************************************
736 * vlc_thread_set_priority: set the priority of the current thread when we
737 * couldn't set it in vlc_thread_create (for instance for the main thread)
738 *****************************************************************************/
739 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
740 int i_line, int i_priority )
742 vlc_object_internals_t *p_priv = p_this->p_internals;
743 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
744 #elif defined( WIN32 ) || defined( UNDER_CE )
745 if( !p_priv->thread_id.hThread )
746 p_priv->thread_id.hThread = GetCurrentThread();
747 if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
749 msg_Warn( p_this, "couldn't set a faster priority" );
753 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
755 if( config_GetInt( p_this, "rt-priority" ) > 0 )
758 int i_error, i_policy;
759 struct sched_param param;
761 memset( ¶m, 0, sizeof(struct sched_param) );
762 if( config_GetType( p_this, "rt-offset" ) )
764 i_priority += config_GetInt( p_this, "rt-offset" );
766 if( i_priority <= 0 )
768 param.sched_priority = (-1) * i_priority;
769 i_policy = SCHED_OTHER;
773 param.sched_priority = i_priority;
776 if( !p_priv->thread_id )
777 p_priv->thread_id = pthread_self();
778 if( (i_error = pthread_setschedparam( p_priv->thread_id,
779 i_policy, ¶m )) )
782 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
792 /*****************************************************************************
793 * vlc_thread_ready: tell the parent thread we were successfully spawned
794 *****************************************************************************/
795 void __vlc_thread_ready( vlc_object_t *p_this )
797 vlc_object_signal( p_this );
800 /*****************************************************************************
801 * vlc_thread_join: wait until a thread exits, inner version
802 *****************************************************************************/
803 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
805 vlc_object_internals_t *p_priv = p_this->p_internals;
807 #if defined( UNDER_CE ) || defined( WIN32 )
809 BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
810 FILETIME*, FILETIME* );
811 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
812 int64_t real_time, kernel_time, user_time;
816 ** object will close its thread handle when destroyed, duplicate it here
817 ** to be on the safe side
819 if( ! DuplicateHandle(GetCurrentProcess(),
820 p_priv->thread_id.hThread,
825 DUPLICATE_SAME_ACCESS) )
827 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
828 (unsigned int)p_priv->thread_id.id,
829 psz_file, i_line, GetLastError() );
830 vlc_threads_error( p_this );
831 p_priv->b_thread = VLC_FALSE;
835 WaitForSingleObject( hThread, INFINITE );
837 msg_Dbg( p_this, "thread %u joined (%s:%d)",
838 (unsigned int)p_priv->thread_id.id,
840 #if defined( UNDER_CE )
841 hmodule = GetModuleHandle( _T("COREDLL") );
843 hmodule = GetModuleHandle( _T("KERNEL32") );
845 OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
846 FILETIME*, FILETIME* ))
847 GetProcAddress( hmodule, _T("GetThreadTimes") );
849 if( OurGetThreadTimes &&
850 OurGetThreadTimes( hThread,
851 &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
854 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
855 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
859 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
860 kernel_ft.dwLowDateTime) / 10;
863 ((((int64_t)user_ft.dwHighDateTime)<<32)|
864 user_ft.dwLowDateTime) / 10;
866 msg_Dbg( p_this, "thread times: "
867 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
868 real_time/60/1000000,
869 (double)((real_time%(60*1000000))/1000000.0),
870 kernel_time/60/1000000,
871 (double)((kernel_time%(60*1000000))/1000000.0),
872 user_time/60/1000000,
873 (double)((user_time%(60*1000000))/1000000.0) );
875 CloseHandle( hThread );
877 #else /* !defined(WIN32) */
881 #if defined( PTH_INIT_IN_PTH_H )
882 i_ret = ( pth_join( p_priv->thread_id, NULL ) == FALSE );
884 #elif defined( ST_INIT_IN_ST_H )
885 i_ret = st_thread_join( p_priv->thread_id, NULL );
887 #elif defined( HAVE_KERNEL_SCHEDULER_H )
889 i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
891 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
892 i_ret = pthread_join( p_priv->thread_id, NULL );
894 #elif defined( HAVE_CTHREADS_H )
895 cthread_join( p_priv->thread_id );
903 msg_Err( p_this, "thread_join(%u) failed at %s:%d (%m)",
904 (unsigned int)p_priv->thread_id, psz_file, i_line );
905 vlc_threads_error( p_this );
909 msg_Dbg( p_this, "thread %u joined (%s:%d)",
910 (unsigned int)p_priv->thread_id, psz_file, i_line );
915 p_priv->b_thread = VLC_FALSE;