mn_lock.hpp
Go to the documentation of this file.
1 
19 #ifndef _MINLIB_ILOCK_INTERFACE_H_
20 #define _MINLIB_ILOCK_INTERFACE_H_
21 
22 #include "mn_config.hpp"
23 
24 #include <assert.h>
25 #include <freertos/FreeRTOS.h>
26 #include <time.h>
27 
28 #include "mn_functional.hpp"
29 #include "mn_copyable.hpp"
30 #include "mn_error.hpp"
31 #include "mn_micros.hpp"
32 
49 #define LOCKED_SECTION(LOCK, OBJECT) if( (mn::basic_autolock<LOCK> lock(OBJECT)) )
50 
51 
52 #define UNLOCKED_SECTION(LOCK, OBJECT) if( (mn::basic_autounlock<LOCK> lock(OBJECT)) )
53 namespace mn {
65  public:
67 
69 
74  virtual int lock(unsigned int timeout = 0) = 0;
75 
76  virtual int time_lock(const struct timespec *timeout) = 0;
80  virtual int unlock() = 0;
81 
89  virtual bool try_lock() {
90  return (lock(0) == 0);
91  }
92 
98  virtual bool is_initialized() const = 0;
99 
104  virtual bool is_locked() const = 0;
105  };
106 
107 
108 
109 
121  template <class LOCK>
123  public:
129  basic_autolock(LOCK &m)
130  : m_ref_lock(m) {
131  assert( (m_ref_lock.lock(portMAX_DELAY) != NO_ERROR) );
132  }
140  basic_autolock(LOCK &m, unsigned long xTicksToWait)
141  : m_ref_lock(m) {
142  assert( (m_ref_lock.lock(xTicksToWait) != NO_ERROR) );
143  }
150  m_ref_lock.unlock();
151  }
152 
153  operator bool () {
154  return m_ref_lock.is_locked();
155  }
156  private:
161  LOCK &m_ref_lock;
162  };
163 
164 
174  template <class LOCK>
176  public:
182  : m_ref_lock(m) {
183  m_ref_lock.unlock();
185  }
186 
187  basic_autounlock(LOCK &m, unsigned long xTicksToWait)
188  : m_ref_lock(m) {
189  m_ref_lock.unlock();
190  m_xTicksToWait = xTicksToWait;
191  }
198  assert( (m_ref_lock.lock(m_xTicksToWait) != NO_ERROR) );
199  }
200 
201  void set_timeout(unsigned long xTicksToWait = portMAX_DELAY) {
202  m_xTicksToWait = xTicksToWait;
203  }
204 
205  operator bool () {
206  return !(m_ref_lock.is_locked());
207  }
208  private:
213  LOCK &m_ref_lock;
214 
218  unsigned long m_xTicksToWait;
219  };
220 
221 }
222 
223 #endif
Definition: mn_lock.hpp:64
virtual bool is_locked() const =0
Is locked?
virtual int unlock()=0
virtual int lock(unsigned int timeout=0)=0
virtual bool is_initialized() const =0
virtual int time_lock(const struct timespec *timeout)=0
ILockObject()
Definition: mn_lock.hpp:68
virtual bool try_lock()
Definition: mn_lock.hpp:89
Definition: mn_lock.hpp:122
~basic_autolock()
Definition: mn_lock.hpp:149
LOCK & m_ref_lock
Definition: mn_lock.hpp:161
basic_autolock(LOCK &m)
Definition: mn_lock.hpp:129
basic_autolock(LOCK &m, unsigned long xTicksToWait)
Definition: mn_lock.hpp:140
Definition: mn_lock.hpp:175
basic_autounlock(LOCK &m)
Definition: mn_lock.hpp:181
~basic_autounlock()
Definition: mn_lock.hpp:197
basic_autounlock(LOCK &m, unsigned long xTicksToWait)
Definition: mn_lock.hpp:187
unsigned long m_xTicksToWait
A saved / coped lock timeout for lock in the destructor.
Definition: mn_lock.hpp:218
LOCK & m_ref_lock
Definition: mn_lock.hpp:213
void set_timeout(unsigned long xTicksToWait=portMAX_DELAY)
Definition: mn_lock.hpp:201
#define portMAX_DELAY
Definition: mn_autolock.hpp:34
#define MN_ONSIGLETN_CLASS
Definition: mn_copyable.hpp:27
A list of all error codes in this lib. This file is part of the Mini Thread Library (https://github....
#define NO_ERROR
Definition: mn_error.hpp:53
Definition: mn_allocator_typetraits.hpp:25