]> git.sesse.net Git - vlc/blobdiff - include/vlc_threads.h
vlc_clone, vlc_join: untangle objects and threads
[vlc] / include / vlc_threads.h
index f025faef5b8737dfea109b3a4eb760df376e4c96..748f15edb0042bba2644d5f40d81a0070251b3a9 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#ifndef _VLC_THREADS_H_
-#define _VLC_THREADS_H_
+#ifndef VLC_THREADS_H_
+#define VLC_THREADS_H_
+
+/**
+ * \file
+ * This file defines structures and functions for handling threads in vlc
+ *
+ */
 
 #if defined( UNDER_CE )
                                                                 /* WinCE API */
@@ -116,18 +122,15 @@ typedef pthread_cond_t  vlc_cond_t;
 typedef pthread_key_t   vlc_threadvar_t;
 
 #elif defined( WIN32 ) || defined( UNDER_CE )
-typedef HANDLE  vlc_thread_t;
-
-typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
-
-typedef HANDLE  vlc_mutex_t;
-
 typedef struct
 {
-    volatile int        i_waiting_threads;
-    HANDLE              event;
-} vlc_cond_t;
+    HANDLE handle;
+    void  *(*entry) (void *);
+    void  *data;
+} *vlc_thread_t;
 
+typedef HANDLE  vlc_mutex_t;
+typedef HANDLE  vlc_cond_t;
 typedef DWORD   vlc_threadvar_t;
 
 #elif defined( SYS_BEOS )
@@ -169,10 +172,13 @@ VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_cond_t * ) );
 VLC_EXPORT( void,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
 VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
 VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
-VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, bool ) );
+VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( vlc_object_t * ), int, bool ) );
 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
 
+VLC_EXPORT( int, vlc_clone, (vlc_thread_t *, void * (*) (void *), void *, int) );
+VLC_EXPORT( int, vlc_join, (vlc_thread_t, void **) );
+
 #define vlc_thread_ready vlc_object_signal
 
 /*****************************************************************************
@@ -188,7 +194,7 @@ VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *
     if (val) \
         vlc_pthread_fatal (action, val, psz_file, i_line)
 #else
-# define VLC_THREAD_ASSERT (void)0
+# define VLC_THREAD_ASSERT ((void)(val))
 #endif
 
 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
@@ -217,7 +223,7 @@ static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
 }
 
 #ifndef vlc_assert_locked
-# define vlc_assert_locked( m ) (void)0
+# define vlc_assert_locked( m ) (void)m
 #endif
 
 /*****************************************************************************
@@ -283,7 +289,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line,
     /* PulseEvent() only works if none of the waiting threads is suspended.
      * This is particularily problematic under a debug session.
      * as documented in http://support.microsoft.com/kb/q173260/ */
-    PulseEvent( p_condvar->event );
+    PulseEvent( *p_condvar );
 
 #elif defined( SYS_BEOS )
     while( p_condvar->thread != -1 )
@@ -324,10 +330,8 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
     VLC_THREAD_ASSERT ("waiting on condition");
 
 #elif defined( UNDER_CE )
-    p_condvar->i_waiting_threads++;
     LeaveCriticalSection( &p_mutex->csection );
-    WaitForSingleObject( p_condvar->event, INFINITE );
-    p_condvar->i_waiting_threads--;
+    WaitForSingleObject( *p_condvar, INFINITE );
 
     /* Reacquire the mutex before returning. */
     vlc_mutex_lock( p_mutex );
@@ -336,9 +340,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
     (void)psz_file; (void)i_line;
 
     /* Increase our wait count */
-    p_condvar->i_waiting_threads++;
-    SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
-    p_condvar->i_waiting_threads--;
+    SignalObjectAndWait( *p_mutex, *p_condvar, INFINITE, FALSE );
 
     /* Reacquire the mutex before returning. */
     vlc_mutex_lock( p_mutex );
@@ -386,10 +388,8 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
     if( delay_ms < 0 )
         delay_ms = 0;
 
-    p_condvar->i_waiting_threads++;
     LeaveCriticalSection( &p_mutex->csection );
-    result = WaitForSingleObject( p_condvar->event, delay_ms );
-    p_condvar->i_waiting_threads--;
+    result = WaitForSingleObject( *p_condvar, delay_ms );
 
     /* Reacquire the mutex before returning. */
     vlc_mutex_lock( p_mutex );
@@ -400,19 +400,22 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
     (void)psz_file; (void)i_line;
 
 #elif defined( WIN32 )
-    mtime_t delay_ms = (deadline - mdate())/1000;
+    mtime_t total = (deadline - mdate())/1000;
     DWORD result;
-    if( delay_ms < 0 )
-        delay_ms = 0;
+    if( total < 0 )
+        total = 0;
 
-    /* Increase our wait count */
-    p_condvar->i_waiting_threads++;
-    result = SignalObjectAndWait( *p_mutex, p_condvar->event,
-                                  delay_ms, FALSE );
-    p_condvar->i_waiting_threads--;
+    do
+    {
+        DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
+        result = SignalObjectAndWait( *p_mutex, *p_condvar,
+                                      delay, FALSE );
+        total -= delay;
+        vlc_mutex_lock (p_mutex);
+    }
+    while (total);
 
     /* Reacquire the mutex before returning. */
-    vlc_mutex_lock( p_mutex );
     if(result == WAIT_TIMEOUT)
        return ETIMEDOUT; /* this error is perfectly normal */
 
@@ -576,12 +579,12 @@ static inline void barrier (void)
     asm volatile ("sync":::"memory");
 #elif defined(__i386__)
     asm volatile ("mfence":::"memory");
-#elif defined (LIBVLC_USE_PTHREAD)
-    static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
-    pthread_mutex_lock (&lock);
-    pthread_mutex_unlock (&lock);
 #else
-# error barrier not implemented!
+    vlc_spinlock_t spin;
+    vlc_spin_init (&spin);
+    vlc_spin_lock (&spin);
+    vlc_spin_unlock (&spin);
+    vlc_spin_destroy (&spin);
 #endif
 }
 
@@ -589,7 +592,7 @@ static inline void barrier (void)
  * vlc_thread_create: create a thread
  *****************************************************************************/
 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
-    __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
+    __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, FUNC, PRIORITY, WAIT )
 
 /*****************************************************************************
  * vlc_thread_set_priority: set the priority of the calling thread