| 1 | #ifndef FIO_SEQLOCK_H |
| 2 | #define FIO_SEQLOCK_H |
| 3 | |
| 4 | #include "../arch/arch.h" |
| 5 | |
| 6 | struct seqlock { |
| 7 | volatile int sequence; |
| 8 | }; |
| 9 | |
| 10 | static inline void seqlock_init(struct seqlock *s) |
| 11 | { |
| 12 | s->sequence = 0; |
| 13 | } |
| 14 | |
| 15 | static inline unsigned int read_seqlock_begin(struct seqlock *s) |
| 16 | { |
| 17 | unsigned int seq; |
| 18 | |
| 19 | do { |
| 20 | seq = s->sequence; |
| 21 | if (!(seq & 1)) |
| 22 | break; |
| 23 | nop; |
| 24 | } while (1); |
| 25 | |
| 26 | read_barrier(); |
| 27 | return seq; |
| 28 | } |
| 29 | |
| 30 | static inline bool read_seqlock_retry(struct seqlock *s, unsigned int seq) |
| 31 | { |
| 32 | read_barrier(); |
| 33 | return s->sequence != seq; |
| 34 | } |
| 35 | |
| 36 | static inline void write_seqlock_begin(struct seqlock *s) |
| 37 | { |
| 38 | s->sequence++; |
| 39 | write_barrier(); |
| 40 | } |
| 41 | |
| 42 | static inline void write_seqlock_end(struct seqlock *s) |
| 43 | { |
| 44 | write_barrier(); |
| 45 | s->sequence++; |
| 46 | } |
| 47 | |
| 48 | #endif |