aboutsummaryrefslogtreecommitdiffstats
path: root/include/pfrw.h
blob: a94fa117abd4c918fcd7198bca8fda38f95667e2 (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2008, 2009, Bjoern B. Brandenburg <bbb [at] cs.unc.edu>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* Starvation-free, real-time-friendly reader-writer locks.
 *
 * This is an implementation of phase-fair reader-writer locks for i386. Readers are
 * only blocked by at most one reader and one writer phase. Writers gain access
 * in FIFO order. Both reader and writer paths require two atomic instructions
 * each (lock/xadd and lock/add).
 *
 * For details, see:
 *
 *     B. Brandenburg and J. Anderson, "Reader-Writer Synchronization for
 *     Shared-Memory Multiprocessor Real-Time Systems", Proceedings of the 21th
 *     Euromicro Conference on Real-Time Systems (ECRTS 2009),
 *     pp. 184-193. IEEE, July 2009.
 *
 * Tested on Linux with gcc 4.3.3.
 */

#ifndef PFRW_H
#define PFRW_H

typedef struct pf_rwlock{
        uint32_t rin, rout, wout, win;	
} pf_rwlock_t;

#define DEFINE_PF_RWLOCK(x) pf_rwlock_t x =	\
	{ 0, 0, 0, 0 }

#define PF_RW_LOCK_BIAS 0x100
#define PF_WRITER_BITS  0x0ff

static inline void pf_read_lock(pf_rwlock_t *l)
{
	unsigned int tmp = PF_RW_LOCK_BIAS, tmp2;
	asm volatile("  lock/xaddl %0, %2  \n"
		     "  testb %b0, %b0  \n"
		     "  jnz   1f      \n"
		     ".subsection 2   \n"
		     "1:"
		     "  movb  %2, %b1  \n"
		     "  cmpb  %b1, %b0  \n"
		     "  jne   2f      \n"
		     "  rep; nop      \n"
		     "  jmp   1b      \n"
		     ".previous       \n"
		     "2:"
		     : "+&q" (tmp), "=&q" (tmp2)
		     : "m" (l->rin)
		     : "memory", "cc");
}

static inline int pf_read_trylock(pf_rwlock_t *l)
{
	unsigned int tmp, was_locked;
	asm volatile("  movl  %2, %1  \n"
		     "1:"
		     "  testb %b1,%b1 \n"
		     "  jnz   2f      \n"
		     "  movl  %1, %0  \n"
		     "  addl  %3, %0  \n"
		     "  lock/cmpxchgl %0,%2\n"
		     /* implicit movl %2, %1 on failure */
		     "  jnz 1b        \n"
		     "2:"
		     : "=&q" (tmp), "=&a" (was_locked)
		     : "m" (l->rin), "i" (PF_RW_LOCK_BIAS)
		     : "memory", "cc");
	return !(was_locked & PF_WRITER_BITS);
}

static inline void pf_read_unlock(pf_rwlock_t *l)
{
	asm volatile("lock/addl %1, %0"
		     : /* no output */
		     : "m" (l->rout),
		       "i" (PF_RW_LOCK_BIAS)
		     : "memory", "cc");
}

static inline void pf_write_lock(pf_rwlock_t *l)
{
	int tmp = 1;
	asm volatile("  lock/xaddl %0, %1  \n"
		     "  cmpl  %0, %2  \n"
		     "  jne   1f      \n"
		     ".subsection 2   \n"
		     "1:"
		     "  rep; nop      \n"
		     "  cmpl  %0, %2  \n"
		     "  je    2f      \n"
		     "  jmp   1b      \n"
		     ".previous       \n"
		     "2:"
		     "  and   $0xff, %0\n"
		     "  or    $0x80, %0\n"
		     "  lock/xaddl %0, %3  \n"
		     "  movb  $0, %b0 \n"
		     "  cmpl  %0, %4  \n"
		     "  jne   3f      \n"
		     ".subsection 2   \n"
		     "3:"
		     "  rep; nop      \n"
		     "  cmpl  %0, %4  \n"
		     "  je    4f      \n"
		     "  jmp   3b      \n"
		     ".previous       \n"
		     "4:"
		     : "+q"(tmp)
		     : "m" (l->win),
		       "m" (l->wout),
		       "m" (l->rin),
		       "m" (l->rout)
		     : "memory", "cc");
}

static inline int pf_write_trylock(pf_rwlock_t *l)
{
	unsigned int tmp, tmp2,  was_locked;

	/* Once a writer has incremented the writer ticket, it can't safely
	 * back out in all situations. This limits the write_trylock()
	 * operation. This approximation only works for CPU counts up to
	 * 127. If more than 127 "fixup" operations are executed consecutively,
	 * a slow reader may fail to discern consecutive writer phases (PHID is
	 * 7 bits in this implementation). If a truly safe trylock() operation
	 * is required, then a compact phase-fair RW lock should be used
	 * instead or readers must execute a while (!read_trylock()); loop. The
	 * following workaround is sufficient for our purposes.
	 */

	/* pre-check for reader first */
	if (l->rin != l->rout)
		return 0;

	asm volatile("  movl  %4, %0  \n"
		     "  movl  %3, %1  \n"
		     "  cmpl  %0, %1  \n"
		     /* writer active?    */
		     "  jne   3f      \n"
		     "  incl  %0      \n"
		     "  lock/cmpxchgl %0,%3\n"
		     /* writer arrived?   */
		     "  jnz   3f      \n"
		     "  movl  %1, %2  \n"
		     "  movl  %6, %0  \n"
		     "  andl  $0xff, %2\n"
		     "  movl  %5, %1  \n"
		     "  or    $0x80, %2\n"
		     "  cmpl  %0, %1  \n"
		     /* reader active?    */
		     "  jne   2f \n"
		     "  addl  %2, %0  \n"
		     "  lock/cmpxchgl %0,%5\n"
		     /* reader arrived?   */
		     "  jnz   2f      \n"
		     /* success           */
		     "  incl  %1      \n"
		     "1:              \n"

		     /* cleanup           */
		     ".subsection 2   \n"
		     "2:"
		     /* finish write      */
		     "  incl  %4      \n"
		     "3:"
		     /* return failure    */
		     "  xorl  %1, %1  \n"
		     "  jmp   1b      \n"
		     ".previous"
		     : "=&r" (tmp),        /* 0 */
		       "=&a" (was_locked), /* 1 */
		       "=&r" (tmp2)        /* 2 */
		     : "m" (l->win),       /* 3 */
		       "m" (l->wout),      /* 4 */
		       "m" (l->rin),       /* 5 */
		       "m" (l->rout)       /* 6 */
		     : "memory", "cc");
	return was_locked;
}

static inline void pf_write_unlock(pf_rwlock_t *l)
{
	unsigned int tmp;
	asm volatile("movl %2, %0 \n"
		     "movb $0, %1 \n"
		     "incl %0     \n"
		     "movl %0, %2   "
		     : "=&r" (tmp)
		     : "m" (l->rin),
		       "m" (l->wout)
		     : "memory", "cc");
}



#ifdef CONFIG_TRACK_LOCK_STATS

typedef struct {
	struct pf_rwlock lock;

	unsigned long wcontended;
	unsigned long wuncontended;
	unsigned long rcontended;
	unsigned long runcontended;
} rwlock_t;

static inline void rw_init(rwlock_t *lock)
{
	lock->lock.rin  = 0;
	lock->lock.rout = 0;
	lock->lock.win  = 0;
	lock->lock.wout = 0;

	lock->rcontended   = 0;
	lock->runcontended = 0;
	lock->wcontended   = 0;
	lock->wuncontended = 0;
}

static inline void rw_writelock(rwlock_t *lock)
{
	if (pf_write_trylock(&lock->lock)) {
		/* got it without contention */
		lock->wuncontended++;
	} else {
		/* nope, must wait */
		pf_write_lock(&lock->lock);
		lock->wcontended++;
	}
}

static inline void rw_readlock(rwlock_t *lock)
{
	if (pf_read_trylock(&lock->lock)) {
		/* got it without contention */
		atomic_inc(&lock->runcontended);
	} else {
		/* nope, must wait */
		atomic_inc(&lock->rcontended);
		pf_read_lock(&lock->lock);
	}
}

static inline void rw_writeunlock(rwlock_t *lock)
{
	pf_write_unlock(&lock->lock);
}

static inline void rw_readunlock(rwlock_t *lock)
{
	pf_read_unlock(&lock->lock);
}

#else

typedef struct pf_rwlock rwlock_t;
#define rw_init(x) do 		{(x)->rin = 0; (x)->rout = 0; (x)->wout = 0; (x)->win = 0;} while (0)
#define rw_readlock(x) 		pf_read_lock(x)
#define rw_readunlock(x) 	pf_read_unlock(x)
#define rw_writelock(x) 	pf_write_lock(x)
#define rw_writeunlock(x) 	pf_write_unlock(x)

#endif


#endif