mn_atomic_spinlock.hpp
Go to the documentation of this file.
1 
19 #ifndef _MINLIB_d82b672b_8681_4cea_bb66_a4aa2d9927e2_H_
20 #define _MINLIB_d82b672b_8681_4cea_bb66_a4aa2d9927e2_H_
21 
22 #include "mn_config.hpp"
23 
24 #include "mn_atomic.hpp"
25 #include "mn_lock.hpp"
26 
27 namespace mn {
28 
33  template<typename T>
34  class atomic_spinlock : public ILockObject {
35  public:
37  using value_type = T;
38  using pointer = value_type*;
41 
46  : m_locked(false), m_value() { }
47 
52  explicit atomic_spinlock(const reference value)
53  : m_locked(false), m_value(value) { }
54 
59  virtual int lock(unsigned int not_use = 0) {
60  while(! m_locked.compare_exchange_t(false, true,
61  atomic_bool::memory_order::Acquire) ) { }
62  return 0;
63  }
64 
65  virtual int time_lock(const struct timespec *timeout) {
66  return lock();
67  }
71  virtual int unlock() {
72  m_locked.store(false, basic_atomic_gcc_memory_order::Release);
73  return 0;
74  }
82  virtual bool try_lock() {
83  if( m_locked.compare_exchange_t(false, true, atomic_bool::memory_order::Acquire) )
84  return false;
85  else
86  lock();
87  return true;
88  }
94  virtual bool is_initialized() const {
95  return true;
96  }
97 
102  operator value_type() { return m_value; }
103 
104  self_type& operator = (const value_type& oValue) {
105  lock_guard lock(*this);
106  m_value = oValue;
107  return *this;
108  }
109 
110  bool operator == (const value_type& oValue) {
111  lock_guard lock(*this);
112  return m_value == oValue;
113  }
114  bool operator != (const value_type& oValue) {
115  lock_guard lock(*this);
116  return m_value != oValue;
117  }
118 
119  atomic_spinlock(const self_type&) = delete;
120  self_type& operator=(const self_type&) = delete;
121  protected:
124  };
125 }
126 
127 #endif
Definition: mn_lock.hpp:64
This class implements a simple spinlack, whit atomic operations.
Definition: mn_atomic_spinlock.hpp:34
atomic_spinlock(const self_type &)=delete
value_type m_value
Definition: mn_atomic_spinlock.hpp:123
virtual int time_lock(const struct timespec *timeout)
Definition: mn_atomic_spinlock.hpp:65
virtual bool is_initialized() const
Definition: mn_atomic_spinlock.hpp:94
atomic_spinlock(const reference value)
Construct a new atomic_spinlock and initializes it with the given value.
Definition: mn_atomic_spinlock.hpp:52
bool operator!=(const value_type &oValue)
Definition: mn_atomic_spinlock.hpp:114
virtual int lock(unsigned int not_use=0)
Definition: mn_atomic_spinlock.hpp:59
T value_type
Definition: mn_atomic_spinlock.hpp:37
virtual bool try_lock()
Definition: mn_atomic_spinlock.hpp:82
value_type * pointer
Definition: mn_atomic_spinlock.hpp:38
atomic_spinlock()
Construct a new atomic_spinlock.
Definition: mn_atomic_spinlock.hpp:45
virtual int unlock()
Definition: mn_atomic_spinlock.hpp:71
atomic_bool m_locked
Definition: mn_atomic_spinlock.hpp:122
self_type & operator=(const self_type &)=delete
self_type & operator=(const value_type &oValue)
Definition: mn_atomic_spinlock.hpp:104
value_type & reference
Definition: mn_atomic_spinlock.hpp:39
bool operator==(const value_type &oValue)
Definition: mn_atomic_spinlock.hpp:110
Definition: mn_lock.hpp:122
Basic atomics types This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/Mini...
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
Special version for bool.
Definition: mn_atomic_primary_types.hpp:55