30 SpinLock() {
new(buffer) std::atomic<bool>(
false); }
35 ~SpinLock() { assert(!get().load()); get().~atomic<
bool>(); }
38 inline void lock() {
while (get().exchange(
true)) { } };
41 inline void unlock() { get().store(
false); }
43 inline bool try_lock() {
return !get().exchange(
true); }
46 void operator=(
const SpinLock &) =
delete;
48 std::atomic<bool> &get() {
return *
reinterpret_cast<std::atomic<bool> *
>(buffer); }
50 char buffer[
sizeof(std::atomic<bool>)];
68 ConditionalLock(T &t,
bool use) : lock_(&t), use_(use) {
if (use_) t.lock(); }
69 ConditionalLock(T *t,
bool use) : lock_(t), use_(use) {
if (use_&&t) t->lock(); }
88void atomicMax(std::atomic<T> &maxValue, T
const &value)
noexcept {
89 T pvalue = maxValue.load();
90 while (pvalue < value and not maxValue.compare_exchange_weak(pvalue, value)) { }
95void atomicMin(std::atomic<T> &minValue, T
const &value)
noexcept {
96 T pvalue = minValue.load();
97 while (pvalue > value and not minValue.compare_exchange_weak(pvalue, value)) { }
Base type definitions for the engine.
void unlock()
Release the lock.
Definition spinlock.h:41
void lock()
Acquire a lock using the test-and-set paradigm.
Definition spinlock.h:38
SpinLock()
By default the QAtomicInt is 0 on creation.
Definition spinlock.h:30
~SpinLock()
A SpinLock object can only be destroyed when it has been released.
Definition spinlock.h:35
#define BASE_EXPORT
Definition basedef.h:24