aboutsummaryrefslogtreecommitdiffstats
path: root/small/urw-locks.patch
blob: bd7c66dbe40b208397dd82c7b9171b4dc857f1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
From 49ff4454db1f25a9502757fa370f5f44fb1e9d67 Mon Sep 17 00:00:00 2001
From: Con Kolivas <kernel@kolivas.org>
Date: Fri, 4 Oct 2013 22:06:24 -0400
Subject: [PATCH 7/7] urw-locks

Implement a variant of read/write locks that use a combination of 1 spinlock
and 1 read/write lock to create an upgradeable read/write lock. All r/u/w
variants take the spinlock first thus these will favour writers over readers.
Once readers have taken the spinlock and the read lock they drop the spinlock
thus allowing multiple readers to exist concurrently. The upgradeable variant
only takes the spinlock and can be upgraded to write locks or downgraded to
read locks. The write locks can be downgraded to read locks.

-ck
---
 include/linux/urwlock.h | 245 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 245 insertions(+)
 create mode 100644 include/linux/urwlock.h

diff --git a/include/linux/urwlock.h b/include/linux/urwlock.h
new file mode 100644
index 0000000..21d6793
--- /dev/null
+++ b/include/linux/urwlock.h
@@ -0,0 +1,245 @@
+/*
+ * include/linux/urwlock.h - Upgradeable read/write locks.
+ *
+ * Copyright (C) 2012 Con Kolivas <kernel@kolivas.org>
+ *
+ * These are upgradeable variants of read/write locks.
+ *
+ * When a lock is chosen, one of read, upgradeable or write lock needs to be
+ * chosen. Much like read/write locks, a read lock cannot be upgraded to a
+ * write lock. However the upgradeable version can be either upgraded to a
+ * write lock, or downgraded to a read lock. Unlike read/write locks, these
+ * locks favour writers over readers. They are significantly more overhead
+ * than either spinlocks or read/write locks as they include one of each,
+ * however they are suited to situations where there are clear distinctions
+ * between read and write patterns, and where the state may be indeterminate
+ * for a period, allowing other readers to continue reading till they need to
+ * declare themselves as read or write.
+ */
+
+#ifndef __LINUX_URWLOCK_H
+#define __LINUX_URWLOCK_H
+
+#include <linux/spinlock.h>
+
+struct urwlock {
+	raw_spinlock_t lock;
+	rwlock_t rwlock;
+};
+
+typedef struct urwlock urwlock_t;
+
+static inline void urwlock_init(urwlock_t *urw)
+{
+	raw_spin_lock_init(&urw->lock);
+	rwlock_init(&urw->rwlock);
+}
+
+/* Low level write and read lock/unlock of the rw lock. */
+static inline void __urw_write_lock(rwlock_t *rw)
+{
+	rwlock_acquire(&rw.dep_map, 0, 0, _RET_IP_);
+	LOCK_CONTENDED(rw, do_raw_write_trylock, do_raw_write_lock);
+}
+
+static inline void __urw_write_unlock(rwlock_t *rw)
+{
+	rwlock_release(&rw.dep_map, 1, _RET_IP_);
+	do_raw_write_unlock(rw);
+}
+
+static inline void __urw_read_lock(rwlock_t *rw)
+{
+	rwlock_acquire_read(&rw.dep_map, 0, 0, _RET_IP_);
+	LOCK_CONTENDED(rw, do_raw_read_trylock, do_raw_read_lock);
+}
+
+static inline void __urw_read_unlock(rwlock_t *rw)
+{
+	rwlock_release(&rw.dep_map, 1, _RET_IP_);
+	do_raw_read_unlock(rw);
+}
+
+/* Write variant of urw lock. Grabs both spinlock and rwlock. */
+static inline void urw_wlock(urwlock_t *urw)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+{
+	raw_spin_lock(&urw->lock);
+	__urw_write_lock(&urw->rwlock);
+}
+
+/* Write variant of urw unlock. Releases both spinlock and rwlock. */
+static inline void urw_wunlock(urwlock_t *urw)
+	__releases(urw->rwlock)
+	__releases(urw->lock)
+{
+	__urw_write_unlock(&urw->rwlock);
+	raw_spin_unlock(&urw->lock);
+}
+
+/*
+ * Read variant of urw lock. Grabs spinlock and rwlock and then releases
+ * spinlock.
+ */
+static inline void urw_rlock(urwlock_t *urw)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+	__releases(urw->lock)
+{
+	raw_spin_lock(&urw->lock);
+	__urw_read_lock(&urw->rwlock);
+	spin_release(&urw->lock.dep_map, 1, _RET_IP_);
+	do_raw_spin_unlock(&urw->lock);
+}
+
+/* Read variant of urw lock. Releases only the rwlock. */
+static inline void urw_runlock(urwlock_t *urw)
+	__releases(urw->rwlock)
+{
+	__urw_read_unlock(&urw->rwlock);
+}
+
+/* Upgradeable variant of urw lock. Grabs only the spinlock. */
+static inline void urw_ulock(urwlock_t *urw)
+	__acquires(urw->lock)
+{
+	raw_spin_lock(&urw->lock);
+}
+
+/* Upgrade the upgradeable variant of urwlock. Grabs the write lock. */
+static inline void urw_upgrade(urwlock_t *urw)
+{
+	__urw_write_lock(&urw->rwlock);
+}
+
+/*
+ * Downgrade the upgradeable variant of urwlock to a read lock. Grabs the
+ * read rwlock and releases the spinlock.
+ */
+static inline void urw_udowngrade(urwlock_t *urw)
+	__acquires(urw->rwlock)
+	__releases(urw->lock)
+{
+	__urw_read_lock(&urw->rwlock);
+	spin_release(&urw->lock.dep_map, 1, _RET_IP_);
+	do_raw_spin_unlock(&urw->lock);
+}
+
+/*
+ * Downgrade the write variant of urwlock to a read lock. Drops the write
+ * rwlock, grabs the read rwlock and releases the spinlock.
+ */
+static inline void urw_wdowngrade(urwlock_t *urw)
+	__releases(urw->rwlock)
+	__acquires(urw->rwlock)
+	__releases(urw->lock)
+{
+	__urw_write_unlock(&urw->rwlock);
+	__urw_read_lock(&urw->rwlock);
+	spin_release(&urw->lock.dep_map, 1, _RET_IP_);
+	do_raw_spin_unlock(&urw->lock);
+}
+
+/*
+ * Unlock the upgradeable variant of urwlock where it has not been up or
+ * downgraded.
+ */
+static inline void urw_uunlock(urwlock_t *urw)
+	__releases(urw->lock)
+{
+	raw_spin_unlock(&urw->lock);
+}
+
+/* IRQ variants of urw locks */
+static inline void urw_wlock_irq(urwlock_t *urw)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+{
+	raw_spin_lock_irq(&urw->lock);
+	__urw_write_lock(&urw->rwlock);
+}
+
+static inline void urw_wunlock_irq(urwlock_t *urw)
+	__releases(urw->rwlock)
+	__releases(urw->lock)
+{
+	__urw_write_unlock(&urw->rwlock);
+	raw_spin_unlock_irq(&urw->lock);
+}
+
+static inline void urw_rlock_irq(urwlock_t *urw)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+	__releases(urw->lock)
+{
+	raw_spin_lock_irq(&urw->lock);
+	__urw_read_lock(&urw->rwlock);
+	spin_release(&urw->lock.dep_map, 1, _RET_IP_);
+	do_raw_spin_unlock(&urw->lock);
+}
+
+static inline void urw_runlock_irq(urwlock_t *urw)
+	__releases(urw->rwlock)
+{
+	read_unlock_irq(&urw->rwlock);
+}
+
+static inline void urw_ulock_irq(urwlock_t *urw)
+	__acquires(urw->lock)
+{
+	raw_spin_lock_irq(&urw->lock);
+}
+
+static inline void urw_uunlock_irq(urwlock_t *urw)
+	__releases(urw->lock)
+{
+	raw_spin_unlock_irq(&urw->lock);
+}
+
+static inline void urw_wlock_irqsave(urwlock_t *urw, unsigned long *flags)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+{
+	raw_spin_lock_irqsave(&urw->lock, *flags);
+	__urw_write_lock(&urw->rwlock);
+}
+
+static inline void urw_wunlock_irqrestore(urwlock_t *urw, unsigned long *flags)
+	__releases(urw->rwlock)
+	__releases(urw->lock)
+{
+	__urw_write_unlock(&urw->rwlock);
+	raw_spin_unlock_irqrestore(&urw->lock, *flags);
+}
+
+static inline void urw_ulock_irqsave(urwlock_t *urw, unsigned long *flags)
+	__acquires(urw->lock)
+{
+	raw_spin_lock_irqsave(&urw->lock, *flags);
+}
+
+static inline void urw_uunlock_irqrestore(urwlock_t *urw, unsigned long *flags)
+	__releases(urw->lock)
+{
+	raw_spin_unlock_irqrestore(&urw->lock, *flags);
+}
+
+static inline void urw_rlock_irqsave(urwlock_t *urw, unsigned long *flags)
+	__acquires(urw->lock)
+	__acquires(urw->rwlock)
+	__releases(urw->lock)
+{
+	raw_spin_lock_irqsave(&urw->lock, *flags);
+	__urw_read_lock(&urw->rwlock);
+	spin_release(&urw->lock.dep_map, 1, _RET_IP_);
+	do_raw_spin_unlock(&urw->lock);
+}
+
+static inline void urw_runlock_irqrestore(urwlock_t *urw, unsigned long *flags)
+	__releases(urw->rwlock)
+{
+	read_unlock_irqrestore(&urw->rwlock, *flags);
+}
+
+#endif /* __LINUX_URWLOCK_H */
-- 
1.8.1.2