Mercury Currency Engine
atomic.hpp
Go to the documentation of this file.
1 //SPDX-License-Identifier: Apache-2.0
2 //Author: Blayne Dennis
7 #ifndef __MERCURY_COROUTINE_ENGINE_ATOMIC__
8 #define __MERCURY_COROUTINE_ENGINE_ATOMIC__
9 
10 // c++
11 #include <atomic>
12 #include <mutex>
13 
14 namespace mce {
15 
19 struct spinlock
20 {
21  spinlock()
22  {
23  lock_.clear();
24  }
25 
26  inline void lock()
27  {
28  while(lock_.test_and_set(std::memory_order_acquire)){ }
29  }
30 
31  inline bool try_lock()
32  {
33  return !(lock_.test_and_set(std::memory_order_acquire));
34  }
35 
36  inline void unlock()
37  {
38  lock_.clear(std::memory_order_release);
39  }
40 
41 private:
42  std::atomic_flag lock_;
43 };
44 
45 }
46 
47 #endif
Core mechanism for atomic synchronization.
Definition: atomic.hpp:20