-}
-
-/*****************************************************************************
- * vlc_mutex_init: initialize a recursive mutex (Do not use)
- *****************************************************************************/
-int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_mutexattr_t attr;
- int i_result;
-
- pthread_mutexattr_init( &attr );
-# if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
- pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
-# else
- pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
-# endif
- i_result = pthread_mutex_init( p_mutex, &attr );
- pthread_mutexattr_destroy( &attr );
- return( i_result );
-#elif defined( WIN32 )
- /* Create mutex returns a recursive mutex */
- *p_mutex = CreateMutex( 0, FALSE, 0 );
- return (*p_mutex != NULL) ? 0 : ENOMEM;
-#else
-# error Unimplemented!
-#endif
-}
-
-
-/*****************************************************************************
- * vlc_mutex_destroy: destroy a mutex, inner version
- *****************************************************************************/
-void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- int val = pthread_mutex_destroy( p_mutex );
- VLC_THREAD_ASSERT ("destroying mutex");
-
-#elif defined( UNDER_CE )
- VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
-
- DeleteCriticalSection( &p_mutex->csection );
-
-#elif defined( WIN32 )
- VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
-
- CloseHandle( *p_mutex );
-
-#endif
-}
-
-/*****************************************************************************
- * vlc_cond_init: initialize a condition variable
- *****************************************************************************/
-int vlc_cond_init( vlc_cond_t *p_condvar )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_condattr_t attr;
- int ret;
-
- ret = pthread_condattr_init (&attr);
- if (ret)
- return ret;
-
-# if !defined (_POSIX_CLOCK_SELECTION)
- /* Fairly outdated POSIX support (that was defined in 2001) */
-# define _POSIX_CLOCK_SELECTION (-1)
-# endif
-# if (_POSIX_CLOCK_SELECTION >= 0)
- /* NOTE: This must be the same clock as the one in mtime.c */
- pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
-# endif
-
- ret = pthread_cond_init (p_condvar, &attr);
- pthread_condattr_destroy (&attr);
- return ret;
-
-#elif defined( UNDER_CE ) || defined( WIN32 )
- /* Create an auto-reset event. */
- *p_condvar = CreateEvent( NULL, /* no security */
- FALSE, /* auto-reset event */
- FALSE, /* start non-signaled */
- NULL ); /* unnamed */
- return *p_condvar ? 0 : ENOMEM;
-
-#endif
-}
-
-/*****************************************************************************
- * vlc_cond_destroy: destroy a condition, inner version
- *****************************************************************************/
-void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- int val = pthread_cond_destroy( p_condvar );
- VLC_THREAD_ASSERT ("destroying condition");
-
-#elif defined( UNDER_CE ) || defined( WIN32 )
- VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
-
- CloseHandle( *p_condvar );
-
-#endif
-}
-
-/*****************************************************************************
- * vlc_tls_create: create a thread-local variable
- *****************************************************************************/
-int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
-{
- int i_ret;
-
-#if defined( LIBVLC_USE_PTHREAD )
- i_ret = pthread_key_create( p_tls, destr );
-#elif defined( UNDER_CE )
- i_ret = ENOSYS;
-#elif defined( WIN32 )
- /* FIXME: remember/use the destr() callback and stop leaking whatever */
- *p_tls = TlsAlloc();
- i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
-#else
-# error Unimplemented!
-#endif
- return i_ret;
-}
-
-void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
-{
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_key_delete (*p_tls);
-#elif defined( UNDER_CE )
-#elif defined( WIN32 )
- TlsFree (*p_tls);
-#else
-# error Unimplemented!
-#endif
-}
-
-#if defined (LIBVLC_USE_PTHREAD)
-#elif defined (WIN32)
-static unsigned __stdcall vlc_entry (void *data)
-{
- vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
- vlc_thread_t self = data;
-
- vlc_threadvar_set (&cancel_key, &cancel_data);
- self->data = self->entry (self->data);
- return 0;
-}
-#endif
-
-/**
- * Creates and starts new thread.
- *
- * @param p_handle [OUT] pointer to write the handle of the created thread to
- * @param entry entry point for the thread
- * @param data data parameter given to the entry point
- * @param priority thread priority value
- * @return 0 on success, a standard error code on error.
- */
-int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
- int priority)
-{
- int ret;
-
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_attr_t attr;
- pthread_attr_init (&attr);
-
- /* Block the signals that signals interface plugin handles.
- * If the LibVLC caller wants to handle some signals by itself, it should
- * block these before whenever invoking LibVLC. And it must obviously not
- * start the VLC signals interface plugin.
- *
- * LibVLC will normally ignore any interruption caused by an asynchronous
- * signal during a system call. But there may well be some buggy cases
- * where it fails to handle EINTR (bug reports welcome). Some underlying
- * libraries might also not handle EINTR properly.
- */
- sigset_t oldset;
- {
- sigset_t set;
- sigemptyset (&set);
- sigdelset (&set, SIGHUP);
- sigaddset (&set, SIGINT);
- sigaddset (&set, SIGQUIT);
- sigaddset (&set, SIGTERM);
-
- sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
- pthread_sigmask (SIG_BLOCK, &set, &oldset);
- }
- {
- struct sched_param sp = { .sched_priority = priority, };
- int policy;
-
- if (sp.sched_priority <= 0)
- sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
- else
- sp.sched_priority += sched_get_priority_min (policy = SCHED_RR);
-
- pthread_attr_setschedpolicy (&attr, policy);
- pthread_attr_setschedparam (&attr, &sp);
- }
-
- ret = pthread_create (p_handle, &attr, entry, data);
- pthread_sigmask (SIG_SETMASK, &oldset, NULL);
- pthread_attr_destroy (&attr);
-
-#elif defined( WIN32 ) || defined( UNDER_CE )
- /* When using the MSVCRT C library you have to use the _beginthreadex
- * function instead of CreateThread, otherwise you'll end up with
- * memory leaks and the signal functions not working (see Microsoft
- * Knowledge Base, article 104641) */
- HANDLE hThread;
- vlc_thread_t th = malloc (sizeof (*th));
-
- if (th == NULL)
- return ENOMEM;
-
- th->data = data;
- th->entry = entry;
-#if defined( UNDER_CE )
- hThread = CreateThread (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#else
- hThread = (HANDLE)(uintptr_t)
- _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#endif
-
- if (hThread)
- {
- /* Thread closes the handle when exiting, duplicate it here
- * to be on the safe side when joining. */
- if (!DuplicateHandle (GetCurrentProcess (), hThread,
- GetCurrentProcess (), &th->handle, 0, FALSE,
- DUPLICATE_SAME_ACCESS))
- {
- CloseHandle (hThread);
- free (th);
- return ENOMEM;
- }
-
- ResumeThread (hThread);
- if (priority)
- SetThreadPriority (hThread, priority);
-
- ret = 0;
- *p_handle = th;
- }
- else
- {
- ret = errno;
- free (th);
- }
-
-#endif
- return ret;
-}
-
-#if defined (WIN32)
-/* APC procedure for thread cancellation */
-static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
-{
- (void)dummy;
- vlc_control_cancel (VLC_DO_CANCEL);
-}
-#endif
-
-/**
- * Marks a thread as cancelled. Next time the target thread reaches a
- * cancellation point (while not having disabled cancellation), it will
- * run its cancellation cleanup handler, the thread variable destructors, and
- * terminate. vlc_join() must be used afterward regardless of a thread being
- * cancelled or not.
- */
-void vlc_cancel (vlc_thread_t thread_id)
-{
-#if defined (LIBVLC_USE_PTHREAD_CANCEL)
- pthread_cancel (thread_id);
-#elif defined (WIN32)
- QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
-#else
-# warning vlc_cancel is not implemented!
-#endif
-}
-
-/**
- * Waits for a thread to complete (if needed), and destroys it.
- * This is a cancellation point; in case of cancellation, the join does _not_
- * occur.
- *
- * @param handle thread handle
- * @param p_result [OUT] pointer to write the thread return value or NULL
- * @return 0 on success, a standard error code otherwise.
- */
-void vlc_join (vlc_thread_t handle, void **result)
-{
-#if defined( LIBVLC_USE_PTHREAD )
- int val = pthread_join (handle, result);
- if (val)
- vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__);
-
-#elif defined( UNDER_CE ) || defined( WIN32 )
- do
- vlc_testcancel ();
- while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
- == WAIT_IO_COMPLETION);
-
- CloseHandle (handle->handle);
- if (result)
- *result = handle->data;
- free (handle);
-
-#endif
-}