Commit 318a1ff5 authored by Jim Bankoski's avatar Jim Bankoski
Browse files

vp8 : use threading mutex's for tsan only.

To avoid decode performance hit of 2% when running on hyperthreaded
cores.

This patch only uses the mutex's when we are running tsan.

This is safe because 32 bit operations like read and store are atomic
on all the platforms we care about. Tsan warns about race situations,
but in this case either situation ( read occurs before write or write
before read) the worst case is that we go around one extra time in the
loop.  So the ordering doesn't really matter.

That said a few other things have been tried :

for instance as per here:
webrtc/base/atomicops.h#52

In this patch they use:
__atomic_load_n(i, __ATOMIC_ACQUIRE);
__atomic_store_n(i, value, __ATOMIC_RELEASE);

This code works on gcc, clang ( replacing protected write and read), and
avoids tsan errors. Incurring no penalty in performance.  In C11 its
replaced by straight atomic operands.

However there is no equivalent in the visual studio's we support as
int32 on all windows platforms is already atomic.  To avoid tsan like
warnings on windows we'd need to use interlocked exchange and the
end result doesn't gain us any thing.

Change-Id: I2066e3c7f42641ebb23d53feb1f16f23f85bcf59
parent 2b1ec65b
...@@ -191,29 +191,24 @@ static inline int sem_destroy(sem_t *sem) { ...@@ -191,29 +191,24 @@ static inline int sem_destroy(sem_t *sem) {
#define x86_pause_hint() #define x86_pause_hint()
#endif #endif
#include "vpx_util/vpx_thread.h" #if defined(__has_feature)
#if __has_feature(thread_sanitizer)
static INLINE void mutex_lock(pthread_mutex_t *const mutex) { #define USE_MUTEX_LOCK 1
const int kMaxTryLocks = 4000; #endif
int locked = 0; #endif
int i;
for (i = 0; i < kMaxTryLocks; ++i) {
if (!pthread_mutex_trylock(mutex)) {
locked = 1;
break;
}
}
if (!locked) pthread_mutex_lock(mutex); #include "vpx_util/vpx_thread.h"
}
static INLINE int protected_read(pthread_mutex_t *const mutex, const int *p) { static INLINE int protected_read(pthread_mutex_t *const mutex, const int *p) {
(void)mutex;
#if defined(USE_MUTEX_LOCK)
int ret; int ret;
mutex_lock(mutex); pthread_mutex_lock(mutex);
ret = *p; ret = *p;
pthread_mutex_unlock(mutex); pthread_mutex_unlock(mutex);
return ret; return ret;
#endif
return *p;
} }
static INLINE void sync_read(pthread_mutex_t *const mutex, int mb_col, static INLINE void sync_read(pthread_mutex_t *const mutex, int mb_col,
...@@ -226,11 +221,17 @@ static INLINE void sync_read(pthread_mutex_t *const mutex, int mb_col, ...@@ -226,11 +221,17 @@ static INLINE void sync_read(pthread_mutex_t *const mutex, int mb_col,
} }
static INLINE void protected_write(pthread_mutex_t *mutex, int *p, int v) { static INLINE void protected_write(pthread_mutex_t *mutex, int *p, int v) {
mutex_lock(mutex); (void)mutex;
#if defined(USE_MUTEX_LOCK)
pthread_mutex_lock(mutex);
*p = v; *p = v;
pthread_mutex_unlock(mutex); pthread_mutex_unlock(mutex);
return;
#endif
*p = v;
} }
#undef USE_MUTEX_LOCK
#endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */ #endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */
#ifdef __cplusplus #ifdef __cplusplus
......
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