]> git.sesse.net Git - vlc/blob - src/misc/threads.c
Typo: use debug code only when wanted (and defined)
[vlc] / src / misc / threads.c
1 /*****************************************************************************
2  * threads.c : threads implementation for the VideoLAN client
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Clément Sténac
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include "libvlc.h"
34 #include <assert.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <signal.h>
39
40 #define VLC_THREADS_UNINITIALIZED  0
41 #define VLC_THREADS_PENDING        1
42 #define VLC_THREADS_ERROR          2
43 #define VLC_THREADS_READY          3
44
45 /*****************************************************************************
46  * Global mutex for lazy initialization of the threads system
47  *****************************************************************************/
48 static volatile unsigned i_initializations = 0;
49
50 #if defined( LIBVLC_USE_PTHREAD )
51 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
52 #endif
53
54 /**
55  * Global process-wide VLC object.
56  * Contains inter-instance data, such as the module cache and global mutexes.
57  */
58 static libvlc_global_data_t *p_root;
59
60 libvlc_global_data_t *vlc_global( void )
61 {
62     assert( i_initializations > 0 );
63     return p_root;
64 }
65
66 #ifndef NDEBUG
67 /**
68  * Object running the current thread
69  */
70 static vlc_threadvar_t thread_object_key;
71
72 vlc_object_t *vlc_threadobj (void)
73 {
74     return vlc_threadvar_get (&thread_object_key);
75 }
76 #endif
77
78 vlc_threadvar_t msg_context_global_key;
79
80 #if defined(LIBVLC_USE_PTHREAD)
81 static inline unsigned long vlc_threadid (void)
82 {
83      union { pthread_t th; unsigned long int i; } v = { };
84      v.th = pthread_self ();
85      return v.i;
86 }
87
88 #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
89 # include <execinfo.h>
90 #endif
91
92 /*****************************************************************************
93  * vlc_thread_fatal: Report an error from the threading layer
94  *****************************************************************************
95  * This is mostly meant for debugging.
96  *****************************************************************************/
97 void vlc_pthread_fatal (const char *action, int error,
98                         const char *file, unsigned line)
99 {
100     fprintf (stderr, "LibVLC fatal error %s in thread %lu at %s:%u: %d\n",
101              action, vlc_threadid (), file, line, error);
102     fflush (stderr);
103
104     /* Sometimes strerror_r() crashes too, so make sure we print an error
105      * message before we invoke it */
106 #ifdef __GLIBC__
107     /* Avoid the strerror_r() prototype brain damage in glibc */
108     errno = error;
109     dprintf (2, " Error message: %m at:\n");
110 #else
111     char buf[1000];
112     const char *msg;
113
114     switch (strerror_r (error, buf, sizeof (buf)))
115     {
116         case 0:
117             msg = buf;
118             break;
119         case ERANGE: /* should never happen */
120             msg = "unknwon (too big to display)";
121             break;
122         default:
123             msg = "unknown (invalid error number)";
124             break;
125     }
126     fprintf (stderr, " Error message: %s\n", msg);
127     fflush (stderr);
128 #endif
129
130 #ifdef HAVE_BACKTRACE
131     void *stack[20];
132     int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
133     backtrace_symbols_fd (stack, len, 2);
134 #endif
135
136     abort ();
137 }
138 #else
139 void vlc_pthread_fatal (const char *action, int error,
140                         const char *file, unsigned line)
141 {
142     (void)action; (void)error; (void)file; (void)line;
143     abort();
144 }
145 #endif
146
147 /*****************************************************************************
148  * vlc_threads_init: initialize threads system
149  *****************************************************************************
150  * This function requires lazy initialization of a global lock in order to
151  * keep the library really thread-safe. Some architectures don't support this
152  * and thus do not guarantee the complete reentrancy.
153  *****************************************************************************/
154 int vlc_threads_init( void )
155 {
156     int i_ret = VLC_SUCCESS;
157
158     /* If we have lazy mutex initialization, use it. Otherwise, we just
159      * hope nothing wrong happens. */
160 #if defined( LIBVLC_USE_PTHREAD )
161     pthread_mutex_lock( &once_mutex );
162 #endif
163
164     if( i_initializations == 0 )
165     {
166         p_root = vlc_custom_create( NULL, sizeof( *p_root ),
167                                     VLC_OBJECT_GENERIC, "root" );
168         if( p_root == NULL )
169         {
170             i_ret = VLC_ENOMEM;
171             goto out;
172         }
173
174         /* We should be safe now. Do all the initialization stuff we want. */
175 #ifndef NDEBUG
176         vlc_threadvar_create( &thread_object_key, NULL );
177 #endif
178         vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy );
179     }
180     i_initializations++;
181
182 out:
183     /* If we have lazy mutex initialization support, unlock the mutex.
184      * Otherwize, we are screwed. */
185 #if defined( LIBVLC_USE_PTHREAD )
186     pthread_mutex_unlock( &once_mutex );
187 #endif
188
189     return i_ret;
190 }
191
192 /*****************************************************************************
193  * vlc_threads_end: stop threads system
194  *****************************************************************************
195  * FIXME: This function is far from being threadsafe.
196  *****************************************************************************/
197 void vlc_threads_end( void )
198 {
199 #if defined( LIBVLC_USE_PTHREAD )
200     pthread_mutex_lock( &once_mutex );
201 #endif
202
203     assert( i_initializations > 0 );
204
205     if( i_initializations == 1 )
206     {
207         vlc_object_release( p_root );
208         vlc_threadvar_delete( &msg_context_global_key );
209 #ifndef NDEBUG
210         vlc_threadvar_delete( &thread_object_key );
211 #endif
212     }
213     i_initializations--;
214
215 #if defined( LIBVLC_USE_PTHREAD )
216     pthread_mutex_unlock( &once_mutex );
217 #endif
218 }
219
220 #if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
221 /* This is not prototyped under glibc, though it exists. */
222 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
223 #endif
224
225 /*****************************************************************************
226  * vlc_mutex_init: initialize a mutex
227  *****************************************************************************/
228 int vlc_mutex_init( vlc_mutex_t *p_mutex )
229 {
230 #if defined( LIBVLC_USE_PTHREAD )
231     pthread_mutexattr_t attr;
232     int                 i_result;
233
234     pthread_mutexattr_init( &attr );
235
236 # ifndef NDEBUG
237     /* Create error-checking mutex to detect problems more easily. */
238 #  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
239     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
240 #  else
241     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
242 #  endif
243 # endif
244     i_result = pthread_mutex_init( p_mutex, &attr );
245     pthread_mutexattr_destroy( &attr );
246     return i_result;
247 #elif defined( UNDER_CE )
248     InitializeCriticalSection( &p_mutex->csection );
249     return 0;
250
251 #elif defined( WIN32 )
252     *p_mutex = CreateMutex( 0, FALSE, 0 );
253     return (*p_mutex != NULL) ? 0 : ENOMEM;
254
255 #elif defined( HAVE_KERNEL_SCHEDULER_H )
256     /* check the arguments and whether it's already been initialized */
257     if( p_mutex == NULL )
258     {
259         return B_BAD_VALUE;
260     }
261
262     if( p_mutex->init == 9999 )
263     {
264         return EALREADY;
265     }
266
267     p_mutex->lock = create_sem( 1, "BeMutex" );
268     if( p_mutex->lock < B_NO_ERROR )
269     {
270         return( -1 );
271     }
272
273     p_mutex->init = 9999;
274     return B_OK;
275
276 #endif
277 }
278
279 /*****************************************************************************
280  * vlc_mutex_init: initialize a recursive mutex (Do not use)
281  *****************************************************************************/
282 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
283 {
284 #if defined( LIBVLC_USE_PTHREAD )
285     pthread_mutexattr_t attr;
286     int                 i_result;
287
288     pthread_mutexattr_init( &attr );
289 #  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
290     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
291 #  else
292     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
293 #  endif
294     i_result = pthread_mutex_init( p_mutex, &attr );
295     pthread_mutexattr_destroy( &attr );
296     return( i_result );
297 #elif defined( WIN32 )
298     /* Create mutex returns a recursive mutex */
299     *p_mutex = CreateMutex( 0, FALSE, 0 );
300     return (*p_mutex != NULL) ? 0 : ENOMEM;
301 #else
302 # error Unimplemented!
303 #endif
304 }
305
306
307 /*****************************************************************************
308  * vlc_mutex_destroy: destroy a mutex, inner version
309  *****************************************************************************/
310 void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
311 {
312 #if defined( LIBVLC_USE_PTHREAD )
313     int val = pthread_mutex_destroy( p_mutex );
314     VLC_THREAD_ASSERT ("destroying mutex");
315
316 #elif defined( UNDER_CE )
317     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
318
319     DeleteCriticalSection( &p_mutex->csection );
320
321 #elif defined( WIN32 )
322     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
323
324     CloseHandle( *p_mutex );
325
326 #elif defined( HAVE_KERNEL_SCHEDULER_H )
327     if( p_mutex->init == 9999 )
328         delete_sem( p_mutex->lock );
329
330     p_mutex->init = 0;
331
332 #endif
333 }
334
335 /*****************************************************************************
336  * vlc_cond_init: initialize a condition
337  *****************************************************************************/
338 int __vlc_cond_init( vlc_cond_t *p_condvar )
339 {
340 #if defined( LIBVLC_USE_PTHREAD )
341     pthread_condattr_t attr;
342     int ret;
343
344     ret = pthread_condattr_init (&attr);
345     if (ret)
346         return ret;
347
348 # if !defined (_POSIX_CLOCK_SELECTION)
349    /* Fairly outdated POSIX support (that was defined in 2001) */
350 #  define _POSIX_CLOCK_SELECTION (-1)
351 # endif
352 # if (_POSIX_CLOCK_SELECTION >= 0)
353     /* NOTE: This must be the same clock as the one in mtime.c */
354     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
355 # endif
356
357     ret = pthread_cond_init (p_condvar, &attr);
358     pthread_condattr_destroy (&attr);
359     return ret;
360
361 #elif defined( UNDER_CE ) || defined( WIN32 )
362     /* Initialize counter */
363     p_condvar->i_waiting_threads = 0;
364
365     /* Create an auto-reset event. */
366     p_condvar->event = CreateEvent( NULL,   /* no security */
367                                     FALSE,  /* auto-reset event */
368                                     FALSE,  /* start non-signaled */
369                                     NULL ); /* unnamed */
370     return !p_condvar->event;
371
372 #elif defined( HAVE_KERNEL_SCHEDULER_H )
373     if( !p_condvar )
374     {
375         return B_BAD_VALUE;
376     }
377
378     if( p_condvar->init == 9999 )
379     {
380         return EALREADY;
381     }
382
383     p_condvar->thread = -1;
384     p_condvar->init = 9999;
385     return 0;
386
387 #endif
388 }
389
390 /*****************************************************************************
391  * vlc_cond_destroy: destroy a condition, inner version
392  *****************************************************************************/
393 void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
394 {
395 #if defined( LIBVLC_USE_PTHREAD )
396     int val = pthread_cond_destroy( p_condvar );
397     VLC_THREAD_ASSERT ("destroying condition");
398
399 #elif defined( UNDER_CE ) || defined( WIN32 )
400     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
401
402     CloseHandle( p_condvar->event );
403
404 #elif defined( HAVE_KERNEL_SCHEDULER_H )
405     p_condvar->init = 0;
406
407 #endif
408 }
409
410 /*****************************************************************************
411  * vlc_tls_create: create a thread-local variable
412  *****************************************************************************/
413 int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
414 {
415     int i_ret;
416
417 #if defined( LIBVLC_USE_PTHREAD )
418     i_ret =  pthread_key_create( p_tls, destr );
419 #elif defined( UNDER_CE )
420     i_ret = ENOSYS;
421 #elif defined( WIN32 )
422     *p_tls = TlsAlloc();
423     i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
424 #else
425 # error Unimplemented!
426 #endif
427     return i_ret;
428 }
429
430 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
431 {
432 #if defined( LIBVLC_USE_PTHREAD )
433     pthread_key_delete (*p_tls);
434 #elif defined( UNDER_CE )
435 #elif defined( WIN32 )
436     TlsFree (*p_tls);
437 #else
438 # error Unimplemented!
439 #endif
440 }
441
442 struct vlc_thread_boot
443 {
444     void * (*entry) (void *);
445     vlc_object_t *object;
446 };
447
448 #if defined (LIBVLC_USE_PTHREAD)
449 # define THREAD_RTYPE void *
450 # define THREAD_RVAL  NULL
451 #elif defined (WIN32)
452 # define THREAD_RTYPE __stdcall unsigned
453 # define THREAD_RVAL 0
454 #endif
455
456 static THREAD_RTYPE thread_entry (void *data)
457 {
458     vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
459     void *(*func) (void *) = ((struct vlc_thread_boot *)data)->entry;
460
461     free (data);
462 #ifndef NDEBUG
463     vlc_threadvar_set (&thread_object_key, obj);
464 #endif
465     msg_Dbg (obj, "thread started");
466     func (obj);
467     msg_Dbg (obj, "thread ended");
468     return THREAD_RVAL;
469 }
470
471 /*****************************************************************************
472  * vlc_thread_create: create a thread, inner version
473  *****************************************************************************
474  * Note that i_priority is only taken into account on platforms supporting
475  * userland real-time priority threads.
476  *****************************************************************************/
477 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
478                          const char *psz_name, void * ( *func ) ( void * ),
479                          int i_priority, bool b_wait )
480 {
481     int i_ret;
482     vlc_object_internals_t *p_priv = vlc_internals( p_this );
483
484     struct vlc_thread_boot *boot = malloc (sizeof (*boot));
485     if (boot == NULL)
486         return errno;
487     boot->entry = func;
488     boot->object = p_this;
489
490     vlc_mutex_lock( &p_this->object_lock );
491
492 #if defined( LIBVLC_USE_PTHREAD )
493     sigset_t set, oldset;
494
495     /* We really don't want signals to (literaly) interrupt our blocking I/O
496      * system calls. SIGPIPE is especially bad, as it can be caused by remote
497      * peers through connected sockets. Generally, we cannot know which signals
498      * are handled by the main program. Also, external LibVLC bindings tend not
499      * to setup a proper signal mask before invoking LibVLC.
500      * Hence, we hereby block all signals, except those for which blocking is
501      * undefined, as listed below. Note that SIGKILL and SIGSTOP need not be
502      * listed (see the documentation for pthread_sigmask) here. */
503     sigfillset (&set);
504     sigdelset (&set, SIGFPE);
505     sigdelset (&set, SIGILL);
506     sigdelset (&set, SIGSEGV);
507     sigdelset (&set, SIGBUS);
508     pthread_sigmask (SIG_BLOCK, &set, &oldset);
509
510     i_ret = pthread_create( &p_priv->thread_id, NULL, thread_entry, boot );
511     pthread_sigmask (SIG_SETMASK, &oldset, NULL);
512
513 #ifndef __APPLE__
514     if( config_GetInt( p_this, "rt-priority" ) > 0 )
515 #endif
516     {
517         int i_error, i_policy;
518         struct sched_param param;
519
520         memset( &param, 0, sizeof(struct sched_param) );
521         if( config_GetType( p_this, "rt-offset" ) )
522             i_priority += config_GetInt( p_this, "rt-offset" );
523         if( i_priority <= 0 )
524         {
525             param.sched_priority = (-1) * i_priority;
526             i_policy = SCHED_OTHER;
527         }
528         else
529         {
530             param.sched_priority = i_priority;
531             i_policy = SCHED_RR;
532         }
533         if( (i_error = pthread_setschedparam( p_priv->thread_id,
534                                                i_policy, &param )) )
535         {
536             errno = i_error;
537             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
538                       psz_file, i_line );
539             i_priority = 0;
540         }
541     }
542 #ifndef __APPLE__
543     else
544         i_priority = 0;
545 #endif
546
547 #elif defined( WIN32 ) || defined( UNDER_CE )
548     {
549         /* When using the MSVCRT C library you have to use the _beginthreadex
550          * function instead of CreateThread, otherwise you'll end up with
551          * memory leaks and the signal functions not working (see Microsoft
552          * Knowledge Base, article 104641) */
553 #if defined( UNDER_CE )
554         HANDLE hThread = CreateThread( NULL, 0, thread_entry,
555                                        (LPVOID)boot, CREATE_SUSPENDED,
556                                         NULL );
557 #else
558         HANDLE hThread = (HANDLE)(uintptr_t)
559             _beginthreadex( NULL, 0, thread_entry, boot,
560                             CREATE_SUSPENDED, NULL );
561 #endif
562         p_priv->thread_id = hThread;
563         ResumeThread(hThread);
564     }
565
566     i_ret = ( p_priv->thread_id ? 0 : errno );
567
568     if( !i_ret && i_priority )
569     {
570         if( !SetThreadPriority(p_priv->thread_id, i_priority) )
571         {
572             msg_Warn( p_this, "couldn't set a faster priority" );
573             i_priority = 0;
574         }
575     }
576
577 #elif defined( HAVE_KERNEL_SCHEDULER_H )
578     p_priv->thread_id = spawn_thread( (thread_func)thread_entry, psz_name,
579                                       i_priority, p_data );
580     i_ret = resume_thread( p_priv->thread_id );
581
582 #endif
583
584     if( i_ret == 0 )
585     {
586         if( b_wait )
587         {
588             msg_Dbg( p_this, "waiting for thread completion" );
589             vlc_object_wait( p_this );
590         }
591
592         p_priv->b_thread = true;
593         msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
594                  (unsigned long)p_priv->thread_id, psz_name, i_priority,
595                  psz_file, i_line );
596     }
597     else
598     {
599         errno = i_ret;
600         msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
601                          psz_name, psz_file, i_line );
602     }
603
604     vlc_mutex_unlock( &p_this->object_lock );
605     return i_ret;
606 }
607
608 /*****************************************************************************
609  * vlc_thread_set_priority: set the priority of the current thread when we
610  * couldn't set it in vlc_thread_create (for instance for the main thread)
611  *****************************************************************************/
612 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
613                                int i_line, int i_priority )
614 {
615     vlc_object_internals_t *p_priv = vlc_internals( p_this );
616
617 #if defined( LIBVLC_USE_PTHREAD )
618 # ifndef __APPLE__
619     if( config_GetInt( p_this, "rt-priority" ) > 0 )
620 # endif
621     {
622         int i_error, i_policy;
623         struct sched_param param;
624
625         memset( &param, 0, sizeof(struct sched_param) );
626         if( config_GetType( p_this, "rt-offset" ) )
627             i_priority += config_GetInt( p_this, "rt-offset" );
628         if( i_priority <= 0 )
629         {
630             param.sched_priority = (-1) * i_priority;
631             i_policy = SCHED_OTHER;
632         }
633         else
634         {
635             param.sched_priority = i_priority;
636             i_policy = SCHED_RR;
637         }
638         if( !p_priv->thread_id )
639             p_priv->thread_id = pthread_self();
640         if( (i_error = pthread_setschedparam( p_priv->thread_id,
641                                                i_policy, &param )) )
642         {
643             errno = i_error;
644             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
645                       psz_file, i_line );
646             i_priority = 0;
647         }
648     }
649
650 #elif defined( WIN32 ) || defined( UNDER_CE )
651     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
652
653     if( !p_priv->thread_id )
654         p_priv->thread_id = GetCurrentThread();
655     if( !SetThreadPriority(p_priv->thread_id, i_priority) )
656     {
657         msg_Warn( p_this, "couldn't set a faster priority" );
658         return 1;
659     }
660
661 #endif
662
663     return 0;
664 }
665
666 /*****************************************************************************
667  * vlc_thread_ready: tell the parent thread we were successfully spawned
668  *****************************************************************************/
669 void __vlc_thread_ready( vlc_object_t *p_this )
670 {
671     vlc_object_signal( p_this );
672 }
673
674 /*****************************************************************************
675  * vlc_thread_join: wait until a thread exits, inner version
676  *****************************************************************************/
677 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
678 {
679     vlc_object_internals_t *p_priv = vlc_internals( p_this );
680     int i_ret = 0;
681
682 #if defined( LIBVLC_USE_PTHREAD )
683     /* Make sure we do return if we are calling vlc_thread_join()
684      * from the joined thread */
685     if (pthread_equal (pthread_self (), p_priv->thread_id))
686         i_ret = pthread_detach (p_priv->thread_id);
687     else
688         i_ret = pthread_join (p_priv->thread_id, NULL);
689
690 #elif defined( UNDER_CE ) || defined( WIN32 )
691     HMODULE hmodule;
692     BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
693                                       FILETIME*, FILETIME* );
694     FILETIME create_ft, exit_ft, kernel_ft, user_ft;
695     int64_t real_time, kernel_time, user_time;
696     HANDLE hThread;
697
698     /*
699     ** object will close its thread handle when destroyed, duplicate it here
700     ** to be on the safe side
701     */
702     if( ! DuplicateHandle(GetCurrentProcess(),
703             p_priv->thread_id,
704             GetCurrentProcess(),
705             &hThread,
706             0,
707             FALSE,
708             DUPLICATE_SAME_ACCESS) )
709     {
710         p_priv->b_thread = false;
711         i_ret = GetLastError();
712         goto error;
713     }
714
715     WaitForSingleObject( hThread, INFINITE );
716
717 #if defined( UNDER_CE )
718     hmodule = GetModuleHandle( _T("COREDLL") );
719 #else
720     hmodule = GetModuleHandle( _T("KERNEL32") );
721 #endif
722     OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
723                                          FILETIME*, FILETIME* ))
724         GetProcAddress( hmodule, _T("GetThreadTimes") );
725
726     if( OurGetThreadTimes &&
727         OurGetThreadTimes( hThread,
728                            &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
729     {
730         real_time =
731           ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
732           ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
733         real_time /= 10;
734
735         kernel_time =
736           ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
737            kernel_ft.dwLowDateTime) / 10;
738
739         user_time =
740           ((((int64_t)user_ft.dwHighDateTime)<<32)|
741            user_ft.dwLowDateTime) / 10;
742
743         msg_Dbg( p_this, "thread times: "
744                  "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
745                  real_time/60/1000000,
746                  (double)((real_time%(60*1000000))/1000000.0),
747                  kernel_time/60/1000000,
748                  (double)((kernel_time%(60*1000000))/1000000.0),
749                  user_time/60/1000000,
750                  (double)((user_time%(60*1000000))/1000000.0) );
751     }
752     CloseHandle( hThread );
753 error:
754
755 #elif defined( HAVE_KERNEL_SCHEDULER_H )
756     int32_t exit_value;
757     i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
758
759 #endif
760
761     if( i_ret )
762     {
763         errno = i_ret;
764         msg_Err( p_this, "thread_join(%lu) failed at %s:%d (%m)",
765                          (unsigned long)p_priv->thread_id, psz_file, i_line );
766     }
767     else
768         msg_Dbg( p_this, "thread %lu joined (%s:%d)",
769                          (unsigned long)p_priv->thread_id, psz_file, i_line );
770
771     p_priv->b_thread = false;
772 }