1 /*****************************************************************************
2 * threads.c : threads implementation for the VideoLAN client
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
7 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
32 #include <vlc_common.h>
42 #if defined( LIBVLC_USE_PTHREAD )
45 static vlc_threadvar_t cancel_key;
50 vlc_dictionary_t list;
54 #ifdef LIBVLC_USE_PTHREAD
55 PTHREAD_MUTEX_INITIALIZER,
59 #ifdef HAVE_EXECINFO_H
60 # include <execinfo.h>
64 * Print a backtrace to the standard error for debugging purpose.
66 void vlc_trace (const char *fn, const char *file, unsigned line)
68 fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
69 fflush (stderr); /* needed before switch to low-level I/O */
72 int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
73 backtrace_symbols_fd (stack, len, 2);
80 static inline unsigned long vlc_threadid (void)
82 #if defined(LIBVLC_USE_PTHREAD)
83 union { pthread_t th; unsigned long int i; } v = { };
84 v.th = pthread_self ();
88 return GetCurrentThreadId ();
96 /*****************************************************************************
97 * vlc_thread_fatal: Report an error from the threading layer
98 *****************************************************************************
99 * This is mostly meant for debugging.
100 *****************************************************************************/
102 vlc_thread_fatal (const char *action, int error,
103 const char *function, const char *file, unsigned line)
105 fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
106 action, error, vlc_threadid ());
107 vlc_trace (function, file, line);
109 /* Sometimes strerror_r() crashes too, so make sure we print an error
110 * message before we invoke it */
112 /* Avoid the strerror_r() prototype brain damage in glibc */
114 fprintf (stderr, " Error message: %m\n");
115 #elif !defined (WIN32)
119 switch (strerror_r (error, buf, sizeof (buf)))
124 case ERANGE: /* should never happen */
125 msg = "unknwon (too big to display)";
128 msg = "unknown (invalid error number)";
131 fprintf (stderr, " Error message: %s\n", msg);
139 # define VLC_THREAD_ASSERT( action ) \
140 if (val) vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__)
142 # define VLC_THREAD_ASSERT( action ) ((void)val)
146 * Per-thread cancellation data
148 #ifndef LIBVLC_USE_PTHREAD_CANCEL
149 typedef struct vlc_cancel_t
151 vlc_cleanup_t *cleaners;
156 # define VLC_CANCEL_INIT { NULL, true, false }
160 BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
167 case DLL_PROCESS_ATTACH:
168 vlc_dictionary_init (&named_mutexes.list, 0);
169 vlc_mutex_init (&named_mutexes.lock);
170 vlc_threadvar_create (&cancel_key, free);
173 case DLL_PROCESS_DETACH:
174 vlc_threadvar_delete( &cancel_key );
175 vlc_mutex_destroy (&named_mutexes.lock);
176 vlc_dictionary_clear (&named_mutexes.list);
183 #if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
184 /* This is not prototyped under glibc, though it exists. */
185 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
188 /*****************************************************************************
189 * vlc_mutex_init: initialize a mutex
190 *****************************************************************************/
191 int vlc_mutex_init( vlc_mutex_t *p_mutex )
193 #if defined( LIBVLC_USE_PTHREAD )
194 pthread_mutexattr_t attr;
197 pthread_mutexattr_init( &attr );
200 /* Create error-checking mutex to detect problems more easily. */
201 # if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
202 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
204 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
207 i_result = pthread_mutex_init( p_mutex, &attr );
208 pthread_mutexattr_destroy( &attr );
211 #elif defined( WIN32 )
212 /* This creates a recursive mutex. This is OK as fast mutexes have
213 * no defined behavior in case of recursive locking. */
214 InitializeCriticalSection (&p_mutex->mutex);
220 /*****************************************************************************
221 * vlc_mutex_init: initialize a recursive mutex (Do not use)
222 *****************************************************************************/
223 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
225 #if defined( LIBVLC_USE_PTHREAD )
226 pthread_mutexattr_t attr;
229 pthread_mutexattr_init( &attr );
230 # if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
231 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
233 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
235 i_result = pthread_mutex_init( p_mutex, &attr );
236 pthread_mutexattr_destroy( &attr );
239 #elif defined( WIN32 )
240 InitializeCriticalSection( &p_mutex->mutex );
248 * Destroys a mutex. The mutex must not be locked.
250 * @param p_mutex mutex to destroy
251 * @return always succeeds
253 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
255 #if defined( LIBVLC_USE_PTHREAD )
256 int val = pthread_mutex_destroy( p_mutex );
257 VLC_THREAD_ASSERT ("destroying mutex");
259 #elif defined( WIN32 )
260 DeleteCriticalSection (&p_mutex->mutex);
266 * Acquires a mutex. If needed, waits for any other thread to release it.
267 * Beware of deadlocks when locking multiple mutexes at the same time,
268 * or when using mutexes from callbacks.
270 * @param p_mutex mutex initialized with vlc_mutex_init() or
271 * vlc_mutex_init_recursive()
273 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
275 #if defined(LIBVLC_USE_PTHREAD)
276 int val = pthread_mutex_lock( p_mutex );
277 VLC_THREAD_ASSERT ("locking mutex");
279 #elif defined( WIN32 )
280 EnterCriticalSection (&p_mutex->mutex);
287 * Releases a mutex (or crashes if the mutex is not locked by the caller).
288 * @param p_mutex mutex locked with vlc_mutex_lock().
290 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
292 #if defined(LIBVLC_USE_PTHREAD)
293 int val = pthread_mutex_unlock( p_mutex );
294 VLC_THREAD_ASSERT ("unlocking mutex");
296 #elif defined( WIN32 )
297 LeaveCriticalSection (&p_mutex->mutex);
302 /*****************************************************************************
303 * vlc_cond_init: initialize a condition variable
304 *****************************************************************************/
305 int vlc_cond_init( vlc_cond_t *p_condvar )
307 #if defined( LIBVLC_USE_PTHREAD )
308 pthread_condattr_t attr;
311 ret = pthread_condattr_init (&attr);
315 # if !defined (_POSIX_CLOCK_SELECTION)
316 /* Fairly outdated POSIX support (that was defined in 2001) */
317 # define _POSIX_CLOCK_SELECTION (-1)
319 # if (_POSIX_CLOCK_SELECTION >= 0)
320 /* NOTE: This must be the same clock as the one in mtime.c */
321 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
324 ret = pthread_cond_init (p_condvar, &attr);
325 pthread_condattr_destroy (&attr);
328 #elif defined( WIN32 )
329 /* Create a manual-reset event (manual reset is needed for broadcast). */
330 *p_condvar = CreateEvent( NULL, TRUE, FALSE, NULL );
331 return *p_condvar ? 0 : ENOMEM;
337 * Destroys a condition variable. No threads shall be waiting or signaling the
339 * @param p_condvar condition variable to destroy
341 void vlc_cond_destroy (vlc_cond_t *p_condvar)
343 #if defined( LIBVLC_USE_PTHREAD )
344 int val = pthread_cond_destroy( p_condvar );
345 VLC_THREAD_ASSERT ("destroying condition");
347 #elif defined( WIN32 )
348 CloseHandle( *p_condvar );
354 * Wakes up one thread waiting on a condition variable, if any.
355 * @param p_condvar condition variable
357 void vlc_cond_signal (vlc_cond_t *p_condvar)
359 #if defined(LIBVLC_USE_PTHREAD)
360 int val = pthread_cond_signal( p_condvar );
361 VLC_THREAD_ASSERT ("signaling condition variable");
363 #elif defined( WIN32 )
364 /* NOTE: This will cause a broadcast, that is wrong.
365 * This will also wake up the next waiting thread if no thread are yet
366 * waiting, which is also wrong. However both of these issues are allowed
367 * by the provision for spurious wakeups. Better have too many wakeups
368 * than too few (= deadlocks). */
369 SetEvent (*p_condvar);
375 * Wakes up all threads (if any) waiting on a condition variable.
376 * @param p_cond condition variable
378 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
380 #if defined (LIBVLC_USE_PTHREAD)
381 pthread_cond_broadcast (p_condvar);
383 #elif defined (WIN32)
384 SetEvent (*p_condvar);
391 # define WaitForMultipleObjectsEx(a,b,c) WaitForMultipleObjects(a,b)
395 * Waits for a condition variable. The calling thread will be suspended until
396 * another thread calls vlc_cond_signal() or vlc_cond_broadcast() on the same
397 * condition variable, the thread is cancelled with vlc_cancel(), or the
398 * system causes a "spurious" unsolicited wake-up.
400 * A mutex is needed to wait on a condition variable. It must <b>not</b> be
401 * a recursive mutex. Although it is possible to use the same mutex for
402 * multiple condition, it is not valid to use different mutexes for the same
403 * condition variable at the same time from different threads.
405 * In case of thread cancellation, the mutex is always locked before
406 * cancellation proceeds.
408 * The canonical way to use a condition variable to wait for event foobar is:
410 vlc_mutex_lock (&lock);
411 mutex_cleanup_push (&lock); // release the mutex in case of cancellation
414 vlc_cond_wait (&wait, &lock);
416 --- foobar is now true, do something about it here --
418 vlc_cleanup_run (); // release the mutex
421 * @param p_condvar condition variable to wait on
422 * @param p_mutex mutex which is unlocked while waiting,
423 * then locked again when waking up.
424 * @param deadline <b>absolute</b> timeout
426 * @return 0 if the condition was signaled, an error code in case of timeout.
428 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
430 #if defined(LIBVLC_USE_PTHREAD)
431 int val = pthread_cond_wait( p_condvar, p_mutex );
432 VLC_THREAD_ASSERT ("waiting on condition");
434 #elif defined( WIN32 )
440 LeaveCriticalSection (&p_mutex->mutex);
441 result = WaitForSingleObjectEx (*p_condvar, INFINITE, TRUE);
442 EnterCriticalSection (&p_mutex->mutex);
444 while (result == WAIT_IO_COMPLETION);
446 ResetEvent (*p_condvar);
452 * Waits for a condition variable up to a certain date.
453 * This works like vlc_cond_wait(), except for the additional timeout.
455 * @param p_condvar condition variable to wait on
456 * @param p_mutex mutex which is unlocked while waiting,
457 * then locked again when waking up.
458 * @param deadline <b>absolute</b> timeout
460 * @return 0 if the condition was signaled, an error code in case of timeout.
462 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
465 #if defined(LIBVLC_USE_PTHREAD)
466 lldiv_t d = lldiv( deadline, CLOCK_FREQ );
467 struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) };
469 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
470 if (val != ETIMEDOUT)
471 VLC_THREAD_ASSERT ("timed-waiting on condition");
474 #elif defined( WIN32 )
481 mtime_t total = (deadline - mdate ())/1000;
485 DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
486 LeaveCriticalSection (&p_mutex->mutex);
487 result = WaitForSingleObjectEx (*p_condvar, delay, TRUE);
488 EnterCriticalSection (&p_mutex->mutex);
490 while (result == WAIT_IO_COMPLETION);
492 ResetEvent (*p_condvar);
494 return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
499 /*****************************************************************************
500 * vlc_tls_create: create a thread-local variable
501 *****************************************************************************/
502 int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
506 #if defined( LIBVLC_USE_PTHREAD )
507 i_ret = pthread_key_create( p_tls, destr );
508 #elif defined( UNDER_CE )
510 #elif defined( WIN32 )
511 /* FIXME: remember/use the destr() callback and stop leaking whatever */
513 i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
515 # error Unimplemented!
520 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
522 #if defined( LIBVLC_USE_PTHREAD )
523 pthread_key_delete (*p_tls);
524 #elif defined( UNDER_CE )
525 #elif defined( WIN32 )
528 # error Unimplemented!
532 #if defined (LIBVLC_USE_PTHREAD)
533 #elif defined (WIN32)
534 static unsigned __stdcall vlc_entry (void *data)
536 vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
537 vlc_thread_t self = data;
539 vlc_threadvar_set (&cancel_key, &cancel_data);
540 self->data = self->entry (self->data);
546 * Creates and starts new thread.
548 * @param p_handle [OUT] pointer to write the handle of the created thread to
549 * @param entry entry point for the thread
550 * @param data data parameter given to the entry point
551 * @param priority thread priority value
552 * @return 0 on success, a standard error code on error.
554 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
559 #if defined( LIBVLC_USE_PTHREAD )
561 pthread_attr_init (&attr);
563 /* Block the signals that signals interface plugin handles.
564 * If the LibVLC caller wants to handle some signals by itself, it should
565 * block these before whenever invoking LibVLC. And it must obviously not
566 * start the VLC signals interface plugin.
568 * LibVLC will normally ignore any interruption caused by an asynchronous
569 * signal during a system call. But there may well be some buggy cases
570 * where it fails to handle EINTR (bug reports welcome). Some underlying
571 * libraries might also not handle EINTR properly.
577 sigdelset (&set, SIGHUP);
578 sigaddset (&set, SIGINT);
579 sigaddset (&set, SIGQUIT);
580 sigaddset (&set, SIGTERM);
582 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
583 pthread_sigmask (SIG_BLOCK, &set, &oldset);
586 struct sched_param sp = { .sched_priority = priority, };
589 if (sp.sched_priority <= 0)
590 sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
592 sp.sched_priority += sched_get_priority_min (policy = SCHED_RR);
594 pthread_attr_setschedpolicy (&attr, policy);
595 pthread_attr_setschedparam (&attr, &sp);
598 /* The thread stack size.
599 * The lower the value, the less address space per thread, the highest
600 * maximum simultaneous threads per process. Too low values will cause
601 * stack overflows and weird crashes. Set with caution. Also keep in mind
602 * that 64-bits platforms consume more stack than 32-bits one.
604 * Thanks to on-demand paging, thread stack size only affects address space
605 * consumption. In terms of memory, threads only use what they need
606 * (rounded up to the page boundary).
608 * For example, on Linux i386, the default is 2 mega-bytes, which supports
609 * about 320 threads per processes. */
610 #define VLC_STACKSIZE (128 * sizeof (void *) * 1024)
613 ret = pthread_attr_setstacksize (&attr, VLC_STACKSIZE);
614 assert (ret == 0); /* fails iif VLC_STACKSIZE is invalid */
617 ret = pthread_create (p_handle, &attr, entry, data);
618 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
619 pthread_attr_destroy (&attr);
621 #elif defined( WIN32 ) || defined( UNDER_CE )
622 /* When using the MSVCRT C library you have to use the _beginthreadex
623 * function instead of CreateThread, otherwise you'll end up with
624 * memory leaks and the signal functions not working (see Microsoft
625 * Knowledge Base, article 104641) */
627 vlc_thread_t th = malloc (sizeof (*th));
634 #if defined( UNDER_CE )
635 hThread = CreateThread (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
637 hThread = (HANDLE)(uintptr_t)
638 _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
643 /* Thread closes the handle when exiting, duplicate it here
644 * to be on the safe side when joining. */
645 if (!DuplicateHandle (GetCurrentProcess (), hThread,
646 GetCurrentProcess (), &th->handle, 0, FALSE,
647 DUPLICATE_SAME_ACCESS))
649 CloseHandle (hThread);
654 ResumeThread (hThread);
656 SetThreadPriority (hThread, priority);
672 /* APC procedure for thread cancellation */
673 static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
676 vlc_control_cancel (VLC_DO_CANCEL);
681 * Marks a thread as cancelled. Next time the target thread reaches a
682 * cancellation point (while not having disabled cancellation), it will
683 * run its cancellation cleanup handler, the thread variable destructors, and
684 * terminate. vlc_join() must be used afterward regardless of a thread being
687 void vlc_cancel (vlc_thread_t thread_id)
689 #if defined (LIBVLC_USE_PTHREAD_CANCEL)
690 pthread_cancel (thread_id);
691 #elif defined (WIN32)
692 QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
694 # warning vlc_cancel is not implemented!
699 * Waits for a thread to complete (if needed), and destroys it.
700 * This is a cancellation point; in case of cancellation, the join does _not_
703 * @param handle thread handle
704 * @param p_result [OUT] pointer to write the thread return value or NULL
705 * @return 0 on success, a standard error code otherwise.
707 void vlc_join (vlc_thread_t handle, void **result)
709 #if defined( LIBVLC_USE_PTHREAD )
710 int val = pthread_join (handle, result);
712 vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__);
714 #elif defined( UNDER_CE ) || defined( WIN32 )
717 while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
718 == WAIT_IO_COMPLETION);
720 CloseHandle (handle->handle);
722 *result = handle->data;
729 struct vlc_thread_boot
731 void * (*entry) (vlc_object_t *);
732 vlc_object_t *object;
735 static void *thread_entry (void *data)
737 vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
738 void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
741 msg_Dbg (obj, "thread started");
743 msg_Dbg (obj, "thread ended");
748 /*****************************************************************************
749 * vlc_thread_create: create a thread, inner version
750 *****************************************************************************
751 * Note that i_priority is only taken into account on platforms supporting
752 * userland real-time priority threads.
753 *****************************************************************************/
754 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
755 const char *psz_name, void * ( *func ) ( vlc_object_t * ),
756 int i_priority, bool b_wait )
759 vlc_object_internals_t *p_priv = vlc_internals( p_this );
761 struct vlc_thread_boot *boot = malloc (sizeof (*boot));
765 boot->object = p_this;
767 /* Make sure we don't re-create a thread if the object has already one */
768 assert( !p_priv->b_thread );
770 #if defined( LIBVLC_USE_PTHREAD )
772 sem_init (&p_priv->thread_ready, 0, 0);
774 if( config_GetInt( p_this, "rt-priority" ) > 0 )
777 /* Hack to avoid error msg */
778 if( config_GetType( p_this, "rt-offset" ) )
779 i_priority += config_GetInt( p_this, "rt-offset" );
781 #elif defined (WIN32)
783 p_priv->thread_ready = CreateEvent (NULL, TRUE, FALSE, NULL);
786 p_priv->b_thread = true;
787 i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority );
790 msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
791 (unsigned long)p_priv->thread_id, psz_name, i_priority,
795 msg_Dbg( p_this, "waiting for thread initialization" );
796 #if defined (LIBVLC_USE_PTHREAD)
797 sem_wait (&p_priv->thread_ready);
798 sem_destroy (&p_priv->thread_ready);
799 #elif defined (WIN32)
800 WaitForSingleObject (p_priv->thread_ready, INFINITE);
801 CloseHandle (p_priv->thread_ready);
807 p_priv->b_thread = false;
809 msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
810 psz_name, psz_file, i_line );
816 #undef vlc_thread_ready
817 void vlc_thread_ready (vlc_object_t *obj)
819 vlc_object_internals_t *priv = vlc_internals (obj);
821 assert (priv->b_thread);
822 #if defined (LIBVLC_USE_PTHREAD)
823 assert (pthread_equal (pthread_self (), priv->thread_id));
824 sem_post (&priv->thread_ready);
825 #elif defined (WIN32)
826 SetEvent (priv->thread_ready);
830 /*****************************************************************************
831 * vlc_thread_set_priority: set the priority of the current thread when we
832 * couldn't set it in vlc_thread_create (for instance for the main thread)
833 *****************************************************************************/
834 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
835 int i_line, int i_priority )
837 vlc_object_internals_t *p_priv = vlc_internals( p_this );
839 if( !p_priv->b_thread )
841 msg_Err( p_this, "couldn't set priority of non-existent thread" );
845 #if defined( LIBVLC_USE_PTHREAD )
847 if( config_GetInt( p_this, "rt-priority" ) > 0 )
850 int i_error, i_policy;
851 struct sched_param param;
853 memset( ¶m, 0, sizeof(struct sched_param) );
854 if( config_GetType( p_this, "rt-offset" ) )
855 i_priority += config_GetInt( p_this, "rt-offset" );
856 if( i_priority <= 0 )
858 param.sched_priority = (-1) * i_priority;
859 i_policy = SCHED_OTHER;
863 param.sched_priority = i_priority;
866 if( (i_error = pthread_setschedparam( p_priv->thread_id,
867 i_policy, ¶m )) )
870 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
876 #elif defined( WIN32 ) || defined( UNDER_CE )
877 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
879 if( !SetThreadPriority(p_priv->thread_id->handle, i_priority) )
881 msg_Warn( p_this, "couldn't set a faster priority" );
890 /*****************************************************************************
891 * vlc_thread_join: wait until a thread exits, inner version
892 *****************************************************************************/
893 void __vlc_thread_join( vlc_object_t *p_this )
895 vlc_object_internals_t *p_priv = vlc_internals( p_this );
897 #if defined( LIBVLC_USE_PTHREAD )
898 vlc_join (p_priv->thread_id, NULL);
900 #elif defined( UNDER_CE ) || defined( WIN32 )
902 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
903 int64_t real_time, kernel_time, user_time;
905 if( ! DuplicateHandle(GetCurrentProcess(),
906 p_priv->thread_id->handle,
911 DUPLICATE_SAME_ACCESS) )
913 p_priv->b_thread = false;
914 return; /* We have a problem! */
917 vlc_join( p_priv->thread_id, NULL );
919 if( GetThreadTimes( hThread, &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
922 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
923 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
927 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
928 kernel_ft.dwLowDateTime) / 10;
931 ((((int64_t)user_ft.dwHighDateTime)<<32)|
932 user_ft.dwLowDateTime) / 10;
934 msg_Dbg( p_this, "thread times: "
935 "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
936 real_time/60/1000000,
937 (double)((real_time%(60*1000000))/1000000.0),
938 kernel_time/60/1000000,
939 (double)((kernel_time%(60*1000000))/1000000.0),
940 user_time/60/1000000,
941 (double)((user_time%(60*1000000))/1000000.0) );
943 CloseHandle( hThread );
946 vlc_join( p_priv->thread_id, NULL );
950 p_priv->b_thread = false;
953 void vlc_thread_cancel (vlc_object_t *obj)
955 vlc_object_internals_t *priv = vlc_internals (obj);
958 vlc_cancel (priv->thread_id);
961 void vlc_control_cancel (int cmd, ...)
963 /* NOTE: This function only modifies thread-specific data, so there is no
964 * need to lock anything. */
965 #ifdef LIBVLC_USE_PTHREAD_CANCEL
971 vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
975 /* Main thread - cannot be cancelled anyway */
978 nfo = malloc (sizeof (*nfo));
980 return; /* Uho! Expect problems! */
981 *nfo = VLC_CANCEL_INIT;
982 vlc_threadvar_set (&cancel_key, nfo);
989 case VLC_SAVE_CANCEL:
991 int *p_state = va_arg (ap, int *);
992 *p_state = nfo->killable;
993 nfo->killable = false;
997 case VLC_RESTORE_CANCEL:
999 int state = va_arg (ap, int);
1000 nfo->killable = state != 0;
1004 case VLC_TEST_CANCEL:
1005 if (nfo->killable && nfo->killed)
1007 for (vlc_cleanup_t *p = nfo->cleaners; p != NULL; p = p->next)
1012 #if defined (LIBVLC_USE_PTHREAD)
1013 pthread_exit (PTHREAD_CANCELLED);
1014 #elif defined (UNDER_CE)
1016 #elif defined (WIN32)
1019 # error Not implemented!
1028 case VLC_CLEANUP_PUSH:
1030 /* cleaner is a pointer to the caller stack, no need to allocate
1031 * and copy anything. As a nice side effect, this cannot fail. */
1032 vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
1033 cleaner->next = nfo->cleaners;
1034 nfo->cleaners = cleaner;
1038 case VLC_CLEANUP_POP:
1040 nfo->cleaners = nfo->cleaners->next;
1049 #undef var_AcquireMutex
1051 * Finds a process-wide mutex, creates it if needed, and locks it.
1052 * Unlock with vlc_mutex_unlock().
1053 * FIXME: This is very inefficient, this is not memory-safe and this leaks
1054 * memory. Use static locks instead.
1056 vlc_mutex_t *var_AcquireMutex( const char *name )
1060 vlc_mutex_lock (&named_mutexes.lock);
1061 lock = vlc_dictionary_value_for_key (&named_mutexes.list, name);
1062 if (lock == kVLCDictionaryNotFound)
1064 lock = malloc (sizeof (*lock));
1065 vlc_mutex_init (lock);
1066 vlc_dictionary_insert (&named_mutexes.list, name, lock);
1068 vlc_mutex_unlock (&named_mutexes.lock);
1070 vlc_mutex_lock (lock);