Quick and dirty CoOS + LWIP ( Webserver )

Dependencies:   mbed lwip

Committer:
astroboy
Date:
Sat Sep 10 22:41:10 2011 +0000
Revision:
0:94897d537b31

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
astroboy 0:94897d537b31 1 /**
astroboy 0:94897d537b31 2 *******************************************************************************
astroboy 0:94897d537b31 3 * @file sem.c
astroboy 0:94897d537b31 4 * @version V1.1.4
astroboy 0:94897d537b31 5 * @date 2011.04.20
astroboy 0:94897d537b31 6 * @brief Semaphore 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>&copy; 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 #if CFG_SEM_EN >0
astroboy 0:94897d537b31 21
astroboy 0:94897d537b31 22 /**
astroboy 0:94897d537b31 23 *******************************************************************************
astroboy 0:94897d537b31 24 * @brief Create a semaphore
astroboy 0:94897d537b31 25 * @param[in] initCnt Semaphore valid counter.
astroboy 0:94897d537b31 26 * @param[in] maxCnt Semaphore max initialize counter.
astroboy 0:94897d537b31 27 * @param[in] sortType Semaphore sort type.
astroboy 0:94897d537b31 28 * @param[out] None
astroboy 0:94897d537b31 29 * @retval E_CREATE_FAIL Create semaphore fail.
astroboy 0:94897d537b31 30 * @retval others Create semaphore successful.
astroboy 0:94897d537b31 31 *
astroboy 0:94897d537b31 32 * @par Description
astroboy 0:94897d537b31 33 * @details This function is called to create a semaphore.
astroboy 0:94897d537b31 34 *******************************************************************************
astroboy 0:94897d537b31 35 */
astroboy 0:94897d537b31 36 OS_EventID CoCreateSem(U16 initCnt,U16 maxCnt,U8 sortType)
astroboy 0:94897d537b31 37 {
astroboy 0:94897d537b31 38 P_ECB pecb;
astroboy 0:94897d537b31 39 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 40 if(initCnt > maxCnt)
astroboy 0:94897d537b31 41 {
astroboy 0:94897d537b31 42 return E_CREATE_FAIL; /* Invalid 'initCnt' or 'maxCnt' */
astroboy 0:94897d537b31 43 }
astroboy 0:94897d537b31 44
astroboy 0:94897d537b31 45 if ((sortType != EVENT_SORT_TYPE_FIFO) && (sortType != EVENT_SORT_TYPE_PRIO))
astroboy 0:94897d537b31 46 {
astroboy 0:94897d537b31 47 return E_CREATE_FAIL; /* Illegal sort type,return error */
astroboy 0:94897d537b31 48 }
astroboy 0:94897d537b31 49 #endif
astroboy 0:94897d537b31 50
astroboy 0:94897d537b31 51 /* Create a semaphore type event control block */
astroboy 0:94897d537b31 52 pecb = CreatEvent(EVENT_TYPE_SEM,sortType,Co_NULL);
astroboy 0:94897d537b31 53 if(pecb == Co_NULL) /* If failed to create event block */
astroboy 0:94897d537b31 54 {
astroboy 0:94897d537b31 55 return E_CREATE_FAIL;
astroboy 0:94897d537b31 56 }
astroboy 0:94897d537b31 57 pecb->eventCounter = initCnt;/* Initialize event block */
astroboy 0:94897d537b31 58 pecb->initialEventCounter = maxCnt;
astroboy 0:94897d537b31 59 return (pecb->id); /* Return event id */
astroboy 0:94897d537b31 60 }
astroboy 0:94897d537b31 61
astroboy 0:94897d537b31 62
astroboy 0:94897d537b31 63 /**
astroboy 0:94897d537b31 64 *******************************************************************************
astroboy 0:94897d537b31 65 * @brief Delete a semaphore
astroboy 0:94897d537b31 66 * @param[in] id Event ID which to be deleted.
astroboy 0:94897d537b31 67 * @param[in] opt Delete option.
astroboy 0:94897d537b31 68 * @arg == OPT_DEL_ANYWAY Delete semaphore always
astroboy 0:94897d537b31 69 * @arg == OPT_DEL_NO_PEND Delete semaphore only when no task pending on.
astroboy 0:94897d537b31 70 * @param[out] None
astroboy 0:94897d537b31 71 * @retval E_INVALID_ID Invalid event ID.
astroboy 0:94897d537b31 72 * @retval E_INVALID_PARAMETER Invalid parameter.
astroboy 0:94897d537b31 73 * @retval E_TASK_WAITTING Tasks waitting for the event,delete fail.
astroboy 0:94897d537b31 74 * @retval E_OK Event deleted successful.
astroboy 0:94897d537b31 75 *
astroboy 0:94897d537b31 76 * @par Description
astroboy 0:94897d537b31 77 * @details This function is called to delete a semaphore.
astroboy 0:94897d537b31 78 *
astroboy 0:94897d537b31 79 * @note
astroboy 0:94897d537b31 80 *******************************************************************************
astroboy 0:94897d537b31 81 */
astroboy 0:94897d537b31 82 StatusType CoDelSem(OS_EventID id,U8 opt)
astroboy 0:94897d537b31 83 {
astroboy 0:94897d537b31 84 P_ECB pecb;
astroboy 0:94897d537b31 85
astroboy 0:94897d537b31 86 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 87 if(id >= CFG_MAX_EVENT)
astroboy 0:94897d537b31 88 {
astroboy 0:94897d537b31 89 return E_INVALID_ID;
astroboy 0:94897d537b31 90 }
astroboy 0:94897d537b31 91 #endif
astroboy 0:94897d537b31 92
astroboy 0:94897d537b31 93 pecb = &EventTbl[id];
astroboy 0:94897d537b31 94
astroboy 0:94897d537b31 95 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 96 if(pecb->eventType != EVENT_TYPE_SEM)
astroboy 0:94897d537b31 97 {
astroboy 0:94897d537b31 98 return E_INVALID_ID; /* The event type is not semaphore */
astroboy 0:94897d537b31 99 }
astroboy 0:94897d537b31 100 #endif
astroboy 0:94897d537b31 101
astroboy 0:94897d537b31 102 return (DeleteEvent(pecb,opt));/* Delete the semaphore event control block*/
astroboy 0:94897d537b31 103 }
astroboy 0:94897d537b31 104
astroboy 0:94897d537b31 105
astroboy 0:94897d537b31 106 /**
astroboy 0:94897d537b31 107 *******************************************************************************
astroboy 0:94897d537b31 108 * @brief Accept a semaphore without waitting
astroboy 0:94897d537b31 109 * @param[in] id Event ID
astroboy 0:94897d537b31 110 * @param[out] None
astroboy 0:94897d537b31 111 * @retval E_INVALID_ID Invalid event ID.
astroboy 0:94897d537b31 112 * @retval E_SEM_EMPTY No semaphore exist.
astroboy 0:94897d537b31 113 * @retval E_OK Get semaphore successful.
astroboy 0:94897d537b31 114 *
astroboy 0:94897d537b31 115 * @par Description
astroboy 0:94897d537b31 116 * @details This function is called accept a semaphore without waitting.
astroboy 0:94897d537b31 117 *******************************************************************************
astroboy 0:94897d537b31 118 */
astroboy 0:94897d537b31 119 StatusType CoAcceptSem(OS_EventID id)
astroboy 0:94897d537b31 120 {
astroboy 0:94897d537b31 121 P_ECB pecb;
astroboy 0:94897d537b31 122 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 123 if(id >= CFG_MAX_EVENT)
astroboy 0:94897d537b31 124 {
astroboy 0:94897d537b31 125 return E_INVALID_ID;
astroboy 0:94897d537b31 126 }
astroboy 0:94897d537b31 127 #endif
astroboy 0:94897d537b31 128
astroboy 0:94897d537b31 129 pecb = &EventTbl[id];
astroboy 0:94897d537b31 130 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 131 if( pecb->eventType != EVENT_TYPE_SEM)
astroboy 0:94897d537b31 132 {
astroboy 0:94897d537b31 133 return E_INVALID_ID;
astroboy 0:94897d537b31 134 }
astroboy 0:94897d537b31 135 #endif
astroboy 0:94897d537b31 136 OsSchedLock();
astroboy 0:94897d537b31 137 if(pecb->eventCounter > 0) /* If semaphore is positive,resource available */
astroboy 0:94897d537b31 138 {
astroboy 0:94897d537b31 139 OsSchedUnlock();
astroboy 0:94897d537b31 140 pecb->eventCounter--; /* Decrement semaphore only if positive */
astroboy 0:94897d537b31 141 return E_OK;
astroboy 0:94897d537b31 142 }
astroboy 0:94897d537b31 143 else /* Resource is not available */
astroboy 0:94897d537b31 144 {
astroboy 0:94897d537b31 145 OsSchedUnlock();
astroboy 0:94897d537b31 146 return E_SEM_EMPTY;
astroboy 0:94897d537b31 147 }
astroboy 0:94897d537b31 148 }
astroboy 0:94897d537b31 149
astroboy 0:94897d537b31 150
astroboy 0:94897d537b31 151 /**
astroboy 0:94897d537b31 152 *******************************************************************************
astroboy 0:94897d537b31 153 * @brief wait for a semaphore
astroboy 0:94897d537b31 154 * @param[in] id Event ID.
astroboy 0:94897d537b31 155 * @param[in] timeout The longest time for writting semaphore.
astroboy 0:94897d537b31 156 * @para 0
astroboy 0:94897d537b31 157 * @para 0x1~0xff
astroboy 0:94897d537b31 158 * @param[out] None
astroboy 0:94897d537b31 159 * @retval E_CALL Error call in ISR.
astroboy 0:94897d537b31 160 * @retval E_INVALID_ID Invalid event ID.
astroboy 0:94897d537b31 161 * @retval E_TIMEOUT Semaphore was not received within the specified
astroboy 0:94897d537b31 162 * 'timeout' time.
astroboy 0:94897d537b31 163 * @retval E_OK The call was successful and your task owns the
astroboy 0:94897d537b31 164 * resource,or the event you are waiting for occurred.
astroboy 0:94897d537b31 165 *
astroboy 0:94897d537b31 166 * @par Description
astroboy 0:94897d537b31 167 * @details This function is called to waits for a semaphore.
astroboy 0:94897d537b31 168 * @note IF this function is called in ISR,nothing to do and return immediately.
astroboy 0:94897d537b31 169 *******************************************************************************
astroboy 0:94897d537b31 170 */
astroboy 0:94897d537b31 171 StatusType CoPendSem(OS_EventID id,U32 timeout)
astroboy 0:94897d537b31 172 {
astroboy 0:94897d537b31 173 P_ECB pecb;
astroboy 0:94897d537b31 174 P_OSTCB curTCB;
astroboy 0:94897d537b31 175 if(OSIntNesting > 0) /* If the caller is ISR */
astroboy 0:94897d537b31 176 {
astroboy 0:94897d537b31 177 return E_CALL;
astroboy 0:94897d537b31 178 }
astroboy 0:94897d537b31 179 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 180 if(id >= CFG_MAX_EVENT)
astroboy 0:94897d537b31 181 {
astroboy 0:94897d537b31 182 return E_INVALID_ID;
astroboy 0:94897d537b31 183 }
astroboy 0:94897d537b31 184 #endif
astroboy 0:94897d537b31 185
astroboy 0:94897d537b31 186 pecb = &EventTbl[id];
astroboy 0:94897d537b31 187 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 188 if(pecb->eventType != EVENT_TYPE_SEM)
astroboy 0:94897d537b31 189 {
astroboy 0:94897d537b31 190 return E_INVALID_ID;
astroboy 0:94897d537b31 191 }
astroboy 0:94897d537b31 192 #endif
astroboy 0:94897d537b31 193 if(OSSchedLock != 0) /* Schdule is locked? */
astroboy 0:94897d537b31 194 {
astroboy 0:94897d537b31 195 return E_OS_IN_LOCK; /* Yes,error return */
astroboy 0:94897d537b31 196 }
astroboy 0:94897d537b31 197 OsSchedLock();
astroboy 0:94897d537b31 198 if(pecb->eventCounter > 0) /* If semaphore is positive,resource available */
astroboy 0:94897d537b31 199 {
astroboy 0:94897d537b31 200 pecb->eventCounter--; /* Decrement semaphore only if positive */
astroboy 0:94897d537b31 201 OsSchedUnlock();
astroboy 0:94897d537b31 202 return E_OK;
astroboy 0:94897d537b31 203 }
astroboy 0:94897d537b31 204 else /* Resource is not available */
astroboy 0:94897d537b31 205 {
astroboy 0:94897d537b31 206 OsSchedUnlock();
astroboy 0:94897d537b31 207 curTCB = TCBRunning;
astroboy 0:94897d537b31 208 if(timeout == 0) /* If time-out is not configured */
astroboy 0:94897d537b31 209 {
astroboy 0:94897d537b31 210 EventTaskToWait(pecb,curTCB); /* Block task until event occurs */
astroboy 0:94897d537b31 211 curTCB->pmail = Co_NULL;
astroboy 0:94897d537b31 212 return E_OK;
astroboy 0:94897d537b31 213 }
astroboy 0:94897d537b31 214 else /* If time-out is configured */
astroboy 0:94897d537b31 215 {
astroboy 0:94897d537b31 216 OsSchedLock();
astroboy 0:94897d537b31 217
astroboy 0:94897d537b31 218 /* Block task until event or timeout occurs */
astroboy 0:94897d537b31 219 EventTaskToWait(pecb,curTCB);
astroboy 0:94897d537b31 220 InsertDelayList(curTCB,timeout);
astroboy 0:94897d537b31 221
astroboy 0:94897d537b31 222 OsSchedUnlock();
astroboy 0:94897d537b31 223 if (curTCB->pmail == Co_NULL) /* If pmail is Co_NULL, time-out occurred*/
astroboy 0:94897d537b31 224 {
astroboy 0:94897d537b31 225 return E_TIMEOUT;
astroboy 0:94897d537b31 226 }
astroboy 0:94897d537b31 227 else /* Event occurred or event have been deleted*/
astroboy 0:94897d537b31 228 {
astroboy 0:94897d537b31 229 curTCB->pmail = Co_NULL;
astroboy 0:94897d537b31 230 return E_OK;
astroboy 0:94897d537b31 231 }
astroboy 0:94897d537b31 232 }
astroboy 0:94897d537b31 233 }
astroboy 0:94897d537b31 234 }
astroboy 0:94897d537b31 235
astroboy 0:94897d537b31 236
astroboy 0:94897d537b31 237 /**
astroboy 0:94897d537b31 238 *******************************************************************************
astroboy 0:94897d537b31 239 * @brief Post a semaphore
astroboy 0:94897d537b31 240 * @param[in] id id of event control block associated with the desired semaphore.
astroboy 0:94897d537b31 241 * @param[out] None
astroboy 0:94897d537b31 242 * @retval E_INVALID_ID Parameter id passed was invalid event ID.
astroboy 0:94897d537b31 243 * @retval E_SEM_FULL Semaphore full.
astroboy 0:94897d537b31 244 * @retval E_OK Semaphore had post successful.
astroboy 0:94897d537b31 245 *
astroboy 0:94897d537b31 246 * @par Description
astroboy 0:94897d537b31 247 * @details This function is called to post a semaphore to corresponding event.
astroboy 0:94897d537b31 248 *
astroboy 0:94897d537b31 249 * @note
astroboy 0:94897d537b31 250 *******************************************************************************
astroboy 0:94897d537b31 251 */
astroboy 0:94897d537b31 252 StatusType CoPostSem(OS_EventID id)
astroboy 0:94897d537b31 253 {
astroboy 0:94897d537b31 254 P_ECB pecb;
astroboy 0:94897d537b31 255 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 256 if(id >= CFG_MAX_EVENT)
astroboy 0:94897d537b31 257 {
astroboy 0:94897d537b31 258 return E_INVALID_ID;
astroboy 0:94897d537b31 259 }
astroboy 0:94897d537b31 260 #endif
astroboy 0:94897d537b31 261
astroboy 0:94897d537b31 262 pecb = &EventTbl[id];
astroboy 0:94897d537b31 263 #if CFG_PAR_CHECKOUT_EN >0
astroboy 0:94897d537b31 264 if(pecb->eventType != EVENT_TYPE_SEM) /* Invalid event control block type */
astroboy 0:94897d537b31 265 {
astroboy 0:94897d537b31 266 return E_INVALID_ID;
astroboy 0:94897d537b31 267 }
astroboy 0:94897d537b31 268 #endif
astroboy 0:94897d537b31 269
astroboy 0:94897d537b31 270 /* Make sure semaphore will not overflow */
astroboy 0:94897d537b31 271 if(pecb->eventCounter == pecb->initialEventCounter)
astroboy 0:94897d537b31 272 {
astroboy 0:94897d537b31 273 return E_SEM_FULL; /* The counter of Semaphore reach the max number*/
astroboy 0:94897d537b31 274 }
astroboy 0:94897d537b31 275 OsSchedLock();
astroboy 0:94897d537b31 276 pecb->eventCounter++; /* Increment semaphore count to register event */
astroboy 0:94897d537b31 277 EventTaskToRdy(pecb); /* Check semaphore event waiting list */
astroboy 0:94897d537b31 278 OsSchedUnlock();
astroboy 0:94897d537b31 279 return E_OK;
astroboy 0:94897d537b31 280
astroboy 0:94897d537b31 281 }
astroboy 0:94897d537b31 282
astroboy 0:94897d537b31 283
astroboy 0:94897d537b31 284 /**
astroboy 0:94897d537b31 285 *******************************************************************************
astroboy 0:94897d537b31 286 * @brief Post a semaphore in ISR
astroboy 0:94897d537b31 287 * @param[in] id identifier of event control block associated with the
astroboy 0:94897d537b31 288 * desired semaphore.
astroboy 0:94897d537b31 289 * @param[out] None
astroboy 0:94897d537b31 290 * @retval E_INVALID_ID Parameter id passed was invalid event ID.
astroboy 0:94897d537b31 291 * @retval E_NO_TASK_WAITTING There are one more tasks waitting for the event.
astroboy 0:94897d537b31 292 * @retval E_OK Semaphore had signaled successful.
astroboy 0:94897d537b31 293 *
astroboy 0:94897d537b31 294 * @par Description
astroboy 0:94897d537b31 295 * @details This function is called in ISR to post a semaphore to corresponding
astroboy 0:94897d537b31 296 * event.
astroboy 0:94897d537b31 297 * @note
astroboy 0:94897d537b31 298 *******************************************************************************
astroboy 0:94897d537b31 299 */
astroboy 0:94897d537b31 300 #if CFG_MAX_SERVICE_REQUEST > 0
astroboy 0:94897d537b31 301 StatusType isr_PostSem(OS_EventID id)
astroboy 0:94897d537b31 302 {
astroboy 0:94897d537b31 303 if(OSSchedLock > 0) /* If scheduler is locked,(the caller is ISR) */
astroboy 0:94897d537b31 304 {
astroboy 0:94897d537b31 305 /* Initiate a post service handling request */
astroboy 0:94897d537b31 306 if(InsertInSRQ(SEM_REQ,id,Co_NULL) == Co_FALSE)
astroboy 0:94897d537b31 307 {
astroboy 0:94897d537b31 308 return E_SEV_REQ_FULL; /* If service request queue is full */
astroboy 0:94897d537b31 309 }
astroboy 0:94897d537b31 310 else /* Operate successfully */
astroboy 0:94897d537b31 311 {
astroboy 0:94897d537b31 312 return E_OK;
astroboy 0:94897d537b31 313 }
astroboy 0:94897d537b31 314 }
astroboy 0:94897d537b31 315 else
astroboy 0:94897d537b31 316 {
astroboy 0:94897d537b31 317 return(CoPostSem(id)); /* Post semaphore */
astroboy 0:94897d537b31 318 }
astroboy 0:94897d537b31 319 }
astroboy 0:94897d537b31 320 #endif
astroboy 0:94897d537b31 321
astroboy 0:94897d537b31 322 #endif