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

📄 setthreadpriority.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKSetThreadPriority.c
*
* Description	:	This file contains the RZKSetThreadPriority function which 
*					is used to change the priority of the thread dynamically.
* 
* 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 "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZQueue.h"
#include "ZScheduler.h"
#include "ZSemaphore.h"
#include "ZInterrupt.h"

#define pThread ((RZK_TCB_t *) hThread)

/** extern functions */
//extern void RZKScheduler();
extern void QueueAppend(RZK_TCB_t *pInsertionPosition ,RZK_TCB_t *pObjectToAppend);
extern void DispatchQueueNodeAppend(RZK_TCB_t *pObjectToAppend);
extern void QueueNodeRemove(RZK_TCB_t *pObjectToRemove );
extern void DispatchQueueNodeRemove(RZK_TCB_t *pObjectToRemove );
extern void RemoveFromResourceQueue(RZK_HANDLE_t hControlBlock, RZK_THREADHANDLE_t hThreadToRemove);
extern void AppendToResourceQueue(RZK_HANDLE_t hObject,RZK_THREADHANDLE_t hThread);
extern void UpdatePriorityRecursive( RZK_HANDLE_t hThread  );


extern RZK_DISPATCHQ_t DispatchQueue[DMAX_PRIORITY_P1];
extern volatile UINT32 DQPriorityBitMap;
extern TICK_t uDefaultTimeSlice;
extern const UINT32 pMap[32];


/*
* Function		:	RZKSetThreadPriority
*
* Description	:	This function changes the priority of the thread dynamically
*					and calls the scheduler to allow highest priority thread to run.
* 
* Inputs		:	hThread - handle to the thread whose priority needs to be changed.
*					threadPriority - New priority value
*
* Outputs		:	RZKERR_SUCCESS				- When the threads priority is changed successfully
					RZKERR_INVALID_HANDLE		- when the handle passed is invalid.
*					RZKERR_INVALID_ARGUMENTS	- when the specified arguments were invalid
*					RZKERR_INVALID_OPERATION	- when the operation is invalid
*
* Dependencies	:	All externs
*/

RZK_STATUS_t RZKSetThreadPriority
(
	RZK_THREADHANDLE_t	hThread,
	RZK_THREAD_PRIORITY_t threadPriority
)
{
	RZK_THREAD_PRIORITY_t uTempPriority,uOriginalPriority;
	UINT CSFlag,was_first_element ;	// changed to UINT from int
	RZK_STATE_t uState;
/* Parameter Validation */	
#ifdef RZK_DEBUG 
	if ((pThread == NULL)||(pThread->uState == 0) || (pThread->magic_num != MAGIC_NUM_THREAD))
		return RZKERR_INVALID_HANDLE;

	if (threadPriority < DMIN_PRIORITY || threadPriority > DMAX_PRIORITY) 
		return RZKERR_INVALID_ARGUMENTS;

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

	/*Set GlobalPRiority*/
//	pThread->cGlobalPriority = threadPriority;
//	uTempPriority = threadPriority >> 2;
	/* From RZK1.1 only dispatch priority concept remains */
	uTempPriority = threadPriority;
	
#ifdef RZK_PRIORITYINHERITANCE
		uOriginalPriority = pThread->uOriginalPriority;
		pThread->uOriginalPriority = uTempPriority;
#endif
	
	uState = RZKDisablePreemption();

	if (uTempPriority != pThread->cDispPriority)
	{

#ifndef RZK_PRIORITYINHERITANCE
		CSFlag = RZK_TRUE;
#endif

#ifdef RZK_PRIORITYINHERITANCE
		CSFlag = ((pThread->cInheritedPriority >= uTempPriority )|| ((pThread->cInheritedPriority < uTempPriority) && (uOriginalPriority < uTempPriority)));
		if (uTempPriority > pThread->cInheritedPriority)
			uTempPriority = pThread->cInheritedPriority;
#endif	

		/*THREAD RUNNING CASE*/
		if((pThread->uState & THREAD_RUNNING)&&(CSFlag))
		{
			UINTRMASK mInterruptMask;
//			pThread->uState &= ~THREAD_RUNNING;
	
			mInterruptMask = RZKDisableInterrupts();
			DispatchQueueNodeRemove(pThread);	
			RZKEnableInterrupts(mInterruptMask);

			pThread->cDispPriority =uTempPriority;
			
			/*Add to the dispatch queue*/
			mInterruptMask = RZKDisableInterrupts();
			DispatchQueueNodeAppend(pThread);
			RZKEnableInterrupts(mInterruptMask);

			pThread->uState |= THREAD_RUNNING;	
		} 
		else
		{
			/*THREAD BLOCKED CASE*/
			if(pThread->uBlockingReason & (BLOCK_PENDMAILBOX |BLOCK_PENDMESSAGEQUEUE | BLOCK_ACQUIRESEMAPHORE ))
			{
				pThread->cDispPriority =uTempPriority;
				was_first_element = (((RZK_SEMAPHORE_t*)pThread->pBlockingResource)->pResNext == pThread);
 				RemoveFromResourceQueue(pThread->pBlockingResource,pThread);
				AppendToResourceQueue(pThread->pBlockingResource,pThread);
		
#ifdef RZK_PRIORITYINHERITANCE	
				/*Check PI Case for Blocked resource*/
				if((((RZK_SEMAPHORE_t*)pThread->pBlockingResource)->uState & OBJECT_PRIORITY_INHERITANCE) &&
					((((RZK_SEMAPHORE_t*)pThread -> pBlockingResource)->pResNext == pThread)||was_first_element))	
				/* updates both the inherited and dispatch priority recursively */
				UpdatePriorityRecursive(pThread);
#endif 
			}
			else
				pThread->cDispPriority = uTempPriority;
		}/* Thread is not RUNNING */
		pThread->errNum = RZKERR_SUCCESS;
	} 
	RZKRestorePreemption(uState);
	return RZKERR_SUCCESS; 
}

⌨️ 快捷键说明

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