blob: d6d5dc98d7da5509347e56737f3944a37c698490 [file] [log] [blame]
Will Deacone1728002014-03-14 17:47:04 +00001#ifndef _ASM_GENERIC_RWSEM_H
2#define _ASM_GENERIC_RWSEM_H
Richard Kuodd472da2011-10-31 18:47:33 -05003
4#ifndef _LINUX_RWSEM_H
5#error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
6#endif
7
8#ifdef __KERNEL__
9
10/*
Will Deacone1728002014-03-14 17:47:04 +000011 * R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
Richard Kuodd472da2011-10-31 18:47:33 -050012 * Adapted largely from include/asm-i386/rwsem.h
13 * by Paul Mackerras <paulus@samba.org>.
14 */
15
16/*
17 * the semaphore definition
18 */
Will Deacone1728002014-03-14 17:47:04 +000019#ifdef CONFIG_64BIT
Richard Kuodd472da2011-10-31 18:47:33 -050020# define RWSEM_ACTIVE_MASK 0xffffffffL
21#else
22# define RWSEM_ACTIVE_MASK 0x0000ffffL
23#endif
24
25#define RWSEM_UNLOCKED_VALUE 0x00000000L
26#define RWSEM_ACTIVE_BIAS 0x00000001L
27#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
28#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
29#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
30
31/*
32 * lock for reading
33 */
34static inline void __down_read(struct rw_semaphore *sem)
35{
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070036 if (unlikely(atomic_long_inc_return_acquire((atomic_long_t *)&sem->count) <= 0))
Richard Kuodd472da2011-10-31 18:47:33 -050037 rwsem_down_read_failed(sem);
38}
39
40static inline int __down_read_trylock(struct rw_semaphore *sem)
41{
42 long tmp;
43
44 while ((tmp = sem->count) >= 0) {
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070045 if (tmp == cmpxchg_acquire(&sem->count, tmp,
Richard Kuodd472da2011-10-31 18:47:33 -050046 tmp + RWSEM_ACTIVE_READ_BIAS)) {
47 return 1;
48 }
49 }
50 return 0;
51}
52
53/*
54 * lock for writing
55 */
56static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
57{
58 long tmp;
59
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070060 tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
Richard Kuodd472da2011-10-31 18:47:33 -050061 (atomic_long_t *)&sem->count);
62 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
63 rwsem_down_write_failed(sem);
64}
65
66static inline void __down_write(struct rw_semaphore *sem)
67{
68 __down_write_nested(sem, 0);
69}
70
71static inline int __down_write_trylock(struct rw_semaphore *sem)
72{
73 long tmp;
74
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070075 tmp = cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
Richard Kuodd472da2011-10-31 18:47:33 -050076 RWSEM_ACTIVE_WRITE_BIAS);
77 return tmp == RWSEM_UNLOCKED_VALUE;
78}
79
80/*
81 * unlock after reading
82 */
83static inline void __up_read(struct rw_semaphore *sem)
84{
85 long tmp;
86
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070087 tmp = atomic_long_dec_return_release((atomic_long_t *)&sem->count);
Richard Kuodd472da2011-10-31 18:47:33 -050088 if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
89 rwsem_wake(sem);
90}
91
92/*
93 * unlock after writing
94 */
95static inline void __up_write(struct rw_semaphore *sem)
96{
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -070097 if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
Richard Kuodd472da2011-10-31 18:47:33 -050098 (atomic_long_t *)&sem->count) < 0))
99 rwsem_wake(sem);
100}
101
102/*
103 * implement atomic add functionality
104 */
105static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
106{
107 atomic_long_add(delta, (atomic_long_t *)&sem->count);
108}
109
110/*
111 * downgrade write lock to read lock
112 */
113static inline void __downgrade_write(struct rw_semaphore *sem)
114{
115 long tmp;
116
Davidlohr Bueso00eb4ba2015-09-30 13:03:15 -0700117 /*
118 * When downgrading from exclusive to shared ownership,
119 * anything inside the write-locked region cannot leak
120 * into the read side. In contrast, anything in the
121 * read-locked region is ok to be re-ordered into the
122 * write side. As such, rely on RELEASE semantics.
123 */
124 tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS,
Richard Kuodd472da2011-10-31 18:47:33 -0500125 (atomic_long_t *)&sem->count);
126 if (tmp < 0)
127 rwsem_downgrade_wake(sem);
128}
129
130/*
131 * implement exchange and add functionality
132 */
133static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
134{
135 return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
136}
137
138#endif /* __KERNEL__ */
Will Deacone1728002014-03-14 17:47:04 +0000139#endif /* _ASM_GENERIC_RWSEM_H */