From e914f8b7ec79f622fa3b017af434642f61d45ce8 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 17 Mar 2011 12:14:40 -0400 Subject: [PATCH] optimize contended normal mutex case; add int compare-and-swap atomic --- arch/i386/atomic.h | 7 +++++++ arch/x86_64/atomic.h | 7 +++++++ src/thread/pthread_mutex_trylock.c | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/i386/atomic.h b/arch/i386/atomic.h index e74e4535..bf3c336e 100644 --- a/arch/i386/atomic.h +++ b/arch/i386/atomic.h @@ -49,6 +49,13 @@ static inline long a_cas_l(volatile void *p, long t, long s) return t; } +static inline int a_cas(volatile int *p, int t, int s) +{ + __asm__( "lock ; cmpxchg %3, %1" + : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" ); + return t; +} + static inline void *a_swap_p(void *volatile *x, void *v) { __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(void **)x) : "0"(v) : "memory" ); diff --git a/arch/x86_64/atomic.h b/arch/x86_64/atomic.h index 7a665c1b..04f6c28d 100644 --- a/arch/x86_64/atomic.h +++ b/arch/x86_64/atomic.h @@ -48,6 +48,13 @@ static inline long a_cas_l(volatile void *p, long t, long s) return t; } +static inline int a_cas(volatile int *p, int t, int s) +{ + __asm__( "lock ; cmpxchgl %3, %1" + : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" ); + return t; +} + static inline void *a_swap_p(void *volatile *x, void *v) { __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(void **)x) : "0"(v) : "memory" ); diff --git a/src/thread/pthread_mutex_trylock.c b/src/thread/pthread_mutex_trylock.c index af421472..6fc604fe 100644 --- a/src/thread/pthread_mutex_trylock.c +++ b/src/thread/pthread_mutex_trylock.c @@ -5,7 +5,7 @@ int pthread_mutex_trylock(pthread_mutex_t *m) int tid; if (m->_m_type == PTHREAD_MUTEX_NORMAL) - return -a_xchg(&m->_m_lock, 1) & EBUSY; + return (m->_m_lock || a_swap(&m->_m_lock, 1)) ? EBUSY : 0; tid = pthread_self()->tid; -- 2.25.1