KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
spinlock.h File Reference

Simple locking. More...

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

Go to the source code of this file.

Macros

#define SPINLOCK_INITIALIZER   0
 Spinlock initializer.
 
#define spinlock_init(A)
 Initialize a spinlock.
 
#define spinlock_lock(A)
 Spin on a lock.
 
#define spinlock_trylock(A)
 Try to lock, without spinning.
 
#define spinlock_unlock(A)
 Free a lock.
 
#define spinlock_is_locked(A)
 Determine if a lock is locked.
 

Typedefs

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.

Author
Megan Potter
See also
kos/sem.h
kos/mutex.h
kos/rwsem.h
kos/recursive_lock.h

Macro Definition Documentation

◆ spinlock_init

#define spinlock_init ( A)
Value:
#define SPINLOCK_INITIALIZER
Spinlock initializer.
Definition spinlock.h:44

Initialize a spinlock.

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

Parameters
AA pointer to the spinlock to be initialized.

◆ SPINLOCK_INITIALIZER

#define SPINLOCK_INITIALIZER   0

Spinlock initializer.

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

◆ spinlock_is_locked

#define spinlock_is_locked ( A)
Value:
( *(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!

Parameters
AA pointer to the spinlock to be checked.

◆ spinlock_lock

#define spinlock_lock ( A)
Value:
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)
volatile int spinlock_t
Spinlock data type.
Definition spinlock.h:37

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.

Parameters
AA pointer to the spinlock to be locked.

◆ spinlock_trylock

#define spinlock_trylock ( A)
Value:
({ \
int __gotlock = 0; \
do { \
spinlock_t *__lock = A; \
__asm__ __volatile__("tas.b @%1\n\t" \
"movt %0\n\t" \
: "=r" (__gotlock) \
: "r" (__lock) \
: "t", "memory"); \
} while(0); \
__gotlock; \
})

Try to lock, without spinning.

This macro will attempt to lock the lock, but will not spin. Instead, it will return whether the lock was obtained or not.

Parameters
AA pointer to the spinlock to be locked.
Returns
0 if the lock is held by another thread. Non-zero if the lock was successfully obtained.

◆ spinlock_unlock

#define spinlock_unlock ( A)
Value:
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!

Parameters
AA pointer to the spinlock to be unlocked.

Typedef Documentation

◆ spinlock_t

typedef volatile int spinlock_t

Spinlock data type.