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

📄 resetsemaphore.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/************************************************************
*	RZK	RTOS
*	Copyright (c)  1999,  ZiLOG.
*	Developed for ZiLOG by DACS,India.
*	  
*	File			:	RZKResetSemaphore
*
*	Author			:	Rohit S.P.
*	
*	Date Of Creation:	Sunday 11th April of 1999
*
*	Revision History :	23-01-01 Shreekanth.B ZIEL
*						RZKERR_CB_BUSY return encapsulated by #ifndef RZK_PERFORMANCE
*
*	Modified By		:	Shreekanth.B
*
*	Modification	:	29th June,1999
*	Date
*
*	Purpose			:    This function brings the semaphore back to created state and 
*									releases all waiting threads on this semaphore and returns 
*									appropriate status
*	  
*	Inputs			:   hSemaphore -  Handle to the semaphore control block to reset
*
*	Outputs			:	RZKERR_SUCCESS - If the semaphore was successfully reset
*							     RZKERR_CB_BUSY - If the semaphore control block is being used exclusively by other thread.
*								 RZKERR_INVALID_HANDLE - If the handle to the semaphore  is invalid.
*	Assumptions		:
*
*	Dependencies	:
************************************************************/

#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZQueue.h"
#include "ZSemaphore.h"
#include "ZScheduler.h"

#define pSemaphore ((RZK_SEMAPHORE_t *) hSemaphore)

/** extern functions */
extern UINTRMASK RZKDisableInterrupts();
extern void RZKEnableInterrupts(UINTRMASK);
//extern void RZKScheduler();
extern void RemoveFromOwnerQueue(RZK_HANDLE_t hThread,RZK_SEMAPHORE_t* hSemaphore);
extern void RestorePriorityRecursive(RZK_HANDLE_t hThread);

RZK_STATUS_t ResetSemaphore(RZK_SEMAPHOREHANDLE_t hSemaphore)
{
	/* Local Variables */
	RZK_STATE_t uState;
	RZK_TCB_t *pTemp;

/* Validate the handle */
#ifdef RZK_DEBUG

	if((pSemaphore == NULL)||(pSemaphore->uState == 0 )||
		(pSemaphore->magic_num != MAGIC_NUM_SEMAPHORE))
		return RZKERR_INVALID_HANDLE;

#endif /* RZK_DEBUG */

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

	uState = RZKDisablePreemption();

	pSemaphore->uState &= (~OBJECT_CREATED);

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

	/* For all threads pending on this semaphore, remove it from TQ2 and add it to dispatch queue */
	pTemp = pSemaphore ->pResNext;
	while(pTemp != NULL)
	{
		UpdateThreadStatus(pTemp, RZKERR_OBJECT_RESET);
		pTemp = pTemp -> pResNext;
	}
	
	/* set the dynamic count */
	pSemaphore->uDynamicCount = pSemaphore -> uInitialCount;
	pSemaphore->pOwnerThread = NULL;

	/* Reset the pLastPosition and pResNext pointers to point to semaphore control block */
	pSemaphore->pLastPosition = pSemaphore->pResNext = ( RZK_TCB_t * ) NULL;

	/* set the uState in the semaphore control block to busy state*/
	pSemaphore->uState |= OBJECT_CREATED ;

	RZKRestorePreemption(uState);
	return RZKERR_SUCCESS;
}	/* End of RZKResetSemaphore */

⌨️ 快捷键说明

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