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 <stdbool.h>
#include <kos/thread.h>

Go to the source code of this file.

Macros

#define SPINLOCK_INITIALIZER   0
 Spinlock initializer.
 
#define spinlock_lock_scoped(lock)
 Spin on a lock with scope management.
 

Typedefs

typedef volatile int spinlock_t
 Spinlock data type.
 

Functions

static void spinlock_init (spinlock_t *lock)
 Initialize a spinlock.
 
static bool spinlock_trylock (spinlock_t *lock)
 Try to lock, without spinning.
 
static void spinlock_lock (spinlock_t *lock)
 Spin on a lock.
 
static bool spinlock_lock_irqsafe (spinlock_t *lock)
 Spin on a lock.
 
static void spinlock_unlock (spinlock_t *lock)
 Free a lock.
 
static bool spinlock_is_locked (spinlock_t *lock)
 Determine if a lock is locked.
 

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_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.

Referenced by spinlock_init().

◆ spinlock_lock_scoped

#define spinlock_lock_scoped ( lock)
Value:
__spinlock_lock_scoped((lock), __LINE__)

Spin on a lock with scope management.

This macro will spin on the lock, similar to spinlock_lock(), with the difference that the lock will automatically be freed once the execution exits the functional block in which the macro was called.

Parameters
lockA pointer to the spinlock to be locked.

Typedef Documentation

◆ spinlock_t

typedef volatile int spinlock_t

Spinlock data type.

Function Documentation

◆ spinlock_init()

static void spinlock_init ( spinlock_t * lock)
inlinestatic

Initialize a spinlock.

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

Parameters
lockA pointer to the spinlock to be initialized.

References SPINLOCK_INITIALIZER.

◆ spinlock_is_locked()

static bool spinlock_is_locked ( spinlock_t * lock)
inlinestatic

Determine if a lock is locked.

This function 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
lockA pointer to the spinlock to be checked.
Returns
True if the spinlock is locked, false otherwise.

◆ spinlock_lock()

static void spinlock_lock ( spinlock_t * lock)
inlinestatic

Spin on a lock.

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

Parameters
lockA pointer to the spinlock to be locked.

References spinlock_trylock(), and thd_pass().

Referenced by spinlock_lock_irqsafe().

◆ spinlock_lock_irqsafe()

static bool spinlock_lock_irqsafe ( spinlock_t * lock)
inlinestatic

Spin on a lock.

This function will spin on the lock, and will not return until the lock has been obtained for the calling thread, unless it is called from within an interrupt context.

Parameters
lockA pointer to the spinlock to be locked.
Returns
True if the spinlock could be locked, false otherwise.

References irq_inside_int(), spinlock_lock(), and spinlock_trylock().

◆ spinlock_trylock()

static bool spinlock_trylock ( spinlock_t * lock)
inlinestatic

Try to lock, without spinning.

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

Parameters
lockA pointer to the spinlock to be locked.
Returns
False if the lock is held by another thread. True if the lock was successfully obtained.

Referenced by spinlock_lock(), and spinlock_lock_irqsafe().

◆ spinlock_unlock()

static void spinlock_unlock ( spinlock_t * lock)
inlinestatic

Free a lock.

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

Parameters
lockA pointer to the spinlock to be unlocked.