Choco OS  V.0.16.9.0
Join to the chocolate world
oc_event.c
Go to the documentation of this file.
1 
27 #include <oc_event.h>
28 #include <oc_object.h>
29 #include <oc_stdlib.h>
30 #include <oc_threadman.h>
31 #include <oc_intman.h>
32 #include <oc_null.h>
33 
39 #define _________________________________________TYPES_SECTION______________________________________________________________________________
40 
41 struct Event_t
42 {
43  oC_ObjectControl_t ObjectControl;
44  uint32_t State;
45  oC_Event_Protect_t Protection;
46 };
47 
48 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
49 
55 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
56 
57 //==========================================================================================================================================
58 //==========================================================================================================================================
59 oC_Event_t oC_Event_New( oC_Event_State_t InitState , Allocator_t Allocator , AllocationFlags_t Flags )
60 {
61  oC_Event_t event = ksmartalloc( sizeof(struct Event_t) , Allocator , Flags);
62 
63  if(event)
64  {
65  event->State = InitState;
66  event->ObjectControl = oC_CountObjectControl(event,oC_ObjectId_Event);
67  event->Protection = oC_Event_Protect_NotProtected;
68  }
69  else
70  {
71  oC_SaveError("Event: Creating new event" , oC_ErrorCode_AllocationError);
72  }
73 
74  return event;
75 }
76 
77 
78 //==========================================================================================================================================
79 //==========================================================================================================================================
80 bool oC_Event_Delete( oC_Event_t * Event , AllocationFlags_t Flags )
81 {
82  bool deleted = false;
83 
84  if(oC_Event_IsCorrect(*Event))
85  {
86  if((*Event)->Protection == oC_Event_Protect_NotProtected)
87  {
88  oC_IntMan_EnterCriticalSection();
89 
90  oC_ThreadMan_UnblockAllBlockedBy(&(*Event)->State , false);
91  (*Event)->ObjectControl = 0;
92 
93  if(ksmartfree(*Event,sizeof(struct Event_t),Flags))
94  {
95  *Event = NULL;
96  deleted = true;
97  }
98  else
99  {
100  oC_SaveError("Event: Delete" , oC_ErrorCode_ReleaseError);
101  }
102 
103  oC_IntMan_ExitCriticalSection();
104  }
105  else
106  {
107  oC_SaveError("Event: Delete - " , oC_ErrorCode_ObjectProtected);
108  }
109  }
110  else
111  {
112  oC_SaveError("Event: Delete - " , oC_ErrorCode_ObjectNotCorrect);
113  }
114 
115  return deleted;
116 }
117 
118 //==========================================================================================================================================
119 //==========================================================================================================================================
120 bool oC_Event_ProtectDelete( oC_Event_t Event , Allocator_t Allocator , oC_Event_Protect_t Protection )
121 {
122  bool success = false;
123 
124  if(oC_Event_IsCorrect(Event))
125  {
126  if(isaddresscorrect(Allocator))
127  {
128  if(Allocator != oC_MemMan_GetAllocatorOfAddress(Event))
129  {
130  oC_IntMan_EnterCriticalSection();
131 
132  Event->Protection = Protection;
133 
134  success = true;
135 
136  oC_IntMan_ExitCriticalSection();
137  }
138  else
139  {
140  oC_SaveError("Event: Protect delete - Allocator not correct (you are not owner of this event)" , oC_ErrorCode_WrongAddress );
141  }
142  }
143  else
144  {
145  oC_SaveError("Event: Protect delete - Allocator not correct " , oC_ErrorCode_WrongAddress );
146  }
147  }
148  else
149  {
150  oC_SaveError("Event: Protect delete - " , oC_ErrorCode_ObjectNotCorrect);
151  }
152 
153  return success;
154 }
155 
156 //==========================================================================================================================================
157 //==========================================================================================================================================
158 bool oC_Event_IsCorrect( oC_Event_t Event )
159 {
160  return oC_MemMan_IsRamAddress(Event) && oC_CheckObjectControl(Event,oC_ObjectId_Event,Event->ObjectControl);
161 }
162 
163 //==========================================================================================================================================
164 //==========================================================================================================================================
165 oC_Event_State_t oC_Event_GetState( oC_Event_t Event )
166 {
167  return oC_Event_IsCorrect(Event) ? Event->State : 0;
168 }
169 
170 //==========================================================================================================================================
171 //==========================================================================================================================================
172 bool oC_Event_SetState( oC_Event_t Event , oC_Event_State_t State )
173 {
174  bool set = false;
175 
176  if(oC_Event_IsCorrect(Event))
177  {
178  oC_IntMan_EnterCriticalSection();
179  Event->State = State;
180 
181  // TODO: Uncomment line below when list will be faster
182  // The line below unblocks all threads blocked by the event, but
183  // it takes a lot of time and it must be called in an interrupt :/
184 // oC_ThreadMan_UnblockAllBlockedBy(&Event->State , true);
185  set = true;
186  oC_IntMan_ExitCriticalSection();
187  }
188  else
189  {
190  oC_SaveError("Event: set state" , oC_ErrorCode_ObjectNotCorrect);
191  }
192 
193  return set;
194 }
195 
196 
197 //==========================================================================================================================================
198 //==========================================================================================================================================
199 bool oC_Event_ClearStateBits( oC_Event_t Event , uint32_t StateMask )
200 {
201  bool cleared = false;
202 
203  if(oC_Event_IsCorrect(Event))
204  {
205  oC_IntMan_EnterCriticalSection();
206  Event->State &= ~StateMask;
207  cleared = true;
208  oC_IntMan_ExitCriticalSection();
209  }
210 
211  return cleared;
212 }
213 
214 //==========================================================================================================================================
215 //==========================================================================================================================================
216 bool oC_Event_ReadState( oC_Event_t Event , oC_Event_State_t * outState )
217 {
218  bool read = false;
219 
220  if(oC_Event_IsCorrect(Event))
221  {
222  oC_IntMan_EnterCriticalSection();
223  *outState = Event->State;
224  read = true;
225  oC_IntMan_ExitCriticalSection();
226  }
227  else
228  {
229  oC_SaveError("Event: read state" , oC_ErrorCode_ObjectNotCorrect);
230  }
231 
232  return read;
233 }
234 
235 //==========================================================================================================================================
244 //==========================================================================================================================================
245 bool oC_Event_WaitForState( oC_Event_t Event , oC_Event_State_t State , oC_Event_StateMask_t StateMask , oC_Time_t Timeout )
246 {
247  bool stateOk = false;
248 
249  if(oC_Event_IsCorrect(Event))
250  {
251  oC_Thread_t thread = oC_ThreadMan_GetCurrentThread();
252 
253  if(StateMask != oC_Event_StateMask_DifferentThan)
254  {
255  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenEqual , State , StateMask , Timeout);
256  }
257  else
258  {
259  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenNotEqual , State , oC_Event_StateMask_Full , Timeout);
260  }
261 
262  while(oC_Thread_IsBlocked(thread));
263 
264  oC_Thread_SetUnblocked(thread);
265 
266  oC_IntMan_EnterCriticalSection();
267 
268  if(oC_Event_IsCorrect(Event))
269  {
270  if(StateMask != oC_Event_StateMask_DifferentThan)
271  {
272  if((Event->State & StateMask) == (State & StateMask))
273  {
274  stateOk = true;
275  }
276  }
277  else
278  {
279  stateOk = (Event->State & oC_Event_StateMask_Full) != (State & oC_Event_StateMask_Full);
280  }
281  }
282  oC_IntMan_ExitCriticalSection();
283  }
284  else
285  {
286  oC_SaveError("Event: wait for state" , oC_ErrorCode_ObjectNotCorrect);
287  }
288 
289  return stateOk;
290 }
291 
292 //==========================================================================================================================================
293 //==========================================================================================================================================
294 bool oC_Event_WaitForValue( oC_Event_t Event , oC_Event_State_t Value , oC_Event_CompareType_t CompareType, oC_Time_t Timeout )
295 {
296  bool stateOk = false;
297 
298  if(oC_Event_IsCorrect(Event) && CompareType < oC_Event_CompareType_NumberOfElements)
299  {
300  oC_Thread_t thread = oC_ThreadMan_GetCurrentThread();
301 
302  if(CompareType == oC_Event_CompareType_Equal)
303  {
304  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenEqual , Value, oC_Event_StateMask_Full, Timeout);
305  }
306  else if(CompareType == oC_Event_CompareType_Greater)
307  {
308  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenGreater, Value, oC_Event_StateMask_Full, Timeout);
309  }
310  else if(CompareType == oC_Event_CompareType_GreaterOrEqual)
311  {
312  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenGreaterOrEqual, Value, oC_Event_StateMask_Full, Timeout);
313  }
314  else if(CompareType == oC_Event_CompareType_Less)
315  {
316  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenSmaller, Value, oC_Event_StateMask_Full, Timeout);
317  }
318  else if(CompareType == oC_Event_CompareType_LessOrEqual)
319  {
320  oC_Thread_SetBlocked(thread, &Event->State , oC_Thread_Unblock_WhenSmallerOrEqual, Value, oC_Event_StateMask_Full, Timeout);
321  }
322 
323  while(oC_Thread_IsBlocked(thread));
324 
325  oC_Thread_SetUnblocked(thread);
326 
327  oC_IntMan_EnterCriticalSection();
328 
329  if(oC_Event_IsCorrect(Event))
330  {
331  if(CompareType == oC_Event_CompareType_Equal)
332  {
333  stateOk = Event->State == Value;
334  }
335  else if(CompareType == oC_Event_CompareType_Greater)
336  {
337  stateOk = Event->State > Value;
338  }
339  else if(CompareType == oC_Event_CompareType_GreaterOrEqual)
340  {
341  stateOk = Event->State >= Value;
342  }
343  else if(CompareType == oC_Event_CompareType_Less)
344  {
345  stateOk = Event->State < Value;
346  }
347  else if(CompareType == oC_Event_CompareType_LessOrEqual)
348  {
349  stateOk = Event->State <= Value;
350  }
351  }
352  oC_IntMan_ExitCriticalSection();
353  }
354  else
355  {
356  oC_SaveError("Event: wait for state" , oC_ErrorCode_ObjectNotCorrect);
357  }
358 
359  return stateOk;
360 }
361 
362 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
Definition: oc_event.c:245
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
Definition: oc_memman.c:1542
identifier for allocations
Definition: oc_stdlib.h:159
The file with helper macros for managing objects.
Allocator_t oC_MemMan_GetAllocatorOfAddress(const void *Address)
returns allocator of address
Definition: oc_memman.c:1098
uint32_t oC_ObjectControl_t
stores object control value
Definition: oc_object.h:141
The file with interface for event module.
The file with interface for interrupt manager.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
Definition: oc_object.h:168
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
Definition: oc_object.h:203
Definition of the null pointer.
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
The file with interface for Thread Manager.
#define NULL
pointer to a zero
Definition: oc_null.h:37