30 #include <oc_stdlib.h> 41 #define _________________________________________LOCAL_TYPES_SECTION_______________________________________________________________________ 46 int16_t RecursiveCount;
52 #undef _________________________________________LOCAL_TYPES_SECTION_______________________________________________________________________ 60 #define _________________________________________LOCAL_VARIABLES_SECTION____________________________________________________________________ 63 #undef _________________________________________LOCAL_VARIABLES_SECTION____________________________________________________________________ 71 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 75 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 82 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 90 oC_Mutex_t mutex = kmalloc(
sizeof(
struct Mutex_t), Allocator , AllocationFlags_NoWait);
94 mutex->LockedFlag =
false;
95 mutex->BlockedThread =
NULL;
105 bool oC_Mutex_Delete(
oC_Mutex_t * Mutex , AllocationFlags_t AllocationFlags )
107 bool deleted =
false;
109 if(oC_Mutex_IsCorrect(*Mutex))
111 oC_IntMan_EnterCriticalSection();
112 (*Mutex)->ObjectControl = 0;
113 oC_ThreadMan_UnblockAllBlockedBy(&((*Mutex)->LockedFlag),
false);
114 if(!kfree(*Mutex,AllocationFlags))
116 oC_SaveError(
"Mutex" , oC_ErrorCode_ReleaseError);
123 oC_IntMan_ExitCriticalSection();
127 oC_SaveError(
"Mutex" , oC_ErrorCode_ObjectNotCorrect);
139 if(oC_Mutex_IsCorrect(Mutex))
141 if(oC_ThreadMan_GetCurrentThread() == Mutex->BlockedThread)
143 oC_IntMan_EnterCriticalSection();
144 Mutex->RecursiveCount++;
146 (Mutex->Type == oC_Mutex_Type_Normal) ||
147 (Mutex->RecursiveCount >= 0)
151 Mutex->LockedFlag =
false;
154 oC_IntMan_ExitCriticalSection();
158 oC_SaveError(
"Mutex" , oC_ErrorCode_WrongThread);
163 oC_SaveError(
"Mutex" , oC_ErrorCode_ObjectNotCorrect);
171 bool oC_Mutex_Take(
oC_Mutex_t Mutex , oC_Time_t Timeout )
175 if(oC_Mutex_IsCorrect(Mutex))
177 oC_Thread_t currentThread = oC_ThreadMan_GetCurrentThread();
179 if(Mutex->LockedFlag)
181 if(currentThread != Mutex->BlockedThread)
183 if( oC_Thread_SetBlocked(currentThread , &Mutex->LockedFlag , oC_Thread_Unblock_WhenEqual ,
false , oC_Thread_UnblockMask_All , Timeout ) )
185 while(oC_Thread_IsBlocked(currentThread));
187 oC_Thread_SetUnblocked(currentThread);
189 oC_IntMan_EnterCriticalSection();
190 if(oC_Mutex_IsCorrect(Mutex) && (Mutex->LockedFlag == 0))
192 Mutex->LockedFlag =
true;
193 Mutex->BlockedThread = currentThread;
194 Mutex->RecursiveCount--;
199 oC_IntMan_ExitCriticalSection();
204 oC_IntMan_EnterCriticalSection();
205 Mutex->RecursiveCount--;
207 oC_IntMan_ExitCriticalSection();
212 oC_IntMan_EnterCriticalSection();
213 Mutex->LockedFlag =
true;
214 Mutex->BlockedThread = currentThread;
215 Mutex->RecursiveCount--;
219 oC_IntMan_ExitCriticalSection();
224 oC_SaveError(
"Mutex" , oC_ErrorCode_ObjectNotCorrect);
237 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 245 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 254 bool reverted =
false;
257 oC_IntMan_EnterCriticalSection();
258 if(oC_SaveIfFalse(
"Mutex", oC_Mutex_IsCorrect(mutex), oC_ErrorCode_ObjectNotCorrect))
260 mutex->BlockedThread =
NULL;
261 mutex->LockedFlag =
false;
262 mutex->RecursiveCount = 0;
265 oC_IntMan_ExitCriticalSection();
270 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
The file with interface for thread managing.
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
identifier for allocations
The file with helper macros for managing objects.
uint32_t oC_ObjectControl_t
stores object control value
The file with interface for interrupt manager.
static bool RevertFunction(oC_Thread_t Thread, void *Mutex, uint32_t Parameter)
called to revert changes made by the thread
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
The file with interface for mutex managing.
Definition of the null pointer.
static const oC_Allocator_t Allocator
The file with interface for Thread Manager.
#define NULL
pointer to a zero