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

Sequence Lock. More...

Data Structures

struct  seqlock_t
 Sequence lock structure. More...
 

Macros

#define SEQLOCK_CREATE   {.sequence = ATOMIC_VAR_INIT(0), .writeLock = LOCK_CREATE}
 Create a sequence lock initializer.
 

Functions

static void seqlock_init (seqlock_t *seqlock)
 Initializes a sequence lock.
 
static void seqlock_write_acquire (seqlock_t *seqlock)
 Acquires the write lock of a sequence lock.
 
static void seqlock_write_release (seqlock_t *seqlock)
 Releases the write lock of a sequence lock.
 
static uint64_t seqlock_read_begin (seqlock_t *seqlock)
 Begins a read operation on a sequence lock.
 
static bool seqlock_read_retry (seqlock_t *seqlock, uint64_t seq)
 Checks if a read operation on a sequence lock needs to be retried.
 

Detailed Description

Sequence Lock.

A sequence lock is similar to a read-write lock, but optimized for scenarios where there are many more readers than writers, or where reads are frequent and writes are rare.

Readers can read the data without acquiring a lock, instead they check a "sequence number" before and after their read to verify that no write occured during their read, if one did, they must retry their read.

Writers acquire the lock exclusively and increment the "sequence number" before and after their write, this means that readers can detect if a write has occured, and if a write is currently in progress by checking if the sequence number is odd.

Macro Definition Documentation

◆ SEQLOCK_CREATE

#define SEQLOCK_CREATE   {.sequence = ATOMIC_VAR_INIT(0), .writeLock = LOCK_CREATE}

Create a sequence lock initializer.

Returns
A seqlock_t initializer.

Definition at line 40 of file seqlock.h.

Function Documentation

◆ seqlock_init()

static void seqlock_init ( seqlock_t seqlock)
inlinestatic

Initializes a sequence lock.

Parameters
seqlockPointer to the sequence lock to initialize.

Definition at line 47 of file seqlock.h.

References atomic_init, lock_init(), seqlock_t::sequence, and seqlock_t::writeLock.

◆ seqlock_read_begin()

static uint64_t seqlock_read_begin ( seqlock_t seqlock)
inlinestatic

Begins a read operation on a sequence lock.

Should be called in a loop, for example:

do {
seq = seqlock_read_begin(&seqlock);
// read data here
} while (seqlock_read_retry(&seqlock, seq));
static uint64_t seqlock_read_begin(seqlock_t *seqlock)
Begins a read operation on a sequence lock.
Definition seqlock.h:92
static bool seqlock_read_retry(seqlock_t *seqlock, uint64_t seq)
Checks if a read operation on a sequence lock needs to be retried.
Definition seqlock.h:104
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Parameters
seqlockPointer to the sequence lock.
Returns
The current sequence number.

Definition at line 92 of file seqlock.h.

References atomic_load_explicit, memory_order_acquire, and seqlock_t::sequence.

Referenced by hpet_read_ns_counter().

◆ seqlock_read_retry()

static bool seqlock_read_retry ( seqlock_t seqlock,
uint64_t  seq 
)
inlinestatic

Checks if a read operation on a sequence lock needs to be retried.

Parameters
seqlockPointer to the sequence lock.
seqThe sequence number returned by seqlock_read_begin().
Returns
true if the read operation needs to be retried, false otherwise.

Definition at line 104 of file seqlock.h.

References atomic_load_explicit, atomic_thread_fence, memory_order_acquire, memory_order_relaxed, and seqlock_t::sequence.

Referenced by hpet_read_ns_counter().

◆ seqlock_write_acquire()

static void seqlock_write_acquire ( seqlock_t seqlock)
inlinestatic

Acquires the write lock of a sequence lock.

This function busy-waits until the write lock is acquired.

Parameters
seqlockPointer to the sequence lock.

Definition at line 60 of file seqlock.h.

References atomic_fetch_add_explicit, lock_acquire(), memory_order_acquire, seqlock_t::sequence, and seqlock_t::writeLock.

Referenced by hpet_timer_handler().

◆ seqlock_write_release()

static void seqlock_write_release ( seqlock_t seqlock)
inlinestatic

Releases the write lock of a sequence lock.

Parameters
seqlockPointer to the sequence lock.

Definition at line 71 of file seqlock.h.

References atomic_fetch_add_explicit, lock_release(), memory_order_release, seqlock_t::sequence, and seqlock_t::writeLock.

Referenced by hpet_timer_handler().