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
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 :
176 { aMutex.Request(); m_pMutex = &aMutex;};
177
182
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:
210 : m_pMutex(aMutex.TryRequest() ? &aMutex : nullptr)
211 {
212 }
213
217 {
218 EarlyUnlock();
219 }
220
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
#define DECLARE_DYNAMIC_PLACEMENT(aClass)
Definition cinospartitionmemory.h:348
#define DECLARE_DYNAMIC(aClass)
Definition cinospartitionmemory.h:328
Definition cinosmutex.h:167
~CINOSLock()
Release the owned mutex.
Definition cinosmutex.h:180
CINOSMutex * m_pMutex
pointer to the owned mutex
Definition cinosmutex.h:194
CINOSLock(CINOSMutex &aMutex)
Request the given mutex. See also Critical sections.
Definition cinosmutex.h:175
void EarlyUnlock()
Release the owned mutex. Can be used to release the mutex before the destructor is called.
Definition cinosmutex.h:186
Definition cinosmcmodule.h:1900
Definition cinosmutex.h:36
ICACHE class CINOSTask * GetOwner()
Return id of mutex owner.
Definition cinosmutex.h:118
ICACHE void Release(bool aSuspend=false, uint32 auState=defINOSTaskPureSuspended)
ICACHE void unlock()
Definition cinosmutex.h:112
tTaskId m_idFstWaiting
id of first task waiting for the mutex
Definition cinosmutex.h:148
uint32 m_uFlags
mutex flags
Definition cinosmutex.h:152
tTaskId m_idOwner
owner of mutex (0->free)
Definition cinosmutex.h:147
SINOSCoreLock m_Lock
core lock
Definition cinosmutex.h:151
const char * m_pName
name of mutex
Definition cinosmutex.h:146
~CINOSMutex()
Destroy mutex. It is not allowed to destroy a mutex with still waiting tasks. This ends up in an asse...
ICACHE uint32 Request(uint32 aTimeout=INOS_WAIT_FOREVER)
const char * GetName()
Return mutex name.
Definition cinosmutex.h:123
CINOSMutex(const char *apName=0, EFlags aeFlags=eFlgNone)
Create a mutex with the given name.
ICACHE void lock()
Definition cinosmutex.h:80
void SetFlag(EFlags aeFlag)
sets the given mutex flag.
bool try_lock()
Definition cinosmutex.h:97
uint32 m_uCounter
number of times the owner 'owns' the mutex
Definition cinosmutex.h:150
void ClrFlag(EFlags aeFlag)
clears the given mutex flag.
CINOSMutex(EFlags aeFlags)
Create a mutex with the given name.
tTaskId m_idLstWaiting
id of last task waiting for the mutex
Definition cinosmutex.h:149
bool TryRequest()
volatile uint32 * GetLockAdr()
Return pointer to core locking structure.
Definition cinosmutex.h:128
Definition cinostask.h:52
Definition cinosmutex.h:198
void EarlyUnlock()
Release the owned mutex. Can be used to release the mutex before the destructor is called....
Definition cinosmutex.h:225
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
CINOSMutex * m_pMutex
pointer to the owned mutex
Definition cinosmutex.h:245
~CINOSTryLock()
Release the owned mutex.
Definition cinosmutex.h:216
Definition inostype.h:258
Definition inostype.h:192