]> git.sesse.net Git - vlc/blobdiff - src/misc/threads.c
update check: Initialize gcrypt threading - fixes #1483
[vlc] / src / misc / threads.c
index 31a22cb3e8419f5a06c1ed4cf3fb0cf2930d4f26..48d349830296e17b6a26cfdd9f057384b97fba1d 100644 (file)
@@ -45,8 +45,6 @@
  * Global mutex for lazy initialization of the threads system
  *****************************************************************************/
 static volatile unsigned i_initializations = 0;
-static volatile int i_status = VLC_THREADS_UNINITIALIZED;
-static vlc_object_t *p_root;
 
 #if defined( UNDER_CE )
 #elif defined( WIN32 )
@@ -55,6 +53,19 @@ static vlc_object_t *p_root;
 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
 #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 )
+{
+    assert( i_initializations > 0 );
+    return p_root;
+}
+
+
 vlc_threadvar_t msg_context_global_key;
 
 #if defined(LIBVLC_USE_PTHREAD)
@@ -116,9 +127,8 @@ void vlc_pthread_fatal (const char *action, int error,
  * keep the library really thread-safe. Some architectures don't support this
  * and thus do not guarantee the complete reentrancy.
  *****************************************************************************/
-int __vlc_threads_init( vlc_object_t *p_this )
+int vlc_threads_init( void )
 {
-    libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_this;
     int i_ret = VLC_SUCCESS;
 
     /* If we have lazy mutex initialization, use it. Otherwise, we just
@@ -130,53 +140,32 @@ int __vlc_threads_init( vlc_object_t *p_this )
     pthread_mutex_lock( &once_mutex );
 #endif
 
-    if( i_status == VLC_THREADS_UNINITIALIZED )
+    if( i_initializations == 0 )
     {
-        i_status = VLC_THREADS_PENDING;
-
-        /* We should be safe now. Do all the initialization stuff we want. */
-        p_libvlc_global->b_ready = false;
-
-        p_root = vlc_custom_create( VLC_OBJECT(p_libvlc_global), 0,
+        p_root = vlc_custom_create( NULL, sizeof( *p_root ),
                                     VLC_OBJECT_GLOBAL, "global" );
         if( p_root == NULL )
-            i_ret = VLC_ENOMEM;
-
-        if( i_ret )
-        {
-            i_status = VLC_THREADS_ERROR;
-        }
-        else
         {
-            i_initializations++;
-            i_status = VLC_THREADS_READY;
+            i_ret = VLC_ENOMEM;
+            goto out;
         }
 
+        /* We should be safe now. Do all the initialization stuff we want. */
+        p_root->b_ready = false;
         vlc_threadvar_create( p_root, &msg_context_global_key );
     }
-    else
-    {
-        /* Just increment the initialization count */
-        i_initializations++;
-    }
+    i_initializations++;
 
-    /* If we have lazy mutex initialization support, unlock the mutex;
-     * otherwize, do a naive wait loop. */
+out:
+    /* If we have lazy mutex initialization support, unlock the mutex.
+     * Otherwize, we are screwed. */
 #if defined( UNDER_CE )
-    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( WIN32 )
-    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
-    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( LIBVLC_USE_PTHREAD )
     pthread_mutex_unlock( &once_mutex );
 #endif
 
-    if( i_status != VLC_THREADS_READY )
-    {
-        return VLC_ETHREAD;
-    }
-
     return i_ret;
 }
 
@@ -185,9 +174,8 @@ int __vlc_threads_init( vlc_object_t *p_this )
  *****************************************************************************
  * FIXME: This function is far from being threadsafe.
  *****************************************************************************/
-int __vlc_threads_end( vlc_object_t *p_this )
+void vlc_threads_end( void )
 {
-    (void)p_this;
 #if defined( UNDER_CE )
 #elif defined( WIN32 )
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
@@ -195,15 +183,10 @@ int __vlc_threads_end( vlc_object_t *p_this )
     pthread_mutex_lock( &once_mutex );
 #endif
 
+    assert( i_initializations > 0 );
     if( i_initializations == 0 )
-        return VLC_EGENERIC;
-
-    i_initializations--;
-    if( i_initializations == 0 )
-    {
-        i_status = VLC_THREADS_UNINITIALIZED;
         vlc_object_release( p_root );
-    }
+    i_initializations--;
 
 #if defined( UNDER_CE )
 #elif defined( WIN32 )
@@ -211,7 +194,6 @@ int __vlc_threads_end( vlc_object_t *p_this )
 #elif defined( LIBVLC_USE_PTHREAD )
     pthread_mutex_unlock( &once_mutex );
 #endif
-    return VLC_SUCCESS;
 }
 
 #ifdef __linux__
@@ -222,7 +204,7 @@ int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
 /*****************************************************************************
  * vlc_mutex_init: initialize a mutex
  *****************************************************************************/
-int __vlc_mutex_init( vlc_mutex_t *p_mutex )
+int vlc_mutex_init( vlc_mutex_t *p_mutex )
 {
 #if defined( UNDER_CE )
     InitializeCriticalSection( &p_mutex->csection );
@@ -276,7 +258,7 @@ int __vlc_mutex_init( vlc_mutex_t *p_mutex )
 /*****************************************************************************
  * vlc_mutex_init: initialize a recursive mutex (Do not use)
  *****************************************************************************/
-int __vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
+int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
 {
 #if defined( WIN32 )
     /* Create mutex returns a recursive mutex */
@@ -440,7 +422,7 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line
 {
     int i_ret;
     void *p_data = (void *)p_this;
-    vlc_object_internals_t *p_priv = p_this->p_internals;
+    vlc_object_internals_t *p_priv = vlc_internals( p_this );
 
     vlc_mutex_lock( &p_this->object_lock );
 
@@ -567,7 +549,7 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line
 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
                                int i_line, int i_priority )
 {
-    vlc_object_internals_t *p_priv = p_this->p_internals;
+    vlc_object_internals_t *p_priv = vlc_internals( p_this );
 #if defined( WIN32 ) || defined( UNDER_CE )
     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
 
@@ -631,7 +613,7 @@ void __vlc_thread_ready( vlc_object_t *p_this )
  *****************************************************************************/
 void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
 {
-    vlc_object_internals_t *p_priv = p_this->p_internals;
+    vlc_object_internals_t *p_priv = vlc_internals( p_this );
 
 #if defined( UNDER_CE ) || defined( WIN32 )
     HMODULE hmodule;
@@ -692,7 +674,7 @@ void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line
            user_ft.dwLowDateTime) / 10;
 
         msg_Dbg( p_this, "thread times: "
-                 "real "I64Fd"m%fs, kernel "I64Fd"m%fs, user "I64Fd"m%fs",
+                 "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
                  real_time/60/1000000,
                  (double)((real_time%(60*1000000))/1000000.0),
                  kernel_time/60/1000000,