seqlock: add simple user space code for sequence locks
authorJens Axboe <axboe@fb.com>
Wed, 26 Apr 2017 20:20:21 +0000 (14:20 -0600)
committerJens Axboe <axboe@fb.com>
Wed, 26 Apr 2017 20:20:21 +0000 (14:20 -0600)
Signed-off-by: Jens Axboe <axboe@fb.com>
lib/seqlock.h [new file with mode: 0644]

diff --git a/lib/seqlock.h b/lib/seqlock.h
new file mode 100644 (file)
index 0000000..1ac1eb6
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef FIO_SEQLOCK_H
+#define FIO_SEQLOCK_H
+
+#include "../arch/arch.h"
+
+struct seqlock {
+       volatile int sequence;
+};
+
+static inline void seqlock_init(struct seqlock *s)
+{
+       s->sequence = 0;
+}
+
+static inline unsigned int read_seqlock_begin(struct seqlock *s)
+{
+       unsigned int seq;
+
+       do {
+               seq = s->sequence;
+               if (!(seq & 1))
+                       break;
+               nop;
+       } while (1);
+
+       read_barrier();
+       return seq;
+}
+
+static inline bool read_seqlock_retry(struct seqlock *s, unsigned int seq)
+{
+       read_barrier();
+       return s->sequence != seq;
+}
+
+static inline void write_seqlock_begin(struct seqlock *s)
+{
+       s->sequence++;
+       write_barrier();
+}
+
+static inline void write_seqlock_end(struct seqlock *s)
+{
+       write_barrier();
+       s->sequence++;
+}
+
+#endif