//等待事件标志组的事件标志位(事件组指针、需要检查的标志位、等待事件标志位的方式、允许等待 //的时钟节拍、出错代码的时钟节拍) OS_FLAGS OSFlagPend (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *err) { #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr; #endif OS_FLAG_NODE node; OS_FLAGS flags_cur; OS_FLAGS flags_rdy; BOOLEAN consume; if (OSIntNesting > 0) { /* See if called from ISR ... */ *err = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */ return ((OS_FLAGS)0); } #if OS_ARG_CHK_EN > 0 if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */ *err = OS_FLAG_INVALID_PGRP; return ((OS_FLAGS)0); } if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type */ *err = OS_ERR_EVENT_TYPE; return ((OS_FLAGS)0); } #endif if (wait_type & OS_FLAG_CONSUME) { /* See if we need to consume the flags */ wait_type &= ~OS_FLAG_CONSUME; consume = TRUE; } else { consume = FALSE; } /*$PAGE*/ OS_ENTER_CRITICAL(); switch (wait_type) { case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */ flags_rdy = pgrp->OSFlagFlags & flags; /* Extract only the bits we want */ if (flags_rdy == flags) { /* Must match ALL the bits that we want */ if (consume == TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags &= ~flags_rdy; /* Clear ONLY the flags that we wanted */ } flags_cur = pgrp->OSFlagFlags; /* Will return the state of the group */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *err = OS_NO_ERR; return (flags_cur); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; case OS_FLAG_WAIT_SET_ANY: flags_rdy = pgrp->OSFlagFlags & flags; /* Extract only the bits we want */ if (flags_rdy != (OS_FLAGS)0) { /* See if any flag set */ if (consume == TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags &= ~flags_rdy; /* Clear ONLY the flags that we got */ } flags_cur = pgrp->OSFlagFlags; /* Will return the state of the group */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *err = OS_NO_ERR; return (flags_cur); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; #if OS_FLAG_WAIT_CLR_EN > 0 case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */ flags_rdy = ~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */ if (flags_rdy == flags) { /* Must match ALL the bits that we want */ if (consume == TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we wanted */ } flags_cur = pgrp->OSFlagFlags; /* Will return the state of the group */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *err = OS_NO_ERR; return (flags_cur); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; case OS_FLAG_WAIT_CLR_ANY: flags_rdy = ~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */ if (flags_rdy != (OS_FLAGS)0) { /* See if any flag cleared */ if (consume == TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we got */ } flags_cur = pgrp->OSFlagFlags; /* Will return the state of the group */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *err = OS_NO_ERR; return (flags_cur); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; #endif default: OS_EXIT_CRITICAL(); flags_cur = (OS_FLAGS)0; *err = OS_FLAG_ERR_WAIT_TYPE; return (flags_cur); } OS_Sched(); /* Find next HPT ready to run */ OS_ENTER_CRITICAL(); if (OSTCBCur->OSTCBStat & OS_STAT_FLAG) { /* Have we timed-out? */ OS_FlagUnlink(&node); OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Yes, make task ready-to-run */ OS_EXIT_CRITICAL(); flags_cur = (OS_FLAGS)0; *err = OS_TIMEOUT; /* Indicate that we timed-out waiting */ } else { if (consume == TRUE) { /* See if we need to consume the flags */ switch (wait_type) { case OS_FLAG_WAIT_SET_ALL: case OS_FLAG_WAIT_SET_ANY: /* Clear ONLY the flags we got */ pgrp->OSFlagFlags &= ~OSTCBCur->OSTCBFlagsRdy; break; #if OS_FLAG_WAIT_CLR_EN > 0 case OS_FLAG_WAIT_CLR_ALL: case OS_FLAG_WAIT_CLR_ANY: /* Set ONLY the flags we got */ pgrp->OSFlagFlags |= OSTCBCur->OSTCBFlagsRdy; break; #endif } } flags_cur = pgrp->OSFlagFlags; OS_EXIT_CRITICAL(); *err = OS_NO_ERR; /* Event(s) must have occurred */ } return (flags_cur); }
OS_FLAGS OSFlagPend(OS_FLAG_GRP * pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U * perr) { OS_FLAG_NODE node; OS_FLAGS flags_rdy; INT8U result; INT8U pend_stat; BOOLEAN consume; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0; #endif #if OS_ARG_CHK_EN > 0 if (perr == (INT8U *) 0) { /* Validate 'perr' */ return ((OS_FLAGS) 0); } if (pgrp == (OS_FLAG_GRP *) 0) { /* Validate 'pgrp' */ *perr = OS_ERR_FLAG_INVALID_PGRP; return ((OS_FLAGS) 0); } #endif if (OSIntNesting > 0) { /* See if called from ISR ... */ *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */ return ((OS_FLAGS) 0); } if (OSLockNesting > 0) { /* See if called with scheduler locked ... */ *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */ return ((OS_FLAGS) 0); } if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type */ *perr = OS_ERR_EVENT_TYPE; return ((OS_FLAGS) 0); } result = (INT8U) (wait_type & OS_FLAG_CONSUME); if (result != (INT8U) 0) { /* See if we need to consume the flags */ wait_type &= ~(INT8U) OS_FLAG_CONSUME; consume = OS_TRUE; } else { consume = OS_FALSE; } /*$PAGE*/ OS_ENTER_CRITICAL(); switch (wait_type) { case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */ flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & flags); /* Extract only the bits we want */ if (flags_rdy == flags) { /* Must match ALL the bits that we want */ if (consume == OS_TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags &= ~flags_rdy; /* Clear ONLY the flags that we wanted */ } OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *perr = OS_ERR_NONE; return (flags_rdy); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; case OS_FLAG_WAIT_SET_ANY: flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & flags); /* Extract only the bits we want */ if (flags_rdy != (OS_FLAGS) 0) { /* See if any flag set */ if (consume == OS_TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags &= ~flags_rdy; /* Clear ONLY the flags that we got */ } OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *perr = OS_ERR_NONE; return (flags_rdy); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; #if OS_FLAG_WAIT_CLR_EN > 0 case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */ flags_rdy = (OS_FLAGS) (~pgrp->OSFlagFlags & flags); /* Extract only the bits we want */ if (flags_rdy == flags) { /* Must match ALL the bits that we want */ if (consume == OS_TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we wanted */ } OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *perr = OS_ERR_NONE; return (flags_rdy); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; case OS_FLAG_WAIT_CLR_ANY: flags_rdy = (OS_FLAGS) (~pgrp->OSFlagFlags & flags); /* Extract only the bits we want */ if (flags_rdy != (OS_FLAGS) 0) { /* See if any flag cleared */ if (consume == OS_TRUE) { /* See if we need to consume the flags */ pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we got */ } OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */ OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */ *perr = OS_ERR_NONE; return (flags_rdy); } else { /* Block task until events occur or timeout */ OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); OS_EXIT_CRITICAL(); } break; #endif default: OS_EXIT_CRITICAL(); flags_rdy = (OS_FLAGS) 0; *perr = OS_ERR_FLAG_WAIT_TYPE; return (flags_rdy); } /*$PAGE*/ OS_Sched(); /* Find next HPT ready to run */ OS_ENTER_CRITICAL(); if (OSTCBCur->OSTCBStatPend != OS_STAT_PEND_OK) { /* Have we timed-out or aborted? */ pend_stat = OSTCBCur->OSTCBStatPend; OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; OS_FlagUnlink(&node); OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Yes, make task ready-to-run */ OS_EXIT_CRITICAL(); flags_rdy = (OS_FLAGS) 0; switch (pend_stat) { case OS_STAT_PEND_ABORT: *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted waiting */ break; case OS_STAT_PEND_TO: default: *perr = OS_ERR_TIMEOUT; /* Indicate that we timed-out waiting */ break; } return (flags_rdy); } flags_rdy = OSTCBCur->OSTCBFlagsRdy; if (consume == OS_TRUE) { /* See if we need to consume the flags */ switch (wait_type) { case OS_FLAG_WAIT_SET_ALL: case OS_FLAG_WAIT_SET_ANY: /* Clear ONLY the flags we got */ pgrp->OSFlagFlags &= ~flags_rdy; break; #if OS_FLAG_WAIT_CLR_EN > 0 case OS_FLAG_WAIT_CLR_ALL: case OS_FLAG_WAIT_CLR_ANY: /* Set ONLY the flags we got */ pgrp->OSFlagFlags |= flags_rdy; break; #endif default: OS_EXIT_CRITICAL(); *perr = OS_ERR_FLAG_WAIT_TYPE; return ((OS_FLAGS) 0); } } OS_EXIT_CRITICAL(); *perr = OS_ERR_NONE; /* Event(s) must have occurred */ return (flags_rdy); }