]> git.sesse.net Git - vlc/blobdiff - include/vlc_threads.h
Oops.
[vlc] / include / vlc_threads.h
index 9cd09408b581ec0f14a7d34fb2dbdf4d3833a00f..2eac7f91dfa4d6781a91173175db5d2ab49ff155 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#if !defined( __LIBVLC__ )
-  #error You are not libvlc or one of its plugins. You cannot include this file
-#endif
+#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 */
 
 /* Thread priorities */
 #ifdef __APPLE__
-#   define VLC_THREAD_PRIORITY_LOW (-47)
-#   define VLC_THREAD_PRIORITY_INPUT 37
-#   define VLC_THREAD_PRIORITY_AUDIO 37
-#   define VLC_THREAD_PRIORITY_VIDEO (-47)
-#   define VLC_THREAD_PRIORITY_OUTPUT 37
-#   define VLC_THREAD_PRIORITY_HIGHEST 37
+#   define VLC_THREAD_PRIORITY_LOW      0
+#   define VLC_THREAD_PRIORITY_INPUT   22
+#   define VLC_THREAD_PRIORITY_AUDIO   22
+#   define VLC_THREAD_PRIORITY_VIDEO    0
+#   define VLC_THREAD_PRIORITY_OUTPUT  22
+#   define VLC_THREAD_PRIORITY_HIGHEST 22
 
 #elif defined(SYS_BEOS)
 #   define VLC_THREAD_PRIORITY_LOW 5
@@ -121,17 +123,8 @@ 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;
-
+typedef HANDLE  vlc_cond_t;
 typedef DWORD   vlc_threadvar_t;
 
 #elif defined( SYS_BEOS )
@@ -173,11 +166,12 @@ 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_ready,  ( vlc_object_t * ) );
 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
 
+#define vlc_thread_ready vlc_object_signal
+
 /*****************************************************************************
  * vlc_mutex_lock: lock a mutex
  *****************************************************************************/
@@ -191,7 +185,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,
@@ -220,7 +214,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
 
 /*****************************************************************************
@@ -286,7 +280,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 )
@@ -327,10 +321,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 );
@@ -339,9 +331,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 );
@@ -389,10 +379,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 );
@@ -403,19 +391,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 */
 
@@ -563,11 +554,36 @@ static inline int vlc_spin_init (vlc_spinlock_t *spin)
 # define vlc_spin_destroy vlc_mutex_destroy
 #endif
 
+/**
+ * Issues a full memory barrier.
+ */
+#if defined (__APPLE__)
+# include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
+#endif
+static inline void barrier (void)
+{
+#if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
+    __sync_synchronize ();
+#elif defined(__APPLE__)
+    OSMemoryBarrier ();
+#elif defined(__powerpc__)
+    asm volatile ("sync":::"memory");
+#elif defined(__i386__)
+    asm volatile ("mfence":::"memory");
+#else
+    vlc_spinlock_t spin;
+    vlc_spin_init (&spin);
+    vlc_spin_lock (&spin);
+    vlc_spin_unlock (&spin);
+    vlc_spin_destroy (&spin);
+#endif
+}
+
 /*****************************************************************************
  * 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
@@ -575,12 +591,6 @@ static inline int vlc_spin_init (vlc_spinlock_t *spin)
 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
 
-/*****************************************************************************
- * vlc_thread_ready: tell the parent thread we were successfully spawned
- *****************************************************************************/
-#define vlc_thread_ready( P_THIS )                                          \
-    __vlc_thread_ready( VLC_OBJECT(P_THIS) )
-
 /*****************************************************************************
  * vlc_thread_join: wait until a thread exits
  *****************************************************************************/