μC/OS-II---消息队列管理1(os_q.c)

目录

  • 消息队列的主要优点
  • 消息队列和消息邮箱
  • 消息队列相关操作
    • 消息队列创建
    • 消息队列删除
    • 在消息队列等待消息

在这里插入图片描述

消息队列的主要优点

  • 消息队列的主要优点是解耦和异步通信。发送者和接收者之间不需要直接建立连接,它们只需要知道消息队列的名称或标识符即可。发送者将消息发送到队列中后,可以继续执行其他任务,而不需要等待接收者处理消息。接收者可以在合适的时候从队列中获取消息进行处理,这样可以实现异步通信,提高系统的并发性和响应性。
  • 通常会在消息队列中引入消息标识符或者消息类型。发送者在发送消息时,会附带一个标识符或类型,接收者在接收消息时,会根据标识符或类型来判断是否是自己需要处理的消息。这样可以确保每个进程只处理属于自己的消息,避免了拿错信息的问题。
  • 此外,还可以使用互斥锁或其他同步机制来保证在读取和写入消息队列时的原子性操作,避免多个进程同时对消息队列进行读写操作而导致的竞争条件。

消息队列和消息邮箱

  • 消息队列是一个独立的数据结构,多个进程可以共享同一个消息队列,而消息邮箱是与进程关联的,每个进程都有自己的邮箱。此外,消息队列通常是先进先出(FIFO)的,而消息邮箱可以根据实现方式不同具有不同的特性,比如可以是先进先出或者按优先级处理消息。
  • Task也可以设计有“私有”消息队列模式。
  • 总的来说,消息队列更适合实现多个进程之间的解耦和异步通信,而消息邮箱更适合实现单个进程内的消息传递。

消息队列相关操作

消息队列创建

OS_EVENT  *OSQCreate (void    **start,
											INT16U    size)
{
	OS_EVENT  *pevent;
	OS_Q      *pq;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
	
	if (OSSafetyCriticalStartFlag == OS_TRUE)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_EVENT *)0);
	}
	
#endif
	
	if (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)                 /* See if we have an event control block              */
	{
		OS_ENTER_CRITICAL();
		pq = OSQFreeList;                        /* Get a free queue control block                     */
		
		if (pq != (OS_Q *)0)                     /* Were we able to get a queue control block ?        */
		{
			OSQFreeList            = OSQFreeList->OSQPtr; /* Yes, Adjust free list pointer to next free*/
			OS_EXIT_CRITICAL();
			pq->OSQStart           = start;               /*      Initialize the queue                 */
			pq->OSQEnd             = &start[size];
			pq->OSQIn              = start;
			pq->OSQOut             = start;
			pq->OSQSize            = size;
			pq->OSQEntries         = 0u;
			pevent->OSEventType    = OS_EVENT_TYPE_Q;
			pevent->OSEventCnt     = 0u;
			pevent->OSEventPtr     = pq;
#if OS_EVENT_NAME_EN > 0u
			pevent->OSEventName    = (INT8U *) (void *)"?";
#endif
			OS_EventWaitListInit (pevent);                /*      Initialize the wait list             */
		}
		
		else
		{
			pevent->OSEventPtr = (void *)OSEventFreeList; /* No,  Return event control block on error  */
			OSEventFreeList    = pevent;
			OS_EXIT_CRITICAL();
			pevent = (OS_EVENT *)0;
		}
	}
	
	return (pevent);
}

消息队列删除

#if OS_Q_DEL_EN > 0u
OS_EVENT  *OSQDel (OS_EVENT  *pevent,
									 INT8U      opt,
									 INT8U     *perr)
{
	BOOLEAN    tasks_waiting;
	OS_EVENT  *pevent_return;
	OS_Q      *pq;
#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_EVENT *)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                           /* Validate 'pevent'                        */
	{
		*perr = OS_ERR_PEVENT_NULL;
		return (pevent);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_Q)            /* 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 queue        */
	{
		tasks_waiting = OS_TRUE;                           /* Yes                                      */
	}
	
	else
	{
		tasks_waiting = OS_FALSE;                          /* No                                       */
	}
	
	switch (opt)
	{
		case OS_DEL_NO_PEND:                               /* Delete queue only if no task waiting     */
			if (tasks_waiting == OS_FALSE)
			{
#if OS_EVENT_NAME_EN > 0u
				pevent->OSEventName    = (INT8U *) (void *)"?";
#endif
				pq                     = (OS_Q *)pevent->OSEventPtr;  /* Return OS_Q to free list     */
				pq->OSQPtr             = OSQFreeList;
				OSQFreeList            = pq;
				pevent->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;   /* Queue has been deleted                   */
			}
			
			else
			{
				OS_EXIT_CRITICAL();
				*perr                  = OS_ERR_TASK_WAITING;
				pevent_return          = pevent;
			}
			
			break;
			
		case OS_DEL_ALWAYS:                                /* Always delete the queue                  */
			while (pevent->OSEventGrp != 0u)              /* Ready ALL tasks waiting for queue        */
			{
				(void)OS_EventTaskRdy (pevent, (void *)0, OS_STAT_Q, OS_STAT_PEND_ABORT);
			}
			
#if OS_EVENT_NAME_EN > 0u
			pevent->OSEventName    = (INT8U *) (void *)"?";
#endif
			pq                     = (OS_Q *)pevent->OSEventPtr;   /* Return OS_Q to free list        */
			pq->OSQPtr             = OSQFreeList;
			OSQFreeList            = pq;
			pevent->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;       /* Queue has been deleted                   */
			break;
			
		default:
			OS_EXIT_CRITICAL();
			*perr                  = OS_ERR_INVALID_OPT;
			pevent_return          = pevent;
			break;
	}
	
	return (pevent_return);
}
#endif

在消息队列等待消息

void  *OSQPend (OS_EVENT  *pevent,
								INT32U     timeout,
								INT8U     *perr)
{
	void      *pmsg;
	OS_Q      *pq;
#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 ((void *)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                 /* Validate 'pevent'                                  */
	{
		*perr = OS_ERR_PEVENT_NULL;
		return ((void *)0);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_Q)  /* Validate event block type                          */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((void *)0);
	}
	
	if (OSIntNesting > 0u)                       /* See if called from ISR ...                         */
	{
		*perr = OS_ERR_PEND_ISR;                 /* ... can't PEND from an ISR                         */
		return ((void *)0);
	}
	
	if (OSLockNesting > 0u)                      /* See if called with scheduler locked ...            */
	{
		*perr = OS_ERR_PEND_LOCKED;              /* ... can't PEND when locked                         */
		return ((void *)0);
	}
	
	OS_ENTER_CRITICAL();
	pq = (OS_Q *)pevent->OSEventPtr;             /* Point at queue control block                       */
	
	if (pq->OSQEntries > 0u)                     /* See if any messages in the queue                   */
	{
		pmsg = *pq->OSQOut++;                    /* Yes, extract oldest message from the queue         */
		pq->OSQEntries--;                        /* Update the number of entries in the queue          */
		
		if (pq->OSQOut == pq->OSQEnd)            /* Wrap OUT pointer if we are at the end of the queue */
		{
			pq->OSQOut = pq->OSQStart;
		}
		
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_NONE;
		return (pmsg);                           /* Return message received                            */
	}
	
	OSTCBCur->OSTCBStat     |= OS_STAT_Q;        /* Task will have to pend for a message to be posted  */
	OSTCBCur->OSTCBStatPend  = OS_STAT_PEND_OK;
	OSTCBCur->OSTCBDly       = timeout;          /* Load timeout into TCB                              */
	OS_EventTaskWait (pevent);                   /* Suspend task until event or timeout occurs         */
	OS_EXIT_CRITICAL();
	OS_Sched();                                  /* Find next highest priority task ready to run       */
	OS_ENTER_CRITICAL();
	
	switch (OSTCBCur->OSTCBStatPend)                  /* See if we timed-out or aborted                */
	{
		case OS_STAT_PEND_OK:                         /* Extract message from TCB (Put there by QPost) */
			pmsg =  OSTCBCur->OSTCBMsg;
			*perr =  OS_ERR_NONE;
			break;
			
		case OS_STAT_PEND_ABORT:
			pmsg = (void *)0;
			*perr =  OS_ERR_PEND_ABORT;               /* Indicate that we aborted                      */
			break;
			
		case OS_STAT_PEND_TO:
		default:
			OS_EventTaskRemove (OSTCBCur, pevent);
			pmsg = (void *)0;
			*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;
#endif
	OSTCBCur->OSTCBMsg           = (void      *)0;    /* Clear  received message                       */
	OS_EXIT_CRITICAL();
	return (pmsg);                                    /* Return received message                       */
}

你可能感兴趣的:(μC/OS-II学习,c语言,操作系统)