]> git.sesse.net Git - vlc/blobdiff - src/misc/threads.c
Remove old BeOS thread support
[vlc] / src / misc / threads.c
index a296b4b12c289f1a6b80e24ebee2dd7883f105de..98b40553503cace34e9cc8b7291a0395c74f44c4 100644 (file)
@@ -52,6 +52,8 @@ static volatile unsigned i_initializations = 0;
 # include <sched.h>
 
 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
+#else
+static vlc_threadvar_t cancel_key;
 #endif
 
 /**
@@ -66,42 +68,54 @@ libvlc_global_data_t *vlc_global( void )
     return p_root;
 }
 
-#ifndef NDEBUG
+#ifdef HAVE_EXECINFO_H
+# include <execinfo.h>
+#endif
+
 /**
- * Object running the current thread
+ * Print a backtrace to the standard error for debugging purpose.
  */
-static vlc_threadvar_t thread_object_key;
-
-vlc_object_t *vlc_threadobj (void)
+void vlc_trace (const char *fn, const char *file, unsigned line)
 {
-    return vlc_threadvar_get (&thread_object_key);
-}
+     fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
+     fflush (stderr); /* needed before switch to low-level I/O */
+#ifdef HAVE_BACKTRACE
+     void *stack[20];
+     int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
+     backtrace_symbols_fd (stack, len, 2);
 #endif
+#ifndef WIN32
+     fsync (2);
+#endif
+}
 
-vlc_threadvar_t msg_context_global_key;
-
-#if defined(LIBVLC_USE_PTHREAD)
 static inline unsigned long vlc_threadid (void)
 {
+#if defined(LIBVLC_USE_PTHREAD)
      union { pthread_t th; unsigned long int i; } v = { };
      v.th = pthread_self ();
      return v.i;
-}
 
-#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
-# include <execinfo.h>
+#elif defined (WIN32)
+     return GetCurrentThreadId ();
+
+#else
+     return 0;
+
 #endif
+}
 
 /*****************************************************************************
  * vlc_thread_fatal: Report an error from the threading layer
  *****************************************************************************
  * This is mostly meant for debugging.
  *****************************************************************************/
-void vlc_pthread_fatal (const char *action, int error,
+void vlc_thread_fatal (const char *action, int error, const char *function,
                         const char *file, unsigned line)
 {
-    fprintf (stderr, "LibVLC fatal error %s in thread %lu at %s:%u: %d\n",
-             action, vlc_threadid (), file, line, error);
+    fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
+             action, error, vlc_threadid ());
+    vlc_trace (function, file, line);
 
     /* Sometimes strerror_r() crashes too, so make sure we print an error
      * message before we invoke it */
@@ -109,7 +123,7 @@ void vlc_pthread_fatal (const char *action, int error,
     /* Avoid the strerror_r() prototype brain damage in glibc */
     errno = error;
     fprintf (stderr, " Error message: %m at:\n");
-#else
+#elif !defined (WIN32)
     char buf[1000];
     const char *msg;
 
@@ -129,24 +143,8 @@ void vlc_pthread_fatal (const char *action, int error,
 #endif
     fflush (stderr);
 
-#ifdef HAVE_BACKTRACE
-    void *stack[20];
-    int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
-    backtrace_symbols_fd (stack, len, 2);
-#endif
-
     abort ();
 }
-#else
-void vlc_pthread_fatal (const char *action, int error,
-                        const char *file, unsigned line)
-{
-    (void)action; (void)error; (void)file; (void)line;
-    abort();
-}
-
-static vlc_threadvar_t cancel_key;
-#endif
 
 /**
  * Per-thread cancellation data
@@ -190,10 +188,6 @@ int vlc_threads_init( void )
         }
 
         /* We should be safe now. Do all the initialization stuff we want. */
-#ifndef NDEBUG
-        vlc_threadvar_create( &thread_object_key, NULL );
-#endif
-        vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy );
 #ifndef LIBVLC_USE_PTHREAD_CANCEL
         vlc_threadvar_create( &cancel_key, free );
 #endif
@@ -228,10 +222,6 @@ void vlc_threads_end( void )
         vlc_object_release( p_root );
 #ifndef LIBVLC_USE_PTHREAD
         vlc_threadvar_delete( &cancel_key );
-#endif
-        vlc_threadvar_delete( &msg_context_global_key );
-#ifndef NDEBUG
-        vlc_threadvar_delete( &thread_object_key );
 #endif
     }
     i_initializations--;
@@ -276,27 +266,6 @@ int vlc_mutex_init( vlc_mutex_t *p_mutex )
     *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 */
-    if( p_mutex == NULL )
-    {
-        return B_BAD_VALUE;
-    }
-
-    if( p_mutex->init == 9999 )
-    {
-        return EALREADY;
-    }
-
-    p_mutex->lock = create_sem( 1, "BeMutex" );
-    if( p_mutex->lock < B_NO_ERROR )
-    {
-        return( -1 );
-    }
-
-    p_mutex->init = 9999;
-    return B_OK;
-
 #endif
 }
 
@@ -347,12 +316,6 @@ void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mute
 
     CloseHandle( *p_mutex );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
-    if( p_mutex->init == 9999 )
-        delete_sem( p_mutex->lock );
-
-    p_mutex->init = 0;
-
 #endif
 }
 
@@ -390,21 +353,6 @@ int __vlc_cond_init( vlc_cond_t *p_condvar )
                               NULL ); /* unnamed */
     return *p_condvar ? 0 : ENOMEM;
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
-    if( !p_condvar )
-    {
-        return B_BAD_VALUE;
-    }
-
-    if( p_condvar->init == 9999 )
-    {
-        return EALREADY;
-    }
-
-    p_condvar->thread = -1;
-    p_condvar->init = 9999;
-    return 0;
-
 #endif
 }
 
@@ -422,9 +370,6 @@ void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condva
 
     CloseHandle( *p_condvar );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
-    p_condvar->init = 0;
-
 #endif
 }
 
@@ -537,7 +482,7 @@ int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
      * memory leaks and the signal functions not working (see Microsoft
      * Knowledge Base, article 104641) */
     HANDLE hThread;
-    vlc_thread_t th = malloc (sizeof (*p_handle));
+    vlc_thread_t th = malloc (sizeof (*th));
 
     if (th == NULL)
         return ENOMEM;
@@ -577,10 +522,6 @@ int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
         free (th);
     }
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
-    *p_handle = spawn_thread( entry, psz_name, priority, data );
-    ret = resume_thread( *p_handle );
-
 #endif
     return ret;
 }
@@ -621,10 +562,12 @@ void vlc_cancel (vlc_thread_t thread_id)
  * @param p_result [OUT] pointer to write the thread return value or NULL
  * @return 0 on success, a standard error code otherwise.
  */
-int vlc_join (vlc_thread_t handle, void **result)
+void vlc_join (vlc_thread_t handle, void **result)
 {
 #if defined( LIBVLC_USE_PTHREAD )
-    return pthread_join (handle, result);
+    int val = pthread_join (handle, result);
+    if (val)
+        vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__);
 
 #elif defined( UNDER_CE ) || defined( WIN32 )
     do
@@ -636,15 +579,7 @@ int vlc_join (vlc_thread_t handle, void **result)
     if (result)
         *result = handle->data;
     free (handle);
-    return 0;
-
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
-    int32_t exit_value;
-    ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
-    if( !ret && result )
-        *result = (void *)exit_value;
 
-    return ret;
 #endif
 }
 
@@ -661,9 +596,6 @@ static void *thread_entry (void *data)
     void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
 
     free (data);
-#ifndef NDEBUG
-    vlc_threadvar_set (&thread_object_key, obj);
-#endif
     msg_Dbg (obj, "thread started");
     func (obj);
     msg_Dbg (obj, "thread ended");
@@ -794,21 +726,12 @@ int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
 /*****************************************************************************
  * vlc_thread_join: wait until a thread exits, inner version
  *****************************************************************************/
-void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
+void __vlc_thread_join( vlc_object_t *p_this )
 {
     vlc_object_internals_t *p_priv = vlc_internals( p_this );
-    int i_ret = 0;
 
 #if defined( LIBVLC_USE_PTHREAD )
-    /* Make sure we do return if we are calling vlc_thread_join()
-     * from the joined thread */
-    if (pthread_equal (pthread_self (), p_priv->thread_id))
-    {
-        msg_Warn (p_this, "joining the active thread (VLC might crash)");
-        i_ret = pthread_detach (p_priv->thread_id);
-    }
-    else
-        i_ret = vlc_join (p_priv->thread_id, NULL);
+    vlc_join (p_priv->thread_id, NULL);
 
 #elif defined( UNDER_CE ) || defined( WIN32 )
     HANDLE hThread;
@@ -824,8 +747,7 @@ void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line
             DUPLICATE_SAME_ACCESS) )
     {
         p_priv->b_thread = false;
-        i_ret = GetLastError();
-        goto error;
+        return; /* We have a problem! */
     }
 
     vlc_join( p_priv->thread_id, NULL );
@@ -855,23 +777,12 @@ void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line
                  (double)((user_time%(60*1000000))/1000000.0) );
     }
     CloseHandle( hThread );
-error:
 
 #else
-    i_ret = vlc_join( p_priv->thread_id, NULL );
+    vlc_join( p_priv->thread_id, NULL );
 
 #endif
 
-    if( i_ret )
-    {
-        errno = i_ret;
-        msg_Err( p_this, "thread_join(%lu) failed at %s:%d (%m)",
-                         (unsigned long)p_priv->thread_id, psz_file, i_line );
-    }
-    else
-        msg_Dbg( p_this, "thread %lu joined (%s:%d)",
-                         (unsigned long)p_priv->thread_id, psz_file, i_line );
-
     p_priv->b_thread = false;
 }
 
@@ -896,16 +807,19 @@ void vlc_control_cancel (int cmd, ...)
     va_start (ap, cmd);
 
     vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
-#ifndef WIN32
     if (nfo == NULL)
     {
+#ifdef WIN32
+        /* Main thread - cannot be cancelled anyway */
+        return;
+#else
         nfo = malloc (sizeof (*nfo));
         if (nfo == NULL)
-            abort ();
+            return; /* Uho! Expect problems! */
         *nfo = VLC_CANCEL_INIT;
         vlc_threadvar_set (&cancel_key, nfo);
-    }
 #endif
+    }
 
     switch (cmd)
     {