]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/include/linux/seqlock.h
435420fe5b18830141dc8687539c22fbecf4e22e
[bcachefs-tools-debian] / c_src / include / linux / seqlock.h
1 #ifndef __LINUX_SEQLOCK_H
2 #define __LINUX_SEQLOCK_H
3
4 #include <linux/compiler.h>
5
6 typedef struct seqcount {
7         unsigned sequence;
8 } seqcount_t;
9
10 static inline void seqcount_init(seqcount_t *s)
11 {
12         s->sequence = 0;
13 }
14
15 static inline unsigned read_seqcount_begin(const seqcount_t *s)
16 {
17         unsigned ret;
18
19 repeat:
20         ret = READ_ONCE(s->sequence);
21         if (unlikely(ret & 1)) {
22                 cpu_relax();
23                 goto repeat;
24         }
25         smp_rmb();
26         return ret;
27 }
28
29 static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
30 {
31         smp_rmb();
32         return unlikely(s->sequence != start);
33 }
34
35 static inline void write_seqcount_begin(seqcount_t *s)
36 {
37         s->sequence++;
38         smp_wmb();
39 }
40
41 static inline void write_seqcount_end(seqcount_t *s)
42 {
43         smp_wmb();
44         s->sequence++;
45 }
46
47 #endif /* __LINUX_SEQLOCK_H */