CooCox CoOS User's Guide
CooCox CoOS User's Guide
CooCox CoOS User's Guide
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Code 3 The use of the mutex section<br />
void myTaskA(void* pdata)<br />
{<br />
mutexID = CoCreateMutex();<br />
CoEnterMutexSection(mutexID ); // enter the mutex section<br />
........... // critical codes<br />
CoLeaveMutexSection(mutexID ); // leave the mutex section<br />
}<br />
void myTaskB(void* pdata)<br />
{<br />
CoEnterMutexSection(mutexID ); // enter the mutex section<br />
........... // critical codes<br />
CoLeaveMutexSection(mutexID ); // leave the mutex section<br />
}<br />
5.1.3 Flags<br />
When a task wants to synchronize with a number of events, flags are<br />
needed. If the task synchronizes with a single event, it can be called<br />
independent synchronization (logical OR relationship). If it synchronizes with a<br />
number of events, then called associated synchronization (logical AND<br />
relationship).<br />
<strong>CooCox</strong> <strong>CoOS</strong> supports 32 flags to the maximum at the same time. It<br />
supports that multiple tasks waiting for a single event or multiple events. When<br />
the flags that the waiting tasks waiting for are in the not-ready state, these<br />
tasks can not be scheduled. However, once the flags turn to the ready state,<br />
they will be resumed soon.<br />
According to the types of the flags, the side effects are different when the<br />
tasks have waited for the flags successfully. There are two kinds of flags in<br />
<strong>CooCox</strong> <strong>CoOS</strong>: the ones reset manually and the ones reset automatically. When<br />
a task has waited for a flag which reset automatically, the system will convert<br />
the flag to not-ready state. On the contrary, if the flag is reset manually, there<br />
won’t be any side effect. Therefore, when a flag which reset manually converts<br />
to the ready state, all the tasks which waiting for this event will convert to the<br />
ready state as far as you call CoClearFlag() to reset the flag to the not-ready<br />
state. When a flag which reset automatically converts to the ready state, only<br />
one task which waiting for this event will convert to the ready state. Since the<br />
waiting list of the event flags is ordered by the principle of FIFO, towards the<br />
event which reset automatically only the first task of the waiting list converts to<br />
the ready state and others that waiting for this flag are still in the waiting state.<br />
Suppose there are three tasks (A, B, C) waiting for the same flag I which<br />
reset manually. When I is ready, all the tasks will be converted (A, B, C) to the<br />
35