blob: 26e4925bc35bb5f76d28aa55119b4a2c2501091d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_SEQLOCK_H
2#define __LINUX_SEQLOCK_H
3/*
4 * Reader/writer consistent mechanism without starving writers. This type of
Robert P. J. Dayd08df602007-02-17 19:07:33 +01005 * lock for data where the reader wants a consistent set of information
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * and is willing to retry if the information changes. Readers never
7 * block but they may have to retry if a writer is in
8 * progress. Writers do not wait for readers.
9 *
10 * This is not as cache friendly as brlock. Also, this will not work
11 * for data that contains pointers, because any writer could
12 * invalidate a pointer that a reader was following.
13 *
14 * Expected reader usage:
15 * do {
16 * seq = read_seqbegin(&foo);
17 * ...
18 * } while (read_seqretry(&foo, seq));
19 *
20 *
21 * On non-SMP the spin locks disappear but the writer still needs
22 * to increment the sequence variables because an interrupt routine could
23 * change the state of the data.
24 *
25 * Based on x86_64 vsyscall gettimeofday
26 * by Keith Owens and Andrea Arcangeli
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/spinlock.h>
30#include <linux/preempt.h>
31
32typedef struct {
33 unsigned sequence;
34 spinlock_t lock;
35} seqlock_t;
36
37/*
38 * These macros triggered gcc-3.x compile-time problems. We think these are
39 * OK now. Be cautious.
40 */
Ingo Molnare4d91912006-07-03 00:24:34 -070041#define __SEQLOCK_UNLOCKED(lockname) \
42 { 0, __SPIN_LOCK_UNLOCKED(lockname) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ingo Molnare4d91912006-07-03 00:24:34 -070044#define SEQLOCK_UNLOCKED \
45 __SEQLOCK_UNLOCKED(old_style_seqlock_init)
46
Ingo Molnar99a3eb32006-12-12 12:10:28 +010047#define seqlock_init(x) \
48 do { \
49 (x)->sequence = 0; \
50 spin_lock_init(&(x)->lock); \
51 } while (0)
Ingo Molnare4d91912006-07-03 00:24:34 -070052
53#define DEFINE_SEQLOCK(x) \
54 seqlock_t x = __SEQLOCK_UNLOCKED(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Lock out other writers and update the count.
57 * Acts like a normal spin_lock/unlock.
58 * Don't need preempt_disable() because that is in the spin_lock already.
59 */
60static inline void write_seqlock(seqlock_t *sl)
61{
62 spin_lock(&sl->lock);
63 ++sl->sequence;
Daniel Walker20f09392007-04-26 09:46:05 -070064 smp_wmb();
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Daniel Walker20f09392007-04-26 09:46:05 -070067static inline void write_sequnlock(seqlock_t *sl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 smp_wmb();
70 sl->sequence++;
71 spin_unlock(&sl->lock);
72}
73
74static inline int write_tryseqlock(seqlock_t *sl)
75{
76 int ret = spin_trylock(&sl->lock);
77
78 if (ret) {
79 ++sl->sequence;
Daniel Walker20f09392007-04-26 09:46:05 -070080 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 return ret;
83}
84
85/* Start of read calculation -- fetch last complete writer token */
mao, bibocde227a2006-04-11 12:54:54 +020086static __always_inline unsigned read_seqbegin(const seqlock_t *sl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 unsigned ret = sl->sequence;
89 smp_rmb();
90 return ret;
91}
92
93/* Test if reader processed invalid data.
94 * If initial values is odd,
95 * then writer had already started when section was entered
96 * If sequence value changed
97 * then writer changed data while in section
98 *
99 * Using xor saves one conditional branch.
100 */
mao, bibocde227a2006-04-11 12:54:54 +0200101static __always_inline int read_seqretry(const seqlock_t *sl, unsigned iv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 smp_rmb();
104 return (iv & 1) | (sl->sequence ^ iv);
105}
106
107
108/*
109 * Version using sequence counter only.
110 * This can be used when code has its own mutex protecting the
111 * updating starting before the write_seqcountbeqin() and ending
112 * after the write_seqcount_end().
113 */
114
115typedef struct seqcount {
116 unsigned sequence;
117} seqcount_t;
118
119#define SEQCNT_ZERO { 0 }
120#define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
121
122/* Start of read using pointer to a sequence counter only. */
123static inline unsigned read_seqcount_begin(const seqcount_t *s)
124{
125 unsigned ret = s->sequence;
126 smp_rmb();
127 return ret;
128}
129
130/* Test if reader processed invalid data.
131 * Equivalent to: iv is odd or sequence number has changed.
132 * (iv & 1) || (*s != iv)
133 * Using xor saves one conditional branch.
134 */
135static inline int read_seqcount_retry(const seqcount_t *s, unsigned iv)
136{
137 smp_rmb();
138 return (iv & 1) | (s->sequence ^ iv);
139}
140
141
142/*
143 * Sequence counter only version assumes that callers are using their
144 * own mutexing.
145 */
146static inline void write_seqcount_begin(seqcount_t *s)
147{
148 s->sequence++;
149 smp_wmb();
150}
151
152static inline void write_seqcount_end(seqcount_t *s)
153{
154 smp_wmb();
155 s->sequence++;
156}
157
158/*
159 * Possible sw/hw IRQ protected versions of the interfaces.
160 */
161#define write_seqlock_irqsave(lock, flags) \
162 do { local_irq_save(flags); write_seqlock(lock); } while (0)
163#define write_seqlock_irq(lock) \
164 do { local_irq_disable(); write_seqlock(lock); } while (0)
165#define write_seqlock_bh(lock) \
166 do { local_bh_disable(); write_seqlock(lock); } while (0)
167
168#define write_sequnlock_irqrestore(lock, flags) \
169 do { write_sequnlock(lock); local_irq_restore(flags); } while(0)
170#define write_sequnlock_irq(lock) \
171 do { write_sequnlock(lock); local_irq_enable(); } while(0)
172#define write_sequnlock_bh(lock) \
173 do { write_sequnlock(lock); local_bh_enable(); } while(0)
174
175#define read_seqbegin_irqsave(lock, flags) \
176 ({ local_irq_save(flags); read_seqbegin(lock); })
177
178#define read_seqretry_irqrestore(lock, iv, flags) \
179 ({ \
180 int ret = read_seqretry(lock, iv); \
181 local_irq_restore(flags); \
182 ret; \
183 })
184
185#endif /* __LINUX_SEQLOCK_H */