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