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