- DWORD handle;
- void (*cleanup) (void *);
- struct vlc_threadvar *prev, *next;
-};
-
-static void vlc_threadvar_cleanup (void)
-{
- struct vlc_threadvar *p;
-
- vlc_mutex_lock (&tls_mutex);
- for (p = tls_list; p != NULL; p = p->next)
- {
- void *value = TlsGetValue (p->handle);
- if (value)
- p->cleanup (value);
- }
- vlc_mutex_unlock (&tls_mutex);
-}
-#endif
-
-/*****************************************************************************
- * vlc_tls_create: create a thread-local variable
- *****************************************************************************/
-int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- return pthread_key_create( p_tls, destr );
-
-#elif defined( WIN32 )
- struct vlc_threadvar *tls = malloc (sizeof (*tls));
- if (tls == NULL)
- return ENOMEM;
-
- tls->handle = TlsAlloc();
- if (tls->handle == TLS_OUT_OF_INDEXES)
- {
- free (tls);
- return EAGAIN;
- }
- tls->cleanup = destr;
- tls->prev = NULL;
- vlc_mutex_lock (&tls_mutex);
- tls->next = tls_list;
- if (tls_list)
- tls_list->prev = tls;
- tls_list = tls;
- vlc_mutex_unlock (&tls_mutex);
- *p_tls = tls;
- return 0;
-
-#else
-# error Unimplemented!
-#endif
-}
-
-void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
-{
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_key_delete (*p_tls);
-
-#elif defined( WIN32 )
- struct vlc_threadvar *tls = *p_tls;
-
- TlsFree (tls->handle);
- vlc_mutex_lock (&tls_mutex);
- if (tls->prev)
- tls->prev->next = tls->next;
- if (tls->next)
- tls->next->prev = tls->prev;
- vlc_mutex_unlock (&tls_mutex);
- free (tls);
-
-#else
-# error Unimplemented!
-#endif
-}
-
-/**
- * Sets a thread-local variable.
- * @param key thread-local variable key (created with vlc_threadvar_create())
- * @param value new value for the variable for the calling thread
- * @return 0 on success, a system error code otherwise.
- */
-int vlc_threadvar_set (vlc_threadvar_t key, void *value)
-{
-#if defined(LIBVLC_USE_PTHREAD)
- return pthread_setspecific (key, value);
-#elif defined( UNDER_CE ) || defined( WIN32 )
- return TlsSetValue (key->handle, value) ? ENOMEM : 0;
-#else
-# error Unimplemented!
-#endif
-}
-
-/**
- * Gets the value of a thread-local variable for the calling thread.
- * This function cannot fail.
- * @return the value associated with the given variable for the calling
- * or NULL if there is no value.
- */
-void *vlc_threadvar_get (vlc_threadvar_t key)
-{
-#if defined(LIBVLC_USE_PTHREAD)
- return pthread_getspecific (key);
-#elif defined( UNDER_CE ) || defined( WIN32 )
- return TlsGetValue (key->handle);
-#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;
-#ifdef UNDER_CE
- cancel_data.cancel_event = self->cancel_event;
-#endif
-
- vlc_threadvar_set (cancel_key, &cancel_data);
- self->data = self->entry (self->data);
- vlc_threadvar_cleanup ();
- 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);
- }
-
- /* The thread stack size.
- * The lower the value, the less address space per thread, the highest
- * maximum simultaneous threads per process. Too low values will cause
- * stack overflows and weird crashes. Set with caution. Also keep in mind
- * that 64-bits platforms consume more stack than 32-bits one.
- *
- * Thanks to on-demand paging, thread stack size only affects address space
- * consumption. In terms of memory, threads only use what they need
- * (rounded up to the page boundary).
- *
- * For example, on Linux i386, the default is 2 mega-bytes, which supports
- * about 320 threads per processes. */
-#define VLC_STACKSIZE (128 * sizeof (void *) * 1024)
-
-#ifdef VLC_STACKSIZE
- ret = pthread_attr_setstacksize (&attr, VLC_STACKSIZE);
- assert (ret == 0); /* fails iif VLC_STACKSIZE is invalid */
-#endif
-
- 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 )
- th->cancel_event = CreateEvent (NULL, FALSE, FALSE, NULL);
- if (th->cancel_event == NULL)
- {
- free(th);
- return errno;
- }
- hThread = CreateThread (NULL, 128*1024, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#else
- hThread = (HANDLE)(uintptr_t)
- _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#endif
-
- if (hThread)
- {
-#ifndef UNDER_CE
- /* 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;
- }
-#else
- th->handle = hThread;
-#endif
-
- ResumeThread (hThread);
- if (priority)
- SetThreadPriority (hThread, priority);
-
- ret = 0;
- *p_handle = th;