⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rzksendtoqueuefront.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKSendToQueueFront.c
*
* Description	:	This fiel contains RZKSendToQueueFront function that 
*					sends the message to the Queue front
* 
* Copyright 2004 ZiLOG Inc.  ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED 
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's 
* sole discretion 
*/


#include <string.h>	
#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZQueue.h" 
#include "ZInterrupt.h"
#include "ZMessageQ.h"
#include "ZScheduler.h"

#define pMessageQueue  ((RZK_MESSAGEQUEUE_t *) hMessageQueue)

/** extern functions */
//extern void RZKScheduler();
//extern void UpdateThreadStatus( RZK_TCB_t * hThread,UINT errNum);
extern void RemoveFromResourceQueue(RZK_HANDLE_t hControlBlock, RZK_THREADHANDLE_t hThreadToRemove);
extern void* RZKMemcpy(void *, const void *, RZK_LENGTH_t); // IAR port, UINT changed to RZK_LENGTH_t
extern RZK_THREADHANDLE_t hCurrentThread;

/*
* Function		:	RZKSendToQueueFront
*
* Description	:	This function sends the message to the front position in the queue. 
*					If the queue is full it returns the error. If there is a queue of threads
*					waiting to receive the message, this function directly copies the 
*					message to the first thread that is waiting to receive the message.
* 
* Inputs		:	hMessageQueue  -  handle to the message queue.
*					pMessage - Pointer to the message to send.
*					uSize - Size of the message to send.
*	
*
* Outputs		:	RZKERR_SUCCESS - If the message is sent to the front position of the queue.
*					RZKERR_CB_BUSY- If the message control block is being exclusively used by another thread.
*					RZKERR_INVALID_HANDLE - If the hMessageQueue parameter is invalid.
*					RZKERR_INVALID_ARGUMENTS - If pMessage parameter is invalid or 
*									uSize is greater than maximum message size of the queue.
*					RZKERR_QUEUE_FULL - If the queue is full.
*
* Dependencies	:	None
*/

RZK_STATUS_t RZKSendToQueueFront
					(
						RZK_MESSAGEQHANDLE_t  hMessageQueue,
						RZK_PTR_t  pMessage,
						COUNT_t uSize
					)
{
	RZK_STATE_t uState;
	/* Local variable */
	/* Check for the validity of the arguments */
#ifndef RZK_EFFICIENTQUEUE
#ifdef RZK_DEBUG
	if ((pMessageQueue==NULL) || ( pMessageQueue->uState == 0 ) ||
			( pMessageQueue->magic_num != MAGIC_NUM_MESSAGEQ))
		return RZKERR_INVALID_HANDLE;
	if	(( pMessage == NULL ) || ( uSize > (pMessageQueue -> uTotalMessageSize)))
		return RZKERR_INVALID_ARGUMENTS;
#endif

#ifndef RZK_PERFORMANCE
	if(!( pMessageQueue -> uState & OBJECT_CREATED))
		return RZKERR_CB_BUSY;
#endif
#endif

	if(pMessageQueue->uMessageSpaceLeft ==0 )
		return RZKERR_QUEUE_FULL;

	/* If there are threads waiting to receive the message,copy the message directly to first thread in the queue. */
	uState = RZKDisablePreemption();
	if(pMessageQueue->etWaitingQueue == MESSAGE_RECEIVE)
	{	
		/* Copy the message directly to thread */
		// IAR optimization opportunity
		if(uSize > pMessageQueue ->pResNext ->uMessageSize)
			uSize = pMessageQueue ->pResNext ->uMessageSize ;
		pMessageQueue->pResNext->uMessageSize = uSize ;
		//RZKMemcpy(&pMessageQueue->pResNext->uMessageSize,&uSize,sizeof(COUNT_t));
		RZKMemcpy(pMessageQueue->pResNext->pMessage, pMessage,uSize );
/*
		if(uSize < pMessageQueue ->pResNext ->uMessageSize)
		{
			// IAR port
			pMessageQueue->pResNext->uMessageSize = uSize ;
			//RZKMemcpy(&pMessageQueue->pResNext->uMessageSize,&uSize,sizeof(COUNT_t));
			RZKMemcpy(pMessageQueue->pResNext->pMessage, pMessage,uSize );
		}
		else
		{
			// IAR port.. Q: Are we not updating the size if the given size is more than permitted?
			RZKMemcpy(pMessageQueue->pResNext->pMessage, pMessage,
				pMessageQueue ->pResNext ->uMessageSize);
		}
*/
		/* Update the parameters in the TCB */
		UpdateThreadStatus( pMessageQueue->pResNext, RZKERR_SUCCESS );

		/* Now remove the thread from this resource queue */
		RemoveFromResourceQueue(pMessageQueue, pMessageQueue -> pResNext );

		/* check if the resource queue is empty */
		if(pMessageQueue -> pResNext == NULL )
			pMessageQueue -> etWaitingQueue = MESSAGE_NONE;
	}
	else /* There are no  threads to receive and queue is not full*/
	{
		if ( pMessageQueue->pRead == pMessageQueue->pStart)
			pMessageQueue->pRead = pMessageQueue->pEnd;
		
		/* Move the pRead pointer back by one message unit and copy the message */
		pMessageQueue->pRead  = (CADDR_t)pMessageQueue->pRead - pMessageQueue->uTotalMessageSize - sizeof(COUNT_t);

		// IAR port, optimization can be done still here...
		*((COUNT_t *)(pMessageQueue -> pRead)) = uSize ;
//		RZKMemcpy(pMessageQueue -> pRead , &uSize, sizeof(COUNT_t));

		pMessageQueue -> pRead = (CADDR_t)pMessageQueue -> pRead + sizeof(COUNT_t);
		RZKMemcpy(pMessageQueue -> pRead, pMessage, uSize);	
		pMessageQueue -> pRead = (CADDR_t)pMessageQueue -> pRead - sizeof(COUNT_t);

		/* Update the message left parameter */
		pMessageQueue -> uMessageSpaceLeft--;
	}    /* end of else condition */

	RZKRestorePreemption (uState );
	return RZKERR_SUCCESS;
}   /* end of RZKSendToQueueFront */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -