solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Classes
spinlock.h File Reference

High-performance reader-writer spinlock implementation. More...

#include <stdatomic.h>
Include dependency graph for spinlock.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  fast_rwlock_t
 Reader-writer spinlock structure. More...
 

Detailed Description

High-performance reader-writer spinlock implementation.

This header provides a custom RW-spinlock optimized for short critical sections and high-concurrency scenarios. It significantly outperforms pthread_rwlock_t when lock hold times are very brief (microseconds) due to lower overhead.

The spinlock uses a single atomic integer to track state:

Warning
This spinlock busy-waits (spins) when contended. Use only for:
  • Very short critical sections (< 100 microseconds)
  • Low to moderate contention scenarios
  • Systems where context switching overhead exceeds spin cost

For longer critical sections or high contention, use pthread_rwlock_t instead.

Note
Thread-safe: All operations are atomic and safe for concurrent use.
Not reentrant: A thread cannot acquire the same lock recursively.

Definition in file spinlock.h.