]> git.sesse.net Git - vlc/blobdiff - src/misc/threads.c
Hide global object within the thread and object subsystem
[vlc] / src / misc / threads.c
index b4aab8f0e8b2283847a4c8bf78f3517a990c812c..3af6d5dddf1321d2048c81652e7dded9cfbf6a1c 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,20 @@ 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 vlc_object_t *p_root;
+static libvlc_global_data_t   libvlc_global;
+
+libvlc_global_data_t *vlc_global( void )
+{
+    assert( i_initializations > 0 );
+    return &libvlc_global;
+}
+
+
 vlc_threadvar_t msg_context_global_key;
 
 #if defined(LIBVLC_USE_PTHREAD)
@@ -116,9 +128,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 +141,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;
+        libvlc_global.b_ready = false;
 
-        p_root = vlc_custom_create( VLC_OBJECT(p_libvlc_global), 0,
+        p_root = vlc_custom_create( VLC_OBJECT(&libvlc_global), 0,
                                     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;
         }
-
         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 +175,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 +184,9 @@ int __vlc_threads_end( vlc_object_t *p_this )
     pthread_mutex_lock( &once_mutex );
 #endif
 
-    if( i_initializations == 0 )
-        return VLC_EGENERIC;
-
-    i_initializations--;
-    if( i_initializations == 0 )
-    {
-        i_status = VLC_THREADS_UNINITIALIZED;
+    assert( i_initializations > 0 );
+    if( --i_initializations == 0 )
         vlc_object_release( p_root );
-    }
 
 #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__
@@ -229,8 +211,8 @@ int __vlc_mutex_init( vlc_mutex_t *p_mutex )
     return 0;
 
 #elif defined( WIN32 )
-    p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
-    return ( p_mutex->mutex != NULL ? 0 : 1 );
+    *p_mutex = CreateMutex( 0, FALSE, 0 );
+    return (*p_mutex != NULL) ? 0 : ENOMEM;
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     /* check the arguments and whether it's already been initialized */
@@ -280,8 +262,8 @@ int __vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
 {
 #if defined( WIN32 )
     /* Create mutex returns a recursive mutex */
-    p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
-    return ( p_mutex->mutex != NULL ? 0 : 1 );
+    *p_mutex = CreateMutex( 0, FALSE, 0 );
+    return (*p_mutex != NULL) ? 0 : ENOMEM;
 #elif defined( LIBVLC_USE_PTHREAD )
     pthread_mutexattr_t attr;
     int                 i_result;
@@ -318,7 +300,7 @@ void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mute
 #elif defined( WIN32 )
     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
 
-    CloseHandle( p_mutex->mutex );
+    CloseHandle( *p_mutex );
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     if( p_mutex->init == 9999 )
@@ -417,7 +399,7 @@ int __vlc_threadvar_create( vlc_threadvar_t *p_tls )
 
 #if defined( HAVE_KERNEL_SCHEDULER_H )
 # error Unimplemented!
-#elif defined( UNDER_CE ) || defined( WIN32 )
+#elif defined( UNDER_CE )
 #elif defined( WIN32 )
     *p_tls = TlsAlloc();
     i_ret = (*p_tls == INVALID_HANDLE_VALUE) ? EAGAIN : 0;
@@ -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,