+}
+
+
+/*****************************************************************************
+ * vlc_cond_timedwait: wait until condition completion or expiration
+ *****************************************************************************
+ * Returns 0 if object signaled, an error code in case of timeout or error.
+ *****************************************************************************/
+#define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
+ __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
+
+static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
+ vlc_cond_t *p_condvar,
+ vlc_mutex_t *p_mutex,
+ mtime_t deadline )
+{
+#if defined(LIBVLC_USE_PTHREAD)
+ lldiv_t d = lldiv( deadline, 1000000 );
+ struct timespec ts = { d.quot, d.rem * 1000 };
+
+ int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
+ if (val == ETIMEDOUT)
+ return ETIMEDOUT; /* this error is perfectly normal */
+ VLC_THREAD_ASSERT ("timed-waiting on condition");
+
+#elif defined( UNDER_CE )
+ mtime_t delay_ms = (deadline - mdate())/1000;
+ DWORD result;
+ 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--;
+
+ /* Reacquire the mutex before returning. */
+ vlc_mutex_lock( p_mutex );
+
+ if(result == WAIT_TIMEOUT)
+ return ETIMEDOUT; /* this error is perfectly normal */
+
+ (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;
+
+ /* Increase our wait count */
+ p_condvar->i_waiting_threads++;
+ result = SignalObjectAndWait( *p_mutex, p_condvar->event,
+ delay_ms, FALSE );
+ p_condvar->i_waiting_threads--;
+
+ /* Reacquire the mutex before returning. */
+ vlc_mutex_lock( p_mutex );
+ if(result == WAIT_TIMEOUT)
+ return ETIMEDOUT; /* this error is perfectly normal */
+
+ (void)psz_file; (void)i_line;
+
+#elif defined( SYS_BEOS )
+# error Unimplemented
+
+#endif
+
+ return 0;
+}
+
+/*****************************************************************************
+ * vlc_cond_destroy: destroy a condition
+ *****************************************************************************/
+#define vlc_cond_destroy( P_COND ) \
+ __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
+
+/*****************************************************************************
+ * vlc_threadvar_set: create: set the value of a thread-local variable
+ *****************************************************************************/
+static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
+{
+ int i_ret;
+
+#if defined(LIBVLC_USE_PTHREAD)
+ i_ret = pthread_setspecific( *p_tls, p_value );
+
+#elif defined( SYS_BEOS )
+ i_ret = EINVAL;
+
+#elif defined( UNDER_CE ) || defined( WIN32 )
+ i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
+
+#endif
+
+ return i_ret;
+}
+
+/*****************************************************************************
+ * vlc_threadvar_get: create: get the value of a thread-local variable
+ *****************************************************************************/
+static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
+{
+ void *p_ret;
+
+#if defined(LIBVLC_USE_PTHREAD)
+ p_ret = pthread_getspecific( *p_tls );
+
+#elif defined( SYS_BEOS )
+ p_ret = NULL;
+
+#elif defined( UNDER_CE ) || defined( WIN32 )
+ p_ret = TlsGetValue( *p_tls );
+
+#endif
+
+ return p_ret;
+}
+
+# if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
+typedef pthread_spinlock_t vlc_spinlock_t;
+
+/**
+ * Initializes a spinlock.
+ */
+static inline int vlc_spin_init (vlc_spinlock_t *spin)
+{
+ return pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE);
+}
+
+/**
+ * Acquires a spinlock.
+ */
+static inline void vlc_spin_lock (vlc_spinlock_t *spin)
+{
+ pthread_spin_lock (spin);
+}
+
+/**
+ * Releases a spinlock.
+ */
+static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
+{
+ pthread_spin_unlock (spin);
+}
+
+/**
+ * Deinitializes a spinlock.
+ */
+static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
+{
+ pthread_spin_destroy (spin);
+}
+
+#elif defined( WIN32 )
+
+typedef CRITICAL_SECTION vlc_spinlock_t;
+
+/**
+ * Initializes a spinlock.
+ */
+static inline int vlc_spin_init (vlc_spinlock_t *spin)
+{
+ return !InitializeCriticalSectionAndSpinCount(spin, 4000);
+}
+
+/**
+ * Acquires a spinlock.
+ */
+static inline void vlc_spin_lock (vlc_spinlock_t *spin)
+{
+ EnterCriticalSection(spin);
+}
+
+/**
+ * Releases a spinlock.
+ */
+static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
+{
+ LeaveCriticalSection(spin);
+}
+
+/**
+ * Deinitializes a spinlock.
+ */
+static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
+{
+ DeleteCriticalSection(spin);
+}
+
+#else
+
+/* Fallback to plain mutexes if spinlocks are not available */
+typedef vlc_mutex_t vlc_spinlock_t;
+
+static inline int vlc_spin_init (vlc_spinlock_t *spin)
+{
+ return vlc_mutex_init (spin);
+}
+
+# define vlc_spin_lock vlc_mutex_lock
+# define vlc_spin_unlock vlc_mutex_unlock
+# 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
+ *****************************************************************************/
+#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_set_priority: set the priority of the calling thread
+ *****************************************************************************/
+#define vlc_thread_set_priority( P_THIS, PRIORITY ) \
+ __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
+
+/*****************************************************************************
+ * vlc_thread_join: wait until a thread exits
+ *****************************************************************************/
+#define vlc_thread_join( P_THIS ) \
+ __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )