μC/OS-II---事件标志组管理2(os_flag.c)

目录

事件标志组标志置1或清0

cpp 复制代码
OS_FLAGS  OSFlagPost (OS_FLAG_GRP  *pgrp,
											OS_FLAGS      flags,
											INT8U         opt,
											INT8U        *perr)
{
	OS_FLAG_NODE *pnode;
	BOOLEAN       sched;
	OS_FLAGS      flags_cur;
	OS_FLAGS      flags_rdy;
	BOOLEAN       rdy;
#if OS_CRITICAL_METHOD == 3u                         /* Allocate storage for CPU status register       */
	OS_CPU_SR     cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                    /* Validate 'pgrp'                                */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)      /* Make sure we are pointing to an event flag grp */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((OS_FLAGS)0);
	}
	
	/*$PAGE*/
	OS_ENTER_CRITICAL();
	
	switch (opt)
	{
		case OS_FLAG_CLR:
			pgrp->OSFlagFlags &= (OS_FLAGS)~flags;  /* Clear the flags specified in the group         */
			break;
			
		case OS_FLAG_SET:
			pgrp->OSFlagFlags |=  flags;            /* Set   the flags specified in the group         */
			break;
			
		default:
			OS_EXIT_CRITICAL();                     /* INVALID option                                 */
			*perr = OS_ERR_FLAG_INVALID_OPT;
			return ((OS_FLAGS)0);
	}
	
	sched = OS_FALSE;                                /* Indicate that we don't need rescheduling       */
	pnode = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
	
	while (pnode != (OS_FLAG_NODE *)0)               /* Go through all tasks waiting on event flag(s)  */
	{
		switch (pnode->OSFlagNodeWaitType)
		{
			case OS_FLAG_WAIT_SET_ALL:               /* See if all req. flags are set for current node */
				flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
				
				if (flags_rdy == pnode->OSFlagNodeFlags)     /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
				
			case OS_FLAG_WAIT_SET_ANY:               /* See if any flag set                            */
				flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
				
				if (flags_rdy != (OS_FLAGS)0)                /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
#if OS_FLAG_WAIT_CLR_EN > 0u
				
			case OS_FLAG_WAIT_CLR_ALL:               /* See if all req. flags are set for current node */
				flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
				
				if (flags_rdy == pnode->OSFlagNodeFlags)     /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
				
			case OS_FLAG_WAIT_CLR_ANY:               /* See if any flag set                            */
				flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
				
				if (flags_rdy != (OS_FLAGS)0)                /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
#endif
				
			default:
				OS_EXIT_CRITICAL();
				*perr = OS_ERR_FLAG_WAIT_TYPE;
				return ((OS_FLAGS)0);
		}
		
		pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext; /* Point to next task waiting for event flag(s) */
	}
	
	OS_EXIT_CRITICAL();
	
	if (sched == OS_TRUE)
	{
		OS_Sched();
	}
	
	OS_ENTER_CRITICAL();
	flags_cur = pgrp->OSFlagFlags;
	OS_EXIT_CRITICAL();
	*perr     = OS_ERR_NONE;
	return (flags_cur);
}

事件标志组获取/无等待

cpp 复制代码
#if OS_FLAG_ACCEPT_EN > 0u
OS_FLAGS  OSFlagAccept (OS_FLAG_GRP  *pgrp,
												OS_FLAGS      flags,
												INT8U         wait_type,
												INT8U        *perr)
{
	OS_FLAGS      flags_rdy;
	INT8U         result;
	BOOLEAN       consume;
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */
	OS_CPU_SR     cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                          /* Validate 'pgrp'                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	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 &= ~OS_FLAG_CONSUME;
		consume    = OS_TRUE;
	}
	
	else
	{
		consume    = OS_FALSE;
	}
	
	/*$PAGE*/
	*perr = OS_ERR_NONE;                                   /* Assume NO error until proven otherwise.  */
	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 &= (OS_FLAGS)~flags_rdy;     /* Clear ONLY the flags we wanted  */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			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 &= (OS_FLAGS)~flags_rdy;     /* Clear ONLY the flags we got     */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
#if OS_FLAG_WAIT_CLR_EN > 0u
			
		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        */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			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           */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
#endif
			
		default:
			OS_EXIT_CRITICAL();
			flags_rdy = (OS_FLAGS)0;
			*perr     = OS_ERR_FLAG_WAIT_TYPE;
			break;
	}
	
	return (flags_rdy);
}
#endif

事件标志组状态查询

cpp 复制代码
#if OS_FLAG_QUERY_EN > 0u
OS_FLAGS  OSFlagQuery (OS_FLAG_GRP  *pgrp,
											 INT8U        *perr)
{
	OS_FLAGS   flags;
#if OS_CRITICAL_METHOD == 3u                      /* Allocate storage for CPU status register          */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                 /* Validate 'pgrp'                                   */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)   /* Validate event block type                         */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((OS_FLAGS)0);
	}
	
	OS_ENTER_CRITICAL();
	flags = pgrp->OSFlagFlags;
	OS_EXIT_CRITICAL();
	*perr = OS_ERR_NONE;
	return (flags);                               /* Return the current value of the event flags       */
}
#endif

设置事件标志组命名

cpp 复制代码
#if OS_FLAG_NAME_EN > 0u
void  OSFlagNameSet (OS_FLAG_GRP  *pgrp,
										 INT8U        *pname,
										 INT8U        *perr)
{
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return;
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                /* Is 'pgrp' a NULL pointer?                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return;
	}
	
	if (pname == (INT8U *)0)                     /* Is 'pname' a NULL pointer?                         */
	{
		*perr = OS_ERR_PNAME_NULL;
		return;
	}
	
#endif
	
	if (OSIntNesting > 0u)                       /* See if trying to call from an ISR                  */
	{
		*perr = OS_ERR_NAME_SET_ISR;
		return;
	}
	
	OS_ENTER_CRITICAL();
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)
	{
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_EVENT_TYPE;
		return;
	}
	
	pgrp->OSFlagName = pname;
	OS_EXIT_CRITICAL();
	*perr            = OS_ERR_NONE;
	return;
}
#endif

获取事件标志组命名

cpp 复制代码
#if OS_FLAG_NAME_EN > 0u
INT8U  OSFlagNameGet (OS_FLAG_GRP   *pgrp,
											INT8U        **pname,
											INT8U         *perr)
{
	INT8U      len;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                /* Is 'pgrp' a NULL pointer?                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return (0u);
	}
	
	if (pname == (INT8U **)0)                     /* Is 'pname' a NULL pointer?                         */
	{
		*perr = OS_ERR_PNAME_NULL;
		return (0u);
	}
	
#endif
	
	if (OSIntNesting > 0u)                       /* See if trying to call from an ISR                  */
	{
		*perr = OS_ERR_NAME_GET_ISR;
		return (0u);
	}
	
	OS_ENTER_CRITICAL();
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)
	{
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_EVENT_TYPE;
		return (0u);
	}
	
	*pname = pgrp->OSFlagName;
	len    = OS_StrLen (*pname);
	OS_EXIT_CRITICAL();
	*perr  = OS_ERR_NONE;
	return (len);
}
#endif
相关推荐
小林熬夜学编程2 分钟前
【Linux系统编程】第五十弹---构建高效单例模式线程池、详解线程安全与可重入性、解析死锁与避免策略,以及STL与智能指针的线程安全性探究
linux·运维·服务器·c语言·c++·安全·单例模式
我qq不是4515165210 分钟前
C语言指针作业
c语言
苏言の狗11 分钟前
小R的二叉树探险 | 模拟
c语言·数据结构·算法·宽度优先
加载中loading...12 分钟前
C/C++实现tcp客户端和服务端的实现(从零开始写自己的高性能服务器)
linux·运维·服务器·c语言·网络
Heisenberg~2 小时前
详解八大排序(五)------(计数排序,时间复杂度)
c语言·数据结构·排序算法
lb36363636365 小时前
分享一下arr的意义(c基础)(必看)(牢记)
c语言·知识点
南东山人7 小时前
一文说清:C和C++混合编程
c语言·c++
stm 学习ing8 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
茶猫_12 小时前
力扣面试题 - 25 二进制数转字符串
c语言·算法·leetcode·职场和发展
ö Constancy13 小时前
Linux 使用gdb调试core文件
linux·c语言·vim