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>
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
41 #define VLC_THREADS_UNINITIALIZED 0
42 #define VLC_THREADS_PENDING 1
43 #define VLC_THREADS_ERROR 2
44 #define VLC_THREADS_READY 3
46 /*****************************************************************************
47 * Global mutex for lazy initialization of the threads system
48 *****************************************************************************/
49 static volatile unsigned i_initializations = 0;
51 #if defined( LIBVLC_USE_PTHREAD )
54 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
58 * Global process-wide VLC object.
59 * Contains inter-instance data, such as the module cache and global mutexes.
61 static libvlc_global_data_t *p_root;
63 libvlc_global_data_t *vlc_global( void )
65 assert( i_initializations > 0 );
71 * Object running the current thread
73 static vlc_threadvar_t thread_object_key;
75 vlc_object_t *vlc_threadobj (void)
77 return vlc_threadvar_get (&thread_object_key);
81 vlc_threadvar_t msg_context_global_key;
83 #if defined(LIBVLC_USE_PTHREAD)
84 static inline unsigned long vlc_threadid (void)
86 union { pthread_t th; unsigned long int i; } v = { };
87 v.th = pthread_self ();
91 #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
92 # include <execinfo.h>
95 /*****************************************************************************
96 * vlc_thread_fatal: Report an error from the threading layer
97 *****************************************************************************
98 * This is mostly meant for debugging.
99 *****************************************************************************/
100 void vlc_pthread_fatal (const char *action, int error,
101 const char *file, unsigned line)
103 fprintf (stderr, "LibVLC fatal error %s in thread %lu at %s:%u: %d\n",
104 action, vlc_threadid (), file, line, error);
106 /* Sometimes strerror_r() crashes too, so make sure we print an error
107 * message before we invoke it */
109 /* Avoid the strerror_r() prototype brain damage in glibc */
111 fprintf (stderr, " Error message: %m at:\n");
116 switch (strerror_r (error, buf, sizeof (buf)))
121 case ERANGE: /* should never happen */
122 msg = "unknwon (too big to display)";
125 msg = "unknown (invalid error number)";
128 fprintf (stderr, " Error message: %s\n", msg);
132 #ifdef HAVE_BACKTRACE
134 int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
135 backtrace_symbols_fd (stack, len, 2);
141 void vlc_pthread_fatal (const char *action, int error,
142 const char *file, unsigned line)
144 (void)action; (void)error; (void)file; (void)line;
148 static vlc_threadvar_t cancel_key;
152 * Per-thread cancellation data
154 #ifndef LIBVLC_USE_PTHREAD_CANCEL
155 typedef struct vlc_cancel_t
157 vlc_cleanup_t *cleaners;
162 # define VLC_CANCEL_INIT { NULL, false, true }
165 /*****************************************************************************
166 * vlc_threads_init: initialize threads system
167 *****************************************************************************
168 * This function requires lazy initialization of a global lock in order to
169 * keep the library really thread-safe. Some architectures don't support this
170 * and thus do not guarantee the complete reentrancy.
171 *****************************************************************************/
172 int vlc_threads_init( void )
174 int i_ret = VLC_SUCCESS;
176 /* If we have lazy mutex initialization, use it. Otherwise, we just
177 * hope nothing wrong happens. */
178 #if defined( LIBVLC_USE_PTHREAD )
179 pthread_mutex_lock( &once_mutex );
182 if( i_initializations == 0 )
184 p_root = vlc_custom_create( (vlc_object_t *)NULL, sizeof( *p_root ),
185 VLC_OBJECT_GENERIC, "root" );
192 /* We should be safe now. Do all the initialization stuff we want. */
194 vlc_threadvar_create( &thread_object_key, NULL );
196 vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy );
197 #ifndef LIBVLC_USE_PTHREAD_CANCEL
198 vlc_threadvar_create( &cancel_key, free );
204 /* If we have lazy mutex initialization support, unlock the mutex.
205 * Otherwize, we are screwed. */
206 #if defined( LIBVLC_USE_PTHREAD )
207 pthread_mutex_unlock( &once_mutex );
213 /*****************************************************************************
214 * vlc_threads_end: stop threads system
215 *****************************************************************************
216 * FIXME: This function is far from being threadsafe.
217 *****************************************************************************/
218 void vlc_threads_end( void )
220 #if defined( LIBVLC_USE_PTHREAD )
221 pthread_mutex_lock( &once_mutex );
224 assert( i_initializations > 0 );
226 if( i_initializations == 1 )
228 vlc_object_release( p_root );
229 #ifndef LIBVLC_USE_PTHREAD
230 vlc_threadvar_delete( &cancel_key );
232 vlc_threadvar_delete( &msg_context_global_key );
234 vlc_threadvar_delete( &thread_object_key );
239 #if defined( LIBVLC_USE_PTHREAD )
240 pthread_mutex_unlock( &once_mutex );
244 #if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
245 /* This is not prototyped under glibc, though it exists. */
246 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
249 /*****************************************************************************
250 * vlc_mutex_init: initialize a mutex
251 *****************************************************************************/
252 int vlc_mutex_init( vlc_mutex_t *p_mutex )
254 #if defined( LIBVLC_USE_PTHREAD )
255 pthread_mutexattr_t attr;
258 pthread_mutexattr_init( &attr );
261 /* Create error-checking mutex to detect problems more easily. */
262 # if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
263 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
265 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
268 i_result = pthread_mutex_init( p_mutex, &attr );
269 pthread_mutexattr_destroy( &attr );
271 #elif defined( UNDER_CE )
272 InitializeCriticalSection( &p_mutex->csection );
275 #elif defined( WIN32 )
276 *p_mutex = CreateMutex( 0, FALSE, 0 );
277 return (*p_mutex != NULL) ? 0 : ENOMEM;
279 #elif defined( HAVE_KERNEL_SCHEDULER_H )
280 /* check the arguments and whether it's already been initialized */
281 if( p_mutex == NULL )
286 if( p_mutex->init == 9999 )
291 p_mutex->lock = create_sem( 1, "BeMutex" );
292 if( p_mutex->lock < B_NO_ERROR )
297 p_mutex->init = 9999;
303 /*****************************************************************************
304 * vlc_mutex_init: initialize a recursive mutex (Do not use)
305 *****************************************************************************/
306 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
308 #if defined( LIBVLC_USE_PTHREAD )
309 pthread_mutexattr_t attr;
312 pthread_mutexattr_init( &attr );
313 # if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
314 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
316 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
318 i_result = pthread_mutex_init( p_mutex, &attr );
319 pthread_mutexattr_destroy( &attr );
321 #elif defined( WIN32 )
322 /* Create mutex returns a recursive mutex */
323 *p_mutex = CreateMutex( 0, FALSE, 0 );
324 return (*p_mutex != NULL) ? 0 : ENOMEM;
326 # error Unimplemented!
331 /*****************************************************************************
332 * vlc_mutex_destroy: destroy a mutex, inner version
333 *****************************************************************************/
334 void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
336 #if defined( LIBVLC_USE_PTHREAD )
337 int val = pthread_mutex_destroy( p_mutex );
338 VLC_THREAD_ASSERT ("destroying mutex");
340 #elif defined( UNDER_CE )
341 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
343 DeleteCriticalSection( &p_mutex->csection );
345 #elif defined( WIN32 )
346 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
348 CloseHandle( *p_mutex );
350 #elif defined( HAVE_KERNEL_SCHEDULER_H )
351 if( p_mutex->init == 9999 )
352 delete_sem( p_mutex->lock );
359 /*****************************************************************************
360 * vlc_cond_init: initialize a condition
361 *****************************************************************************/
362 int __vlc_cond_init( vlc_cond_t *p_condvar )
364 #if defined( LIBVLC_USE_PTHREAD )
365 pthread_condattr_t attr;
368 ret = pthread_condattr_init (&attr);
372 # if !defined (_POSIX_CLOCK_SELECTION)
373 /* Fairly outdated POSIX support (that was defined in 2001) */
374 # define _POSIX_CLOCK_SELECTION (-1)
376 # if (_POSIX_CLOCK_SELECTION >= 0)
377 /* NOTE: This must be the same clock as the one in mtime.c */
378 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
381 ret = pthread_cond_init (p_condvar, &attr);
382 pthread_condattr_destroy (&attr);
385 #elif defined( UNDER_CE ) || defined( WIN32 )
386 /* Create an auto-reset event. */
387 *p_condvar = CreateEvent( NULL, /* no security */
388 FALSE, /* auto-reset event */
389 FALSE, /* start non-signaled */
390 NULL ); /* unnamed */
391 return *p_condvar ? 0 : ENOMEM;
393 #elif defined( HAVE_KERNEL_SCHEDULER_H )
399 if( p_condvar->init == 9999 )
404 p_condvar->thread = -1;
405 p_condvar->init = 9999;
411 /*****************************************************************************
412 * vlc_cond_destroy: destroy a condition, inner version
413 *****************************************************************************/
414 void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
416 #if defined( LIBVLC_USE_PTHREAD )
417 int val = pthread_cond_destroy( p_condvar );
418 VLC_THREAD_ASSERT ("destroying condition");
420 #elif defined( UNDER_CE ) || defined( WIN32 )
421 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
423 CloseHandle( *p_condvar );
425 #elif defined( HAVE_KERNEL_SCHEDULER_H )
431 /*****************************************************************************
432 * vlc_tls_create: create a thread-local variable
433 *****************************************************************************/
434 int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
438 #if defined( LIBVLC_USE_PTHREAD )
439 i_ret = pthread_key_create( p_tls, destr );
440 #elif defined( UNDER_CE )
442 #elif defined( WIN32 )
443 /* FIXME: remember/use the destr() callback and stop leaking whatever */
445 i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
447 # error Unimplemented!
452 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
454 #if defined( LIBVLC_USE_PTHREAD )
455 pthread_key_delete (*p_tls);
456 #elif defined( UNDER_CE )
457 #elif defined( WIN32 )
460 # error Unimplemented!
464 #if defined (LIBVLC_USE_PTHREAD)
465 #elif defined (WIN32)
466 static unsigned __stdcall vlc_entry (void *data)
468 vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
469 vlc_thread_t self = data;
471 vlc_threadvar_set (&cancel_key, &cancel_data);
472 self->data = self->entry (self->data);
478 * Creates and starts new thread.
480 * @param p_handle [OUT] pointer to write the handle of the created thread to
481 * @param entry entry point for the thread
482 * @param data data parameter given to the entry point
483 * @param priority thread priority value
484 * @return 0 on success, a standard error code on error.
486 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
491 #if defined( LIBVLC_USE_PTHREAD )
493 pthread_attr_init (&attr);
495 /* Block the signals that signals interface plugin handles.
496 * If the LibVLC caller wants to handle some signals by itself, it should
497 * block these before whenever invoking LibVLC. And it must obviously not
498 * start the VLC signals interface plugin.
500 * LibVLC will normally ignore any interruption caused by an asynchronous
501 * signal during a system call. But there may well be some buggy cases
502 * where it fails to handle EINTR (bug reports welcome). Some underlying
503 * libraries might also not handle EINTR properly.
509 sigdelset (&set, SIGHUP);
510 sigaddset (&set, SIGINT);
511 sigaddset (&set, SIGQUIT);
512 sigaddset (&set, SIGTERM);
514 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
515 pthread_sigmask (SIG_BLOCK, &set, &oldset);
518 struct sched_param sp = { .sched_priority = priority, };
521 if (sp.sched_priority <= 0)
522 sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
524 sp.sched_priority += sched_get_priority_min (policy = SCHED_RR);
526 pthread_attr_setschedpolicy (&attr, policy);
527 pthread_attr_setschedparam (&attr, &sp);
530 ret = pthread_create (p_handle, &attr, entry, data);
531 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
532 pthread_attr_destroy (&attr);
534 #elif defined( WIN32 ) || defined( UNDER_CE )
535 /* When using the MSVCRT C library you have to use the _beginthreadex
536 * function instead of CreateThread, otherwise you'll end up with
537 * memory leaks and the signal functions not working (see Microsoft
538 * Knowledge Base, article 104641) */
540 vlc_thread_t th = malloc (sizeof (*p_handle));
547 #if defined( UNDER_CE )
548 hThread = CreateThread (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
550 hThread = (HANDLE)(uintptr_t)
551 _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
556 /* Thread closes the handle when exiting, duplicate it here
557 * to be on the safe side when joining. */
558 if (!DuplicateHandle (GetCurrentProcess (), hThread,
559 GetCurrentProcess (), &th->handle, 0, FALSE,
560 DUPLICATE_SAME_ACCESS))
562 CloseHandle (hThread);
567 ResumeThread (hThread);
569 SetThreadPriority (hThread, priority);
580 #elif defined( HAVE_KERNEL_SCHEDULER_H )
581 *p_handle = spawn_thread( entry, psz_name, priority, data );
582 ret = resume_thread( *p_handle );
589 /* APC procedure for thread cancellation */
590 static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
593 vlc_control_cancel (VLC_DO_CANCEL);
598 * Marks a thread as cancelled. Next time the target thread reaches a
599 * cancellation point (while not having disabled cancellation), it will
600 * run its cancellation cleanup handler, the thread variable destructors, and
601 * terminate. vlc_join() must be used afterward regardless of a thread being
604 void vlc_cancel (vlc_thread_t thread_id)
606 #if defined (LIBVLC_USE_PTHREAD_CANCEL)
607 pthread_cancel (thread_id);
608 #elif defined (WIN32)
609 QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
611 # warning vlc_cancel is not implemented!
616 * Waits for a thread to complete (if needed), and destroys it.
617 * This is a cancellation point; in case of cancellation, the join does _not_
620 * @param handle thread handle
621 * @param p_result [OUT] pointer to write the thread return value or NULL
622 * @return 0 on success, a standard error code otherwise.
624 int vlc_join (vlc_thread_t handle, void **result)
626 #if defined( LIBVLC_USE_PTHREAD )
627 return pthread_join (handle, result);
629 #elif defined( UNDER_CE ) || defined( WIN32 )
632 while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
633 == WAIT_IO_COMPLETION);
635 CloseHandle (handle->handle);
637 *result = handle->data;
641 #elif defined( HAVE_KERNEL_SCHEDULER_H )
643 ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
645 *result = (void *)exit_value;
652 struct vlc_thread_boot
654 void * (*entry) (vlc_object_t *);
655 vlc_object_t *object;
658 static void *thread_entry (void *data)
660 vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
661 void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
665 vlc_threadvar_set (&thread_object_key, obj);
667 msg_Dbg (obj, "thread started");
669 msg_Dbg (obj, "thread ended");
674 /*****************************************************************************
675 * vlc_thread_create: create a thread, inner version
676 *****************************************************************************
677 * Note that i_priority is only taken into account on platforms supporting
678 * userland real-time priority threads.
679 *****************************************************************************/
680 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
681 const char *psz_name, void * ( *func ) ( vlc_object_t * ),
682 int i_priority, bool b_wait )
685 vlc_object_internals_t *p_priv = vlc_internals( p_this );
687 struct vlc_thread_boot *boot = malloc (sizeof (*boot));
691 boot->object = p_this;
693 vlc_object_lock( p_this );
695 /* Make sure we don't re-create a thread if the object has already one */
696 assert( !p_priv->b_thread );
698 #if defined( LIBVLC_USE_PTHREAD )
700 if( config_GetInt( p_this, "rt-priority" ) > 0 )
703 /* Hack to avoid error msg */
704 if( config_GetType( p_this, "rt-offset" ) )
705 i_priority += config_GetInt( p_this, "rt-offset" );
709 i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority );
714 msg_Dbg( p_this, "waiting for thread initialization" );
715 vlc_object_wait( p_this );
718 p_priv->b_thread = true;
719 msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
720 (unsigned long)p_priv->thread_id, psz_name, i_priority,
726 msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
727 psz_name, psz_file, i_line );
730 vlc_object_unlock( p_this );
734 /*****************************************************************************
735 * vlc_thread_set_priority: set the priority of the current thread when we
736 * couldn't set it in vlc_thread_create (for instance for the main thread)
737 *****************************************************************************/
738 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
739 int i_line, int i_priority )
741 vlc_object_internals_t *p_priv = vlc_internals( p_this );
743 if( !p_priv->b_thread )
745 msg_Err( p_this, "couldn't set priority of non-existent thread" );
749 #if defined( LIBVLC_USE_PTHREAD )
751 if( config_GetInt( p_this, "rt-priority" ) > 0 )
754 int i_error, i_policy;
755 struct sched_param param;
757 memset( ¶m, 0, sizeof(struct sched_param) );
758 if( config_GetType( p_this, "rt-offset" ) )
759 i_priority += config_GetInt( p_this, "rt-offset" );
760 if( i_priority <= 0 )
762 param.sched_priority = (-1) * i_priority;
763 i_policy = SCHED_OTHER;
767 param.sched_priority = i_priority;
770 if( (i_error = pthread_setschedparam( p_priv->thread_id,
771 i_policy, ¶m )) )
774 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
780 #elif defined( WIN32 ) || defined( UNDER_CE )
781 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
783 if( !SetThreadPriority(p_priv->thread_id->handle, i_priority) )
785 msg_Warn( p_this, "couldn't set a faster priority" );
794 /*****************************************************************************
795 * vlc_thread_join: wait until a thread exits, inner version
796 *****************************************************************************/
797 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
799 vlc_object_internals_t *p_priv = vlc_internals( p_this );
802 #if defined( LIBVLC_USE_PTHREAD )
803 /* Make sure we do return if we are calling vlc_thread_join()
804 * from the joined thread */
805 if (pthread_equal (pthread_self (), p_priv->thread_id))
807 msg_Warn (p_this, "joining the active thread (VLC might crash)");
808 i_ret = pthread_detach (p_priv->thread_id);
811 i_ret = vlc_join (p_priv->thread_id, NULL);
813 #elif defined( UNDER_CE ) || defined( WIN32 )
815 FILETIME create_ft, exit_ft, kernel_ft, user_ft;
816 int64_t real_time, kernel_time, user_time;
818 if( ! DuplicateHandle(GetCurrentProcess(),
819 p_priv->thread_id->handle,
824 DUPLICATE_SAME_ACCESS) )
826 p_priv->b_thread = false;
827 i_ret = GetLastError();
831 vlc_join( p_priv->thread_id, NULL );
833 if( GetThreadTimes( hThread, &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
836 ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
837 ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
841 ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
842 kernel_ft.dwLowDateTime) / 10;
845 ((((int64_t)user_ft.dwHighDateTime)<<32)|
846 user_ft.dwLowDateTime) / 10;
848 msg_Dbg( p_this, "thread times: "
849 "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
850 real_time/60/1000000,
851 (double)((real_time%(60*1000000))/1000000.0),
852 kernel_time/60/1000000,
853 (double)((kernel_time%(60*1000000))/1000000.0),
854 user_time/60/1000000,
855 (double)((user_time%(60*1000000))/1000000.0) );
857 CloseHandle( hThread );
861 i_ret = vlc_join( p_priv->thread_id, NULL );
868 msg_Err( p_this, "thread_join(%lu) failed at %s:%d (%m)",
869 (unsigned long)p_priv->thread_id, psz_file, i_line );
872 msg_Dbg( p_this, "thread %lu joined (%s:%d)",
873 (unsigned long)p_priv->thread_id, psz_file, i_line );
875 p_priv->b_thread = false;
878 void vlc_thread_cancel (vlc_object_t *obj)
880 vlc_object_internals_t *priv = vlc_internals (obj);
883 vlc_cancel (priv->thread_id);
886 void vlc_control_cancel (int cmd, ...)
888 /* NOTE: This function only modifies thread-specific data, so there is no
889 * need to lock anything. */
890 #ifdef LIBVLC_USE_PTHREAD_CANCEL
898 vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
902 nfo = malloc (sizeof (*nfo));
905 *nfo = VLC_CANCEL_INIT;
906 vlc_threadvar_set (&cancel_key, nfo);
912 case VLC_SAVE_CANCEL:
914 int *p_state = va_arg (ap, int *);
915 *p_state = nfo->killable;
916 nfo->killable = false;
920 case VLC_RESTORE_CANCEL:
922 int state = va_arg (ap, int);
923 nfo->killable = state != 0;
927 case VLC_TEST_CANCEL:
928 if (nfo->killable && nfo->killed)
930 for (vlc_cleanup_t *p = nfo->cleaners; p != NULL; p = p->next)
933 #if defined (LIBVLC_USE_PTHREAD)
934 pthread_exit (PTHREAD_CANCELLED);
935 #elif defined (WIN32)
938 # error Not implemented!
947 case VLC_CLEANUP_PUSH:
949 /* cleaner is a pointer to the caller stack, no need to allocate
950 * and copy anything. As a nice side effect, this cannot fail. */
951 vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
952 cleaner->next = nfo->cleaners;
953 nfo->cleaners = cleaner;
957 case VLC_CLEANUP_POP:
959 nfo->cleaners = nfo->cleaners->next;