2 * Seqlock implementation for QEMU
4 * Copyright Red Hat, Inc. 2013
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.
14 #ifndef QEMU_SEQLOCK_H
15 #define QEMU_SEQLOCK_H
17 #include "qemu/atomic.h"
18 #include "qemu/thread.h"
20 typedef struct QemuSeqLock QemuSeqLock
;
26 static inline void seqlock_init(QemuSeqLock
*sl
)
31 /* Lock out other writers and update the count. */
32 static inline void seqlock_write_begin(QemuSeqLock
*sl
)
34 atomic_set(&sl
->sequence
, sl
->sequence
+ 1);
36 /* Write sequence before updating other fields. */
40 static inline void seqlock_write_end(QemuSeqLock
*sl
)
42 /* Write other fields before finalizing sequence. */
45 atomic_set(&sl
->sequence
, sl
->sequence
+ 1);
48 static inline unsigned seqlock_read_begin(QemuSeqLock
*sl
)
50 /* Always fail if a write is in progress. */
51 unsigned ret
= atomic_read(&sl
->sequence
);
53 /* Read sequence before reading other fields. */
58 static inline int seqlock_read_retry(const QemuSeqLock
*sl
, unsigned start
)
60 /* Read other fields before reading final sequence. */
62 return unlikely(atomic_read(&sl
->sequence
) != start
);