#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 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;
#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
#ifdef __GLIBC__
/* Avoid the strerror_r() prototype brain damage in glibc */
errno = error;
- fprintf (stderr, " Error message: %m at:\n");
+ fprintf (stderr, " Error message: %m\n");
#elif !defined (WIN32)
char buf[1000];
const char *msg;
*****************************************************************************/
int vlc_threads_init( void )
{
- int i_ret = VLC_SUCCESS;
-
/* If we have lazy mutex initialization, use it. Otherwise, we just
* hope nothing wrong happens. */
#if defined( LIBVLC_USE_PTHREAD )
if( i_initializations == 0 )
{
- 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;
- }
-
- /* We should be safe now. Do all the initialization stuff we want. */
+ vlc_dictionary_init (&named_mutexes.list, 0);
+ vlc_mutex_init (&named_mutexes.lock);
#ifndef LIBVLC_USE_PTHREAD_CANCEL
vlc_threadvar_create( &cancel_key, free );
#endif
}
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 VLC_SUCCESS;
}
/*****************************************************************************
if( i_initializations == 1 )
{
- vlc_object_release( p_root );
#ifndef LIBVLC_USE_PTHREAD
vlc_threadvar_delete( &cancel_key );
#endif
+ vlc_mutex_destroy (&named_mutexes.lock);
+ vlc_dictionary_clear (&named_mutexes.list);
}
i_initializations--;
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 = InterlockedCompareExchange (&p_mutex->owner, self, 0);
- 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 */
- DWORD self = GetCurrentThreadId ();
- self = InterlockedCompareExchange (&p_mutex->owner, self, 0);
- assert (self == GetCurrentThreadId ());
- /* 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 ();
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);
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)
{
for (vlc_cleanup_t *p = nfo->cleaners; p != NULL; p = p->next)
p->proc (p->data);
+#ifndef WIN32
free (nfo);
+#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;
+}