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.
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
;
26 static inline void seqlock_init(QemuSeqLock
*sl
, QemuMutex
*mutex
)
32 /* Lock out other writers and update the count. */
33 static inline void seqlock_write_lock(QemuSeqLock
*sl
)
36 qemu_mutex_lock(sl
->mutex
);
40 /* Write sequence before updating other fields. */
44 static inline void seqlock_write_unlock(QemuSeqLock
*sl
)
46 /* Write other fields before finalizing sequence. */
51 qemu_mutex_unlock(sl
->mutex
);
55 static inline unsigned seqlock_read_begin(QemuSeqLock
*sl
)
57 /* Always fail if a write is in progress. */
58 unsigned ret
= atomic_read(&sl
->sequence
);
60 /* Read sequence before reading other fields. */
65 static inline int seqlock_read_retry(const QemuSeqLock
*sl
, unsigned start
)
67 /* Read other fields before reading final sequence. */
69 return unlikely(atomic_read(&sl
->sequence
) != start
);