blob: 1327c91ea39c77f0752c94e0a83cb49cac26089d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * asm-ia64/rwsem.h: R/W semaphores for ia64
3 *
4 * Copyright (C) 2003 Ken Chen <kenneth.w.chen@intel.com>
5 * Copyright (C) 2003 Asit Mallick <asit.k.mallick@intel.com>
Christoph Lameter16592d22005-08-22 12:20:00 -07006 * Copyright (C) 2005 Christoph Lameter <clameter@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on asm-i386/rwsem.h and other architecture implementation.
9 *
10 * The MSW of the count is the negated number of active writers and
11 * waiting lockers, and the LSW is the total number of active locks.
12 *
13 * The lock count is initialized to 0 (no active and no waiting lockers).
14 *
Christoph Lameter16592d22005-08-22 12:20:00 -070015 * When a writer subtracts WRITE_BIAS, it'll get 0xffffffff00000001 for
16 * the case of an uncontended lock. Readers increment by 1 and see a positive
17 * value when uncontended, negative if there are writers (and maybe) readers
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * waiting (in which case it goes to sleep).
19 */
20
21#ifndef _ASM_IA64_RWSEM_H
22#define _ASM_IA64_RWSEM_H
23
24#include <linux/list.h>
25#include <linux/spinlock.h>
26
27#include <asm/intrinsics.h>
28
29/*
30 * the semaphore definition
31 */
32struct rw_semaphore {
Christoph Lameter16592d22005-08-22 12:20:00 -070033 signed long count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 spinlock_t wait_lock;
35 struct list_head wait_list;
36#if RWSEM_DEBUG
37 int debug;
38#endif
39};
40
Christoph Lameter16592d22005-08-22 12:20:00 -070041#define RWSEM_UNLOCKED_VALUE __IA64_UL_CONST(0x0000000000000000)
42#define RWSEM_ACTIVE_BIAS __IA64_UL_CONST(0x0000000000000001)
43#define RWSEM_ACTIVE_MASK __IA64_UL_CONST(0x00000000ffffffff)
44#define RWSEM_WAITING_BIAS -__IA64_UL_CONST(0x0000000100000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
46#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
47
48/*
49 * initialization
50 */
51#if RWSEM_DEBUG
52#define __RWSEM_DEBUG_INIT , 0
53#else
54#define __RWSEM_DEBUG_INIT /* */
55#endif
56
57#define __RWSEM_INITIALIZER(name) \
58 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
59 LIST_HEAD_INIT((name).wait_list) \
60 __RWSEM_DEBUG_INIT }
61
62#define DECLARE_RWSEM(name) \
63 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
64
65extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
66extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
67extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
68extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
69
70static inline void
71init_rwsem (struct rw_semaphore *sem)
72{
73 sem->count = RWSEM_UNLOCKED_VALUE;
74 spin_lock_init(&sem->wait_lock);
75 INIT_LIST_HEAD(&sem->wait_list);
76#if RWSEM_DEBUG
77 sem->debug = 0;
78#endif
79}
80
81/*
82 * lock for reading
83 */
84static inline void
85__down_read (struct rw_semaphore *sem)
86{
Christoph Lameter16592d22005-08-22 12:20:00 -070087 long result = ia64_fetchadd8_acq((unsigned long *)&sem->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (result < 0)
90 rwsem_down_read_failed(sem);
91}
92
93/*
94 * lock for writing
95 */
96static inline void
97__down_write (struct rw_semaphore *sem)
98{
Christoph Lameter16592d22005-08-22 12:20:00 -070099 long old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 do {
102 old = sem->count;
103 new = old + RWSEM_ACTIVE_WRITE_BIAS;
104 } while (cmpxchg_acq(&sem->count, old, new) != old);
105
106 if (old != 0)
107 rwsem_down_write_failed(sem);
108}
109
110/*
111 * unlock after reading
112 */
113static inline void
114__up_read (struct rw_semaphore *sem)
115{
Christoph Lameter16592d22005-08-22 12:20:00 -0700116 long result = ia64_fetchadd8_rel((unsigned long *)&sem->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
119 rwsem_wake(sem);
120}
121
122/*
123 * unlock after writing
124 */
125static inline void
126__up_write (struct rw_semaphore *sem)
127{
Christoph Lameter16592d22005-08-22 12:20:00 -0700128 long old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 do {
131 old = sem->count;
132 new = old - RWSEM_ACTIVE_WRITE_BIAS;
133 } while (cmpxchg_rel(&sem->count, old, new) != old);
134
135 if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
136 rwsem_wake(sem);
137}
138
139/*
140 * trylock for reading -- returns 1 if successful, 0 if contention
141 */
142static inline int
143__down_read_trylock (struct rw_semaphore *sem)
144{
Christoph Lameter16592d22005-08-22 12:20:00 -0700145 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 while ((tmp = sem->count) >= 0) {
147 if (tmp == cmpxchg_acq(&sem->count, tmp, tmp+1)) {
148 return 1;
149 }
150 }
151 return 0;
152}
153
154/*
155 * trylock for writing -- returns 1 if successful, 0 if contention
156 */
157static inline int
158__down_write_trylock (struct rw_semaphore *sem)
159{
Christoph Lameter16592d22005-08-22 12:20:00 -0700160 long tmp = cmpxchg_acq(&sem->count, RWSEM_UNLOCKED_VALUE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 RWSEM_ACTIVE_WRITE_BIAS);
162 return tmp == RWSEM_UNLOCKED_VALUE;
163}
164
165/*
166 * downgrade write lock to read lock
167 */
168static inline void
169__downgrade_write (struct rw_semaphore *sem)
170{
Christoph Lameter16592d22005-08-22 12:20:00 -0700171 long old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 do {
174 old = sem->count;
175 new = old - RWSEM_WAITING_BIAS;
176 } while (cmpxchg_rel(&sem->count, old, new) != old);
177
178 if (old < 0)
179 rwsem_downgrade_wake(sem);
180}
181
182/*
183 * Implement atomic add functionality. These used to be "inline" functions, but GCC v3.1
184 * doesn't quite optimize this stuff right and ends up with bad calls to fetchandadd.
185 */
Christoph Lameter16592d22005-08-22 12:20:00 -0700186#define rwsem_atomic_add(delta, sem) atomic64_add(delta, (atomic64_t *)(&(sem)->count))
187#define rwsem_atomic_update(delta, sem) atomic64_add_return(delta, (atomic64_t *)(&(sem)->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700189static inline int rwsem_is_locked(struct rw_semaphore *sem)
190{
191 return (sem->count != 0);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#endif /* _ASM_IA64_RWSEM_H */