]> git.sesse.net Git - vlc/blob - src/misc/threads.c
Do not leak the global libvlc pointer
[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
187     if( i_initializations == 1 )
188         vlc_object_release( p_root );
189     i_initializations--;
190
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_unlock( &once_mutex );
196 #endif
197 }
198
199 #ifdef __linux__
200 /* This is not prototyped under Linux, though it exists. */
201 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
202 #endif
203
204 /*****************************************************************************
205  * vlc_mutex_init: initialize a mutex
206  *****************************************************************************/
207 int vlc_mutex_init( vlc_mutex_t *p_mutex )
208 {
209 #if defined( UNDER_CE )
210     InitializeCriticalSection( &p_mutex->csection );
211     return 0;
212
213 #elif defined( WIN32 )
214     *p_mutex = CreateMutex( 0, FALSE, 0 );
215     return (*p_mutex != NULL) ? 0 : ENOMEM;
216
217 #elif defined( HAVE_KERNEL_SCHEDULER_H )
218     /* check the arguments and whether it's already been initialized */
219     if( p_mutex == NULL )
220     {
221         return B_BAD_VALUE;
222     }
223
224     if( p_mutex->init == 9999 )
225     {
226         return EALREADY;
227     }
228
229     p_mutex->lock = create_sem( 1, "BeMutex" );
230     if( p_mutex->lock < B_NO_ERROR )
231     {
232         return( -1 );
233     }
234
235     p_mutex->init = 9999;
236     return B_OK;
237
238 #elif defined( LIBVLC_USE_PTHREAD )
239     pthread_mutexattr_t attr;
240     int                 i_result;
241
242     pthread_mutexattr_init( &attr );
243
244 # ifndef NDEBUG
245     /* Create error-checking mutex to detect problems more easily. */
246 #  if defined(SYS_LINUX)
247     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
248 #  else
249     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
250 #  endif
251 # endif
252     i_result = pthread_mutex_init( p_mutex, &attr );
253     pthread_mutexattr_destroy( &attr );
254     return i_result;
255 #endif
256 }
257
258 /*****************************************************************************
259  * vlc_mutex_init: initialize a recursive mutex (Do not use)
260  *****************************************************************************/
261 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
262 {
263 #if defined( WIN32 )
264     /* Create mutex returns a recursive mutex */
265     *p_mutex = CreateMutex( 0, FALSE, 0 );
266     return (*p_mutex != NULL) ? 0 : ENOMEM;
267 #elif defined( LIBVLC_USE_PTHREAD )
268     pthread_mutexattr_t attr;
269     int                 i_result;
270
271     pthread_mutexattr_init( &attr );
272 # ifndef NDEBUG
273     /* Create error-checking mutex to detect problems more easily. */
274 #   if defined(SYS_LINUX)
275     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
276 #   else
277     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
278 #   endif
279 # endif
280     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
281     i_result = pthread_mutex_init( p_mutex, &attr );
282     pthread_mutexattr_destroy( &attr );
283     return( i_result );
284 #else
285 # error Unimplemented!
286 #endif
287 }
288
289
290 /*****************************************************************************
291  * vlc_mutex_destroy: destroy a mutex, inner version
292  *****************************************************************************/
293 void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
294 {
295 #if defined( UNDER_CE )
296     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
297
298     DeleteCriticalSection( &p_mutex->csection );
299
300 #elif defined( WIN32 )
301     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
302
303     CloseHandle( *p_mutex );
304
305 #elif defined( HAVE_KERNEL_SCHEDULER_H )
306     if( p_mutex->init == 9999 )
307         delete_sem( p_mutex->lock );
308
309     p_mutex->init = 0;
310
311 #elif defined( LIBVLC_USE_PTHREAD )
312     int val = pthread_mutex_destroy( p_mutex );
313     VLC_THREAD_ASSERT ("destroying mutex");
314
315 #endif
316 }
317
318 /*****************************************************************************
319  * vlc_cond_init: initialize a condition
320  *****************************************************************************/
321 int __vlc_cond_init( vlc_cond_t *p_condvar )
322 {
323 #if defined( UNDER_CE ) || defined( WIN32 )
324     /* Initialize counter */
325     p_condvar->i_waiting_threads = 0;
326
327     /* Create an auto-reset event. */
328     p_condvar->event = CreateEvent( NULL,   /* no security */
329                                     FALSE,  /* auto-reset event */
330                                     FALSE,  /* start non-signaled */
331                                     NULL ); /* unnamed */
332     return !p_condvar->event;
333
334 #elif defined( HAVE_KERNEL_SCHEDULER_H )
335     if( !p_condvar )
336     {
337         return B_BAD_VALUE;
338     }
339
340     if( p_condvar->init == 9999 )
341     {
342         return EALREADY;
343     }
344
345     p_condvar->thread = -1;
346     p_condvar->init = 9999;
347     return 0;
348
349 #elif defined( LIBVLC_USE_PTHREAD )
350     pthread_condattr_t attr;
351     int ret;
352
353     ret = pthread_condattr_init (&attr);
354     if (ret)
355         return ret;
356
357 # if !defined (_POSIX_CLOCK_SELECTION)
358    /* Fairly outdated POSIX support (that was defined in 2001) */
359 #  define _POSIX_CLOCK_SELECTION (-1)
360 # endif
361 # if (_POSIX_CLOCK_SELECTION >= 0)
362     /* NOTE: This must be the same clock as the one in mtime.c */
363     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
364 # endif
365
366     ret = pthread_cond_init (p_condvar, &attr);
367     pthread_condattr_destroy (&attr);
368     return ret;
369
370 #endif
371 }
372
373 /*****************************************************************************
374  * vlc_cond_destroy: destroy a condition, inner version
375  *****************************************************************************/
376 void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
377 {
378 #if defined( UNDER_CE ) || defined( WIN32 )
379     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
380
381     CloseHandle( p_condvar->event );
382
383 #elif defined( HAVE_KERNEL_SCHEDULER_H )
384     p_condvar->init = 0;
385
386 #elif defined( LIBVLC_USE_PTHREAD )
387     int val = pthread_cond_destroy( p_condvar );
388     VLC_THREAD_ASSERT ("destroying condition");
389
390 #endif
391 }
392
393 /*****************************************************************************
394  * vlc_tls_create: create a thread-local variable
395  *****************************************************************************/
396 int __vlc_threadvar_create( vlc_threadvar_t *p_tls )
397 {
398     int i_ret = -1;
399
400 #if defined( HAVE_KERNEL_SCHEDULER_H )
401 # error Unimplemented!
402 #elif defined( UNDER_CE )
403 #elif defined( WIN32 )
404     *p_tls = TlsAlloc();
405     i_ret = (*p_tls == INVALID_HANDLE_VALUE) ? EAGAIN : 0;
406
407 #elif defined( LIBVLC_USE_PTHREAD )
408     i_ret =  pthread_key_create( p_tls, NULL );
409 #endif
410     return i_ret;
411 }
412
413 /*****************************************************************************
414  * vlc_thread_create: create a thread, inner version
415  *****************************************************************************
416  * Note that i_priority is only taken into account on platforms supporting
417  * userland real-time priority threads.
418  *****************************************************************************/
419 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
420                          const char *psz_name, void * ( *func ) ( void * ),
421                          int i_priority, bool b_wait )
422 {
423     int i_ret;
424     void *p_data = (void *)p_this;
425     vlc_object_internals_t *p_priv = vlc_internals( p_this );
426
427     vlc_mutex_lock( &p_this->object_lock );
428
429 #if defined( WIN32 ) || defined( UNDER_CE )
430     {
431         /* When using the MSVCRT C library you have to use the _beginthreadex
432          * function instead of CreateThread, otherwise you'll end up with
433          * memory leaks and the signal functions not working (see Microsoft
434          * Knowledge Base, article 104641) */
435 #if defined( UNDER_CE )
436         DWORD  threadId;
437         HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func,
438                                       (LPVOID)p_data, CREATE_SUSPENDED,
439                       &threadId );
440 #else
441         unsigned threadId;
442         uintptr_t hThread = _beginthreadex( NULL, 0,
443                         (LPTHREAD_START_ROUTINE)func,
444                                             (void*)p_data, CREATE_SUSPENDED,
445                         &threadId );
446 #endif
447         p_priv->thread_id.id = (DWORD)threadId;
448         p_priv->thread_id.hThread = (HANDLE)hThread;
449         ResumeThread((HANDLE)hThread);
450     }
451
452     i_ret = ( p_priv->thread_id.hThread ? 0 : 1 );
453
454     if( !i_ret && i_priority )
455     {
456         if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
457         {
458             msg_Warn( p_this, "couldn't set a faster priority" );
459             i_priority = 0;
460         }
461     }
462
463 #elif defined( HAVE_KERNEL_SCHEDULER_H )
464     p_priv->thread_id = spawn_thread( (thread_func)func, psz_name,
465                                       i_priority, p_data );
466     i_ret = resume_thread( p_priv->thread_id );
467
468 #elif defined( LIBVLC_USE_PTHREAD )
469     i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data );
470
471 #ifndef __APPLE__
472     if( config_GetInt( p_this, "rt-priority" ) )
473 #endif
474     {
475         int i_error, i_policy;
476         struct sched_param param;
477
478         memset( &param, 0, sizeof(struct sched_param) );
479         if( config_GetType( p_this, "rt-offset" ) )
480         {
481             i_priority += config_GetInt( p_this, "rt-offset" );
482         }
483         if( i_priority <= 0 )
484         {
485             param.sched_priority = (-1) * i_priority;
486             i_policy = SCHED_OTHER;
487         }
488         else
489         {
490             param.sched_priority = i_priority;
491             i_policy = SCHED_RR;
492         }
493         if( (i_error = pthread_setschedparam( p_priv->thread_id,
494                                                i_policy, &param )) )
495         {
496             errno = i_error;
497             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
498                       psz_file, i_line );
499             i_priority = 0;
500         }
501     }
502 #ifndef __APPLE__
503     else
504     {
505         i_priority = 0;
506     }
507 #endif
508
509 #endif
510
511     if( i_ret == 0 )
512     {
513         if( b_wait )
514         {
515             msg_Dbg( p_this, "waiting for thread completion" );
516             vlc_object_wait( p_this );
517         }
518
519         p_priv->b_thread = true;
520
521 #if defined( WIN32 ) || defined( UNDER_CE )
522         msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
523                  (unsigned int)p_priv->thread_id.id, psz_name,
524          i_priority, psz_file, i_line );
525 #else
526         msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)",
527                  (unsigned int)p_priv->thread_id, psz_name, i_priority,
528                  psz_file, i_line );
529 #endif
530
531
532         vlc_mutex_unlock( &p_this->object_lock );
533     }
534     else
535     {
536         errno = i_ret;
537         msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
538                          psz_name, psz_file, i_line );
539         vlc_mutex_unlock( &p_this->object_lock );
540     }
541
542     return i_ret;
543 }
544
545 /*****************************************************************************
546  * vlc_thread_set_priority: set the priority of the current thread when we
547  * couldn't set it in vlc_thread_create (for instance for the main thread)
548  *****************************************************************************/
549 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
550                                int i_line, int i_priority )
551 {
552     vlc_object_internals_t *p_priv = vlc_internals( p_this );
553 #if defined( WIN32 ) || defined( UNDER_CE )
554     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
555
556     if( !p_priv->thread_id.hThread )
557         p_priv->thread_id.hThread = GetCurrentThread();
558     if( !SetThreadPriority(p_priv->thread_id.hThread, i_priority) )
559     {
560         msg_Warn( p_this, "couldn't set a faster priority" );
561         return 1;
562     }
563
564 #elif defined( LIBVLC_USE_PTHREAD )
565 # ifndef __APPLE__
566     if( config_GetInt( p_this, "rt-priority" ) > 0 )
567 # endif
568     {
569         int i_error, i_policy;
570         struct sched_param param;
571
572         memset( &param, 0, sizeof(struct sched_param) );
573         if( config_GetType( p_this, "rt-offset" ) )
574         {
575             i_priority += config_GetInt( p_this, "rt-offset" );
576         }
577         if( i_priority <= 0 )
578         {
579             param.sched_priority = (-1) * i_priority;
580             i_policy = SCHED_OTHER;
581         }
582         else
583         {
584             param.sched_priority = i_priority;
585             i_policy = SCHED_RR;
586         }
587         if( !p_priv->thread_id )
588             p_priv->thread_id = pthread_self();
589         if( (i_error = pthread_setschedparam( p_priv->thread_id,
590                                                i_policy, &param )) )
591         {
592             errno = i_error;
593             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
594                       psz_file, i_line );
595             i_priority = 0;
596         }
597     }
598 #endif
599
600     return 0;
601 }
602
603 /*****************************************************************************
604  * vlc_thread_ready: tell the parent thread we were successfully spawned
605  *****************************************************************************/
606 void __vlc_thread_ready( vlc_object_t *p_this )
607 {
608     vlc_object_signal( p_this );
609 }
610
611 /*****************************************************************************
612  * vlc_thread_join: wait until a thread exits, inner version
613  *****************************************************************************/
614 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
615 {
616     vlc_object_internals_t *p_priv = vlc_internals( p_this );
617
618 #if defined( UNDER_CE ) || defined( WIN32 )
619     HMODULE hmodule;
620     BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
621                                       FILETIME*, FILETIME* );
622     FILETIME create_ft, exit_ft, kernel_ft, user_ft;
623     int64_t real_time, kernel_time, user_time;
624     HANDLE hThread;
625
626     /*
627     ** object will close its thread handle when destroyed, duplicate it here
628     ** to be on the safe side
629     */
630     if( ! DuplicateHandle(GetCurrentProcess(),
631             p_priv->thread_id.hThread,
632             GetCurrentProcess(),
633             &hThread,
634             0,
635             FALSE,
636             DUPLICATE_SAME_ACCESS) )
637     {
638         msg_Err( p_this, "thread_join(%u) failed at %s:%d (%u)",
639                          (unsigned int)p_priv->thread_id.id,
640              psz_file, i_line, (unsigned int)GetLastError() );
641         p_priv->b_thread = false;
642         return;
643     }
644
645     WaitForSingleObject( hThread, INFINITE );
646
647     msg_Dbg( p_this, "thread %u joined (%s:%d)",
648              (unsigned int)p_priv->thread_id.id,
649              psz_file, i_line );
650 #if defined( UNDER_CE )
651     hmodule = GetModuleHandle( _T("COREDLL") );
652 #else
653     hmodule = GetModuleHandle( _T("KERNEL32") );
654 #endif
655     OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
656                                          FILETIME*, FILETIME* ))
657         GetProcAddress( hmodule, _T("GetThreadTimes") );
658
659     if( OurGetThreadTimes &&
660         OurGetThreadTimes( hThread,
661                            &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
662     {
663         real_time =
664           ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
665           ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
666         real_time /= 10;
667
668         kernel_time =
669           ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
670            kernel_ft.dwLowDateTime) / 10;
671
672         user_time =
673           ((((int64_t)user_ft.dwHighDateTime)<<32)|
674            user_ft.dwLowDateTime) / 10;
675
676         msg_Dbg( p_this, "thread times: "
677                  "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
678                  real_time/60/1000000,
679                  (double)((real_time%(60*1000000))/1000000.0),
680                  kernel_time/60/1000000,
681                  (double)((kernel_time%(60*1000000))/1000000.0),
682                  user_time/60/1000000,
683                  (double)((user_time%(60*1000000))/1000000.0) );
684     }
685     CloseHandle( hThread );
686
687 #else /* !defined(WIN32) */
688
689     int i_ret = 0;
690
691 #if defined( HAVE_KERNEL_SCHEDULER_H )
692     int32_t exit_value;
693     i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
694
695 #elif defined( LIBVLC_USE_PTHREAD )
696     /* Make sure we do return if we are calling vlc_thread_join()
697      * from the joined thread */
698     if (pthread_equal (pthread_self (), p_priv->thread_id))
699         i_ret = pthread_detach (p_priv->thread_id);
700     else
701         i_ret = pthread_join (p_priv->thread_id, NULL);
702 #endif
703
704     if( i_ret )
705     {
706         errno = i_ret;
707         msg_Err( p_this, "thread_join(%u) failed at %s:%d (%m)",
708                          (unsigned int)p_priv->thread_id, psz_file, i_line );
709     }
710     else
711     {
712         msg_Dbg( p_this, "thread %u joined (%s:%d)",
713                          (unsigned int)p_priv->thread_id, psz_file, i_line );
714     }
715
716 #endif
717
718     p_priv->b_thread = false;
719 }
720