PatchworkOS  19e446b
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()   {.nextTicket = ATOMIC_VAR_INIT(0), .nowServing = ATOMIC_VAR_INIT(0), .canary = LOCK_CANARY}
 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 bool lock_try_acquire (lock_t *lock)
 Tries to acquire a lock.
 
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 26 of file lock.h.

◆ LOCK_CANARY

#define LOCK_CANARY   0xDEADBEEF

Lock canary value to detect memory corruption.

Definition at line 31 of file lock.h.

◆ LOCK_SCOPE

#define LOCK_SCOPE (   lock)
Value:
__attribute__((cleanup(lock_cleanup))) lock_t* CONCAT(l, __COUNTER__) = (lock); \
lock_acquire((lock))
static void lock_cleanup(lock_t **lock)
Definition lock.h:188
#define CONCAT(a, b)
Concatenates two tokens.
Definition defs.h:77
static lock_t lock
Definition percpu.c:31
A simple ticket lock implementation.
Definition lock.h:44

Acquires a lock for the reminder of the current scope.

Parameters
lockPointer to the lock to acquire.

Definition at line 58 of file lock.h.

◆ LOCK_CREATE

#define LOCK_CREATE ( )    {.nextTicket = ATOMIC_VAR_INIT(0), .nowServing = ATOMIC_VAR_INIT(0), .canary = LOCK_CANARY}

Create a lock initializer.

Returns
A lock_t initializer.

Definition at line 69 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 79 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 96 of file lock.h.

Here is the call graph for this function:

◆ lock_try_acquire()

static bool lock_try_acquire ( lock_t lock)
inlinestatic

Tries to acquire a lock.

Parameters
lockPointer to the lock to acquire.
Returns
true if the lock was acquired, false otherwise.

Definition at line 138 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 175 of file lock.h.

Here is the call graph for this function:

◆ lock_cleanup()

static void lock_cleanup ( lock_t **  lock)
inlinestatic

Definition at line 188 of file lock.h.

Here is the call graph for this function: