Clean up header guards that don't match their file name
[qemu/kevin.git] / include / qemu / seqlock.h
blob18eb3fda9de2d4b5bc6af4fcaa36318c75fbee92
1 /*
2 * Seqlock implementation for QEMU
4 * Copyright Red Hat, Inc. 2013
6 * Author:
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #ifndef QEMU_SEQLOCK_H
14 #define QEMU_SEQLOCK_H 1
16 #include "qemu/atomic.h"
17 #include "qemu/thread.h"
19 typedef struct QemuSeqLock QemuSeqLock;
21 struct QemuSeqLock {
22 unsigned sequence;
25 static inline void seqlock_init(QemuSeqLock *sl)
27 sl->sequence = 0;
30 /* Lock out other writers and update the count. */
31 static inline void seqlock_write_begin(QemuSeqLock *sl)
33 ++sl->sequence;
35 /* Write sequence before updating other fields. */
36 smp_wmb();
39 static inline void seqlock_write_end(QemuSeqLock *sl)
41 /* Write other fields before finalizing sequence. */
42 smp_wmb();
44 ++sl->sequence;
47 static inline unsigned seqlock_read_begin(QemuSeqLock *sl)
49 /* Always fail if a write is in progress. */
50 unsigned ret = atomic_read(&sl->sequence);
52 /* Read sequence before reading other fields. */
53 smp_rmb();
54 return ret & ~1;
57 static inline int seqlock_read_retry(const QemuSeqLock *sl, unsigned start)
59 /* Read other fields before reading final sequence. */
60 smp_rmb();
61 return unlikely(atomic_read(&sl->sequence) != start);
64 #endif