CatapultServer  v0.5.0.1 (Elephant)
SpinLock.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <atomic>
23 #include <mutex>
24 #include <thread>
25 
26 namespace catapult { namespace utils {
27 
31  class SpinLock {
32  public:
35  unlock();
36  }
37 
38  public:
40  inline void lock() {
41  while (!try_lock())
42  std::this_thread::yield();
43  }
44 
46  inline bool try_lock() {
47  return !m_isLocked.test_and_set(std::memory_order_acquire);
48  }
49 
51  inline void unlock() noexcept {
52  m_isLocked.clear(std::memory_order_release);
53  }
54 
55  private:
56  std::atomic_flag m_isLocked;
57  };
58 
60  using SpinLockGuard = std::lock_guard<SpinLock>;
61 }}
catapult::utils::SpinLock::SpinLock
SpinLock()
Creates an unlocked lock.
Definition: SpinLock.h:34
catapult::observers::DEFINE_OBSERVER
DEFINE_OBSERVER(AccountAddress, model::AccountAddressNotification, [](const auto &notification, const auto &context) { DefaultAccountVisitor visitor(context);visitor.visit(notification.Address);})
catapult::utils::SpinLock::lock
void lock()
Blocks until a lock can be obtained for the current execution agent.
Definition: SpinLock.h:40
catapult::utils::SpinLock
Definition: SpinLock.h:31
Observers.h
catapult::utils::SpinLock::try_lock
bool try_lock()
Attempts to acquire the lock for the current execution agent without blocking.
Definition: SpinLock.h:46
catapult::utils::SpinLock::unlock
void unlock() noexcept
Releases the lock held by the execution agent. Throws no exceptions.
Definition: SpinLock.h:51
catapult::utils::SpinLock::m_isLocked
std::atomic_flag m_isLocked
Definition: SpinLock.h:56
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::model::SourceChangeNotification::SourceChangeType::Relative
catapult::utils::SpinLockGuard
std::lock_guard< SpinLock > SpinLockGuard
A spin lock guard.
Definition: SpinLock.h:60