]> git.sesse.net Git - vlc/blobdiff - include/threads.h
* ALL: internationalized all configuration strings.
[vlc] / include / threads.h
index a03da32d8955cf53b5b9241c4a01f3aa459fcb3a..f56180e7e4633abc5a13ec95adef06022ebe28ba 100644 (file)
@@ -3,7 +3,7 @@
  * This header provides a portable threads implementation.
  *****************************************************************************
  * Copyright (C) 1999, 2000 VideoLAN
  * This header provides a portable threads implementation.
  *****************************************************************************
  * Copyright (C) 1999, 2000 VideoLAN
- * $Id: threads.h,v 1.36 2002/02/27 03:47:56 sam Exp $
+ * $Id: threads.h,v 1.41 2002/04/18 12:51:59 sam Exp $
  *
  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
  *          Samuel Hocevar <sam@via.ecp.fr>
  *
  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
  *          Samuel Hocevar <sam@via.ecp.fr>
 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
 #   include <st.h>
 
 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
 #   include <st.h>
 
+#elif defined( WIN32 )
+#   include <process.h>
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
 #   include <pthread.h>
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
 #   include <pthread.h>
+#   ifdef DEBUG
+/* Needed for pthread_cond_timedwait */
+#       include <errno.h>
+#   endif
 /* This is not prototyped under Linux, though it exists. */
 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
 
 /* This is not prototyped under Linux, though it exists. */
 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
 
@@ -48,11 +55,6 @@ int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
 #   include <kernel/scheduler.h>
 #   include <byteorder.h>
 
 #   include <kernel/scheduler.h>
 #   include <byteorder.h>
 
-#elif defined( WIN32 )
-#define WIN32_LEAN_AND_MEAN
-#   include <windows.h>
-#   include <process.h>
-
 #else
 #   error no threads available on your system !
 
 #else
 #   error no threads available on your system !
 
@@ -98,6 +100,23 @@ typedef st_thread_t *    vlc_thread_t;
 typedef st_mutex_t *     vlc_mutex_t;
 typedef st_cond_t *      vlc_cond_t;
 
 typedef st_mutex_t *     vlc_mutex_t;
 typedef st_cond_t *      vlc_cond_t;
 
+#elif defined( WIN32 )
+typedef HANDLE vlc_thread_t;
+
+typedef struct
+{
+    CRITICAL_SECTION csection;
+    HANDLE           mutex;
+} vlc_mutex_t;
+
+typedef struct
+{
+    int             i_waiting_threads;
+    HANDLE          signal;
+} vlc_cond_t;
+
+typedef unsigned (__stdcall *PTHREAD_START) (void *);
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 typedef pthread_t        vlc_thread_t;
 typedef pthread_mutex_t  vlc_mutex_t;
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 typedef pthread_t        vlc_thread_t;
 typedef pthread_mutex_t  vlc_mutex_t;
@@ -142,18 +161,6 @@ typedef struct
     thread_id       thread;
 } vlc_cond_t;
 
     thread_id       thread;
 } vlc_cond_t;
 
-#elif defined( WIN32 )
-typedef HANDLE           vlc_thread_t;
-typedef CRITICAL_SECTION vlc_mutex_t;
-
-typedef struct
-{
-    int             i_waiting_threads;
-    HANDLE          signal;
-} vlc_cond_t;
-
-typedef unsigned (__stdcall *PTHREAD_START) (void *);
-
 #endif
 
 typedef void *(*vlc_thread_func_t)(void *p_data);
 #endif
 
 typedef void *(*vlc_thread_func_t)(void *p_data);
@@ -209,6 +216,9 @@ static __inline__ int vlc_threads_init( void )
 #elif defined( ST_INIT_IN_ST_H )
     return st_init();
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_init();
 
+#elif defined( WIN32 )
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return 0;
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return 0;
 
@@ -218,9 +228,6 @@ static __inline__ int vlc_threads_init( void )
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     return 0;
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     return 0;
 
-#elif defined( WIN32 )
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -235,6 +242,9 @@ static __inline__ int vlc_threads_end( void )
 #elif defined( ST_INIT_IN_ST_H )
     return 0;
 
 #elif defined( ST_INIT_IN_ST_H )
     return 0;
 
+#elif defined( WIN32 )
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return 0;
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return 0;
 
@@ -244,9 +254,6 @@ static __inline__ int vlc_threads_end( void )
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     return 0;
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     return 0;
 
-#elif defined( WIN32 )
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -262,6 +269,24 @@ static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
     *p_mutex = st_mutex_new();
     return ( *p_mutex == NULL ) ? errno : 0;
 
     *p_mutex = st_mutex_new();
     return ( *p_mutex == NULL ) ? errno : 0;
 
+#elif defined( WIN32 )
+    /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
+     * function and have a 100% correct vlc_cond_wait() implementation.
+     * As this function is not available on Win9x, we can use the faster
+     * CriticalSections */
+    if( (GetVersion() < 0x80000000) && !p_main_sys->b_fast_pthread )
+    {
+        /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
+        p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
+        return ( p_mutex->mutex ? 0 : 1 );
+    }
+    else
+    {
+        InitializeCriticalSection( &p_mutex->csection );
+        p_mutex->mutex = NULL;
+        return 0;
+    }
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 #   if defined(DEBUG) && defined(SYS_LINUX)
     /* Create error-checking mutex to detect threads problems more easily. */
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 #   if defined(DEBUG) && defined(SYS_LINUX)
     /* Create error-checking mutex to detect threads problems more easily. */
@@ -303,10 +328,6 @@ static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
     p_mutex->init = 9999;
     return B_OK;
 
     p_mutex->init = 9999;
     return B_OK;
 
-#elif defined( WIN32 )
-    InitializeCriticalSection( p_mutex );
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -318,7 +339,7 @@ static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
         _vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_lock( P_MUTEX )                                        \
         _vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_lock( P_MUTEX )                                        \
-        _vlc_mutex_lock( NULL, 0, P_MUTEX )
+        _vlc_mutex_lock( "(unknown)", 0, P_MUTEX )
 #endif
 
 static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
@@ -330,6 +351,17 @@ static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_lock( *p_mutex );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_lock( *p_mutex );
 
+#elif defined( WIN32 )
+    if( p_mutex->mutex )
+    {
+        WaitForSingleObject( p_mutex->mutex, INFINITE );
+    }
+    else
+    {
+        EnterCriticalSection( &p_mutex->csection );
+    }
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_return = pthread_mutex_lock( p_mutex );
     if( i_return )
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_return = pthread_mutex_lock( p_mutex );
     if( i_return )
@@ -359,10 +391,6 @@ static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
     err = acquire_sem( p_mutex->lock );
     return err;
 
     err = acquire_sem( p_mutex->lock );
     return err;
 
-#elif defined( WIN32 )
-    EnterCriticalSection( p_mutex );
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -374,7 +402,7 @@ static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
         _vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_unlock( P_MUTEX )                                      \
         _vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_unlock( P_MUTEX )                                      \
-        _vlc_mutex_unlock( NULL, 0, P_MUTEX )
+        _vlc_mutex_unlock( "(unknown)", 0, P_MUTEX )
 #endif
 
 static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
@@ -386,6 +414,17 @@ static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_unlock( *p_mutex );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_unlock( *p_mutex );
 
+#elif defined( WIN32 )
+    if( p_mutex->mutex )
+    {
+        ReleaseMutex( p_mutex->mutex );
+    }
+    else
+    {
+        LeaveCriticalSection( &p_mutex->csection );
+    }
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_return = pthread_mutex_unlock( p_mutex );
     if( i_return )
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_return = pthread_mutex_unlock( p_mutex );
     if( i_return )
@@ -413,10 +452,6 @@ static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
     release_sem( p_mutex->lock );
     return B_OK;
 
     release_sem( p_mutex->lock );
     return B_OK;
 
-#elif defined( WIN32 )
-    LeaveCriticalSection( p_mutex );
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -428,7 +463,7 @@ static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
         _vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_destroy( P_MUTEX )                                     \
         _vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
 #else
 #   define vlc_mutex_destroy( P_MUTEX )                                     \
-        _vlc_mutex_destroy( NULL, 0, P_MUTEX )
+        _vlc_mutex_destroy( "(unknown)", 0, P_MUTEX )
 #endif
 
 static __inline__ int _vlc_mutex_destroy( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_mutex_destroy( char * psz_file, int i_line,
@@ -440,6 +475,17 @@ static __inline__ int _vlc_mutex_destroy( char * psz_file, int i_line,
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_destroy( *p_mutex );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_mutex_destroy( *p_mutex );
 
+#elif defined( WIN32 )
+    if( p_mutex->mutex )
+    {
+        CloseHandle( p_mutex->mutex );
+    }
+    else
+    {
+        DeleteCriticalSection( &p_mutex->csection );
+    }
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
     int i_return = pthread_mutex_destroy( p_mutex );
     if( i_return )
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
     int i_return = pthread_mutex_destroy( p_mutex );
     if( i_return )
@@ -461,10 +507,6 @@ static __inline__ int _vlc_mutex_destroy( char * psz_file, int i_line,
     p_mutex->init = 0;
     return B_OK;
 
     p_mutex->init = 0;
     return B_OK;
 
-#elif defined( WIN32 )
-    DeleteCriticalSection( p_mutex );
-    return 0;
-
 #endif    
 }
 
 #endif    
 }
 
@@ -480,6 +522,18 @@ static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
     *p_condvar = st_cond_new();
     return ( *p_condvar == NULL ) ? errno : 0;
 
     *p_condvar = st_cond_new();
     return ( *p_condvar == NULL ) ? errno : 0;
 
+#elif defined( WIN32 )
+    /* initialise counter */
+    p_condvar->i_waiting_threads = 0;
+
+    /* Create an auto-reset event. */
+    p_condvar->signal = CreateEvent( NULL, /* no security */
+                                     FALSE,  /* auto-reset event */
+                                     FALSE,  /* non-signaled initially */
+                                     NULL ); /* unnamed */
+
+    return( !p_condvar->signal );
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_init( p_condvar, NULL );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_init( p_condvar, NULL );
 
@@ -507,18 +561,6 @@ static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
     p_condvar->init = 9999;
     return 0;
 
     p_condvar->init = 9999;
     return 0;
 
-#elif defined( WIN32 )
-    /* initialise counter */
-    p_condvar->i_waiting_threads = 0;
-
-    /* Create an auto-reset event. */
-    p_condvar->signal = CreateEvent( NULL, /* no security */
-                                     FALSE,  /* auto-reset event */
-                                     FALSE,  /* non-signaled initially */
-                                     NULL ); /* unnamed */
-
-    return( !p_condvar->signal );
-    
 #endif
 }
 
 #endif
 }
 
@@ -533,6 +575,13 @@ static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_signal( *p_condvar );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_signal( *p_condvar );
 
+#elif defined( WIN32 )
+    /* Release one waiting thread if one is available. */
+    /* For this trick to work properly, the vlc_cond_signal must be surrounded
+     * by a mutex. This will prevent another thread from stealing the signal */
+    PulseEvent( p_condvar->signal );
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_signal( p_condvar );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_signal( p_condvar );
 
@@ -580,19 +629,6 @@ static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
     }
     return 0;
 
     }
     return 0;
 
-#elif defined( WIN32 )
-    /* Release one waiting thread if one is available. */
-    /* For this trick to work properly, the vlc_cond_signal must be surrounded
-     * by a mutex. This will prevent another thread from stealing the signal */
-    int i_waiting_threads = p_condvar->i_waiting_threads;
-    while( p_condvar->i_waiting_threads
-           && p_condvar->i_waiting_threads == i_waiting_threads )
-    {
-        PulseEvent( p_condvar->signal );
-        Sleep( 0 ); /* deschedule the current thread */
-    }
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -612,6 +648,17 @@ static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_broadcast( p_condvar );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_broadcast( p_condvar );
 
+#elif defined( WIN32 )
+    /* Release all waiting threads. */
+    /* For this trick to work properly, the vlc_cond_signal must be surrounded
+     * by a mutex. This will prevent another thread from stealing the signal */
+    while( p_condvar->i_waiting_threads )
+    {
+        PulseEvent( p_condvar->signal );
+        Sleep( 1 ); /* deschedule the current thread */
+    }
+    return 0;
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_broadcast( p_condvar );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     return pthread_cond_broadcast( p_condvar );
 
@@ -659,17 +706,6 @@ static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
     }
     return 0;
 
     }
     return 0;
 
-#elif defined( WIN32 )
-    /* Release all waiting threads. */
-    /* For this trick to work properly, the vlc_cond_signal must be surrounded
-     * by a mutex. This will prevent another thread from stealing the signal */
-    while( p_condvar->i_waiting_threads )
-    {
-        PulseEvent( p_condvar->signal );
-        Sleep( 0 ); /* deschedule the current thread */
-    }
-    return 0;
-
 #endif
 }
 
 #endif
 }
 
@@ -681,7 +717,7 @@ static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
         _vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
 #else
 #   define vlc_cond_wait( P_COND, P_MUTEX )                                 \
         _vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
 #else
 #   define vlc_cond_wait( P_COND, P_MUTEX )                                 \
-        _vlc_cond_wait( NULL, 0, P_COND, P_MUTEX )
+        _vlc_cond_wait( "(unknown)", 0, P_COND, P_MUTEX )
 #endif
 
 static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
@@ -700,6 +736,38 @@ static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
 
     return i_ret;
 
 
     return i_ret;
 
+#elif defined( WIN32 )
+    /* The ideal would be to use a function which atomically releases the
+     * mutex and initiate the waiting.
+     * Unfortunately only the SignalObjectAndWait function does this and it's
+     * only supported on WinNT/2K, furthermore it cannot take multiple
+     * events as parameters.
+     *
+     * The solution we use should however fulfill all our needs (even though
+     * it is not a correct pthreads implementation)
+     */
+    int i_result;
+
+    p_condvar->i_waiting_threads ++;
+
+    if( p_mutex->mutex )
+    {
+        p_main_sys->SignalObjectAndWait( p_mutex->mutex, p_condvar->signal,
+                                        INFINITE, FALSE );
+    }
+    else
+    {
+        /* Release the mutex */
+        vlc_mutex_unlock( p_mutex );
+        i_result = WaitForSingleObject( p_condvar->signal, INFINITE); 
+        p_condvar->i_waiting_threads --;
+    }
+
+    /* Reacquire the mutex before returning. */
+    vlc_mutex_lock( p_mutex );
+
+    return( i_result == WAIT_FAILED );
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 
 #ifndef DEBUG
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
 
 #ifndef DEBUG
@@ -716,15 +784,21 @@ static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
         timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
         timeout.tv_nsec = now.tv_usec * 1000;
 
         timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
         timeout.tv_nsec = now.tv_usec * 1000;
 
-        if( (i_result = pthread_cond_timedwait( p_condvar, p_mutex, &timeout )) )
+        i_result = pthread_cond_timedwait( p_condvar, p_mutex, &timeout );
+
+        if( i_result == ETIMEDOUT )
         {
             intf_WarnMsg( 1, "thread %d warning: Possible deadlock detected in cond_wait at %s:%d (%s)",
                           pthread_self(), psz_file, i_line, strerror(i_result) );
         {
             intf_WarnMsg( 1, "thread %d warning: Possible deadlock detected in cond_wait at %s:%d (%s)",
                           pthread_self(), psz_file, i_line, strerror(i_result) );
+            continue;
         }
         }
-        else
+
+        if( i_result )
         {
         {
-            return i_result;
+            intf_ErrMsg( "thread %d error: cond_wait failed at %s:%d (%s)",
+                         pthread_self(), psz_file, i_line, strerror(i_result) );
         }
         }
+        return( i_result );
     }
 #endif
 
     }
 #endif
 
@@ -759,33 +833,6 @@ static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
     vlc_mutex_lock( p_mutex );
     return 0;
 
     vlc_mutex_lock( p_mutex );
     return 0;
 
-#elif defined( WIN32 )
-    /* The ideal would be to use a function which atomically releases the
-     * mutex and initiate the waiting.
-     * Unfortunately only the SignalObjectAndWait function does this and it's
-     * only supported on WinNT/2K, furthermore it cannot take multiple
-     * events as parameters.
-     *
-     * The solution we use should however fulfill all our needs (even though
-     * it is not a correct pthreads implementation)
-     */
-    int i_result;
-
-    p_condvar->i_waiting_threads ++;
-
-    /* Release the mutex */
-    vlc_mutex_unlock( p_mutex );
-
-    i_result = WaitForSingleObject( p_condvar->signal, INFINITE); 
-
-    /* maybe we should protect this with a mutex ? */
-    p_condvar->i_waiting_threads --;
-
-    /* Reacquire the mutex before returning. */
-    vlc_mutex_lock( p_mutex );
-
-    return( i_result == WAIT_FAILED );
-
 #endif
 }
 
 #endif
 }
 
@@ -797,7 +844,7 @@ static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
         _vlc_cond_destroy( __FILE__, __LINE__, P_COND )
 #else
 #   define vlc_cond_destroy( P_COND )                                       \
         _vlc_cond_destroy( __FILE__, __LINE__, P_COND )
 #else
 #   define vlc_cond_destroy( P_COND )                                       \
-        _vlc_cond_destroy( NULL, 0, P_COND )
+        _vlc_cond_destroy( "(unknown)", 0, P_COND )
 #endif
 
 static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
@@ -809,6 +856,9 @@ static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_destroy( *p_condvar );
 
 #elif defined( ST_INIT_IN_ST_H )
     return st_cond_destroy( *p_condvar );
 
+#elif defined( WIN32 )
+    return( !CloseHandle( p_condvar->signal ) );
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_result = pthread_cond_destroy( p_condvar );
     if( i_result )
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     int i_result = pthread_cond_destroy( p_condvar );
     if( i_result )
@@ -825,9 +875,6 @@ static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
     p_condvar->init = 0;
     return 0;
 
     p_condvar->init = 0;
     return 0;
 
-#elif defined( WIN32 )
-    return( !CloseHandle( p_condvar->signal ) );
-
 #endif    
 }
 
 #endif    
 }
 
@@ -839,7 +886,7 @@ static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
         _vlc_thread_create( __FILE__, __LINE__, P_THREAD, PSZ_NAME, FUNC, P_DATA )
 #else
 #   define vlc_thread_create( P_THREAD, PSZ_NAME, FUNC, P_DATA )            \
         _vlc_thread_create( __FILE__, __LINE__, P_THREAD, PSZ_NAME, FUNC, P_DATA )
 #else
 #   define vlc_thread_create( P_THREAD, PSZ_NAME, FUNC, P_DATA )            \
-        _vlc_thread_create( NULL, 0, P_THREAD, PSZ_NAME, FUNC, P_DATA )
+        _vlc_thread_create( "(unknown)", 0, P_THREAD, PSZ_NAME, FUNC, P_DATA )
 #endif
 
 static __inline__ int _vlc_thread_create( char * psz_file, int i_line,
 #endif
 
 static __inline__ int _vlc_thread_create( char * psz_file, int i_line,
@@ -875,6 +922,16 @@ static __inline__ int _vlc_thread_create( char * psz_file, int i_line,
     *p_thread = st_thread_create( func, p_data, 1, 0 );
     i_ret = ( p_thread == NULL );
     
     *p_thread = st_thread_create( func, p_data, 1, 0 );
     i_ret = ( p_thread == NULL );
     
+#elif defined( WIN32 )
+    unsigned threadID;
+    /* When using the MSVCRT C library you have to use the _beginthreadex
+     * function instead of CreateThread, otherwise you'll end up with memory
+     * leaks and the signal functions not working */
+    *p_thread = (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
+                                        p_data, 0, &threadID );
+    
+    i_ret = ( *p_thread ? 0 : 1 );
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     i_ret = pthread_create( p_thread, NULL, func, p_data );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     i_ret = pthread_create( p_thread, NULL, func, p_data );
 
@@ -887,23 +944,6 @@ static __inline__ int _vlc_thread_create( char * psz_file, int i_line,
                               B_NORMAL_PRIORITY, p_data );
     i_ret = resume_thread( *p_thread );
 
                               B_NORMAL_PRIORITY, p_data );
     i_ret = resume_thread( *p_thread );
 
-#elif defined( WIN32 )
-#if 0
-    DWORD threadID;
-    /* This method is not recommended when using the MSVCRT C library,
-     * so we'll have to use _beginthreadex instead */
-    *p_thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, 
-                             p_data, 0, &threadID);
-#endif
-    unsigned threadID;
-    /* When using the MSVCRT C library you have to use the _beginthreadex
-     * function instead of CreateThread, otherwise you'll end up with memory
-     * leaks and the signal function not working */
-    *p_thread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) func, 
-                             p_data, 0, &threadID);
-    
-    i_ret = ( *p_thread ? 0 : 1 );
-
 #endif
 
 #ifdef GPROF
 #endif
 
 #ifdef GPROF
@@ -943,6 +983,11 @@ static __inline__ void vlc_thread_exit( void )
     int result;
     st_thread_exit( &result );
     
     int result;
     st_thread_exit( &result );
     
+#elif defined( WIN32 )
+    /* For now we don't close the thread handles (because of race conditions).
+     * Need to be looked at. */
+    _endthreadex(0);
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     pthread_exit( 0 );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     pthread_exit( 0 );
 
@@ -953,14 +998,6 @@ static __inline__ void vlc_thread_exit( void )
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     exit_thread( 0 );
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     exit_thread( 0 );
 
-#elif defined( WIN32 )
-#if 0
-    ExitThread( 0 );
-#endif
-    /* For now we don't close the thread handles (because of race conditions).
-     * Need to be looked at. */
-    _endthreadex(0);
-
 #endif
 }
 
 #endif
 }
 
@@ -972,7 +1009,7 @@ static __inline__ void vlc_thread_exit( void )
         _vlc_thread_join( __FILE__, __LINE__, THREAD ) 
 #else
 #   define vlc_thread_join( THREAD )                                        \
         _vlc_thread_join( __FILE__, __LINE__, THREAD ) 
 #else
 #   define vlc_thread_join( THREAD )                                        \
-        _vlc_thread_join( NULL, 0, THREAD ) 
+        _vlc_thread_join( "(unknown)", 0, THREAD ) 
 #endif
 
 static __inline__ void _vlc_thread_join( char * psz_file, int i_line,
 #endif
 
 static __inline__ void _vlc_thread_join( char * psz_file, int i_line,
@@ -986,6 +1023,9 @@ static __inline__ void _vlc_thread_join( char * psz_file, int i_line,
 #elif defined( ST_INIT_IN_ST_H )
     i_ret = st_thread_join( thread, NULL );
     
 #elif defined( ST_INIT_IN_ST_H )
     i_ret = st_thread_join( thread, NULL );
     
+#elif defined( WIN32 )
+    WaitForSingleObject( thread, INFINITE );
+
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     i_ret = pthread_join( thread, NULL );
 
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     i_ret = pthread_join( thread, NULL );
 
@@ -997,9 +1037,6 @@ static __inline__ void _vlc_thread_join( char * psz_file, int i_line,
     int32 exit_value;
     wait_for_thread( thread, &exit_value );
 
     int32 exit_value;
     wait_for_thread( thread, &exit_value );
 
-#elif defined( WIN32 )
-    WaitForSingleObject( thread, INFINITE );
-
 #endif
 
     if( i_ret )
 #endif
 
     if( i_ret )
@@ -1033,4 +1070,3 @@ static void *vlc_thread_wrapper( void *p_wrapper )
     return func( p_data );
 }
 #endif
     return func( p_data );
 }
 #endif
-