Pothos  0.7.0-gf7fbae99
The Pothos dataflow programming software suite
SpinLock.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 #include <Pothos/Config.hpp>
13 #include <atomic>
14 
15 namespace Pothos {
16 namespace Util {
17 
27 {
28 public:
29 
31  SpinLock(void);
32 
34  bool try_lock(void) noexcept;
35 
37  void lock(void) noexcept;
38 
40  void unlock(void) noexcept;
41 
42 private:
43  std::atomic_flag _lock;
44 };
45 
46 } //namespace Util
47 } //namespace Pothos
48 
50 {
51  this->unlock();
52 }
53 
54 inline bool Pothos::Util::SpinLock::try_lock(void) noexcept
55 {
56  return not _lock.test_and_set(std::memory_order_acquire);
57 }
58 
59 inline void Pothos::Util::SpinLock::lock(void) noexcept
60 {
61  while (not this->try_lock()){}
62 }
63 
64 inline void Pothos::Util::SpinLock::unlock(void) noexcept
65 {
66  _lock.clear(std::memory_order_release);
67 }
#define POTHOS_API
Definition: Config.hpp:41
Definition: SpinLock.hpp:26
bool try_lock(void) noexcept
Try to lock, return true for lock.
Definition: SpinLock.hpp:54
void unlock(void) noexcept
Unlock the spin lock (should be already locked)
Definition: SpinLock.hpp:64
Definition: ArchiveEntry.hpp:20
void lock(void) noexcept
Lock the spin lock, block if already locked.
Definition: SpinLock.hpp:59
SpinLock(void)
Create a new unlocked spin lock.
Definition: SpinLock.hpp:49