* 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 */
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 )
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
/*****************************************************************************
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,
}
#ifndef vlc_assert_locked
-# define vlc_assert_locked( m ) (void)0
+# define vlc_assert_locked( m ) (void)m
#endif
/*****************************************************************************
/* 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 )
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 );
(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 );
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 );
(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 */
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
}
* 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