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