next up previous contents
Next: PI API for Sound Up: Hardware Abstraction Layer: Portable Previous: Syscall   Contents

Subsections

Spinlocks

Spinlocks are a much lighter weight (though less efficient) way to prevent contention between threads. In KOS they can also allow you to prevent contention between interrupt handlers and normal user code while still providing performance by allowing critical sections that still allow interrupts to happen.

The data type for a spinlock is spinlock_t. This value can either be initialized statically with SPINLOCK_INITIALIZER, or it can be initialized programmatically with spinlock_init.

These are usually implemented as macros for speed.

void spinlock_init(spinlock_t * lock)

Initializes the given spinlock. It will be unlocked initially.

void spinlock_lock(spinlock_t * lock)

Attempts to lock the spinlock. If it is already locked then the function will not return until the caller owns the lock.

void spinlock_unlock(spinlock_t * lock)

Unlocks the given spinlock, if it's locked. Note that any thread can unlock any spinlock - there is no lock ownership.

int spinlock_is_locked(spinlock_t * lock)

Returns non-zero if the given lock is locked. This is helpful for, e.g., allocating memory inside an interrupt.


next up previous contents
Next: PI API for Sound Up: Hardware Abstraction Layer: Portable Previous: Syscall   Contents
Dan Potter 2002-07-29