]> git.sesse.net Git - vlc/blobdiff - include/vlc_threads.h
Fix memleak in opencv.
[vlc] / include / vlc_threads.h
index bb57941980aabe319ef43cbd196617b57d839d5a..01e8b716aa389c84d993fc2f467fc78b48ea87b7 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_
 
@@ -38,7 +34,7 @@
 #   include <process.h>                                         /* Win32 API */
 #   include <errno.h>
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
+#elif defined( SYS_BEOS )                                            /* BeOS */
 #   include <kernel/OS.h>
 #   include <kernel/scheduler.h>
 #   include <byteorder.h>
@@ -47,6 +43,7 @@
 #   define LIBVLC_USE_PTHREAD 1
 #   define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
 
+#   include <stdlib.h> /* lldiv_t definition (only in C99) */
 #   include <unistd.h> /* _POSIX_SPIN_LOCKS */
 #   include <pthread.h>
     /* Needed for pthread_cond_timedwait */
 
 /* 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
 #   define VLC_THREAD_PRIORITY_HIGHEST 15
 
 #elif defined(LIBVLC_USE_PTHREAD)
-#   define VLC_THREAD_PRIORITY_LOW 0
-#   define VLC_THREAD_PRIORITY_INPUT 20
-#   define VLC_THREAD_PRIORITY_AUDIO 10
-#   define VLC_THREAD_PRIORITY_VIDEO 0
-#   define VLC_THREAD_PRIORITY_OUTPUT 30
-#   define VLC_THREAD_PRIORITY_HIGHEST 40
+#   define VLC_THREAD_PRIORITY_LOW      0
+#   define VLC_THREAD_PRIORITY_INPUT   10
+#   define VLC_THREAD_PRIORITY_AUDIO    5
+#   define VLC_THREAD_PRIORITY_VIDEO    0
+#   define VLC_THREAD_PRIORITY_OUTPUT  15
+#   define VLC_THREAD_PRIORITY_HIGHEST 20
 
 #elif defined(WIN32) || defined(UNDER_CE)
 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
@@ -119,16 +116,7 @@ typedef pthread_cond_t  vlc_cond_t;
 typedef pthread_key_t   vlc_threadvar_t;
 
 #elif defined( WIN32 ) || defined( UNDER_CE )
-typedef struct
-{
-    /* thread id */
-    DWORD  id;
-    /*
-    ** handle to created thread, needs be closed to dispose of it
-    ** even after thread has exited
-    */
-    HANDLE hThread;
-} vlc_thread_t;
+typedef HANDLE  vlc_thread_t;
 
 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
 
@@ -142,7 +130,7 @@ typedef struct
 
 typedef DWORD   vlc_threadvar_t;
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
 /* This is the BeOS implementation of the vlc threads, note that the mutex is
  * not a real mutex and the cond_var is not like a pthread cond_var but it is
  * enough for what we need */
@@ -179,26 +167,28 @@ VLC_EXPORT( int,  vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
 VLC_EXPORT( void,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
 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 * ) );
+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_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
  *****************************************************************************/
 #define vlc_mutex_lock( P_MUTEX )                                           \
     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
 
-#if defined(LIBVLC_USE_PTHREAD)
 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
 
+#if defined(LIBVLC_USE_PTHREAD)
 # define VLC_THREAD_ASSERT( action ) \
     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,
@@ -211,16 +201,16 @@ static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
     VLC_THREAD_ASSERT ("locking mutex");
 
 #elif defined( UNDER_CE )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     EnterCriticalSection( &p_mutex->csection );
 
 #elif defined( WIN32 )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     WaitForSingleObject( *p_mutex, INFINITE );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     acquire_sem( p_mutex->lock );
 
 #endif
@@ -244,16 +234,16 @@ static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
     VLC_THREAD_ASSERT ("unlocking mutex");
 
 #elif defined( UNDER_CE )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     LeaveCriticalSection( &p_mutex->csection );
 
 #elif defined( WIN32 )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     ReleaseMutex( *p_mutex );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     release_sem( p_mutex->lock );
 
 #endif
@@ -285,7 +275,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line,
     VLC_THREAD_ASSERT ("signaling condition variable");
 
 #elif defined( UNDER_CE ) || defined( WIN32 )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     /* Release one waiting thread if one is available. */
     /* For this trick to work properly, the vlc_cond_signal must be surrounded
@@ -295,7 +285,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line,
      * as documented in http://support.microsoft.com/kb/q173260/ */
     PulseEvent( p_condvar->event );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     while( p_condvar->thread != -1 )
     {
         thread_info info;
@@ -343,7 +333,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
     vlc_mutex_lock( p_mutex );
 
 #elif defined( WIN32 )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
+    (void)psz_file; (void)i_line;
 
     /* Increase our wait count */
     p_condvar->i_waiting_threads++;
@@ -353,7 +343,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
     /* Reacquire the mutex before returning. */
     vlc_mutex_lock( p_mutex );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     /* The p_condvar->thread var is initialized before the unlock because
      * it enables to identify when the thread is interrupted beetwen the
      * unlock line and the suspend_thread line */
@@ -392,7 +382,6 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
 
 #elif defined( UNDER_CE )
     mtime_t delay_ms = (deadline - mdate())/1000;
-
     DWORD result;
     if( delay_ms < 0 )
         delay_ms = 0;
@@ -408,12 +397,11 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
     if(result == WAIT_TIMEOUT)
        return ETIMEDOUT; /* this error is perfectly normal */
 
-#elif defined( WIN32 )
-    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
-
-    DWORD result;
+    (void)psz_file; (void)i_line;
 
+#elif defined( WIN32 )
     mtime_t delay_ms = (deadline - mdate())/1000;
+    DWORD result;
     if( delay_ms < 0 )
         delay_ms = 0;
 
@@ -428,7 +416,9 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
     if(result == WAIT_TIMEOUT)
        return ETIMEDOUT; /* this error is perfectly normal */
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+    (void)psz_file; (void)i_line;
+
+#elif defined( SYS_BEOS )
 #   error Unimplemented
 
 #endif
@@ -442,12 +432,6 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
 #define vlc_cond_destroy( P_COND )                                          \
     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
 
-/*****************************************************************************
- * vlc_threadvar_create: create a thread-local variable
- *****************************************************************************/
-#define vlc_threadvar_create( PTHIS, P_TLS )                                 \
-   __vlc_threadvar_create( P_TLS )
-
 /*****************************************************************************
  * vlc_threadvar_set: create: set the value of a thread-local variable
  *****************************************************************************/
@@ -458,7 +442,7 @@ static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
 #if defined(LIBVLC_USE_PTHREAD)
     i_ret = pthread_setspecific( *p_tls, p_value );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     i_ret = EINVAL;
 
 #elif defined( UNDER_CE ) || defined( WIN32 )
@@ -479,7 +463,7 @@ static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
 #if defined(LIBVLC_USE_PTHREAD)
     p_ret = pthread_getspecific( *p_tls );
 
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
+#elif defined( SYS_BEOS )
     p_ret = NULL;
 
 #elif defined( UNDER_CE ) || defined( WIN32 )
@@ -576,6 +560,31 @@ 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");
+#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!
+#endif
+}
+
 /*****************************************************************************
  * vlc_thread_create: create a thread
  *****************************************************************************/
@@ -588,12 +597,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
  *****************************************************************************/