Commit 463b756d authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Simplify threading code a bit

parent 63baa1df
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
/* WinCE API */ /* WinCE API */
#elif defined( WIN32 ) #elif defined( WIN32 )
# include <process.h> /* Win32 API */ # include <process.h> /* Win32 API */
# include <errno.h>
#elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */ #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
# include <kernel/OS.h> # include <kernel/OS.h>
...@@ -146,10 +147,7 @@ typedef struct ...@@ -146,10 +147,7 @@ typedef struct
int i_win9x_cv; int i_win9x_cv;
} vlc_cond_t; } vlc_cond_t;
typedef struct typedef DWORD vlc_threadvar_t;
{
DWORD handle;
} vlc_threadvar_t;
#elif defined( HAVE_KERNEL_SCHEDULER_H ) #elif defined( HAVE_KERNEL_SCHEDULER_H )
/* This is the BeOS implementation of the vlc threads, note that the mutex is /* This is the BeOS implementation of the vlc threads, note that the mutex is
...@@ -177,19 +175,9 @@ typedef struct ...@@ -177,19 +175,9 @@ typedef struct
#else #else
typedef pthread_t vlc_thread_t; typedef pthread_t vlc_thread_t;
typedef struct typedef pthread_mutex_t vlc_mutex_t;
{ typedef pthread_cond_t vlc_cond_t;
pthread_mutex_t mutex; typedef pthread_key_t vlc_threadvar_t;
} vlc_mutex_t;
typedef struct
{
pthread_cond_t cond;
} vlc_cond_t;
typedef struct
{
pthread_key_t handle;
} vlc_threadvar_t;
#endif #endif
......
...@@ -111,8 +111,8 @@ static inline void __vlc_mutex_lock( const char * psz_file, int i_line, ...@@ -111,8 +111,8 @@ static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
# define vlc_assert_locked( m ) \ # define vlc_assert_locked( m ) \
assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK) assert (pthread_mutex_lock (m) == EDEADLK)
int val = pthread_mutex_lock( &p_mutex->mutex ); int val = pthread_mutex_lock( p_mutex );
VLC_THREAD_ASSERT ("locking mutex"); VLC_THREAD_ASSERT ("locking mutex");
#endif #endif
...@@ -148,7 +148,7 @@ static inline void __vlc_mutex_unlock( const char * psz_file, int i_line, ...@@ -148,7 +148,7 @@ static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
release_sem( p_mutex->lock ); release_sem( p_mutex->lock );
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
int val = pthread_mutex_unlock( &p_mutex->mutex ); int val = pthread_mutex_unlock( p_mutex );
VLC_THREAD_ASSERT ("unlocking mutex"); VLC_THREAD_ASSERT ("unlocking mutex");
#endif #endif
...@@ -248,7 +248,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line, ...@@ -248,7 +248,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line,
} }
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
int val = pthread_cond_signal( &p_condvar->cond ); int val = pthread_cond_signal( p_condvar );
VLC_THREAD_ASSERT ("signaling condition variable"); VLC_THREAD_ASSERT ("signaling condition variable");
#endif #endif
...@@ -356,7 +356,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line, ...@@ -356,7 +356,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
vlc_mutex_lock( p_mutex ); vlc_mutex_lock( p_mutex );
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
int val = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex ); int val = pthread_cond_wait( p_condvar, p_mutex );
VLC_THREAD_ASSERT ("waiting on condition"); VLC_THREAD_ASSERT ("waiting on condition");
#endif #endif
...@@ -489,7 +489,7 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line, ...@@ -489,7 +489,7 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
lldiv_t d = lldiv( deadline, 1000000 ); lldiv_t d = lldiv( deadline, 1000000 );
struct timespec ts = { d.quot, d.rem * 1000 }; struct timespec ts = { d.quot, d.rem * 1000 };
int val = pthread_cond_timedwait (&p_condvar->cond, &p_mutex->mutex, &ts); int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
if (val == ETIMEDOUT) if (val == ETIMEDOUT)
return ETIMEDOUT; /* this error is perfectly normal */ return ETIMEDOUT; /* this error is perfectly normal */
VLC_THREAD_ASSERT ("timed-waiting on condition"); VLC_THREAD_ASSERT ("timed-waiting on condition");
...@@ -519,13 +519,13 @@ static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value ) ...@@ -519,13 +519,13 @@ static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
int i_ret; int i_ret;
#if defined( HAVE_KERNEL_SCHEDULER_H ) #if defined( HAVE_KERNEL_SCHEDULER_H )
return -1; i_ret = EINVAL;
#elif defined( UNDER_CE ) || defined( WIN32 ) #elif defined( UNDER_CE ) || defined( WIN32 )
i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 ); i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
i_ret = pthread_setspecific( p_tls->handle, p_value ); i_ret = pthread_setspecific( *p_tls, p_value );
#endif #endif
...@@ -537,15 +537,16 @@ static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value ) ...@@ -537,15 +537,16 @@ static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
*****************************************************************************/ *****************************************************************************/
static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls ) static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
{ {
void* p_ret; void *p_ret;
#if defined( HAVE_KERNEL_SCHEDULER_H ) #if defined( HAVE_KERNEL_SCHEDULER_H )
p_ret = NULL; p_ret = NULL;
#elif defined( UNDER_CE ) || defined( WIN32 ) #elif defined( UNDER_CE ) || defined( WIN32 )
p_ret = TlsGetValue( p_tls->handle ); p_ret = TlsGetValue( *p_tls );
#elif defined(LIBVLC_USE_PTHREAD) #elif defined(LIBVLC_USE_PTHREAD)
p_ret = pthread_getspecific( p_tls->handle ); p_ret = pthread_getspecific( *p_tls );
#endif #endif
......
...@@ -287,26 +287,22 @@ int __vlc_mutex_init( vlc_mutex_t *p_mutex ) ...@@ -287,26 +287,22 @@ int __vlc_mutex_init( vlc_mutex_t *p_mutex )
return B_OK; return B_OK;
#elif defined( LIBVLC_USE_PTHREAD ) #elif defined( LIBVLC_USE_PTHREAD )
# ifndef NDEBUG pthread_mutexattr_t attr;
{ int i_result;
/* Create error-checking mutex to detect problems more easily. */
pthread_mutexattr_t attr;
int i_result;
pthread_mutexattr_init( &attr );
# if defined(SYS_LINUX)
pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
# else
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
# endif
i_result = pthread_mutex_init( &p_mutex->mutex, &attr ); pthread_mutexattr_init( &attr );
pthread_mutexattr_destroy( &attr );
return( i_result );
}
# endif /* NDEBUG */
return pthread_mutex_init( &p_mutex->mutex, NULL );
# ifndef NDEBUG
/* Create error-checking mutex to detect problems more easily. */
# if defined(SYS_LINUX)
pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
# else
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
# endif
# endif
i_result = pthread_mutex_init( p_mutex, &attr );
pthread_mutexattr_destroy( &attr );
return i_result;
#endif #endif
} }
...@@ -333,7 +329,7 @@ int __vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) ...@@ -333,7 +329,7 @@ int __vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
# endif # endif
# endif # endif
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ); pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
i_result = pthread_mutex_init( &p_mutex->mutex, &attr ); i_result = pthread_mutex_init( p_mutex, &attr );
pthread_mutexattr_destroy( &attr ); pthread_mutexattr_destroy( &attr );
return( i_result ); return( i_result );
#else #else
...@@ -367,7 +363,7 @@ void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mute ...@@ -367,7 +363,7 @@ void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mute
p_mutex->init = 0; p_mutex->init = 0;
#elif defined( LIBVLC_USE_PTHREAD ) #elif defined( LIBVLC_USE_PTHREAD )
int val = pthread_mutex_destroy( &p_mutex->mutex ); int val = pthread_mutex_destroy( p_mutex );
VLC_THREAD_ASSERT ("destroying mutex"); VLC_THREAD_ASSERT ("destroying mutex");
#endif #endif
...@@ -458,7 +454,7 @@ int __vlc_cond_init( vlc_cond_t *p_condvar ) ...@@ -458,7 +454,7 @@ int __vlc_cond_init( vlc_cond_t *p_condvar )
pthread_condattr_setclock (&attr, CLOCK_MONOTONIC); pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
# endif # endif
ret = pthread_cond_init (&p_condvar->cond, &attr); ret = pthread_cond_init (p_condvar, &attr);
pthread_condattr_destroy (&attr); pthread_condattr_destroy (&attr);
return ret; return ret;
...@@ -493,7 +489,7 @@ void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condva ...@@ -493,7 +489,7 @@ void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condva
p_condvar->init = 0; p_condvar->init = 0;
#elif defined( LIBVLC_USE_PTHREAD ) #elif defined( LIBVLC_USE_PTHREAD )
int val = pthread_cond_destroy( &p_condvar->cond ); int val = pthread_cond_destroy( p_condvar );
VLC_THREAD_ASSERT ("destroying condition"); VLC_THREAD_ASSERT ("destroying condition");
#endif #endif
...@@ -510,11 +506,11 @@ int __vlc_threadvar_create( vlc_threadvar_t *p_tls ) ...@@ -510,11 +506,11 @@ int __vlc_threadvar_create( vlc_threadvar_t *p_tls )
# error Unimplemented! # error Unimplemented!
#elif defined( UNDER_CE ) || defined( WIN32 ) #elif defined( UNDER_CE ) || defined( WIN32 )
#elif defined( WIN32 ) #elif defined( WIN32 )
p_tls->handle = TlsAlloc(); *p_tls = TlsAlloc();
i_ret = !( p_tls->handle == 0xFFFFFFFF ); i_ret = (*p_tls == INVALID_HANDLE_VALUE) ? EAGAIN : 0;
#elif defined( LIBVLC_USE_PTHREAD ) #elif defined( LIBVLC_USE_PTHREAD )
i_ret = pthread_key_create( &p_tls->handle, NULL ); i_ret = pthread_key_create( p_tls, NULL );
#endif #endif
return i_ret; return i_ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment