KallistiOS  2.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Macros | Typedefs
spinlock.h File Reference

Simple locking. More...

#include <sys/cdefs.h>
#include <kos/thread.h>

Go to the source code of this file.


 Spinlock initializer.
#define spinlock_init(A)   *(A) = SPINLOCK_INITIALIZER
 Initialize a spinlock.
#define spinlock_lock(A)
 Spin on a lock.
#define spinlock_unlock(A)
 Free a lock.
#define spinlock_is_locked(A)   ( *(A) != 0 )
 Determine if a lock is locked.


typedef volatile int spinlock_t
 Spinlock data type.

Detailed Description

Simple locking.

This file contains definitions for very simple locks. Most of the time, you will probably not use such low-level locking, but will opt for something more fully featured like mutexes, semaphores, reader-writer semaphores, or recursive locks.

Dan Potter
See also:

Macro Definition Documentation

#define spinlock_init (   A)    *(A) = SPINLOCK_INITIALIZER

Initialize a spinlock.

This function-like macro abstracts initializing a spinlock, in case the initializer is not applicable to what you are doing.

AA pointer to the spinlock to be initialized.

Spinlock initializer.

All created spinlocks should be initialized with this initializer so that they are in a sane state, ready to be used.

#define spinlock_is_locked (   A)    ( *(A) != 0 )

Determine if a lock is locked.

This macro will return whether or not the lock specified is actually locked when it is called. This is NOT a thread-safe way of determining if a lock will be locked when you get around to locking it!

AA pointer to the spinlock to be checked.
#define spinlock_lock (   A)
do { \
spinlock_t * __lock = A; \
int __gotlock = 0; \
while(1) { \
asm volatile ("tas.b @%1\n\t" \
"movt %0\n\t" \
: "=r" (__gotlock) : "r" (__lock) : "t", "memory"); \
if (!__gotlock) \
thd_pass(); \
else break; \
} \
} while (0)

Spin on a lock.

This macro will spin on the lock, and will not return until the lock has been obtained for the calling thread.

AA pointer to the spinlock to be locked.
#define spinlock_unlock (   A)
do { \
*(A) = 0; \
} while (0)

Free a lock.

This macro will unlock the lock that is currently held by the calling thread. Do not use this macro unless you actually hold the lock!

AA pointer to the spinlock to be unlocked.

Typedef Documentation

typedef volatile int spinlock_t

Spinlock data type.