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

📄 rzkreceivefromqueue.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			: RZKReceiveFromQueue.c

* Description	: This file contains the RZKReceiveFromQueue function which is
*					used to receive a message from MQ
* 
* 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 pCurrentThread  ((RZK_TCB_t *) hCurrentThread)
#define pMessageQueue  ((RZK_MESSAGEQUEUE_t *) hMessageQueue)

/** extern functions */

extern void* RZKMemcpy(void *, const void *, RZK_LENGTH_t); // IAR port, UINT changed to RZK_LENGTH_t
//extern void RZKScheduler();
//extern void UpdateThreadStatus( RZK_TCB_t * hThread, RZK_STATUS_t errNum); // IAR changed from UINT to RZK_STATUS_t
extern void RemoveFromResourceQueue(RZK_HANDLE_t hControlBlock, RZK_THREADHANDLE_t hThreadToRemove);
extern void AppendToResourceQueue(RZK_HANDLE_t hObject,RZK_THREADHANDLE_t hThread);
extern void WaitOnResourceQueue(RZK_HANDLE_t hControlBlock,UINT16 uBlockingReason,TICK_t tBlockTime);

/** extern variables */
extern RZK_THREADHANDLE_t hCurrentThread;
extern RZK_DISPATCHQ_t DispatchQueue[];


/*
* Function		:	RZKReceiveFromQueue
*
* Description	:	This Function receives a message from 
*					the front of message queue. If the message 
*					is not available the thread does a timed / infinite 
*					wait until it receives a message.The message is 
*					received depending on the receiving attribute of
*					the message control block.(FIFO or PRIORITY).
*  
* Inputs		:	hMessageQueue - Specifies a handle to the message queue.  
*					pMessage -  Specifies an address of a buffer for the message to be filled in.
*					*uSize - Pointer to the length in bytes of message to be received and after 
*					the API is executed it contains the number of bytes actually received.
*					tBlockTime - Specifies the maximum time to wait if message is not  available.
*
* Outputs		:	RZKERR_SUCCESS - If the receive operation completed successfully.
*					RZKERR_INVALID_ARGUMENTS - If anyone of the specified arguments is invalid.         
*					RZKERR_CB_BUSY - If the message control block is being exclusively used by another thread.
*					RZKERR_OBJECT_TIMEOUT - If the pending time expires.
*
* Dependencies	:	All externs
*/

RZK_STATUS_t   RZKReceiveFromQueue
						 ( 
							  RZK_MESSAGEQHANDLE_t hMessageQueue,
							  RZK_PTR_t pMessage,
							  COUNT_t *uSize,
							  TICK_t tBlockTime
						 )

{
	/* Local Variable */
	// IAR change
//	RZK_TCB_t * pInsertionPos;
//	COUNT_t uSizeActual[sizeof(COUNT_t)];
	COUNT_t uSizeActual ;
	RZK_STATE_t uState;

	/* Check the validity of 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;

		if ( uSize == NULL)
			return RZKERR_INVALID_ARGUMENTS;
#endif	/* End of validity */

#ifndef RZK_PERFORMANCE
	if (!(pMessageQueue->uState & OBJECT_CREATED))
		return RZKERR_CB_BUSY;
#endif
#endif
	/* If message queue is not empty copy the first message from the queue */
	uState = RZKDisablePreemption();

	if ( pMessageQueue->uMessageSpaceLeft != pMessageQueue->uQueueLength)
	{
		// IAR change
		uSizeActual = (COUNT_t) *((COUNT_t *)(pMessageQueue ->pRead)) ;
//		RZKMemcpy (uSizeActual, pMessageQueue ->pRead, sizeof(COUNT_t));

		pMessageQueue->pRead = (CADDR_t)pMessageQueue->pRead + sizeof(COUNT_t);	
		
		/* If the requested size of message is less than the actual size of message in the Queue
		return uSize number of bytes else return the uSizeActual number oif bytes*/
		// IAR optimization opportunity
		if(*uSize > uSizeActual)
			*uSize = uSizeActual;
		RZKMemcpy(pMessage,pMessageQueue ->pRead ,*uSize);
/*
		if(*uSize < *uSizeActual)
		{

			RZKMemcpy(pMessage,pMessageQueue ->pRead ,*uSize);

		}
		else
		{

			RZKMemcpy(pMessage,pMessageQueue ->pRead ,uSizeActual); // IAR changed
			*uSize = *uSizeActual;
		}
*/
	/* increment pRead by maximum message size. if pRead has reached pEnd, 
		make it point to pStart else return pRead */

		pMessageQueue->pRead = (CADDR_t)pMessageQueue->pRead + pMessageQueue->uTotalMessageSize;
		if (pMessageQueue->pRead == pMessageQueue->pEnd)
			pMessageQueue->pRead = pMessageQueue->pStart;
		pMessageQueue->uMessageSpaceLeft ++;

		/* if there is a queue of threads blocking to send wake
		the first thread in the queue */
		
		if(pMessageQueue -> etWaitingQueue ==  MESSAGE_SEND)
		{	
			COUNT_t size = pMessageQueue->pResNext->uMessageSize ;

			/* This will update both the read and write pointers */
						
			// IAR change
			*(COUNT_t *)(pMessageQueue->pWrite) = size ;
//			RZKMemcpy(pMessageQueue->pWrite, &uSize, sizeof(COUNT_t));	

			pMessageQueue->pWrite = (CADDR_t)pMessageQueue->pWrite + sizeof(COUNT_t); 
			RZKMemcpy(pMessageQueue->pWrite,pMessageQueue->pResNext->pMessage,size);	
			pMessageQueue->uMessageSpaceLeft --;
			pMessageQueue->pWrite = (CADDR_t)pMessageQueue->pWrite + pMessageQueue->uTotalMessageSize;
			if(pMessageQueue->pWrite == pMessageQueue->pEnd)
				pMessageQueue->pWrite = pMessageQueue->pStart;
			
			UpdateThreadStatus(pMessageQueue -> pResNext, RZKERR_SUCCESS);
			RemoveFromResourceQueue(pMessageQueue,pMessageQueue -> pResNext);
			if(pMessageQueue -> pResNext == NULL)
			{
				pMessageQueue -> etWaitingQueue = MESSAGE_NONE;
			}
		}
					
		/* If there are no threads in the queue,make etWaitingQueue to MESSAGE_NONE */
		
		RZKRestorePreemption(uState);
		pCurrentThread -> errNum = RZKERR_SUCCESS;
		return RZKERR_SUCCESS;
	}	/* End of if statement for queue not empty */
	else
	{ 
		/* if Message Queue is empty and blocking time is 0, then return immediately */
		if(tBlockTime == 0)
		{
			RZKRestorePreemption(uState);
			pCurrentThread->errNum = RZKERR_TIMEOUT;
			return RZKERR_TIMEOUT;
		}
		/* Make pMessage of the thread control block to point to pMessage */
		pCurrentThread->pMessage = (CADDR_t)pMessage;//changed
		pCurrentThread->uMessageSize = *uSize;

		AppendToResourceQueue(pMessageQueue,hCurrentThread);		
		pMessageQueue -> etWaitingQueue = MESSAGE_RECEIVE;
		
		WaitOnResourceQueue(pMessageQueue, BLOCK_PENDMESSAGEQUEUE, tBlockTime);
				
		/* Reset the running bit in the TCB */
		pCurrentThread -> uState &= ~(THREAD_RUNNING);
		
		/* Call scheduler */
		RZKRestorePreemption(uState);
		*uSize = pCurrentThread->uMessageSize;

		return pCurrentThread -> errNum;
	}
} /* End of RZKReceiveFromQueue */

⌨️ 快捷键说明

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