From 463b756d19d348b4a8d39b293e370b18585a4ef4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Wed, 23 Apr 2008 21:53:10 +0300 Subject: [PATCH] Simplify threading code a bit --- include/vlc_threads.h | 22 ++++-------------- include/vlc_threads_funcs.h | 25 ++++++++++---------- src/misc/threads.c | 46 +++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/include/vlc_threads.h b/include/vlc_threads.h index 4b695f7ce3..96ad309f42 100644 --- a/include/vlc_threads.h +++ b/include/vlc_threads.h @@ -38,6 +38,7 @@ /* WinCE API */ #elif defined( WIN32 ) # include /* Win32 API */ +# include #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */ # include @@ -146,10 +147,7 @@ typedef struct int i_win9x_cv; } vlc_cond_t; -typedef struct -{ - DWORD handle; -} vlc_threadvar_t; +typedef DWORD vlc_threadvar_t; #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* This is the BeOS implementation of the vlc threads, note that the mutex is @@ -177,19 +175,9 @@ typedef struct #else typedef pthread_t vlc_thread_t; -typedef struct -{ - pthread_mutex_t mutex; -} vlc_mutex_t; -typedef struct -{ - pthread_cond_t cond; -} vlc_cond_t; - -typedef struct -{ - pthread_key_t handle; -} vlc_threadvar_t; +typedef pthread_mutex_t vlc_mutex_t; +typedef pthread_cond_t vlc_cond_t; +typedef pthread_key_t vlc_threadvar_t; #endif diff --git a/include/vlc_threads_funcs.h b/include/vlc_threads_funcs.h index adadb033e3..981a8d5bb1 100644 --- a/include/vlc_threads_funcs.h +++ b/include/vlc_threads_funcs.h @@ -111,8 +111,8 @@ static inline void __vlc_mutex_lock( const char * psz_file, int i_line, #elif defined(LIBVLC_USE_PTHREAD) # define vlc_assert_locked( m ) \ - assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK) - int val = pthread_mutex_lock( &p_mutex->mutex ); + assert (pthread_mutex_lock (m) == EDEADLK) + int val = pthread_mutex_lock( p_mutex ); VLC_THREAD_ASSERT ("locking mutex"); #endif @@ -148,7 +148,7 @@ static inline void __vlc_mutex_unlock( const char * psz_file, int i_line, release_sem( p_mutex->lock ); #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"); #endif @@ -248,7 +248,7 @@ static inline void __vlc_cond_signal( const char * psz_file, int i_line, } #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"); #endif @@ -356,7 +356,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line, vlc_mutex_lock( p_mutex ); #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"); #endif @@ -489,7 +489,7 @@ static inline int __vlc_cond_timedwait( const char * psz_file, int i_line, lldiv_t d = lldiv( deadline, 1000000 ); 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) return ETIMEDOUT; /* this error is perfectly normal */ 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 ) int i_ret; #if defined( HAVE_KERNEL_SCHEDULER_H ) - return -1; + i_ret = EINVAL; #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) - i_ret = pthread_setspecific( p_tls->handle, p_value ); + i_ret = pthread_setspecific( *p_tls, p_value ); #endif @@ -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 ) { - void* p_ret; + void *p_ret; #if defined( HAVE_KERNEL_SCHEDULER_H ) p_ret = NULL; + #elif defined( UNDER_CE ) || defined( WIN32 ) - p_ret = TlsGetValue( p_tls->handle ); + p_ret = TlsGetValue( *p_tls ); #elif defined(LIBVLC_USE_PTHREAD) - p_ret = pthread_getspecific( p_tls->handle ); + p_ret = pthread_getspecific( *p_tls ); #endif diff --git a/src/misc/threads.c b/src/misc/threads.c index f535232c30..9980e0df18 100644 --- a/src/misc/threads.c +++ b/src/misc/threads.c @@ -287,26 +287,22 @@ int __vlc_mutex_init( vlc_mutex_t *p_mutex ) return B_OK; #elif defined( LIBVLC_USE_PTHREAD ) -# ifndef NDEBUG - { - /* 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 + pthread_mutexattr_t attr; + int i_result; - i_result = pthread_mutex_init( &p_mutex->mutex, &attr ); - pthread_mutexattr_destroy( &attr ); - return( i_result ); - } -# endif /* NDEBUG */ - return pthread_mutex_init( &p_mutex->mutex, NULL ); + pthread_mutexattr_init( &attr ); +# 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 } @@ -333,7 +329,7 @@ int __vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) # endif # endif 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 ); return( i_result ); #else @@ -367,7 +363,7 @@ void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mute p_mutex->init = 0; #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"); #endif @@ -458,7 +454,7 @@ int __vlc_cond_init( vlc_cond_t *p_condvar ) pthread_condattr_setclock (&attr, CLOCK_MONOTONIC); # endif - ret = pthread_cond_init (&p_condvar->cond, &attr); + ret = pthread_cond_init (p_condvar, &attr); pthread_condattr_destroy (&attr); return ret; @@ -493,7 +489,7 @@ void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condva p_condvar->init = 0; #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"); #endif @@ -510,11 +506,11 @@ int __vlc_threadvar_create( vlc_threadvar_t *p_tls ) # error Unimplemented! #elif defined( UNDER_CE ) || defined( WIN32 ) #elif defined( WIN32 ) - p_tls->handle = TlsAlloc(); - i_ret = !( p_tls->handle == 0xFFFFFFFF ); + *p_tls = TlsAlloc(); + i_ret = (*p_tls == INVALID_HANDLE_VALUE) ? EAGAIN : 0; #elif defined( LIBVLC_USE_PTHREAD ) - i_ret = pthread_key_create( &p_tls->handle, NULL ); + i_ret = pthread_key_create( p_tls, NULL ); #endif return i_ret; } -- 2.39.5