1 #ifndef __LINUX_SEQLOCK_H
2 #define __LINUX_SEQLOCK_H
4 * Reader/writer consistent mechanism without starving writers. This type of
5 * lock for data where the reader wants a consistent set of information
6 * 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.
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.
14 * Expected reader usage:
16 * seq = read_seqbegin(&foo);
18 * } while (read_seqretry(&foo, seq));
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.
25 * Based on x86_64 vsyscall gettimeofday
26 * by Keith Owens and Andrea Arcangeli
29 #include <linux/spinlock.h>
30 #include <linux/preempt.h>
31 #include <asm/processor.h>
34 * Version using sequence counter only.
35 * This can be used when code has its own mutex protecting the
36 * updating starting before the write_seqcountbeqin() and ending
37 * after the write_seqcount_end().
39 typedef struct seqcount
{
43 #define SEQCNT_ZERO { 0 }
44 #define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
47 * __read_seqcount_begin - begin a seq-read critical section (without barrier)
48 * @s: pointer to seqcount_t
49 * Returns: count to be passed to read_seqcount_retry
51 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
52 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
53 * provided before actually loading any of the variables that are to be
54 * protected in this critical section.
56 * Use carefully, only in critical code, and comment how the barrier is
59 static inline unsigned __read_seqcount_begin(const seqcount_t
*s
)
64 ret
= ACCESS_ONCE(s
->sequence
);
65 if (unlikely(ret
& 1)) {
73 * read_seqcount_begin - begin a seq-read critical section
74 * @s: pointer to seqcount_t
75 * Returns: count to be passed to read_seqcount_retry
77 * read_seqcount_begin opens a read critical section of the given seqcount.
78 * Validity of the critical section is tested by checking read_seqcount_retry
81 static inline unsigned read_seqcount_begin(const seqcount_t
*s
)
83 unsigned ret
= __read_seqcount_begin(s
);
89 * raw_seqcount_begin - begin a seq-read critical section
90 * @s: pointer to seqcount_t
91 * Returns: count to be passed to read_seqcount_retry
93 * raw_seqcount_begin opens a read critical section of the given seqcount.
94 * Validity of the critical section is tested by checking read_seqcount_retry
97 * Unlike read_seqcount_begin(), this function will not wait for the count
98 * to stabilize. If a writer is active when we begin, we will fail the
99 * read_seqcount_retry() instead of stabilizing at the beginning of the
102 static inline unsigned raw_seqcount_begin(const seqcount_t
*s
)
104 unsigned ret
= ACCESS_ONCE(s
->sequence
);
110 * __read_seqcount_retry - end a seq-read critical section (without barrier)
111 * @s: pointer to seqcount_t
112 * @start: count, from read_seqcount_begin
113 * Returns: 1 if retry is required, else 0
115 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
116 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
117 * provided before actually loading any of the variables that are to be
118 * protected in this critical section.
120 * Use carefully, only in critical code, and comment how the barrier is
123 static inline int __read_seqcount_retry(const seqcount_t
*s
, unsigned start
)
125 return unlikely(s
->sequence
!= start
);
129 * read_seqcount_retry - end a seq-read critical section
130 * @s: pointer to seqcount_t
131 * @start: count, from read_seqcount_begin
132 * Returns: 1 if retry is required, else 0
134 * read_seqcount_retry closes a read critical section of the given seqcount.
135 * If the critical section was invalid, it must be ignored (and typically
138 static inline int read_seqcount_retry(const seqcount_t
*s
, unsigned start
)
141 return __read_seqcount_retry(s
, start
);
146 * Sequence counter only version assumes that callers are using their
149 static inline void write_seqcount_begin(seqcount_t
*s
)
155 static inline void write_seqcount_end(seqcount_t
*s
)
162 * write_seqcount_barrier - invalidate in-progress read-side seq operations
163 * @s: pointer to seqcount_t
165 * After write_seqcount_barrier, no read-side seq operations will complete
166 * successfully and see data older than this.
168 static inline void write_seqcount_barrier(seqcount_t
*s
)
175 struct seqcount seqcount
;
180 * These macros triggered gcc-3.x compile-time problems. We think these are
181 * OK now. Be cautious.
183 #define __SEQLOCK_UNLOCKED(lockname) \
185 .seqcount = SEQCNT_ZERO, \
186 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
189 #define seqlock_init(x) \
191 seqcount_init(&(x)->seqcount); \
192 spin_lock_init(&(x)->lock); \
195 #define DEFINE_SEQLOCK(x) \
196 seqlock_t x = __SEQLOCK_UNLOCKED(x)
199 * Read side functions for starting and finalizing a read side section.
201 static inline unsigned read_seqbegin(const seqlock_t
*sl
)
203 return read_seqcount_begin(&sl
->seqcount
);
206 static inline unsigned read_seqretry(const seqlock_t
*sl
, unsigned start
)
208 return read_seqcount_retry(&sl
->seqcount
, start
);
212 * Lock out other writers and update the count.
213 * Acts like a normal spin_lock/unlock.
214 * Don't need preempt_disable() because that is in the spin_lock already.
216 static inline void write_seqlock(seqlock_t
*sl
)
218 spin_lock(&sl
->lock
);
219 write_seqcount_begin(&sl
->seqcount
);
222 static inline void write_sequnlock(seqlock_t
*sl
)
224 write_seqcount_end(&sl
->seqcount
);
225 spin_unlock(&sl
->lock
);
228 static inline void write_seqlock_bh(seqlock_t
*sl
)
230 spin_lock_bh(&sl
->lock
);
231 write_seqcount_begin(&sl
->seqcount
);
234 static inline void write_sequnlock_bh(seqlock_t
*sl
)
236 write_seqcount_end(&sl
->seqcount
);
237 spin_unlock_bh(&sl
->lock
);
240 static inline void write_seqlock_irq(seqlock_t
*sl
)
242 spin_lock_irq(&sl
->lock
);
243 write_seqcount_begin(&sl
->seqcount
);
246 static inline void write_sequnlock_irq(seqlock_t
*sl
)
248 write_seqcount_end(&sl
->seqcount
);
249 spin_unlock_irq(&sl
->lock
);
252 static inline unsigned long __write_seqlock_irqsave(seqlock_t
*sl
)
256 spin_lock_irqsave(&sl
->lock
, flags
);
257 write_seqcount_begin(&sl
->seqcount
);
261 #define write_seqlock_irqsave(lock, flags) \
262 do { flags = __write_seqlock_irqsave(lock); } while (0)
265 write_sequnlock_irqrestore(seqlock_t
*sl
, unsigned long flags
)
267 write_seqcount_end(&sl
->seqcount
);
268 spin_unlock_irqrestore(&sl
->lock
, flags
);
271 #endif /* __LINUX_SEQLOCK_H */