Skip to content

Commit

Permalink
qemu-thread: add simple test-and-set spinlock
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Delbergue <guillaume.delbergue@greensocs.com>
[Rewritten. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Guillaume Delbergue authored and bonzini committed Mar 15, 2016
1 parent a31d080 commit e136163
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/qemu/thread.h
Expand Up @@ -3,6 +3,8 @@

#include <inttypes.h>
#include <stdbool.h>
#include <errno.h>
#include "qemu/atomic.h"

typedef struct QemuMutex QemuMutex;
typedef struct QemuCond QemuCond;
Expand Down Expand Up @@ -62,4 +64,33 @@ struct Notifier;
void qemu_thread_atexit_add(struct Notifier *notifier);
void qemu_thread_atexit_remove(struct Notifier *notifier);

typedef struct QemuSpin {
int value;
} QemuSpin;

static inline void qemu_spin_init(QemuSpin *spin)
{
spin->value = 0;
}

static inline void qemu_spin_lock(QemuSpin *spin)
{
do {
while (atomic_read(&spin->value));
} while (atomic_xchg(&spin->value, true));
}

static inline int qemu_spin_trylock(QemuSpin *spin)
{
if (atomic_read(&spin->value) || atomic_xchg(&spin->value, true)) {
return -EBUSY;
}
return 0;
}

static inline void qemu_spin_unlock(QemuSpin *spin)
{
atomic_mb_set(&spin->value, 0);
}

#endif

0 comments on commit e136163

Please sign in to comment.