qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1
25
26#ifndef QB_SPINLOCK_H
27#define QB_SPINLOCK_H
28#include <atomic>
29#include <qb/system/timestamp.h>
30
31namespace qb::lockfree {
32
41class SpinLock {
42public:
46 SpinLock() noexcept
47 : _lock(false) {}
48
52 SpinLock(const SpinLock &) = delete;
53
57 SpinLock(SpinLock &&) = delete;
58
62 ~SpinLock() = default;
63
67 SpinLock &operator=(const SpinLock &) = delete;
68
73
79 bool
80 locked() noexcept {
81 return _lock.load(std::memory_order_acquire);
82 }
83
92 bool
93 trylock() noexcept {
94 return !_lock.exchange(true, std::memory_order_acquire);
95 }
96
106 bool
107 trylock(int64_t spin) noexcept {
108 // Try to acquire spin-lock at least one time
109 do {
110 if (trylock())
111 return true;
112 } while (spin-- > 0);
113
114 // Failed to acquire spin-lock
115 return false;
116 }
117
127 bool
128 trylock_for(const Timespan &timespan) noexcept {
129 // Calculate a finish timestamp
130 Timestamp finish = NanoTimestamp() + timespan;
131
132 // Try to acquire spin-lock at least one time
133 do {
134 if (trylock())
135 return true;
136 } while (NanoTimestamp() < finish);
137
138 // Failed to acquire spin-lock
139 return false;
140 }
141
151 bool
152 trylock_until(const UtcTimestamp &timestamp) noexcept {
153 return trylock_for(timestamp - UtcTimestamp());
154 }
155
163 void
164 lock() noexcept {
165 while (_lock.exchange(true, std::memory_order_acquire))
166 ;
167 }
168
174 void
175 unlock() noexcept {
176 _lock.store(false, std::memory_order_release);
177 }
178
179private:
180 std::atomic<bool> _lock;
181};
182
183} // namespace qb::lockfree
184
185#endif // QB_SPINLOCK_H
bool trylock() noexcept
Try to acquire the lock without spinning.
Definition spinlock.h:93
void lock() noexcept
Acquire the lock, waiting indefinitely if necessary.
Definition spinlock.h:164
SpinLock & operator=(const SpinLock &)=delete
Copy assignment operator is deleted to prevent copying of locks.
SpinLock() noexcept
Default constructor that initializes the lock to unlocked state.
Definition spinlock.h:46
SpinLock(SpinLock &&)=delete
Move constructor is deleted to prevent moving of locks.
bool trylock(int64_t spin) noexcept
Try to acquire the lock with a maximum number of spin attempts.
Definition spinlock.h:107
bool trylock_for(const Timespan &timespan) noexcept
Try to acquire the lock for a specified time duration.
Definition spinlock.h:128
SpinLock & operator=(SpinLock &&)=delete
Move assignment operator is deleted to prevent moving of locks.
void unlock() noexcept
Release the lock.
Definition spinlock.h:175
SpinLock(const SpinLock &)=delete
Copy constructor is deleted to prevent copying of locks.
~SpinLock()=default
Default destructor.
bool locked() noexcept
Check if the spinlock is currently locked.
Definition spinlock.h:80
bool trylock_until(const UtcTimestamp &timestamp) noexcept
Try to acquire the lock until a specified point in time.
Definition spinlock.h:152
TimePoint Timestamp
Backward compatibility alias for TimePoint.
Definition timestamp.h:1100
UtcTimePoint UtcTimestamp
Backward compatibility alias for UtcTimePoint.
Definition timestamp.h:1273
HighResTimePoint NanoTimestamp
Backward compatibility alias for HighResTimePoint.
Definition timestamp.h:1289
Duration Timespan
Backward compatibility alias for Duration.
Definition timestamp.h:1092
High-precision timing utilities.