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

📄 rzkreleasesemaphore.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKReleaseSemaphore.c
*
* Description	:	This file contains the RZKReleaseSemaphore function that
*					releases a semaphore that had been acquired previously.
* 
* 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 "ZSemaphore.h"
#include "ZScheduler.h"
#include "ZInterrupt.h"

#define pCurrentThread ((RZK_TCB_t *) hCurrentThread)
#define pSemaphore ((RZK_SEMAPHORE_t *) hSemaphore)

/** extern functions */
//extern void RZKScheduler();
extern void RemoveFromOwnerQueue(RZK_HANDLE_t hThread,RZK_SEMAPHORE_t* hSemaphore);
extern void RestorePriorityRecursive(RZK_HANDLE_t hThread);
//extern void UpdateThreadStatus( RZK_TCB_t * hThread,UINT errNum);
extern void AddToOwnerQueue(RZK_HANDLE_t hThread,RZK_SEMAPHORE_t* hSemaphore);
extern void GetInheritedPriority(RZK_HANDLE_t hThread,RZK_HANDLE_t hBlockingThread);

/** extern variables */
extern	RZK_THREADHANDLE_t hCurrentThread;
extern UINT8 uNestedISRCount;   

/*
* Function		:	RZKReleaseSemaphore
*
* Description	:	Will release a previously acquired semaphore, 
*					by incrementing the semaphore count value thus enabling
*					one more thread to acquire it.
* 
* Inputs		:	hSemaphore - Handle to the 
*					semaphore control block which is to be released.
*
* Outputs		:	RZKERR_SUCCESS - If the semaphore was successfully released.
*					RZKERR_INVALID_HANDLE - If the semaphore handle is invalid.
*					RZKERR_INVALID_OPERATION - If the releasing thread is not the		
*					the owner of the binary semaphore.
*
* Dependencies	:	All externs
*/

RZK_STATUS_t RZKReleaseSemaphore
		(
		 RZK_SEMAPHOREHANDLE_t hSemaphore  
		 )

{
	/* Local Variables */
	RZK_TCB_t	*pTemp,*pOwner;
	RZK_STATE_t 	uState;

	
#ifdef RZK_DEBUG
		if( (pSemaphore == NULL)||(pSemaphore -> uState == 0) ||
			(pSemaphore -> magic_num != MAGIC_NUM_SEMAPHORE))
			return RZKERR_INVALID_HANDLE;
		if(pSemaphore -> uDynamicCount == pSemaphore -> uInitialCount)
			return RZKERR_INVALID_OPERATION;
#endif /* RZK_DEBUG */
	
#ifndef RZK_PERFORMANCE
		if(!(pSemaphore ->uState & OBJECT_CREATED ))
			return RZKERR_CB_BUSY;
#endif
		/* If a thread which is not the owner try to releases a 
		semaphore error is thrown(only if all conditions related to PI are satisfied i.e., 
		RECV_ORDER_PRIORITY/binary semaphore/RZK_PRIORITYINHERITANCE Restored) */	 	
		#ifdef RZK_PRIORITYINHERITANCE
        if( (pSemaphore->uState & OBJECT_PRIORITY_INHERITANCE) && 
            (uNestedISRCount != 0) )
        return RZKERR_INVALID_OPERATION;
		if( (pSemaphore->pOwnerThread != pCurrentThread) && 
			(pSemaphore->uState & OBJECT_PRIORITY_INHERITANCE) )
			return RZKERR_SEM_NOTOWNED;
		#endif

		uState = RZKDisablePreemption();
	
	 	#ifdef RZK_PRIORITYINHERITANCE
			/* The semaphore is taken out from the owner's queue*/
			if((pSemaphore->uState & OBJECT_PRIORITY_INHERITANCE) && (pSemaphore->pOwnerThread != NULL))
			{
				pOwner = ( RZK_TCB_t * ) pSemaphore->pOwnerThread;
				RemoveFromOwnerQueue(pOwner,pSemaphore);
				/* restores both the inherited and dispatch priority recursively */ 
				RestorePriorityRecursive(pOwner);
			}
		#endif /* end of RZK_PRIORITYINHERITANCE */

		
		/* Check the threads, which are pending on a semaphore */
		pTemp = pSemaphore -> pResNext;
		
		if (pTemp != NULL)
		{
			UpdateThreadStatus(pTemp,RZKERR_SUCCESS);
			RemoveFromResourceQueue(pSemaphore,pTemp);
			#ifdef RZK_PRIORITYINHERITANCE
			if(pSemaphore->uState & OBJECT_PRIORITY_INHERITANCE)
			{
				pSemaphore->pOwnerThread = pTemp;
				AddToOwnerQueue(pTemp, pSemaphore);
				GetInheritedPriority(pTemp, NULL);
				RestorePriorityRecursive(pTemp);
			}
			#endif

		} 
		else
		{
			pSemaphore->pOwnerThread = NULL;
			++pSemaphore -> uDynamicCount;
		}

	RZKRestorePreemption(uState);
	return RZKERR_SUCCESS;

}/* End of RZKReleaseSemaphore */ 

⌨️ 快捷键说明

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