rigtorp/Seqlock is a Backend project on GitHub, written primarily in C++. It has 256 stars. An implementation of Seqlock in C++11
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A seqlock can be used as an alternative to
a readers-writer lock. It
will never block the writer and doesn't require any memory bus locks.
Example
struct Data {
std::size_t a, b, c;
};
Seqlock<Data> sl;
sl.store({0, 0, 0});
auto t = std::thread([&] {
for (;;) {
auto d = sl.load();
if (d.a + 100 == d.b && d.c == d.a + d.b) {
return;
}
}
});
sl.store({100, 200, 300});
t.join();
Usage
Create a seqlock:
struct Data {
std::size_t a, b, c;
};
Seqlock<Data> sl;
Store a value (can only be called from a single thread):
sl.store({1, 2, 3});
Load a value (can be called from multiple threads):
auto v = sl.load();
Implementation
Implementing the seqlock in portable C++11 is quite tricky. The basic
seqlock implementation unconditionally loads the sequence number,
then unconditionally loads the protected data and finally
unconditionally loads the sequence number again. Since loading the
protected data is done unconditionally on the sequence number the
compiler is free to move these loads before or after the loads from
the sequence number.
T load() const noexcept {
T copy;
size_t seq0, seq1;
do {
seq0 = seq_.load(std::memory_order_acquire);
copy = value_;
seq1 = seq_.load(std::memory_order_acquire);
} while (seq0 != seq1 || seq0 & 1);
return copy;
}
Compiling this code specialized for int with g++-5.2 -std=c++11 -O3 yields
the following assembly:
We see that the compiler did indeed reorder the load of the protected
data outside the critical section and the data is no longer protected
from torn reads. Interestingly compiling using clang++-3.7 -std=c++11 -O3 produces the correct assembly:
Make the location of the protected data dependent on the sequence
number by storing multiple instances of the data and selecting which
one to read from based on the sequence number. This solution should
be portable to all CPU architectures, but requires extra space.
For x86 it's enough to insert a compiler barrier using
std::atomic_signal_fence(std::memory_order_acq_rel). This will
only work on the x86 memory model. On
ARM memory model you need to inserts a dmb memory
barrier instruction, which is not possible in C++11.
Since my target architecture is x86 I've implemented the second
option:
T load() const noexcept {
T copy;
size_t seq0, seq1;
do {
seq0 = seq_.load(std::memory_order_acquire);
std::atomic_signal_fence(std::memory_order_acq_rel);
copy = value_;
std::atomic_signal_fence(std::memory_order_acq_rel);
seq1 = seq_.load(std::memory_order_acquire);
} while (seq0 != seq1 || seq0 & 1);
return copy;
}
Compiled with g++-5.2 -std=c++11 -O3 it produces the
following correct assembly:
The store operation is implemented in a similar manner to the load
operation. Additionally the data and sequence counter is aligned and
padded to prevent false sharing with adjacent data.
References:
fast reader/writer lock for gettimeofday 2.5.30
Can Seqlocks Get Along With Programming Language Memory Models?
(slides)
x86-TSO: A Rigorous and Usable Programmer’s Model for x86 Multiprocessors
A Tutorial Introduction to the ARM and POWER Relaxed Memory Models
Testing
Testing lock-free algorithms is hard. I'm using two approaches to test
the implementation:
A test that load() and store() publish results in the correct
order on a single thread.
A multithreaded fuzz test that load() never see a partial
store() (torn read).
Potential improvements
Allow partial updates and reads using a visitor pattern.
Support for multiple writers can be achieved by using a CAS loop to
acquire the odd sequence number in the store operation. This would
have the same effect as wrapping the seqlock in a spinlock.
Trade-off space for reduced readers-writer contention by striping
writes across multiple seqlocks.
The most recent commit recorded on rigtorp/Seqlock was 1 month ago, based on the GitHub push timestamp. The repository has 41 forks — one of the better signals of community interest.
How does rigtorp/Seqlock compare to other Backend projects?
rigtorp/Seqlock is tracked by TopGit in the Backend category, with 256 GitHub stars and written in C++. Browse the Backend topic page on TopGit to compare it against similar projects by stars and activity.
How many stars does rigtorp/Seqlock have?
rigtorp/Seqlock has 256 GitHub stars — refresh the page for the live number, or check github.com/rigtorp/Seqlock. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is rigtorp/Seqlock open source?
Yes — rigtorp/Seqlock ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/rigtorp/Seqlock.
What is rigtorp/Seqlock?
rigtorp/Seqlock (rigtorp/Seqlock) is a C++ project on GitHub. From the project's own README: An implementation of Seqlock in C++11
Where do I read more about rigtorp/Seqlock?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/rigtorp/Seqlock is the definitive source.
Read full README in the tab above.
Want a second opinion on Seqlock?
Ask an AI that can read this page — one click and you get its take on Seqlock.