bond/inc/kern/spin_lock.h

28 lines
569 B
C
Raw Normal View History

2019-06-26 05:47:18 +00:00
#pragma once
#include <kern/cdef.h>
2019-11-28 18:02:52 +00:00
// implements a simple ticket lock
struct spin_lock {
// LOW 16 bits: cur ticket
// HIGH 16 bits: cur owner
DECL_ATOMIC(uint32) val;
2019-06-26 05:47:18 +00:00
};
2019-11-28 18:02:52 +00:00
#define SPIN_LOCK_INITIALIZER {.val = ATOMIC_VAR_INIT(0)}
STATIC_ASSERT(sizeof(struct spin_lock) == sizeof(uint32), "spin_lock size isn't 32 bits");
2019-06-26 05:47:18 +00:00
void
2019-11-28 18:02:52 +00:00
spin_lock_init(struct spin_lock *lock);
2019-06-26 05:47:18 +00:00
void
2019-11-28 18:02:52 +00:00
spin_lock_acq(struct spin_lock *lock);
2019-06-26 05:47:18 +00:00
void
2019-11-28 18:02:52 +00:00
spin_lock_rel(struct spin_lock *lock);
// returns non-zero on success otherwise zero
int
spin_lock_try_acq(struct spin_lock *lock);