PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches

Ticket spinlock. More...

Collaboration diagram for Lock:

Detailed Description

Ticket spinlock.

Data Structures

struct  lock_t
 A simple ticket lock implementation. More...
 

Macros

#define LOCK_DEADLOCK_ITERATIONS   10000000
 Number of iterations before we consider a deadlock to have occurred in lock_acquire. This is only used in debug builds.
 
#define LOCK_CANARY   0xDEADBEEF
 Lock canary value to detect memory corruption.
 
#define LOCK_SCOPE(lock)
 Acquires a lock for the reminder of the current scope.
 
#define LOCK_CREATE()
 Create a lock initializer.
 

Functions

static void lock_init (lock_t *lock)
 Initializes a lock.
 
static void lock_acquire (lock_t *lock)
 Acquires a lock, blocking until it is available.
 
static void lock_release (lock_t *lock)
 Releases a lock.
 
static void lock_cleanup (lock_t **lock)
 

Macro Definition Documentation

◆ LOCK_DEADLOCK_ITERATIONS

#define LOCK_DEADLOCK_ITERATIONS   10000000

Number of iterations before we consider a deadlock to have occurred in lock_acquire. This is only used in debug builds.

Definition at line 25 of file lock.h.

◆ LOCK_CANARY

#define LOCK_CANARY   0xDEADBEEF

Lock canary value to detect memory corruption.

Definition at line 30 of file lock.h.

◆ LOCK_SCOPE

#define LOCK_SCOPE (   lock)
Value:
__attribute__((cleanup(lock_cleanup))) lock_t* CONCAT(l, __COUNTER__) = (lock); \
lock_acquire((lock))
#define CONCAT(a, b)
Concatenates two tokens.
Definition defs.h:76
static void lock_cleanup(lock_t **lock)
Definition lock.h:159
static lock_t lock
Definition io.c:13
A simple ticket lock implementation.
Definition lock.h:43

Acquires a lock for the reminder of the current scope.

Parameters
lockPointer to the lock to acquire.

Definition at line 57 of file lock.h.

◆ LOCK_CREATE

#define LOCK_CREATE ( )
Value:
(lock_t) \
{ \
.nextTicket = ATOMIC_VAR_INIT(0), .nowServing = ATOMIC_VAR_INIT(0), .canary = LOCK_CANARY \
}
#define LOCK_CANARY
Lock canary value to detect memory corruption.
Definition lock.h:30
#define ATOMIC_VAR_INIT(value)
Definition stdatomic.h:74

Create a lock initializer.

Returns
A lock_t initializer.

Definition at line 68 of file lock.h.

Function Documentation

◆ lock_init()

static void lock_init ( lock_t lock)
inlinestatic

Initializes a lock.

Parameters
lockPointer to the lock to initialize.

Definition at line 86 of file lock.h.

Here is the caller graph for this function:

◆ lock_acquire()

static void lock_acquire ( lock_t lock)
inlinestatic

Acquires a lock, blocking until it is available.

This function disables interrupts on the current CPU. It is not recursive, and attempting to acquire a lock that is already held by the same CPU will result in a deadlock.

Parameters
lockPointer to the lock to acquire.

Definition at line 103 of file lock.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_release()

static void lock_release ( lock_t lock)
inlinestatic

Releases a lock.

This function restores the interrupt state on the current CPU to what it was before the lock was acquired.

Parameters
lockPointer to the lock to release.

Definition at line 146 of file lock.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_cleanup()

static void lock_cleanup ( lock_t **  lock)
inlinestatic

Definition at line 159 of file lock.h.

Here is the call graph for this function: