* 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 */
#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 )
}
#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
}