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