astroboy astroboy
/
CoOS_LWIP
Quick and dirty CoOS + LWIP ( Webserver )
CoOS/kernel/event.c@0:94897d537b31, 2011-09-10 (annotated)
- Committer:
- astroboy
- Date:
- Sat Sep 10 22:41:10 2011 +0000
- Revision:
- 0:94897d537b31
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
astroboy | 0:94897d537b31 | 1 | /** |
astroboy | 0:94897d537b31 | 2 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 3 | * @file event.c |
astroboy | 0:94897d537b31 | 4 | * @version V1.1.4 |
astroboy | 0:94897d537b31 | 5 | * @date 2011.04.20 |
astroboy | 0:94897d537b31 | 6 | * @brief event management implementation code of CooCox CoOS kernel. |
astroboy | 0:94897d537b31 | 7 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 8 | * @copy |
astroboy | 0:94897d537b31 | 9 | * |
astroboy | 0:94897d537b31 | 10 | * INTERNAL FILE,DON'T PUBLIC. |
astroboy | 0:94897d537b31 | 11 | * |
astroboy | 0:94897d537b31 | 12 | * <h2><center>© COPYRIGHT 2009 CooCox </center></h2> |
astroboy | 0:94897d537b31 | 13 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 14 | */ |
astroboy | 0:94897d537b31 | 15 | |
astroboy | 0:94897d537b31 | 16 | |
astroboy | 0:94897d537b31 | 17 | /*---------------------------- Include ---------------------------------------*/ |
astroboy | 0:94897d537b31 | 18 | #include <coocox.h> |
astroboy | 0:94897d537b31 | 19 | |
astroboy | 0:94897d537b31 | 20 | |
astroboy | 0:94897d537b31 | 21 | /*---------------------------- Variable Define -------------------------------*/ |
astroboy | 0:94897d537b31 | 22 | #if CFG_EVENT_EN > 0 |
astroboy | 0:94897d537b31 | 23 | |
astroboy | 0:94897d537b31 | 24 | ECB EventTbl[CFG_MAX_EVENT]= {{0}};/*!< Table which save event control block.*/ |
astroboy | 0:94897d537b31 | 25 | P_ECB FreeEventList = 0; /*!< Pointer to free event control block. */ |
astroboy | 0:94897d537b31 | 26 | |
astroboy | 0:94897d537b31 | 27 | |
astroboy | 0:94897d537b31 | 28 | /** |
astroboy | 0:94897d537b31 | 29 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 30 | * @brief Create a empty list of event control block |
astroboy | 0:94897d537b31 | 31 | * @param[in] None |
astroboy | 0:94897d537b31 | 32 | * @param[out] None |
astroboy | 0:94897d537b31 | 33 | * @retval None |
astroboy | 0:94897d537b31 | 34 | * |
astroboy | 0:94897d537b31 | 35 | * @par Description |
astroboy | 0:94897d537b31 | 36 | * @details This function is called by OSInit() API to create a ECB list,supply |
astroboy | 0:94897d537b31 | 37 | * a pointer to next event control block that not used. |
astroboy | 0:94897d537b31 | 38 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 39 | */ |
astroboy | 0:94897d537b31 | 40 | void CreateEventList(void) |
astroboy | 0:94897d537b31 | 41 | { |
astroboy | 0:94897d537b31 | 42 | U8 i; |
astroboy | 0:94897d537b31 | 43 | P_ECB pecb1; |
astroboy | 0:94897d537b31 | 44 | #if CFG_MAX_EVENT > 1 |
astroboy | 0:94897d537b31 | 45 | P_ECB pecb2; |
astroboy | 0:94897d537b31 | 46 | #endif |
astroboy | 0:94897d537b31 | 47 | i=0; |
astroboy | 0:94897d537b31 | 48 | pecb1 = &EventTbl[0]; /* Get first item */ |
astroboy | 0:94897d537b31 | 49 | #if CFG_MAX_EVENT == 1 /* Build event list for only one item */ |
astroboy | 0:94897d537b31 | 50 | pecb1->eventPtr = Co_NULL; |
astroboy | 0:94897d537b31 | 51 | pecb1->id = i; /* Assign ID. */ |
astroboy | 0:94897d537b31 | 52 | pecb1->eventType = EVENT_TYPE_INVALID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 53 | #endif |
astroboy | 0:94897d537b31 | 54 | |
astroboy | 0:94897d537b31 | 55 | #if CFG_MAX_EVENT > 1 /* Build event list for more than one item */ |
astroboy | 0:94897d537b31 | 56 | pecb2 = &EventTbl[1]; |
astroboy | 0:94897d537b31 | 57 | for(;i< (CFG_MAX_EVENT-1);i++ ) |
astroboy | 0:94897d537b31 | 58 | { |
astroboy | 0:94897d537b31 | 59 | pecb1->eventPtr = (void*)pecb2; /* Set link for list */ |
astroboy | 0:94897d537b31 | 60 | pecb1->id = i; /* Assign ID. */ |
astroboy | 0:94897d537b31 | 61 | pecb1->eventType = EVENT_TYPE_INVALID;/* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 62 | pecb1++; /* Get next item */ |
astroboy | 0:94897d537b31 | 63 | pecb2++; |
astroboy | 0:94897d537b31 | 64 | } |
astroboy | 0:94897d537b31 | 65 | pecb1->eventType = EVENT_TYPE_INVALID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 66 | pecb1->eventPtr = Co_NULL; /* Set link for last item */ |
astroboy | 0:94897d537b31 | 67 | pecb1->id = i; |
astroboy | 0:94897d537b31 | 68 | #endif |
astroboy | 0:94897d537b31 | 69 | |
astroboy | 0:94897d537b31 | 70 | FreeEventList = &EventTbl[0]; /* Set free event item */ |
astroboy | 0:94897d537b31 | 71 | } |
astroboy | 0:94897d537b31 | 72 | |
astroboy | 0:94897d537b31 | 73 | |
astroboy | 0:94897d537b31 | 74 | |
astroboy | 0:94897d537b31 | 75 | /** |
astroboy | 0:94897d537b31 | 76 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 77 | * @brief Release a ECB |
astroboy | 0:94897d537b31 | 78 | * @param[in] pecb A pointer to event control block which be released. |
astroboy | 0:94897d537b31 | 79 | * @param[out] None |
astroboy | 0:94897d537b31 | 80 | * @retval None |
astroboy | 0:94897d537b31 | 81 | * |
astroboy | 0:94897d537b31 | 82 | * @par Description |
astroboy | 0:94897d537b31 | 83 | * @details This function is called to release a event control block when a |
astroboy | 0:94897d537b31 | 84 | * event be deleted. |
astroboy | 0:94897d537b31 | 85 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 86 | */ |
astroboy | 0:94897d537b31 | 87 | static void ReleaseECB(P_ECB pecb) |
astroboy | 0:94897d537b31 | 88 | { |
astroboy | 0:94897d537b31 | 89 | pecb->eventType = EVENT_TYPE_INVALID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 90 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 91 | pecb->eventPtr = FreeEventList; /* Release ECB that event hold */ |
astroboy | 0:94897d537b31 | 92 | FreeEventList = pecb; /* Reset free event item */ |
astroboy | 0:94897d537b31 | 93 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 94 | } |
astroboy | 0:94897d537b31 | 95 | |
astroboy | 0:94897d537b31 | 96 | |
astroboy | 0:94897d537b31 | 97 | |
astroboy | 0:94897d537b31 | 98 | /** |
astroboy | 0:94897d537b31 | 99 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 100 | * @brief Create a event |
astroboy | 0:94897d537b31 | 101 | * @param[in] eventType The type of event which being created. |
astroboy | 0:94897d537b31 | 102 | * @param[in] eventSortType Event sort type. |
astroboy | 0:94897d537b31 | 103 | * @param[in] eventCounter Event counter,ONLY for EVENT_TYPE_SEM. |
astroboy | 0:94897d537b31 | 104 | * @param[in] eventPtr Event struct pointer,ONLY for Queue.Co_NULL for other |
astroboy | 0:94897d537b31 | 105 | * event type. |
astroboy | 0:94897d537b31 | 106 | * @param[out] None |
astroboy | 0:94897d537b31 | 107 | * @retval Co_NULL Invalid pointer,create event fail. |
astroboy | 0:94897d537b31 | 108 | * @retval others Pointer to event control block which had assigned right now. |
astroboy | 0:94897d537b31 | 109 | * |
astroboy | 0:94897d537b31 | 110 | * @par Description |
astroboy | 0:94897d537b31 | 111 | * @details This function is called by CreateSem(),... |
astroboy | 0:94897d537b31 | 112 | * to get a event control block and initial the event content. |
astroboy | 0:94897d537b31 | 113 | * |
astroboy | 0:94897d537b31 | 114 | * @note This is a internal function of CooCox CoOS,User can't call. |
astroboy | 0:94897d537b31 | 115 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 116 | */ |
astroboy | 0:94897d537b31 | 117 | P_ECB CreatEvent(U8 eventType,U8 eventSortType,void* eventPtr) |
astroboy | 0:94897d537b31 | 118 | { |
astroboy | 0:94897d537b31 | 119 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 120 | |
astroboy | 0:94897d537b31 | 121 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 122 | if(FreeEventList == Co_NULL) /* Is there no free evnet item */ |
astroboy | 0:94897d537b31 | 123 | { |
astroboy | 0:94897d537b31 | 124 | OsSchedUnlock(); /* Yes,unlock schedule */ |
astroboy | 0:94897d537b31 | 125 | return 0; /* Return error */ |
astroboy | 0:94897d537b31 | 126 | } |
astroboy | 0:94897d537b31 | 127 | pecb = FreeEventList;/* Assign the free event item to this event */ |
astroboy | 0:94897d537b31 | 128 | FreeEventList = (P_ECB)FreeEventList->eventPtr; /* Reset free event item */ |
astroboy | 0:94897d537b31 | 129 | OsSchedUnlock(); /* Unlock schedul */ |
astroboy | 0:94897d537b31 | 130 | |
astroboy | 0:94897d537b31 | 131 | pecb->eventType = eventType; /* Initialize event item as user set */ |
astroboy | 0:94897d537b31 | 132 | pecb->eventSortType = eventSortType; |
astroboy | 0:94897d537b31 | 133 | pecb->eventPtr = eventPtr; |
astroboy | 0:94897d537b31 | 134 | pecb->eventTCBList = 0; |
astroboy | 0:94897d537b31 | 135 | return pecb; /* Return event item pointer */ |
astroboy | 0:94897d537b31 | 136 | } |
astroboy | 0:94897d537b31 | 137 | |
astroboy | 0:94897d537b31 | 138 | |
astroboy | 0:94897d537b31 | 139 | /** |
astroboy | 0:94897d537b31 | 140 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 141 | * @brief Delete a event |
astroboy | 0:94897d537b31 | 142 | * @param[in] pecb Pointer to event control block which will be deleted. |
astroboy | 0:94897d537b31 | 143 | * @param[in] opt Delete option. |
astroboy | 0:94897d537b31 | 144 | * @arg == OPT_DEL_ANYWAY Delete event always |
astroboy | 0:94897d537b31 | 145 | * @arg == OPT_DEL_NO_PEND Delete event only when no task pending on. |
astroboy | 0:94897d537b31 | 146 | * @param[out] None |
astroboy | 0:94897d537b31 | 147 | * @retval E_INVALID_PARAMETER Parameter passed is invalid,deleted fail. |
astroboy | 0:94897d537b31 | 148 | * @retval E_TASK_WAITTING These are one more tasks waitting event. |
astroboy | 0:94897d537b31 | 149 | * @retval E_OK Delete event control block successful. |
astroboy | 0:94897d537b31 | 150 | * |
astroboy | 0:94897d537b31 | 151 | * @par Description |
astroboy | 0:94897d537b31 | 152 | * @details This function is called to delete a event from the event wait list |
astroboy | 0:94897d537b31 | 153 | * use specify option. |
astroboy | 0:94897d537b31 | 154 | * |
astroboy | 0:94897d537b31 | 155 | * @note This is a internal function of Coocox CoOS,user can't call. |
astroboy | 0:94897d537b31 | 156 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 157 | */ |
astroboy | 0:94897d537b31 | 158 | StatusType DeleteEvent(P_ECB pecb,U8 opt) |
astroboy | 0:94897d537b31 | 159 | { |
astroboy | 0:94897d537b31 | 160 | P_OSTCB ptcb; |
astroboy | 0:94897d537b31 | 161 | if(opt == OPT_DEL_NO_PEND) /* Do delete event when no task pend? */ |
astroboy | 0:94897d537b31 | 162 | { |
astroboy | 0:94897d537b31 | 163 | if(pecb->eventTCBList != Co_NULL) /* Yes,is there task pend this event? */ |
astroboy | 0:94897d537b31 | 164 | { |
astroboy | 0:94897d537b31 | 165 | return E_TASK_WAITING; /* Yes,error return */ |
astroboy | 0:94897d537b31 | 166 | } |
astroboy | 0:94897d537b31 | 167 | else |
astroboy | 0:94897d537b31 | 168 | { |
astroboy | 0:94897d537b31 | 169 | ReleaseECB(pecb); /* No,release resource that event hold*/ |
astroboy | 0:94897d537b31 | 170 | } |
astroboy | 0:94897d537b31 | 171 | } |
astroboy | 0:94897d537b31 | 172 | else if(opt == OPT_DEL_ANYWAY) /* Do delete event anyway? */ |
astroboy | 0:94897d537b31 | 173 | { |
astroboy | 0:94897d537b31 | 174 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 175 | while(pecb->eventTCBList != Co_NULL) /* Is there task pend this event? */ |
astroboy | 0:94897d537b31 | 176 | { /* Yes,remove it */ |
astroboy | 0:94897d537b31 | 177 | ptcb = pecb->eventTCBList;/* Get first task in event waiting list */ |
astroboy | 0:94897d537b31 | 178 | if(ptcb->delayTick != INVALID_VALUE) /* Is task in delay list? */ |
astroboy | 0:94897d537b31 | 179 | { |
astroboy | 0:94897d537b31 | 180 | RemoveDelayList(ptcb); /* Yes,remove task from delay list */ |
astroboy | 0:94897d537b31 | 181 | } |
astroboy | 0:94897d537b31 | 182 | |
astroboy | 0:94897d537b31 | 183 | /* Set next item as event waiting list head */ |
astroboy | 0:94897d537b31 | 184 | pecb->eventTCBList = ptcb->waitNext; |
astroboy | 0:94897d537b31 | 185 | ptcb->waitNext = 0; /* Clear link for event waiting list */ |
astroboy | 0:94897d537b31 | 186 | ptcb->eventID = INVALID_ID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 187 | |
astroboy | 0:94897d537b31 | 188 | InsertToTCBRdyList(ptcb); /* Insert task into ready list */ |
astroboy | 0:94897d537b31 | 189 | } |
astroboy | 0:94897d537b31 | 190 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 191 | ReleaseECB(pecb); /* Release resource that event hold */ |
astroboy | 0:94897d537b31 | 192 | } |
astroboy | 0:94897d537b31 | 193 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 194 | } |
astroboy | 0:94897d537b31 | 195 | |
astroboy | 0:94897d537b31 | 196 | |
astroboy | 0:94897d537b31 | 197 | /** |
astroboy | 0:94897d537b31 | 198 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 199 | * @brief Insert a task to event wait list |
astroboy | 0:94897d537b31 | 200 | * @param[in] pecb Pointer to event control block corresponding to the event. |
astroboy | 0:94897d537b31 | 201 | * @param[in] ptcb Pointer to task that will be insert to event wait list. |
astroboy | 0:94897d537b31 | 202 | * @param[out] None |
astroboy | 0:94897d537b31 | 203 | * @retval None |
astroboy | 0:94897d537b31 | 204 | * |
astroboy | 0:94897d537b31 | 205 | * @par Description |
astroboy | 0:94897d537b31 | 206 | * @details This function is called to insert a task by fllowing manner: |
astroboy | 0:94897d537b31 | 207 | * opt == EVENT_SORT_TYPE_FIFO By FIFO. |
astroboy | 0:94897d537b31 | 208 | * opt == EVENT_SORT_TYPE_PRIO By priority order,hghest priority |
astroboy | 0:94897d537b31 | 209 | * as head,lowest priority as end. |
astroboy | 0:94897d537b31 | 210 | * (Highest-->...-->Lowest-->Co_NULL) |
astroboy | 0:94897d537b31 | 211 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 212 | */ |
astroboy | 0:94897d537b31 | 213 | void EventTaskToWait(P_ECB pecb,P_OSTCB ptcb) |
astroboy | 0:94897d537b31 | 214 | { |
astroboy | 0:94897d537b31 | 215 | P_OSTCB ptcb1; |
astroboy | 0:94897d537b31 | 216 | #if (CFG_EVENT_SORT == 2) || (CFG_EVENT_SORT == 3) |
astroboy | 0:94897d537b31 | 217 | P_OSTCB ptcb2; |
astroboy | 0:94897d537b31 | 218 | #endif |
astroboy | 0:94897d537b31 | 219 | |
astroboy | 0:94897d537b31 | 220 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 221 | ptcb1 = pecb->eventTCBList; /* Get first task in event waiting list */ |
astroboy | 0:94897d537b31 | 222 | ptcb->eventID = pecb->id; /* Set event ID for task */ |
astroboy | 0:94897d537b31 | 223 | |
astroboy | 0:94897d537b31 | 224 | #if CFG_EVENT_SORT == 3 /* Does event waiting list sort as FIFO? */ |
astroboy | 0:94897d537b31 | 225 | |
astroboy | 0:94897d537b31 | 226 | if(pecb->eventSortType == EVENT_SORT_TYPE_FIFO) |
astroboy | 0:94897d537b31 | 227 | #endif |
astroboy | 0:94897d537b31 | 228 | |
astroboy | 0:94897d537b31 | 229 | #if (CFG_EVENT_SORT == 1) || (CFG_EVENT_SORT == 3) |
astroboy | 0:94897d537b31 | 230 | { |
astroboy | 0:94897d537b31 | 231 | if(ptcb1 == Co_NULL) /* Is no item in event waiting list?*/ |
astroboy | 0:94897d537b31 | 232 | { |
astroboy | 0:94897d537b31 | 233 | pecb->eventTCBList = ptcb; /* Yes,set task as first item */ |
astroboy | 0:94897d537b31 | 234 | } |
astroboy | 0:94897d537b31 | 235 | else |
astroboy | 0:94897d537b31 | 236 | { |
astroboy | 0:94897d537b31 | 237 | while(ptcb1->waitNext != Co_NULL)/* No,insert task in last */ |
astroboy | 0:94897d537b31 | 238 | { |
astroboy | 0:94897d537b31 | 239 | ptcb1 = ptcb1->waitNext; |
astroboy | 0:94897d537b31 | 240 | } |
astroboy | 0:94897d537b31 | 241 | ptcb1->waitNext = ptcb; /* Set link for list */ |
astroboy | 0:94897d537b31 | 242 | ptcb->waitPrev = ptcb1; |
astroboy | 0:94897d537b31 | 243 | } |
astroboy | 0:94897d537b31 | 244 | } |
astroboy | 0:94897d537b31 | 245 | #endif |
astroboy | 0:94897d537b31 | 246 | |
astroboy | 0:94897d537b31 | 247 | #if CFG_EVENT_SORT ==3 /* Does event waiting list sort as preemptive priority?*/ |
astroboy | 0:94897d537b31 | 248 | else if(pecb->eventSortType == EVENT_SORT_TYPE_PRIO) |
astroboy | 0:94897d537b31 | 249 | #endif |
astroboy | 0:94897d537b31 | 250 | #if (CFG_EVENT_SORT == 2) || (CFG_EVENT_SORT == 3) |
astroboy | 0:94897d537b31 | 251 | { |
astroboy | 0:94897d537b31 | 252 | if(ptcb1 == Co_NULL) /* Is no item in event waiting list? */ |
astroboy | 0:94897d537b31 | 253 | { |
astroboy | 0:94897d537b31 | 254 | pecb->eventTCBList = ptcb; /* Yes,set task as first item */ |
astroboy | 0:94897d537b31 | 255 | } |
astroboy | 0:94897d537b31 | 256 | /* Is PRI of task higher than list first item? */ |
astroboy | 0:94897d537b31 | 257 | else if(ptcb1->prio > ptcb->prio) |
astroboy | 0:94897d537b31 | 258 | { |
astroboy | 0:94897d537b31 | 259 | pecb->eventTCBList = ptcb; /* Reset task as first item */ |
astroboy | 0:94897d537b31 | 260 | ptcb->waitNext = ptcb1; /* Set link for list */ |
astroboy | 0:94897d537b31 | 261 | ptcb1->waitPrev = ptcb; |
astroboy | 0:94897d537b31 | 262 | } |
astroboy | 0:94897d537b31 | 263 | else /* No,find correct place to insert */ |
astroboy | 0:94897d537b31 | 264 | { |
astroboy | 0:94897d537b31 | 265 | ptcb2 = ptcb1->waitNext; |
astroboy | 0:94897d537b31 | 266 | while(ptcb2 != Co_NULL) /* Is last item? */ |
astroboy | 0:94897d537b31 | 267 | { |
astroboy | 0:94897d537b31 | 268 | if(ptcb2->prio > ptcb->prio) /* No,is correct place? */ |
astroboy | 0:94897d537b31 | 269 | { |
astroboy | 0:94897d537b31 | 270 | break; /* Yes,break Circulation */ |
astroboy | 0:94897d537b31 | 271 | } |
astroboy | 0:94897d537b31 | 272 | ptcb1 = ptcb2; /* Save current item */ |
astroboy | 0:94897d537b31 | 273 | ptcb2 = ptcb2->waitNext; /* Get next item */ |
astroboy | 0:94897d537b31 | 274 | } |
astroboy | 0:94897d537b31 | 275 | ptcb1->waitNext = ptcb; /* Set link for list */ |
astroboy | 0:94897d537b31 | 276 | ptcb->waitPrev = ptcb1; |
astroboy | 0:94897d537b31 | 277 | ptcb->waitNext = ptcb2; |
astroboy | 0:94897d537b31 | 278 | if(ptcb2 != Co_NULL) |
astroboy | 0:94897d537b31 | 279 | { |
astroboy | 0:94897d537b31 | 280 | ptcb2->waitPrev = ptcb; |
astroboy | 0:94897d537b31 | 281 | } |
astroboy | 0:94897d537b31 | 282 | } |
astroboy | 0:94897d537b31 | 283 | } |
astroboy | 0:94897d537b31 | 284 | #endif |
astroboy | 0:94897d537b31 | 285 | ptcb->state = TASK_WAITING; /* Set task status to TASK_WAITING state */ |
astroboy | 0:94897d537b31 | 286 | TaskSchedReq = Co_TRUE; |
astroboy | 0:94897d537b31 | 287 | OsSchedUnlock(); /* Unlock schedule,and call task schedule */ |
astroboy | 0:94897d537b31 | 288 | } |
astroboy | 0:94897d537b31 | 289 | |
astroboy | 0:94897d537b31 | 290 | |
astroboy | 0:94897d537b31 | 291 | /** |
astroboy | 0:94897d537b31 | 292 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 293 | * @brief Move a task from event WAITING list to the DELAY list |
astroboy | 0:94897d537b31 | 294 | * @param[in] pecb Pointer to event control block corresponding to the event. |
astroboy | 0:94897d537b31 | 295 | * @param[out] None |
astroboy | 0:94897d537b31 | 296 | * @retval None |
astroboy | 0:94897d537b31 | 297 | * |
astroboy | 0:94897d537b31 | 298 | * @par Description |
astroboy | 0:94897d537b31 | 299 | * @details This function is called to remove a task from event wait list,and |
astroboy | 0:94897d537b31 | 300 | * then insert it into the READY list. |
astroboy | 0:94897d537b31 | 301 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 302 | */ |
astroboy | 0:94897d537b31 | 303 | void EventTaskToRdy(P_ECB pecb) |
astroboy | 0:94897d537b31 | 304 | { |
astroboy | 0:94897d537b31 | 305 | P_OSTCB ptcb; |
astroboy | 0:94897d537b31 | 306 | #if CFG_QUEUE_EN >0 |
astroboy | 0:94897d537b31 | 307 | P_QCB pqcb; |
astroboy | 0:94897d537b31 | 308 | #endif |
astroboy | 0:94897d537b31 | 309 | ptcb = pecb->eventTCBList; |
astroboy | 0:94897d537b31 | 310 | if(ptcb == 0) |
astroboy | 0:94897d537b31 | 311 | return; |
astroboy | 0:94897d537b31 | 312 | |
astroboy | 0:94897d537b31 | 313 | pecb->eventTCBList = ptcb->waitNext;/* Get first task in event waiting list*/ |
astroboy | 0:94897d537b31 | 314 | if(pecb->eventTCBList != Co_NULL) /* Is no item in event waiting list? */ |
astroboy | 0:94897d537b31 | 315 | { |
astroboy | 0:94897d537b31 | 316 | pecb->eventTCBList->waitPrev = 0; /* No,clear link for first item */ |
astroboy | 0:94897d537b31 | 317 | } |
astroboy | 0:94897d537b31 | 318 | |
astroboy | 0:94897d537b31 | 319 | ptcb->waitNext = 0; /* Clear event waiting link for task*/ |
astroboy | 0:94897d537b31 | 320 | ptcb->eventID = INVALID_ID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 321 | |
astroboy | 0:94897d537b31 | 322 | if(ptcb->delayTick != INVALID_VALUE) /* Is task in delay list? */ |
astroboy | 0:94897d537b31 | 323 | { |
astroboy | 0:94897d537b31 | 324 | RemoveDelayList(ptcb); /* Yes,remove task from DELAY list */ |
astroboy | 0:94897d537b31 | 325 | } |
astroboy | 0:94897d537b31 | 326 | if(pecb->eventType == EVENT_TYPE_MBOX)/* Is it a mailbox event? */ |
astroboy | 0:94897d537b31 | 327 | { |
astroboy | 0:94897d537b31 | 328 | ptcb->pmail = pecb->eventPtr; /* Yes,send mail to task */ |
astroboy | 0:94897d537b31 | 329 | pecb->eventPtr = Co_NULL; /* Clear event sign */ |
astroboy | 0:94897d537b31 | 330 | pecb->eventCounter--; |
astroboy | 0:94897d537b31 | 331 | } |
astroboy | 0:94897d537b31 | 332 | #if CFG_QUEUE_EN >0 |
astroboy | 0:94897d537b31 | 333 | else if(pecb->eventType == EVENT_TYPE_QUEUE) /* Is it a queue event? */ |
astroboy | 0:94897d537b31 | 334 | { |
astroboy | 0:94897d537b31 | 335 | pqcb = (P_QCB)pecb->eventPtr; /* Yes,get queue pointer */ |
astroboy | 0:94897d537b31 | 336 | ptcb->pmail = *(pqcb->qStart + pqcb->head); /* Send mail to task */ |
astroboy | 0:94897d537b31 | 337 | pqcb->head++; /* Clear event sign */ |
astroboy | 0:94897d537b31 | 338 | pqcb->qSize--; |
astroboy | 0:94897d537b31 | 339 | if(pqcb->head == pqcb->qMaxSize) |
astroboy | 0:94897d537b31 | 340 | { |
astroboy | 0:94897d537b31 | 341 | pqcb->head = 0; |
astroboy | 0:94897d537b31 | 342 | } |
astroboy | 0:94897d537b31 | 343 | } |
astroboy | 0:94897d537b31 | 344 | #endif |
astroboy | 0:94897d537b31 | 345 | |
astroboy | 0:94897d537b31 | 346 | #if CFG_MAILBOX_EN >0 |
astroboy | 0:94897d537b31 | 347 | else if(pecb->eventType == EVENT_TYPE_SEM)/* Is it a semaphore event? */ |
astroboy | 0:94897d537b31 | 348 | { |
astroboy | 0:94897d537b31 | 349 | pecb->eventCounter--; /* Yes,clear event sign */ |
astroboy | 0:94897d537b31 | 350 | ptcb->pmail = (void*)0xffffffff; /* Indicate task woke by event */ |
astroboy | 0:94897d537b31 | 351 | } |
astroboy | 0:94897d537b31 | 352 | #endif |
astroboy | 0:94897d537b31 | 353 | if(ptcb == TCBRunning) |
astroboy | 0:94897d537b31 | 354 | { |
astroboy | 0:94897d537b31 | 355 | ptcb->state = TASK_RUNNING; |
astroboy | 0:94897d537b31 | 356 | } |
astroboy | 0:94897d537b31 | 357 | else |
astroboy | 0:94897d537b31 | 358 | { |
astroboy | 0:94897d537b31 | 359 | InsertToTCBRdyList(ptcb); /* Insert task into ready list */ |
astroboy | 0:94897d537b31 | 360 | } |
astroboy | 0:94897d537b31 | 361 | } |
astroboy | 0:94897d537b31 | 362 | |
astroboy | 0:94897d537b31 | 363 | |
astroboy | 0:94897d537b31 | 364 | |
astroboy | 0:94897d537b31 | 365 | /** |
astroboy | 0:94897d537b31 | 366 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 367 | * @brief Move a task from event wait list to the ready list |
astroboy | 0:94897d537b31 | 368 | * @param[in] pecb Pointer to event control block corresponding to the event. |
astroboy | 0:94897d537b31 | 369 | * @param[out] None |
astroboy | 0:94897d537b31 | 370 | * @retval None |
astroboy | 0:94897d537b31 | 371 | * |
astroboy | 0:94897d537b31 | 372 | * @par Description |
astroboy | 0:94897d537b31 | 373 | * @details This function is called to remove a task from event wait list,and |
astroboy | 0:94897d537b31 | 374 | * then insert it to the ready list. |
astroboy | 0:94897d537b31 | 375 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 376 | */ |
astroboy | 0:94897d537b31 | 377 | void RemoveEventWaittingList(P_OSTCB ptcb) |
astroboy | 0:94897d537b31 | 378 | { |
astroboy | 0:94897d537b31 | 379 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 380 | pecb = &EventTbl[ptcb->eventID]; /* Get event control block */ |
astroboy | 0:94897d537b31 | 381 | |
astroboy | 0:94897d537b31 | 382 | /* Is there only one item in event waiting list? */ |
astroboy | 0:94897d537b31 | 383 | if((ptcb->waitNext == 0) && (ptcb->waitPrev == 0)) |
astroboy | 0:94897d537b31 | 384 | { |
astroboy | 0:94897d537b31 | 385 | pecb->eventTCBList = 0; /* Yes,set event waiting list as Co_NULL */ |
astroboy | 0:94897d537b31 | 386 | } |
astroboy | 0:94897d537b31 | 387 | else if(ptcb->waitPrev == 0)/* Is the first item in event waiting list?*/ |
astroboy | 0:94897d537b31 | 388 | { |
astroboy | 0:94897d537b31 | 389 | /* Yes,remove task from list,and reset event waiting list */ |
astroboy | 0:94897d537b31 | 390 | ptcb->waitNext->waitPrev = 0; |
astroboy | 0:94897d537b31 | 391 | pecb->eventTCBList = ptcb->waitNext; |
astroboy | 0:94897d537b31 | 392 | ptcb->waitNext = 0; |
astroboy | 0:94897d537b31 | 393 | } |
astroboy | 0:94897d537b31 | 394 | else if(ptcb->waitNext == 0)/* Is the last item in event waiting list? */ |
astroboy | 0:94897d537b31 | 395 | { |
astroboy | 0:94897d537b31 | 396 | ptcb->waitPrev->waitNext = 0; /* Yes,remove task form list */ |
astroboy | 0:94897d537b31 | 397 | ptcb->waitPrev = 0; |
astroboy | 0:94897d537b31 | 398 | } |
astroboy | 0:94897d537b31 | 399 | else /* No, remove task from list */ |
astroboy | 0:94897d537b31 | 400 | { |
astroboy | 0:94897d537b31 | 401 | ptcb->waitPrev->waitNext = ptcb->waitNext; |
astroboy | 0:94897d537b31 | 402 | ptcb->waitNext->waitPrev = ptcb->waitPrev; |
astroboy | 0:94897d537b31 | 403 | ptcb->waitPrev = 0; |
astroboy | 0:94897d537b31 | 404 | ptcb->waitNext = 0; |
astroboy | 0:94897d537b31 | 405 | } |
astroboy | 0:94897d537b31 | 406 | ptcb->eventID = INVALID_ID; /* Sign that not to use. */ |
astroboy | 0:94897d537b31 | 407 | } |
astroboy | 0:94897d537b31 | 408 | |
astroboy | 0:94897d537b31 | 409 | #endif //CFG_EVENT_EN |
astroboy | 0:94897d537b31 | 410 |