HelenOS sources
This source file includes following definitions.
- spinlock_initialize
- spinlock_trylock
- spinlock_lock
- spinlock_unlock
- spinlock_locked
#ifndef KERN_SPINLOCK_H_
#define KERN_SPINLOCK_H_
#include <stdatomic.h>
#include <stdbool.h>
#include <arch/types.h>
#include <assert.h>
#define DEADLOCK_THRESHOLD 100000000
#if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
#include <log.h>
#define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
#define DEADLOCK_PROBE(pname, value) \
if ((pname)++ > (value)) { \
(pname) = 0; \
log(LF_OTHER, LVL_WARN, \
"Deadlock probe %s: exceeded threshold %u\n" \
"cpu%u: function=%s, line=%u\n", \
#pname, (value), CPU->id, __func__, __LINE__); \
}
#else
#define DEADLOCK_PROBE_INIT(pname)
#define DEADLOCK_PROBE(pname, value)
#endif
typedef struct spinlock {
#ifdef CONFIG_SMP
atomic_flag flag;
#ifdef CONFIG_DEBUG_SPINLOCK
const char *name;
#endif
#endif
} spinlock_t;
#define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
#define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
#ifdef CONFIG_SMP
#ifdef CONFIG_DEBUG_SPINLOCK
#define SPINLOCK_INITIALIZER(desc_name) { .name = (desc_name), .flag = ATOMIC_FLAG_INIT }
#else
#define SPINLOCK_INITIALIZER(desc_name) { .flag = ATOMIC_FLAG_INIT }
#endif
#else
#define SPINLOCK_INITIALIZER(desc_name) {}
#endif
#define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
#define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
static spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
#if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
#define ASSERT_SPINLOCK(expr, lock) assert_verbose(expr, (lock)->name)
#else
#define ASSERT_SPINLOCK(expr, lock) assert(expr)
#endif
#define SPINLOCK_INITIALIZE(lock_name) \
SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
#define SPINLOCK_STATIC_INITIALIZE(lock_name) \
SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
#ifdef CONFIG_SMP
extern void spinlock_initialize(spinlock_t *, const char *);
extern bool spinlock_trylock(spinlock_t *);
extern void spinlock_lock(spinlock_t *);
extern void spinlock_unlock(spinlock_t *);
extern bool spinlock_locked(spinlock_t *);
#else
#include <preemption.h>
static inline void spinlock_initialize(spinlock_t *l, const char *name)
{
}
static inline bool spinlock_trylock(spinlock_t *l)
{
preemption_disable();
return true;
}
static inline void spinlock_lock(spinlock_t *l)
{
preemption_disable();
}
static inline void spinlock_unlock(spinlock_t *l)
{
preemption_enable();
}
static inline bool spinlock_locked(spinlock_t *l)
{
return true;
}
#endif
typedef struct {
spinlock_t lock;
bool guard;
ipl_t ipl;
#ifdef CONFIG_DEBUG_SPINLOCK
_Atomic(struct cpu *) owner;
#endif
} irq_spinlock_t;
#define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
#define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
#define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
#define IRQ_SPINLOCK_INITIALIZER(desc_name) \
{ \
.lock = SPINLOCK_INITIALIZER(desc_name), \
.guard = false, \
.ipl = 0, \
}
#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
static irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
#define IRQ_SPINLOCK_INITIALIZE(lock_name) \
IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
#define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
extern void irq_spinlock_lock(irq_spinlock_t *, bool);
extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
extern bool irq_spinlock_trylock(irq_spinlock_t *);
extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
extern bool irq_spinlock_locked(irq_spinlock_t *);
#endif
HelenOS homepage, sources at GitHub