Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / powerpc / include / asm / mutex.h
blob127ab23e1f6ccdbe038b6cfdd84c569742968e27
1 /*
2 * Optimised mutex implementation of include/asm-generic/mutex-dec.h algorithm
3 */
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)
9 int t;
11 __asm__ __volatile__ (
12 "1: lwarx %0,0,%1 # mutex trylock\n\
13 cmpw 0,%0,%2\n\
14 bne- 2f\n"
15 PPC405_ERR77(0,%1)
16 " stwcx. %3,0,%1\n\
17 bne- 1b"
18 PPC_ACQUIRE_BARRIER
19 "\n\
20 2:"
21 : "=&r" (t)
22 : "r" (&v->counter), "r" (old), "r" (new)
23 : "cc", "memory");
25 return t;
28 static inline int __mutex_dec_return_lock(atomic_t *v)
30 int t;
32 __asm__ __volatile__(
33 "1: lwarx %0,0,%1 # mutex lock\n\
34 addic %0,%0,-1\n"
35 PPC405_ERR77(0,%1)
36 " stwcx. %0,0,%1\n\
37 bne- 1b"
38 PPC_ACQUIRE_BARRIER
39 : "=&r" (t)
40 : "r" (&v->counter)
41 : "cc", "memory");
43 return t;
46 static inline int __mutex_inc_return_unlock(atomic_t *v)
48 int t;
50 __asm__ __volatile__(
51 PPC_RELEASE_BARRIER
52 "1: lwarx %0,0,%1 # mutex unlock\n\
53 addic %0,%0,1\n"
54 PPC405_ERR77(0,%1)
55 " stwcx. %0,0,%1 \n\
56 bne- 1b"
57 : "=&r" (t)
58 : "r" (&v->counter)
59 : "cc", "memory");
61 return t;
64 /**
65 * __mutex_fastpath_lock - try to take the lock by moving the count
66 * from 1 to a 0 value
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.
74 static inline void
75 __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *))
77 if (unlikely(__mutex_dec_return_lock(count) < 0))
78 fail_fn(count);
81 /**
82 * __mutex_fastpath_lock_retval - try to take the lock by moving the count
83 * from 1 to a 0 value
84 * @count: pointer of type atomic_t
86 * Change the count from 1 to a value lower than 1. This function returns 0
87 * if the fastpath succeeds, or -1 otherwise.
89 static inline int
90 __mutex_fastpath_lock_retval(atomic_t *count)
92 if (unlikely(__mutex_dec_return_lock(count) < 0))
93 return -1;
94 return 0;
97 /**
98 * __mutex_fastpath_unlock - try to promote the count from 0 to 1
99 * @count: pointer of type atomic_t
100 * @fail_fn: function to call if the original value was not 0
102 * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>.
103 * In the failure case, this function is allowed to either set the value to
104 * 1, or to set it to a value lower than 1.
106 static inline void
107 __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *))
109 if (unlikely(__mutex_inc_return_unlock(count) <= 0))
110 fail_fn(count);
113 #define __mutex_slowpath_needs_to_unlock() 1
116 * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
118 * @count: pointer of type atomic_t
119 * @fail_fn: fallback function
121 * Change the count from 1 to 0, and return 1 (success), or if the count
122 * was not 1, then return 0 (failure).
124 static inline int
125 __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
127 if (likely(__mutex_cmpxchg_lock(count, 1, 0) == 1))
128 return 1;
129 return 0;
132 #endif