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