|
PatchworkOS
28a9544
A non-POSIX operating system.
|
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. | |
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.
| #define SEQLOCK_CREATE {.sequence = ATOMIC_VAR_INIT(0), .writeLock = LOCK_CREATE} |
|
inlinestatic |
Initializes a sequence lock.
| seqlock | Pointer 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.
Begins a read operation on a sequence lock.
Should be called in a loop, for example:
| seqlock | Pointer to the sequence lock. |
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().
Checks if a read operation on a sequence lock needs to be retried.
| seqlock | Pointer to the sequence lock. |
| seq | The sequence number returned by seqlock_read_begin(). |
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().
|
inlinestatic |
Acquires the write lock of a sequence lock.
This function busy-waits until the write lock is acquired.
| seqlock | Pointer 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().
|
inlinestatic |
Releases the write lock of a sequence lock.
| seqlock | Pointer 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().