Skip to content

Commit e136163

Browse files
Guillaume Delberguebonzini
authored andcommitted
qemu-thread: add simple test-and-set spinlock
Signed-off-by: Guillaume Delbergue <guillaume.delbergue@greensocs.com> [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent a31d080 commit e136163

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

include/qemu/thread.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <inttypes.h>
55
#include <stdbool.h>
6+
#include <errno.h>
7+
#include "qemu/atomic.h"
68

79
typedef struct QemuMutex QemuMutex;
810
typedef struct QemuCond QemuCond;
@@ -62,4 +64,33 @@ struct Notifier;
6264
void qemu_thread_atexit_add(struct Notifier *notifier);
6365
void qemu_thread_atexit_remove(struct Notifier *notifier);
6466

67+
typedef struct QemuSpin {
68+
int value;
69+
} QemuSpin;
70+
71+
static inline void qemu_spin_init(QemuSpin *spin)
72+
{
73+
spin->value = 0;
74+
}
75+
76+
static inline void qemu_spin_lock(QemuSpin *spin)
77+
{
78+
do {
79+
while (atomic_read(&spin->value));
80+
} while (atomic_xchg(&spin->value, true));
81+
}
82+
83+
static inline int qemu_spin_trylock(QemuSpin *spin)
84+
{
85+
if (atomic_read(&spin->value) || atomic_xchg(&spin->value, true)) {
86+
return -EBUSY;
87+
}
88+
return 0;
89+
}
90+
91+
static inline void qemu_spin_unlock(QemuSpin *spin)
92+
{
93+
atomic_mb_set(&spin->value, 0);
94+
}
95+
6596
#endif

0 commit comments

Comments
 (0)