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