mn_clock.hpp
Go to the documentation of this file.
1 
19 #ifndef MINLIB_ESP32_COUNTING_LOCK_
20 #define MINLIB_ESP32_COUNTING_LOCK_
21 
22 #include "mn_config.hpp"
23 
24 #include "mn_basic_semaphore.hpp"
25 
26 namespace mn {
33  template <class TLockType>
34  class basic_clock : public basic_semaphore {
35  public:
36  using LockType_t = TLockType;
37 
45  basic_clock(LockType_t& m, bool bLocked, unsigned int timeout)
46  : m_refLockRefObject(m), m_iLocks(0) {
47  if(bLocked) lock(timeout);
48  m_pSpinlock = m.get_handle();
49  }
53  basic_clock(basic_clock const&) = delete;
54 
59  if(m_iLocks > 0) {
60  m_refLockRefObject.unlock();
61  }
62  m_iLocks = 0;
63  }
64 
71  virtual int lock(unsigned int timeout) {
72  int lock_ret = ERR_SPINLOCK_LOCK;
73 
74  if(m_iLocks > 0) {
75  m_iLocks++; lock_ret = ERR_SPINLOCK_OK;
76  } else if(m_refLockRefObject.lock(timeout) == ERR_SPINLOCK_OK) {
77  m_iLocks++; lock_ret = ERR_SPINLOCK_OK; }
78 
79  return lock_ret;
80  }
86  virtual int unlock() {
87  int lock_ret = ERR_SPINLOCK_OK;
88 
89  if(m_iLocks > 0) {
90  m_iLocks--;
91  if(m_iLocks == 0) {
92  lock_ret = m_refLockRefObject.unlock();
93  }
94  }
95  return lock_ret;
96  }
97 
102  bool is_locked() { return m_iLocks > 0; }
107  bool is_unlocked() { return m_iLocks == 0; }
108 
113  operator bool () { return m_iLocks > 0; }
114 
118  void operator = (basic_clock const&) = delete;
119  protected:
121  unsigned int m_iLocks;
122  };
123 }
Definition: mn_clock.hpp:34
basic_clock(LockType_t &m, bool bLocked, unsigned int timeout)
Definition: mn_clock.hpp:45
virtual int unlock()
Definition: mn_clock.hpp:86
~basic_clock()
Definition: mn_clock.hpp:58
bool is_locked()
Definition: mn_clock.hpp:102
virtual int lock(unsigned int timeout)
Definition: mn_clock.hpp:71
bool is_unlocked()
Definition: mn_clock.hpp:107
void operator=(basic_clock const &)=delete
TLockType LockType_t
Definition: mn_clock.hpp:36
basic_clock(basic_clock const &)=delete
unsigned int m_iLocks
Definition: mn_clock.hpp:121
LockType_t & m_refLockRefObject
Definition: mn_clock.hpp:120
Definition: mn_basic_semaphore.hpp:41
void * m_pSpinlock
Definition: mn_basic_semaphore.hpp:129
#define ERR_SPINLOCK_OK
Definition: mn_error.hpp:74
#define ERR_SPINLOCK_LOCK
Definition: mn_error.hpp:78
Definition: mn_allocator_typetraits.hpp:25