mirror of
https://github.com/sockspls/badfish
synced 2025-05-03 01:59:36 +00:00
Introduce Spinlock class
Initialization is more complex than what I'd like due to MSVC compatibility that for some reason does not like: std::atomic_flag lock = ATOMIC_FLAG_INIT; No functional change.
This commit is contained in:
parent
098f645d26
commit
775f8239d3
1 changed files with 14 additions and 0 deletions
14
src/thread.h
14
src/thread.h
|
@ -20,6 +20,7 @@
|
||||||
#ifndef THREAD_H_INCLUDED
|
#ifndef THREAD_H_INCLUDED
|
||||||
#define THREAD_H_INCLUDED
|
#define THREAD_H_INCLUDED
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -69,6 +70,19 @@ struct SplitPoint {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// Spinlock class wraps low level atomic operations to provide spin lock functionality
|
||||||
|
|
||||||
|
class Spinlock {
|
||||||
|
|
||||||
|
std::atomic_flag lock;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Spinlock() { std::atomic_flag_clear(&lock); }
|
||||||
|
void acquire() { while (lock.test_and_set(std::memory_order_acquire)) {} }
|
||||||
|
void release() { lock.clear(std::memory_order_release); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
||||||
/// specialized thread classes.
|
/// specialized thread classes.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue