]> git.sesse.net Git - vlc/blobdiff - src/misc/threads.c
Don't clutter the production builds with the (useless IMHO) vlc_thread_error
[vlc] / src / misc / threads.c
index 69abe6e175855eab87a07592c1473ec65af0623f..8adc91a9f3c4d0dc05de465c6832a1d9c60582fd 100644 (file)
@@ -77,6 +77,18 @@ static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 vlc_threadvar_t msg_context_global_key;
 
+/*****************************************************************************
+ * vlc_threads_error: Report an error from the threading mecanism
+ *****************************************************************************
+ * This is especially useful to debug those errors, as this is a nice symbol
+ * on which you can break.
+ *****************************************************************************/
+void vlc_threads_error( vlc_object_t *p_this )
+{
+     msg_Err( p_this, "Error detected. Put a breakpoint in '%s' to debug.",
+            __func__ );
+}
+
 /*****************************************************************************
  * vlc_threads_init: initialize threads system
  *****************************************************************************
@@ -308,6 +320,7 @@ int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
 #   else
         pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
 #   endif
+
         i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
         pthread_mutexattr_destroy( &attr );
         return( i_result );
@@ -322,6 +335,40 @@ int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
 #endif
 }
 
+/*****************************************************************************
+ * vlc_mutex_init: initialize a recursive mutex (Do not use)
+ *****************************************************************************/
+int __vlc_mutex_init_recursive( vlc_object_t *p_this, 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 );
+#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
+    pthread_mutexattr_t attr;
+    int                 i_result;
+
+    pthread_mutexattr_init( &attr );
+# if defined(DEBUG)
+    /* Create error-checking mutex to detect problems more easily. */
+#   if defined(SYS_LINUX)
+    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
+#   else
+    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
+#   endif
+# endif
+    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
+    i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
+    pthread_mutexattr_destroy( &attr );
+    return( i_result );
+#else
+    msg_Err(p_this, "no recursive mutex found. Falling back to regular mutex.\n"
+                    "Expect hangs\n")
+    return __vlc_mutex_init( p_this, p_mutex );
+#endif
+}
+
+
 /*****************************************************************************
  * vlc_mutex_destroy: destroy a mutex, inner version
  *****************************************************************************/
@@ -330,7 +377,6 @@ int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex
     int i_result;
     /* In case of error : */
     int i_thread = -1;
-    const char * psz_error = "";
 
 #if defined( PTH_INIT_IN_PTH_H )
     return 0;
@@ -367,7 +413,7 @@ int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex
     if( i_result )
     {
         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
-        psz_error = strerror(i_result);
+        errno = i_result;
     }
 
 #elif defined( HAVE_CTHREADS_H )
@@ -378,8 +424,9 @@ int __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex
     if( i_result )
     {
         msg_Err( p_mutex->p_this,
-                 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
-                 i_thread, psz_file, i_line, i_result, psz_error );
+                 "thread %d: mutex_destroy failed at %s:%d (%d:%m)",
+                 i_thread, psz_file, i_line, i_result );
+        vlc_threads_error( p_mutex->p_this );
     }
     return i_result;
 }
@@ -471,7 +518,11 @@ int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
     if (ret)
         return ret;
 
-# if defined (_POSIX_CLOCK_SELECTION) && (_POSIX_CLOCK_SELECTION - 0 > 0)
+# if !defined (_POSIX_CLOCK_SELECTION)
+   /* Fairly outdated POSIX support (that was defined in 2001) */
+#  define _POSIX_CLOCK_SELECTION (-1)
+# endif
+# if (_POSIX_CLOCK_SELECTION >= 0)
     /* NOTE: This must be the same clock as the one in mtime.c */
     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
 # endif
@@ -500,7 +551,6 @@ int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar
     int i_result;
     /* In case of error : */
     int i_thread = -1;
-    const char * psz_error = "";
 
 #if defined( PTH_INIT_IN_PTH_H )
     return 0;
@@ -530,7 +580,7 @@ int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar
     if( i_result )
     {
         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
-        psz_error = strerror(i_result);
+        errno = i_result;
     }
 
 #elif defined( HAVE_CTHREADS_H )
@@ -541,8 +591,9 @@ int __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar
     if( i_result )
     {
         msg_Err( p_condvar->p_this,
-                 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
-                 i_thread, psz_file, i_line, i_result, psz_error );
+                 "thread %d: cond_destroy failed at %s:%d (%d:%m)",
+                 i_thread, psz_file, i_line, i_result );
+        vlc_threads_error( p_condvar->p_this );
     }
     return i_result;
 }
@@ -665,8 +716,9 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line
         if( (i_error = pthread_setschedparam( p_priv->thread_id,
                                                i_policy, &param )) )
         {
-            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
-                      psz_file, i_line, strerror(i_error) );
+            errno = i_error;
+            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
+                      psz_file, i_line );
             i_priority = 0;
         }
     }
@@ -708,8 +760,10 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line
     }
     else
     {
-        msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
-                         psz_name, psz_file, i_line, strerror(i_ret) );
+        errno = i_ret;
+        msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
+                         psz_name, psz_file, i_line );
+        vlc_threads_error( p_this );
         vlc_mutex_unlock( &p_this->object_lock );
     }
 
@@ -762,8 +816,9 @@ int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
         if( (i_error = pthread_setschedparam( p_priv->thread_id,
                                                i_policy, &param )) )
         {
-            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
-                      psz_file, i_line, strerror(i_error) );
+            errno = i_error;
+            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
+                      psz_file, i_line );
             i_priority = 0;
         }
     }
@@ -810,6 +865,7 @@ void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line
         msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
                          (unsigned int)p_priv->thread_id.id,
              psz_file, i_line, GetLastError() );
+        vlc_threads_error( p_this );
         p_priv->b_thread = VLC_FALSE;
         return;
     }
@@ -881,9 +937,10 @@ void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line
 
     if( i_ret )
     {
-        msg_Err( p_this, "thread_join(%u) failed at %s:%d (%s)",
-                         (unsigned int)p_priv->thread_id, psz_file, i_line,
-                         strerror(i_ret) );
+        errno = i_ret;
+        msg_Err( p_this, "thread_join(%u) failed at %s:%d (%m)",
+                         (unsigned int)p_priv->thread_id, psz_file, i_line );
+        vlc_threads_error( p_this );
     }
     else
     {