INOS
cinosmutex.h
Go to the documentation of this file.
1 //******************************************************************************
26 //******************************************************************************
27 
28 #if !defined( INC_CINOSMUTEX_H )
29 #define INC_CINOSMUTEX_H
30 
31 //------------------------------------------------------------------------------
32 //--- class CINOSMutex ---------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
36 {
37  //--- user interface ---------------------------------------------------
38 
39  // public members
40  public :
41  // mutex flags
42  enum EFlags{
43  // none
44  eFlgNone = 0x00000000,
45  // classical (old style) non recursive behaviour
46  eFlgClassic = 0x00000001,
47  // reclaim lock at request
48  eFlgReclaimLock = 0x00000002,
49  };
50 
56  explicit CINOSMutex(EFlags aeFlags);
57 
64  CINOSMutex(const char* apName = 0, EFlags aeFlags = eFlgNone);
65 
69  ~CINOSMutex();
70 
77  ICACHE uint32 Request(uint32 aTimeout=INOS_WAIT_FOREVER);
80  ICACHE void lock() {
81  Request();
82  }
83 
84  /* \brief Requests the given two mutexes using a deadlock avoidance
85  * algorithm to avoid deadlock. Inspired by std::lock
86  */
87  static void RequestBoth(CINOSMutex& aMutex1, CINOSMutex& aMutex2);
88 
94  bool TryRequest();
97  bool try_lock() {
98  return TryRequest();
99  }
100 
109  ICACHE void Release(bool aSuspend=false, uint32 auState = defINOSTaskPureSuspended);
112  ICACHE void unlock() {
113  Release();
114  }
115 
118  ICACHE class CINOSTask* GetOwner()
119  { return m_idOwner; };
120 
123  const char* GetName()
124  { return m_pName; };
125 
128  volatile uint32* GetLockAdr()
129  { return m_Lock.GetAddr(); };
130 
133  void SetFlag(EFlags aeFlag);
134 
137  void ClrFlag(EFlags aeFlag);
138 
139 
140  //--- internals --------------------------------------------------------
141 
142  friend class CINOSTask;
143 
144  // protected members
145  protected :
146  const char* m_pName;
150  uint32 m_uCounter;
152  uint32 m_uFlags;
153 
154  // allow dynamic object handling (new/delete)
156  // objects of CINOSMutex need to be created very early during startup, when
157  // the CTORs have not been executed. Therefore, in order to create such
158  // objects, we need to provide the placement operator new.
160 };
161 
162 //------------------------------------------------------------------------------
163 //--- class CINOSLock ----------------------------------------------------------
164 //------------------------------------------------------------------------------
165 
167 {
168  //--- user interface ---------------------------------------------------
169 
170  // public members
171  public :
175  explicit CINOSLock(CINOSMutex& aMutex)
176  { aMutex.Request(); m_pMutex = &aMutex;};
177 
181  { if(m_pMutex) m_pMutex->Release(); };
182 
186  void EarlyUnlock()
187  { m_pMutex->Release(); m_pMutex = NULL; };
188 
189  //--- internals --------------------------------------------------------
190 
191  // protected members
192  protected :
195 };
196 
198 {
199  //--- user interface ---------------------------------------------------
200 
201  // public members
202  public:
209  explicit CINOSTryLock(CINOSMutex& aMutex)
210  : m_pMutex(aMutex.TryRequest() ? &aMutex : nullptr)
211  {
212  }
213 
217  {
218  EarlyUnlock();
219  }
220 
225  void EarlyUnlock()
226  {
227  if(m_pMutex)
228  {
229  m_pMutex->Release();
230  m_pMutex = nullptr;
231  }
232  }
233 
237  operator bool() const {
238  return (m_pMutex != nullptr);
239  }
240  //--- internals --------------------------------------------------------
241 
242  // protected members
243  protected:
246 };
247 
248 //------------------------------------------------------------------------------
249 // end of file
250 //------------------------------------------------------------------------------
251 
252 #endif // INC_CINOSMUTEX_h
CINOSMutex::Request
ICACHE uint32 Request(uint32 aTimeout=INOS_WAIT_FOREVER)
CINOSMutex
Definition: cinosmutex.h:35
CINOSMutex::GetOwner
ICACHE class CINOSTask * GetOwner()
Return id of mutex owner.
Definition: cinosmutex.h:118
CINOSMutex::try_lock
bool try_lock()
Definition: cinosmutex.h:97
CINOSMutex::m_idFstWaiting
tTaskId m_idFstWaiting
id of first task waiting for the mutex
Definition: cinosmutex.h:148
CINOSMutex::GetName
const char * GetName()
Return mutex name.
Definition: cinosmutex.h:123
CINOSMutex::lock
ICACHE void lock()
Definition: cinosmutex.h:80
CINOSTryLock::~CINOSTryLock
~CINOSTryLock()
Release the owned mutex.
Definition: cinosmutex.h:216
SINOSCoreLock
Definition: inostype.h:257
CINOSLock::m_pMutex
CINOSMutex * m_pMutex
pointer to the owned mutex
Definition: cinosmutex.h:187
CINOSMutex::Release
ICACHE void Release(bool aSuspend=false, uint32 auState=defINOSTaskPureSuspended)
CINOSTryLock::EarlyUnlock
void EarlyUnlock()
Release the owned mutex. Can be used to release the mutex before the destructor is called....
Definition: cinosmutex.h:225
CINOSLock::CINOSLock
CINOSLock(CINOSMutex &aMutex)
Request the given mutex. See also Critical sections.
Definition: cinosmutex.h:175
tTaskId
Definition: inostype.h:192
CINOSLock
Definition: cinosmutex.h:166
CINOSTryLock::m_pMutex
CINOSMutex * m_pMutex
pointer to the owned mutex
Definition: cinosmutex.h:245
CINOSLock::~CINOSLock
~CINOSLock()
Release the owned mutex.
Definition: cinosmutex.h:180
CINOSMutex::CINOSMutex
CINOSMutex(EFlags aeFlags)
Create a mutex with the given name.
DECLARE_DYNAMIC_PLACEMENT
#define DECLARE_DYNAMIC_PLACEMENT(aClass)
Definition: cinospartitionmemory.h:348
CINOSMutex::m_Lock
SINOSCoreLock m_Lock
core lock
Definition: cinosmutex.h:151
CINOSMutex::m_idOwner
tTaskId m_idOwner
owner of mutex (0->free)
Definition: cinosmutex.h:147
CINOSMutex::ClrFlag
void ClrFlag(EFlags aeFlag)
clears the given mutex flag.
CINOSMutex::unlock
ICACHE void unlock()
Definition: cinosmutex.h:112
CINOSMutex::m_uCounter
uint32 m_uCounter
number of times the owner 'owns' the mutex
Definition: cinosmutex.h:150
CINOSMutex::m_pName
const char * m_pName
name of mutex
Definition: cinosmutex.h:146
CINOSLock::EarlyUnlock
void EarlyUnlock()
Release the owned mutex. Can be used to release the mutex before the destructor is called.
Definition: cinosmutex.h:186
CINOSMutex::GetLockAdr
volatile uint32 * GetLockAdr()
Return pointer to core locking structure.
Definition: cinosmutex.h:128
CINOSMutex::~CINOSMutex
~CINOSMutex()
Destroy mutex. It is not allowed to destroy a mutex with still waiting tasks. This ends up in an asse...
CINOSMutex::m_idLstWaiting
tTaskId m_idLstWaiting
id of last task waiting for the mutex
Definition: cinosmutex.h:149
CINOSMutex::m_uFlags
uint32 m_uFlags
mutex flags
Definition: cinosmutex.h:152
CINOSTryLock
Definition: cinosmutex.h:197
DECLARE_DYNAMIC
#define DECLARE_DYNAMIC(aClass)
Definition: cinospartitionmemory.h:328
CINOSMutex::TryRequest
bool TryRequest()
CINOSMutex::SetFlag
void SetFlag(EFlags aeFlag)
sets the given mutex flag.
CINOSTryLock::CINOSTryLock
CINOSTryLock(CINOSMutex &aMutex)
Try to request the given mutex. IOW: Locks if mutex is not held by another task, but does nothing oth...
Definition: cinosmutex.h:209
CINOSTask
Definition: cinostask.h:51