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

📄 rzksendtoqueue.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKSendToQueue.c
*
* Description	:	This file contains the RZKSendToQueue function 
*					which sends a message to the 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 pMessageQueue  ((RZK_MESSAGEQUEUE_t *) hMessageQueue)
#define pCurrentThread  ((RZK_TCB_t *) hCurrentThread)

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

/** extern variables */
extern RZK_THREADHANDLE_t hCurrentThread;


/*
* Function		:	RZKSendToQueue
*
* Description	:	This function does a timed blocking send to a queue specified by a
*					valid handle. Blocking of the thread happens when the queue can 
*					not store any more messages, since the maximum length specified
*					during queue creation has been reached. The message is appended
*					to the tail of queue.
* 
* Inputs		:	hMessageQueue  -  handle to the message queue 
*					pMessage - Pointer to the message to send to queue.
*					uSize - Size of the message.
*					tBlockTime - Time for which the thread will block in this queue, 
*																				if it can not send the message immediately.	
*
* Outputs		:	RZKERR_SUCCESS - If the message is successfully copied.
*					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 pMessageQueue  parameters is invalid
*
* Dependencies	:	hCurrentThread
*/

RZK_STATUS_t RZKSendToQueue 
					(
						RZK_MESSAGEQHANDLE_t  hMessageQueue,
						RZK_PTR_t  pMessage,
						COUNT_t uSize, 
						TICK_t tBlockTime
					)
{
	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 ((( pMessageQueue->uTotalMessageSize) <  uSize ) || ( pMessage == NULL))
		return RZKERR_INVALID_ARGUMENTS;

#endif

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

	uState = RZKDisablePreemption();

	/* check whether there are any threads waiting to receive the message */
	pCurrentThread -> errNum = RZKERR_SUCCESS;
	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 change
			pMessageQueue->pResNext->uMessageSize = uSize ;
//			RZKMemcpy(&pMessageQueue->pResNext->uMessageSize,&uSize,sizeof(COUNT_t));
			RZKMemcpy(pMessageQueue->pResNext->pMessage, pMessage,uSize );

		}
		else
		{

			RZKMemcpy(pMessageQueue->pResNext->pMessage, pMessage,
				pMessageQueue->pResNext->uMessageSize);

		}
*/
		/* Update the therad status */
		UpdateThreadStatus(pMessageQueue->pResNext, RZKERR_SUCCESS);

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

		if(pMessageQueue -> pResNext == NULL )
			pMessageQueue -> etWaitingQueue = MESSAGE_NONE;

		RZKRestorePreemption(uState);
		return RZKERR_SUCCESS;
	}

	/* check whether the queue is full and tBlockTime parameter is 0.If so return RZKERR_QUEUE_FULL error */

	if(pMessageQueue ->uMessageSpaceLeft == 0) 
	{
		if(tBlockTime == 0)
		{
			RZKRestorePreemption(uState);
			pCurrentThread->errNum = RZKERR_TIMEOUT;
			return RZKERR_TIMEOUT;
		}		
		else
		{
			pCurrentThread->pMessage = pMessage;
			pCurrentThread->uMessageSize = uSize;
			
			(pMessageQueue->pResNext == NULL) ? AddToResourceQueueBeg(pMessageQueue,hCurrentThread)
							: AddToResourceQueue(pMessageQueue->pLastPosition,hCurrentThread);
			pMessageQueue->pLastPosition = pCurrentThread;


			WaitOnResourceQueue(pMessageQueue,
				BLOCK_POSTMESSAGEQUEUE,tBlockTime);
					
			/* set the waiting queue type */
			pMessageQueue->etWaitingQueue = MESSAGE_SEND;

			/* set the errNum in the TCB to success */
			pCurrentThread -> errNum = RZKERR_SUCCESS;

			/* Reset the running bit in the TCB */
			pCurrentThread -> uState &= ~(THREAD_RUNNING);

			/* check if the message queue control block is still valid(either it is not reset or deleted) */
			RZKRestorePreemption(uState);
			return pCurrentThread->errNum;
		}
	} /* end of if condition for queue full*/

	/* Copy the message */
	//IAR change
	*(COUNT_t *) (pMessageQueue->pWrite) = uSize ;	
//	RZKMemcpy(pMessageQueue->pWrite, &uSize, sizeof(COUNT_t)); 
	pMessageQueue->pWrite = (CADDR_t)pMessageQueue->pWrite + sizeof(COUNT_t); 
	RZKMemcpy(pMessageQueue->pWrite,pMessage,uSize);
	pMessageQueue->uMessageSpaceLeft --;
	pMessageQueue->pWrite = (CADDR_t)pMessageQueue->pWrite + pMessageQueue->uTotalMessageSize;
	if( pMessageQueue->pWrite == pMessageQueue->pEnd)
		pMessageQueue->pWrite = pMessageQueue->pStart;

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

⌨️ 快捷键说明

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