2 * Optimised mutex implementation of include/asm-generic/mutex-dec.h algorithm
4 #ifndef _ASM_POWERPC_MUTEX_H
5 #define _ASM_POWERPC_MUTEX_H
7 static inline int __mutex_cmpxchg_lock(atomic_t *v, int old, int new)
11 __asm__ __volatile__ (
12 "1: lwarx %0,0,%1 # mutex trylock\n\
22 : "r" (&v->counter), "r" (old), "r" (new)
28 static inline int __mutex_dec_return_lock(atomic_t *v)
33 "1: lwarx %0,0,%1 # mutex lock\n\
46 static inline int __mutex_inc_return_unlock(atomic_t *v)
52 "1: lwarx %0,0,%1 # mutex unlock\n\
65 * __mutex_fastpath_lock - try to take the lock by moving the count
67 * @count: pointer of type atomic_t
68 * @fail_fn: function to call if the original value was not 1
70 * Change the count from 1 to a value lower than 1, and call <fail_fn> if
71 * it wasn't 1 originally. This function MUST leave the value lower than
72 * 1 even when the "1" assertion wasn't true.
75 __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *))
77 if (unlikely(__mutex_dec_return_lock(count) < 0))
82 * __mutex_fastpath_lock_retval - try to take the lock by moving the count
84 * @count: pointer of type atomic_t
85 * @fail_fn: function to call if the original value was not 1
87 * Change the count from 1 to a value lower than 1, and call <fail_fn> if
88 * it wasn't 1 originally. This function returns 0 if the fastpath succeeds,
89 * or anything the slow path function returns.
92 __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
94 if (unlikely(__mutex_dec_return_lock(count) < 0))
95 return fail_fn(count);
100 * __mutex_fastpath_unlock - try to promote the count from 0 to 1
101 * @count: pointer of type atomic_t
102 * @fail_fn: function to call if the original value was not 0
104 * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>.
105 * In the failure case, this function is allowed to either set the value to
106 * 1, or to set it to a value lower than 1.
109 __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *))
111 if (unlikely(__mutex_inc_return_unlock(count) <= 0))
115 #define __mutex_slowpath_needs_to_unlock() 1
118 * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
120 * @count: pointer of type atomic_t
121 * @fail_fn: fallback function
123 * Change the count from 1 to 0, and return 1 (success), or if the count
124 * was not 1, then return 0 (failure).
127 __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
129 if (likely(__mutex_cmpxchg_lock(count, 1, 0) == 1))