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

📄 deletethread.c

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

#define pThread ((RZK_TCB_t *)  hThread)
#define pCurrentThread ((RZK_TCB_t *)  hCurrentThread)

/** extern functions */
//extern void RZKScheduler( void );
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 RestorePriorityRecursive(RZK_HANDLE_t hThread);
extern void UpdateTQ2OnRemove(RZK_TCB_t    * pThreadToRemove);

/** extern variables */
extern RZK_DISPATCHQ_t DispatchQueue[DMAX_PRIORITY_P1];	/* declared in Createthread.c */
extern RZK_THREADHANDLE_t hCurrentThread;	/* declared in Scheduler.c */
extern RZK_TIMEQ_t TimeQueue[TMAX_QUEUE];	
extern UINT8 uRegBankBusyBitMap;
extern volatile UINT32 DQPriorityBitMap;
extern const UINT32 pMap[32];


/*
* Function		:	RZKDeleteThread
*
* Description	:	This function removes the specified thread from the respective queue 
*					and sets the THREAD_TERMINATED flag and then invalidates the TCB
* 
* Inputs		:	hThread - handle to thread	
*
* Outputs		:	RZKERR_SUCCESS - On successful termination of 	thread.
					RZKERR_INVALID_hANDLE - when the handle hassed is invalid.
*
* Dependencies	:	All externs
*/

RZK_STATUS_t RZKDeleteThread( RZK_THREADHANDLE_t hThread)
{
	/* Local Variables */
//	RZK_SEMAPHORE_t *pTempObject;
	RZK_STATUS_t status;
	RZK_STATE_t uState;
        FNP_THREAD_ENTRY *pFn ;	// For IAR
//	void (** pFn) (); // For IAR
	
#ifdef RZK_DEBUG
	if ((pThread == NULL) || (pThread->uState == 0) || (pThread->magic_num != MAGIC_NUM_THREAD) )
		return RZKERR_INVALID_HANDLE;

	if( (pThread->uState & THREAD_TERMINATED) )
		return RZKERR_INVALID_OPERATION;
#endif

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

	if(pThread -> cResLocked != 0)
		return RZKERR_INVALID_OPERATION;

	uState = RZKDisablePreemption();

	/* Release all the semaphore owned by this thread*/
#ifdef RZK_PRIORITYINHERITANCE
	/* pTempObject is of semaphore type */
	while(pThread->pOwnedResNext)
	{
		/* Release semaphores owned by the thread, during deletion*/
		status = RZKReleaseSemaphore(pThread->pOwnedResNext);
		if(status != RZKERR_SUCCESS)
		{
			RZKRestorePreemption(uState) ;		
			return status;
		}
	}
#endif /* End of RZK_PRIORITYINHERITANCE */


	/* If the thread is running, then remove it from the Dispatch Queue */
	if(pThread->uState & THREAD_RUNNING)
	{
		UINTRMASK mInterruptMask;
		/* Reset the RUNNING bit */
//		pThread->uState &= ~(THREAD_RUNNING);

		mInterruptMask = RZKDisableInterrupts();
		DispatchQueueNodeRemove(pThread);	
		RZKEnableInterrupts(mInterruptMask);
	}

	if (pThread->uState & THREAD_BLOCKEDSUSPEND) 
	{
		if(pThread->uBlockingReason & BLOCK_TIMEDSUSPEND)
		{
			/* Remove from Time Queue 1 */
			if(( void *) pThread->pNext != ( void *) &TimeQueue[TIME_Q1])
			{
				pThread->pNext->tWaitTime += (pThread->tWaitTime);
			}
			QueueNodeRemove(pThread);
		}	
		else	/* remove from Resource Queue */
		{
			RemoveFromResourceQueue(pThread->pBlockingResource,pThread);
			#ifdef RZK_PRIORITYINHERITANCE

				/* restores both the inherited and dispatch priority recursively */
				if(((RZK_SEMAPHORE_t*)pThread->pBlockingResource)->uState & OBJECT_PRIORITY_INHERITANCE)
					RestorePriorityRecursive(((RZK_SEMAPHORE_t*)pThread-> pBlockingResource)->pOwnerThread);

			#endif /* End of RZK_PRIORITYINHERITANCE */
			
			/* If the resource queue is empty make the etWaitingQueue equal to MESSAGE_NONE */

			if((((RZK_MESSAGEQUEUE_t*)pThread-> pBlockingResource)->pResNext == NULL) && (pThread->uBlockingReason &(BLOCK_POSTMESSAGEQUEUE |BLOCK_PENDMESSAGEQUEUE | BLOCK_POSTMAILBOX | BLOCK_PENDMAILBOX)))
			{
				((RZK_MESSAGEQUEUE_t*)pThread-> pBlockingResource)->etWaitingQueue = MESSAGE_NONE;
			}
		}
	}	/* End of BLOCKED_SUSPEND*/

	if (pThread->uState & THREAD_TIMEDBLOCK)	
	{
		/* Remove from TQ2 */
		UpdateTQ2OnRemove (pThread); /*This function both updates and removes the thread from the queue*/
	}
	pThread->uBlockingReason = 0;
	

	/* Clear the bit for this thread in BankBitmap, if a bank not assigned */
	/* If THREAD_RUNNING was not added to this, then pre-emption will take place */
	if( pCurrentThread == hThread )
		pThread->uState |= (THREAD_BUSY | THREAD_TERMINATED);
	else
		pThread->uState = (THREAD_BUSY | THREAD_TERMINATED);

	pThread->hObject =	NULL;

    // Changed for IAR
	if( (pFn = ( FNP_THREAD_ENTRY * ) pThread->pCleanupFunc) != NULL)
	{
		while(*pFn != ( FNP_THREAD_ENTRY ) NULL)/* Loop until NULL */
		{
			(*pFn) ();/* Execute each function */
			pFn++;
		}
	}

#ifdef	RZK_DEBUG
	pThread->magic_num = 0;
#endif
		
#ifdef RZK_NT
	GetExitCodeThread(((RZK_TCB_t *)pThread)->hNTThread,&(((RZK_TCB_t *)pThread)->ExitCode));
	TerminateThread(((RZK_TCB_t *)pThread)->hNTThread,((RZK_TCB_t *)pThread)->ExitCode);	
#endif
	pThread->uState = 0;

	{
		UINTRMASK mInt;
		mInt = RZKDisableInterrupts();
		
		if(pThread == pCurrentThread)
			RZKEnablePreemption();
		else
			RZKRestorePreemption(uState);
		
		RZKEnableInterrupts(mInt);
	}
/*	{
		pThread->uState &= ~(THREAD_RUNNING);
		RZKRestorePreemption(uState);
	}*/

	return RZKERR_SUCCESS;
} 	/* End ofTerminateThread */

⌨️ 快捷键说明

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