[patch 02/13] mutex subsystem, add asm-generic/mutex-[dec|xchg|null].h implementations

[patch 02/13] mutex subsystem, add asm-generic/mutex-[dec|xchg|null].h implementations

Post by Ingo Molna » Sat, 31 Dec 2005 06:10:12


dd three (generic) mutex fastpath implementations.

The mutex-xchg.h implementation is atomic_xchg() based, and should
work fine on every architecture.

The mutex-dec.h implementation is atomic_dec_return() based - this
one too should work on every architecture, but might not perform the
most optimally on architectures that have no atomic-dec/inc instructions.

The mutex-null.h implementation forces all calls into the slowpath. This
is used for mutex debugging, but it can also be used on platforms that do
not want (or need) a fastpath at all.

Signed-off-by: Ingo Molnar < XXXX@XXXXX.COM >
Signed-off-by: Arjan van de Ven < XXXX@XXXXX.COM >

----

include/asm-generic/mutex-dec.h | 103 +++++++++++++++++++++++++++++++++++
include/asm-generic/mutex-null.h | 24 ++++++++
include/asm-generic/mutex-xchg.h | 112 +++++++++++++++++++++++++++++++++++++++
3 files changed, 239 insertions(+)

Index: linux/include/asm-generic/mutex-dec.h
===================================================================
--- /dev/null
+++ linux/include/asm-generic/mutex-dec.h
@@ -0,0 +1,103 @@
+/*
+ * asm-generic/mutex-dec.h
+ *
+ * Generic implementation of the mutex fastpath, based on atomic
+ * decrement/increment.
+ */
+#ifndef _ASM_GENERIC_MUTEX_DEC_H
+#define _ASM_GENERIC_MUTEX_DEC_H
+
+/**
+ * __mutex_fastpath_lock - try to take the lock by moving the count
+ * from 1 to a 0 value
+ * @count: pointer of type atomic_t
+ * @fn: function to call if the original value was not 1
+ *
+ * Change the count from 1 to a value lower than 1, and call <fn> if it
+ * wasn't 1 originally. This function MUST leave the value lower than 1
+ * even when the "1" assertion wasn't true.
+ */
+#define __mutex_fastpath_lock(count, fn) \
+do { \
+ if (unlikely(atomic_dec_return(count) < 0)) \
+ fn(count); \
+} while (0)
+
+/**
+ * __mutex_fastpath_lock_retval - try to take the lock by moving the count
+ * from 1 to a 0 value
+ * @count: pointer of type atomic_t
+ * @fn: function to call if the original value was not 1
+ *
+ * Change the count from 1 to a value lower than 1, and call <fn> if it
+ * wasn't 1 originally. This function returns 0 if the fastpath succeeds,
+ * or anything the slow path function returns.
+ */
+static inline int
+__mutex_fastpath_lock_retval(atomic_t *count, int (*fn)(atomic_t *))
+{
+ if (unlikely(atomic_dec_return(count) < 0))
+ return fn(count);
+ else
+ return 0;
+}
+
+/**
+ * __mutex_fastpath_unlock - try to promote the count from 0 to 1
+ * @count: pointer of type atomic_t
+ * @fn: function to call if the original value was not 0
+ *
+ * Try to promote the count from 0 to 1. If it wasn't 0, call <fn>.
+ * In the failure case, this function is allowed to either set the value to
+ * 1, or to set it to a value lower than 1.
+ *
+ * If the implementation sets it to a value of lower than 1, then the
+ * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
+ * to return 0 otherwise.
+ */
+#define __mutex_fastpath_unlock(count, fn) \
+do { \
+ if (unlikely(atomic_inc_return(count) <= 0)) \
+ fn(count); \
+} while (0)
+
+#define __mutex_slowpath_needs_to_unlock() 1
+
+/**
+ * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
+ *
+ * @count: pointer of type atomic_t
+ * @fn: fallba
 
 
 

1. [patch 03/13] mutex subsystem, add include/asm-i386/mutex.h

2. [patch 01/15] Generic Mutex Subsystem, add-atomic-xchg-i386.patch


add atomic_cmpxchg() to i386. Needed by the new mutex code.

Signed-off-by: Ingo Molnar < XXXX@XXXXX.COM >

----

include/asm-i386/atomic.h | 1 +
1 files changed, 1 insertion(+)

Index: linux/include/asm-i386/atomic.h
===================================================================
--- linux.orig/include/asm-i386/atomic.h
+++ linux/include/asm-i386/atomic.h
@@ -216,6 +216,7 @@ static __inline__ int atomic_sub_return(
}

#define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
+#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

/**
* atomic_add_unless - add unless the number is a given value
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to XXXX@XXXXX.COM
More majordomo info at http://www.yqcomputer.com/
Please read the FAQ at http://www.yqcomputer.com/

3. [patch 03/11] mutex subsystem, add include/asm-i386/mutex.h

4. [patch 04/20] mutex subsystem, add include/asm-i386/mutex.h

5. [patch 06/11] mutex subsystem, add default include/asm-*/mutex.h files

6. [patch 07/19] mutex subsystem, add default include/asm-*/mutex.h files

7. [patch 04/21] mutex subsystem, add include/asm-i386/mutex.h

8. [patch 05/20] mutex subsystem, add include/asm-x86_64/mutex.h

9. [patch 04/11] mutex subsystem, add include/asm-x86_64/mutex.h

10. [patch 05/21] mutex subsystem, add include/asm-x86_64/mutex.h

11. [patch 06/21] mutex subsystem, add include/asm-arm/mutex.h

12. [patch 07/21] mutex subsystem, add default include/asm-*/mutex.h files

13. [patch 05/11] mutex subsystem, add include/asm-arm/mutex.h

14. [patch 06/19] mutex subsystem, add include/asm-arm/mutex.h

15. [patch 06/20] mutex subsystem, add include/asm-arm/mutex.h