Skip to contentSkip to conversation

x86: optimize a bit __raw_read_trylock

PATCH In review Frederic Weisbecker 2 participants 2 messages Jan 25, 2009 – Jan 26, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86: optimize a bit __raw_read_trylock

The current version of __raw_read_trylock starts with decrementing the lock and read its new value as a separate operation after that.

That makes 3 dereferences (read, write (after sub), read) whereas a single atomic_dec_return does only two pointers dereferences (read, write).

---
 arch/x86/include/asm/spinlock.h |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/spinlock.h +1 -2
index 2bd6b11..139b424 100644
@@ -267,8 +267,7 @@ static inline int __raw_read_trylock(raw_rwlock_t *lock)
 {
 	atomic_t *count = (atomic_t *)lock;
 
-	atomic_dec(count);
-	if (atomic_read(count) >= 0)
+	if (atomic_dec_return(count) >= 0)
 		return 1;
 	atomic_inc(count);
 	return 0;
Signature · 1 line
1.6.1
#1 lore raw
Quoted 16 lines · Frederic Weisbecker

The current version of __raw_read_trylock starts with decrementing the lock and read its new value as a separate operation after that.

That makes 3 dereferences (read, write (after sub), read) whereas a single atomic_dec_return does only two pointers dereferences (read, write).

---
 arch/x86/include/asm/spinlock.h |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/spinlock.h +0 -0
index 2bd6b11..139b424 100644

arch/x86/include/asm/spinlock.h static inline int __raw_read_trylock(raw_rwlock_t *lock) · line 273

Quoted 9 lines
arch/x86/include/asm/spinlock.h +1 -2
@@ -267,8 +267,7 @@ static inline int __raw_read_trylock(raw_rwlock_t *lock)
 {
 	atomic_t *count = (atomic_t *)lock;
 
-	atomic_dec(count);
-	if (atomic_read(count) >= 0)
+	if (atomic_dec_return(count) >= 0)
 		return 1;
 	atomic_inc(count);
 	return 0;

nice one ... applied it to tip/x86/asm, thanks Frederic!

Ingo