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