RavEngine
Loading...
Searching...
No Matches
SpinLock.hpp
1#pragma once
2#include <atomic>
3
4namespace RavEngine{
5
9class SpinLock{
10 std::atomic_flag flag
11#ifndef _WIN32
12 ATOMIC_FLAG_INIT
13#endif
14 ;
15public:
16 inline void lock(){
17 while(flag.test_and_set());
18 }
19
20 inline void unlock(){
21 flag.clear();
22 }
23
24 // constructors and operators
25 SpinLock(){};
26 //SpinLock(SpinLock&& other){}
27
28 // copy-assign or copy-construct a lock does NOT
29 // copy its state. These exist as conveniences for other things.
30 SpinLock(const SpinLock& other){}
31 inline void operator=(const SpinLock& other){}
32};
33
34// do not dynamic allocate
35template<typename T>
36struct RAIILock {
37 T& lock;
38
39 RAIILock(T& lock) : lock(lock) {
40 lock.lock();
41 }
42 ~RAIILock() {
43 lock.unlock();
44 }
45};
46
47}
Definition SpinLock.hpp:9
Definition Animation.hpp:6
Definition SpinLock.hpp:36