#endif
#include <signal.h>
-#define VLC_THREADS_UNINITIALIZED 0
-#define VLC_THREADS_PENDING 1
-#define VLC_THREADS_ERROR 2
-#define VLC_THREADS_READY 3
-
-/*****************************************************************************
- * Global mutex for lazy initialization of the threads system
- *****************************************************************************/
-static volatile unsigned i_initializations = 0;
-
#if defined( LIBVLC_USE_PTHREAD )
# include <sched.h>
-
-static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
static vlc_threadvar_t cancel_key;
#endif
-/**
- * Global process-wide VLC object.
- * Contains inter-instance data, such as the module cache and global mutexes.
- */
-static libvlc_global_data_t *p_root;
-
-libvlc_global_data_t *vlc_global( void )
+static struct
{
- assert( i_initializations > 0 );
- return p_root;
-}
+ vlc_dictionary_t list;
+ vlc_mutex_t lock;
+} named_mutexes = {
+ { 0, NULL, },
+#ifdef LIBVLC_USE_PTHREAD
+ PTHREAD_MUTEX_INITIALIZER,
+#endif
+};
#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
# define VLC_CANCEL_INIT { NULL, true, false }
#endif
-/*****************************************************************************
- * vlc_threads_init: initialize threads system
- *****************************************************************************
- * This function requires lazy initialization of a global lock in order to
- * keep the library really thread-safe. Some architectures don't support this
- * and thus do not guarantee the complete reentrancy.
- *****************************************************************************/
-int vlc_threads_init( void )
+#ifdef WIN32
+BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
{
- int i_ret = VLC_SUCCESS;
+ (void) hinstDll;
+ (void) lpvReserved;
- /* If we have lazy mutex initialization, use it. Otherwise, we just
- * hope nothing wrong happens. */
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_mutex_lock( &once_mutex );
-#endif
-
- if( i_initializations == 0 )
+ switch (fdwReason)
{
- p_root = vlc_custom_create( (vlc_object_t *)NULL, sizeof( *p_root ),
- VLC_OBJECT_GENERIC, "root" );
- if( p_root == NULL )
- {
- i_ret = VLC_ENOMEM;
- goto out;
- }
+ case DLL_PROCESS_ATTACH:
+ vlc_dictionary_init (&named_mutexes.list, 0);
+ vlc_mutex_init (&named_mutexes.lock);
+ vlc_threadvar_create (&cancel_key, free);
+ break;
- /* We should be safe now. Do all the initialization stuff we want. */
-#ifndef LIBVLC_USE_PTHREAD_CANCEL
- vlc_threadvar_create( &cancel_key, free );
-#endif
+ case DLL_PROCESS_DETACH:
+ vlc_threadvar_delete( &cancel_key );
+ vlc_mutex_destroy (&named_mutexes.lock);
+ vlc_dictionary_clear (&named_mutexes.list);
+ break;
}
- i_initializations++;
-
-out:
- /* If we have lazy mutex initialization support, unlock the mutex.
- * Otherwize, we are screwed. */
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_mutex_unlock( &once_mutex );
-#endif
-
- return i_ret;
+ return TRUE;
}
-
-/*****************************************************************************
- * vlc_threads_end: stop threads system
- *****************************************************************************
- * FIXME: This function is far from being threadsafe.
- *****************************************************************************/
-void vlc_threads_end( void )
-{
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_mutex_lock( &once_mutex );
-#endif
-
- assert( i_initializations > 0 );
-
- if( i_initializations == 1 )
- {
- vlc_object_release( p_root );
-#ifndef LIBVLC_USE_PTHREAD
- vlc_threadvar_delete( &cancel_key );
#endif
- }
- i_initializations--;
-
-#if defined( LIBVLC_USE_PTHREAD )
- pthread_mutex_unlock( &once_mutex );
-#endif
-}
#if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
/* This is not prototyped under glibc, though it exists. */
return i_result;
#elif defined( WIN32 )
+ /* This creates a recursive mutex. This is OK as fast mutexes have
+ * no defined behavior in case of recursive locking. */
InitializeCriticalSection (&p_mutex->mutex);
- p_mutex->recursive = false;
return 0;
#endif
#elif defined( WIN32 )
InitializeCriticalSection( &p_mutex->mutex );
- p_mutex->owner = 0; /* the error value for GetThreadId()! */
- p_mutex->recursion = 0;
- p_mutex->recursive = true;
return 0;
#endif
VLC_THREAD_ASSERT ("locking mutex");
#elif defined( WIN32 )
- if (p_mutex->recursive)
- {
- DWORD self = GetCurrentThreadId ();
-
- if ((DWORD)InterlockedCompareExchange (&p_mutex->owner, self,
- self) == self)
- { /* We already locked this recursive mutex */
- p_mutex->recursion++;
- return;
- }
-
- /* We need to lock this recursive mutex */
- EnterCriticalSection (&p_mutex->mutex);
- self = InterlockedExchange (&p_mutex->owner, self);
- assert (self == 0); /* no previous owner */
- return;
- }
- /* Non-recursive mutex */
EnterCriticalSection (&p_mutex->mutex);
#endif
VLC_THREAD_ASSERT ("unlocking mutex");
#elif defined( WIN32 )
- if (p_mutex->recursive)
- {
- if (p_mutex->recursion != 0)
- {
- p_mutex->recursion--;
- return; /* We still own this mutex */
- }
-
- /* We release the mutex */
- InterlockedExchange (&p_mutex->owner, 0);
- /* fall through */
- }
LeaveCriticalSection (&p_mutex->mutex);
#endif
#elif defined( WIN32 )
DWORD result;
- assert (!p_mutex->recursive);
do
{
vlc_testcancel ();
#elif defined( WIN32 )
DWORD result;
- assert (!p_mutex->recursive);
do
{
vlc_testcancel ();
boot->entry = func;
boot->object = p_this;
- vlc_object_lock( p_this );
-
/* Make sure we don't re-create a thread if the object has already one */
assert( !p_priv->b_thread );
#if defined( LIBVLC_USE_PTHREAD )
+ if (b_wait)
+ sem_init (&p_priv->thread_ready, 0, 0);
#ifndef __APPLE__
if( config_GetInt( p_this, "rt-priority" ) > 0 )
#endif
if( config_GetType( p_this, "rt-offset" ) )
i_priority += config_GetInt( p_this, "rt-offset" );
}
+#elif defined (WIN32)
+ if (b_wait)
+ p_priv->thread_ready = CreateEvent (NULL, TRUE, FALSE, NULL);
#endif
+ p_priv->b_thread = true;
i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority );
if( i_ret == 0 )
{
+ msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
+ (unsigned long)p_priv->thread_id, psz_name, i_priority,
+ psz_file, i_line );
if( b_wait )
{
msg_Dbg( p_this, "waiting for thread initialization" );
- vlc_object_wait( p_this );
+#if defined (LIBVLC_USE_PTHREAD)
+ sem_wait (&p_priv->thread_ready);
+ sem_destroy (&p_priv->thread_ready);
+#elif defined (WIN32)
+ WaitForSingleObject (p_priv->thread_ready, INFINITE);
+ CloseHandle (p_priv->thread_ready);
+#endif
}
-
- p_priv->b_thread = true;
- msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
- (unsigned long)p_priv->thread_id, psz_name, i_priority,
- psz_file, i_line );
}
else
{
+ p_priv->b_thread = false;
errno = i_ret;
msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
psz_name, psz_file, i_line );
}
- vlc_object_unlock( p_this );
return i_ret;
}
+#undef vlc_thread_ready
+void vlc_thread_ready (vlc_object_t *obj)
+{
+ vlc_object_internals_t *priv = vlc_internals (obj);
+
+ assert (priv->b_thread);
+#if defined (LIBVLC_USE_PTHREAD)
+ assert (pthread_equal (pthread_self (), priv->thread_id));
+ sem_post (&priv->thread_ready);
+#elif defined (WIN32)
+ SetEvent (priv->thread_ready);
+#endif
+}
+
/*****************************************************************************
* vlc_thread_set_priority: set the priority of the current thread when we
* couldn't set it in vlc_thread_create (for instance for the main thread)
#endif
#if defined (LIBVLC_USE_PTHREAD)
pthread_exit (PTHREAD_CANCELLED);
+#elif defined (UNDER_CE)
+ ExitThread(0);
#elif defined (WIN32)
_endthread ();
#else
va_end (ap);
#endif
}
+
+
+#undef var_AcquireMutex
+/**
+ * Finds a process-wide mutex, creates it if needed, and locks it.
+ * Unlock with vlc_mutex_unlock().
+ * FIXME: This is very inefficient, this is not memory-safe and this leaks
+ * memory. Use static locks instead.
+ */
+vlc_mutex_t *var_AcquireMutex( const char *name )
+{
+ vlc_mutex_t *lock;
+
+ vlc_mutex_lock (&named_mutexes.lock);
+ lock = vlc_dictionary_value_for_key (&named_mutexes.list, name);
+ if (lock == kVLCDictionaryNotFound)
+ {
+ lock = malloc (sizeof (*lock));
+ vlc_mutex_init (lock);
+ vlc_dictionary_insert (&named_mutexes.list, name, lock);
+ }
+ vlc_mutex_unlock (&named_mutexes.lock);
+
+ vlc_mutex_lock (lock);
+ return lock;
+}