OS_EVENT *OSSemCreate(INT16U cnt){OS_EVENT *pevent;#ifOS_CRITICAL_METHOD ==3u/* Allocate storage for CPU status register */OS_CPU_SR cpu_sr =0u;#endif#ifdefOS_SAFETY_CRITICAL_IEC61508if(OSSafetyCriticalStartFlag == OS_TRUE){OS_SAFETY_CRITICAL_EXCEPTION();return((OS_EVENT *)0);}#endifif(OSIntNesting >0u)/* See if called from ISR ... */{return((OS_EVENT *)0);/* ... can't CREATE from an ISR */}OS_ENTER_CRITICAL();pevent = OSEventFreeList;/* Get next free event control block */if(OSEventFreeList !=(OS_EVENT *)0)/* See if pool of free ECB pool was empty */{OSEventFreeList =(OS_EVENT *)OSEventFreeList->OSEventPtr;}OS_EXIT_CRITICAL();if(pevent !=(OS_EVENT *)0)/* Get an event control block */{pevent->OSEventType = OS_EVENT_TYPE_SEM;pevent->OSEventCnt = cnt;/* Set semaphore value */pevent->OSEventPtr =(void*)0;/* Unlink from ECB free list */#ifOS_EVENT_NAME_EN >0upevent->OSEventName =(INT8U *)(void*)"?";#endifOS_EventWaitListInit(pevent);/* Initialize to 'nobody waiting' on sem. */}return(pevent);}
信号量删除
#ifOS_SEM_DEL_EN >0u
OS_EVENT *OSSemDel(OS_EVENT *pevent,INT8U opt,INT8U *perr){BOOLEAN tasks_waiting;OS_EVENT *pevent_return;#ifOS_CRITICAL_METHOD ==3u/* Allocate storage for CPU status register */OS_CPU_SR cpu_sr =0u;#endif#ifdefOS_SAFETY_CRITICALif(perr ==(INT8U *)0){OS_SAFETY_CRITICAL_EXCEPTION();return((OS_EVENT *)0);}#endif#ifOS_ARG_CHK_EN >0uif(pevent ==(OS_EVENT *)0)/* Validate 'pevent' */{*perr = OS_ERR_PEVENT_NULL;return(pevent);}#endifif(pevent->OSEventType != OS_EVENT_TYPE_SEM)/* Validate event block type */{*perr = OS_ERR_EVENT_TYPE;return(pevent);}if(OSIntNesting >0u)/* See if called from ISR ... */{*perr = OS_ERR_DEL_ISR;/* ... can't DELETE from an ISR */return(pevent);}OS_ENTER_CRITICAL();if(pevent->OSEventGrp !=0u)/* See if any tasks waiting on semaphore */{tasks_waiting = OS_TRUE;/* Yes */}else{tasks_waiting = OS_FALSE;/* No */}switch(opt){case OS_DEL_NO_PEND:/* Delete semaphore only if no task waiting */if(tasks_waiting == OS_FALSE){#ifOS_EVENT_NAME_EN >0upevent->OSEventName =(INT8U *)(void*)"?";#endifpevent->OSEventType = OS_EVENT_TYPE_UNUSED;pevent->OSEventPtr = OSEventFreeList;/* Return Event Control Block to free list */pevent->OSEventCnt =0u;OSEventFreeList = pevent;/* Get next free event control block */OS_EXIT_CRITICAL();*perr = OS_ERR_NONE;pevent_return =(OS_EVENT *)0;/* Semaphore has been deleted */}else{OS_EXIT_CRITICAL();*perr = OS_ERR_TASK_WAITING;pevent_return = pevent;}break;case OS_DEL_ALWAYS:/* Always delete the semaphore */while(pevent->OSEventGrp !=0u)/* Ready ALL tasks waiting for semaphore */{(void)OS_EventTaskRdy(pevent,(void*)0, OS_STAT_SEM, OS_STAT_PEND_ABORT);}#ifOS_EVENT_NAME_EN >0upevent->OSEventName =(INT8U *)(void*)"?";#endifpevent->OSEventType = OS_EVENT_TYPE_UNUSED;pevent->OSEventPtr = OSEventFreeList;/* Return Event Control Block to free list */pevent->OSEventCnt =0u;OSEventFreeList = pevent;/* Get next free event control block */OS_EXIT_CRITICAL();if(tasks_waiting == OS_TRUE)/* Reschedule only if task(s) were waiting */{OS_Sched();/* Find highest priority task ready to run */}*perr = OS_ERR_NONE;pevent_return =(OS_EVENT *)0;/* Semaphore has been deleted */break;default:OS_EXIT_CRITICAL();*perr = OS_ERR_INVALID_OPT;pevent_return = pevent;break;}return(pevent_return);}#endif
获取/等待信号量
voidOSSemPend(OS_EVENT *pevent,INT32U timeout,INT8U *perr){#ifOS_CRITICAL_METHOD ==3u/* Allocate storage for CPU status register */OS_CPU_SR cpu_sr =0u;#endif#ifdefOS_SAFETY_CRITICALif(perr ==(INT8U *)0){OS_SAFETY_CRITICAL_EXCEPTION();return;}#endif#ifOS_ARG_CHK_EN >0uif(pevent ==(OS_EVENT *)0)/* Validate 'pevent' */{*perr = OS_ERR_PEVENT_NULL;return;}#endifif(pevent->OSEventType != OS_EVENT_TYPE_SEM)/* Validate event block type */{*perr = OS_ERR_EVENT_TYPE;return;}if(OSIntNesting >0u)/* See if called from ISR ... */{*perr = OS_ERR_PEND_ISR;/* ... can't PEND from an ISR */return;}if(OSLockNesting >0u)/* See if called with scheduler locked ... */{*perr = OS_ERR_PEND_LOCKED;/* ... can't PEND when locked */return;}OS_ENTER_CRITICAL();if(pevent->OSEventCnt >0u)/* If sem. is positive, resource available ... */{pevent->OSEventCnt--;/* ... decrement semaphore only if positive. */OS_EXIT_CRITICAL();*perr = OS_ERR_NONE;return;}/* Otherwise, must wait until event occurs */OSTCBCur->OSTCBStat |= OS_STAT_SEM;/* Resource not available, pend on semaphore */OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;OSTCBCur->OSTCBDly = timeout;/* Store pend timeout in TCB */OS_EventTaskWait(pevent);/* Suspend task until event or timeout occurs */OS_EXIT_CRITICAL();OS_Sched();/* Find next highest priority task ready */OS_ENTER_CRITICAL();switch(OSTCBCur->OSTCBStatPend)/* See if we timed-out or aborted */{case OS_STAT_PEND_OK:*perr = OS_ERR_NONE;break;case OS_STAT_PEND_ABORT:*perr = OS_ERR_PEND_ABORT;/* Indicate that we aborted */break;case OS_STAT_PEND_TO:default:OS_EventTaskRemove(OSTCBCur, pevent);*perr = OS_ERR_TIMEOUT;/* Indicate that we didn't get event within TO */break;}OSTCBCur->OSTCBStat = OS_STAT_RDY;/* Set task status to ready */OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;/* Clear pend status */OSTCBCur->OSTCBEventPtr =(OS_EVENT *)0;/* Clear event pointers */#if(OS_EVENT_MULTI_EN >0u)OSTCBCur->OSTCBEventMultiPtr =(OS_EVENT **)0;#endifOS_EXIT_CRITICAL();}
发出信号量
INT8U OSSemPost(OS_EVENT *pevent){#ifOS_CRITICAL_METHOD ==3u/* Allocate storage for CPU status register */OS_CPU_SR cpu_sr =0u;#endif#ifOS_ARG_CHK_EN >0uif(pevent ==(OS_EVENT *)0)/* Validate 'pevent' */{return(OS_ERR_PEVENT_NULL);}#endifif(pevent->OSEventType != OS_EVENT_TYPE_SEM)/* Validate event block type */{return(OS_ERR_EVENT_TYPE);}OS_ENTER_CRITICAL();if(pevent->OSEventGrp !=0u)/* See if any task waiting for semaphore */{/* Ready HPT waiting on event */(void)OS_EventTaskRdy(pevent,(void*)0, OS_STAT_SEM, OS_STAT_PEND_OK);OS_EXIT_CRITICAL();OS_Sched();/* Find HPT ready to run */return(OS_ERR_NONE);}if(pevent->OSEventCnt <65535u)/* Make sure semaphore will not overflow */{pevent->OSEventCnt++;/* Increment semaphore count to register event */OS_EXIT_CRITICAL();return(OS_ERR_NONE);}OS_EXIT_CRITICAL();/* Semaphore value has reached its maximum */return(OS_ERR_SEM_OVF);}