📄 timeq2.c
字号:
/*
* File : TimeQ2.c
*
* Description : This file contains functions Related to TimeQ2.
*
* 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"
extern RZK_TIMEQ_t TimeQueue[TMAX_QUEUE];
/** extern functions */
//extern void QueueAppend(RZK_TCB_t *pInsertionPosition ,RZK_TCB_t *pObjectToAppend);
//extern void QueueNodeRemove(RZK_TCB_t *pObjectToRemove );
/* Function : AddToTimeQ2
*
* Description : This function adds the thread to the Time Q2.
*
* Inputs : RZK_TCB_t * pThreadToAdd.
*
* Outputs : None.
*
* Dependencies : None.
*/
void AddToTimeQ2(RZK_TCB_t *hObjectToAdd)
{
RZK_TCB_t* pInsertionPos = ( RZK_TCB_t*) &TimeQueue[TIME_Q2];
TICK_t tInitialTime = hObjectToAdd -> tWaitTime;
if( pInsertionPos != TimeQueue[TIME_Q2].pPrevious)
{
/* Find insertion position in TimeQ2 */
pInsertionPos = FindTQ2InsertionPosition ( hObjectToAdd );
/* Insert the thread at the respective position */
QueueAppend ( pInsertionPos, hObjectToAdd );
/* Update Time Q2 after inserting the thread */
if ( pInsertionPos == ( RZK_TCB_t*) &TimeQueue[TIME_Q2])
UpdateTQ2OnInsert(pInsertionPos);
else
{
hObjectToAdd-> tWaitTime = tInitialTime - ((RZK_TCB_t *)TimeQueue[TIME_Q2].pNext) ->tWaitTime;
}
}
else
QueueAppend ( pInsertionPos, hObjectToAdd );
}
/*
* Function : FindTQ2InsertionPosition
*
* Description : This function adds the thread to the Time Q2.
*
* Inputs : RZK_TCB_t * pThreadToAdd.
*
* Outputs : RZK_TCB_t *
*
*
* Dependencies : None
*
*/
RZK_TCB_t * FindTQ2InsertionPosition (RZK_TCB_t *pThreadToAdd)
{
/* Declare a temporary pointer of type void */
RZK_TCB_t * pTemp;
/*Initialize the variable m_tDifferencetime and initialize it to zero. */
TICK_t m_tDifferenceTime = 0;
/*
Go to the first valid node which is after
the refrence node.The pNext pointer of that
refrence node will point to the first valid
node of the queue.*/
/* Assign the address of the Time Queue2 to pTemp.*/
pTemp = TimeQueue[TIME_Q2] . pNext;
/* If the thread to be added is the first element,
it's wait time is not updated*/
m_tDifferenceTime = (pThreadToAdd -> tWaitTime) - (pTemp -> tWaitTime);
while((pThreadToAdd -> tWaitTime > pTemp -> tWaitTime) &&
( ( void * ) (pTemp = pTemp -> pNext) != ( void * ) &TimeQueue[TIME_Q2]))
{
pThreadToAdd -> tWaitTime = m_tDifferenceTime;
}
/*Return pointer to the previous node in the Queue.*/
return pTemp -> pPrevious;
}/*End of FindTQ2nsertionPosition. */
/*
* Function : UpdateTQ2OnInsert
*
* Description : This function is updated only when a thread is inserted in the front
* position of TimeQ2. It will update the tWaitTime of all the threads
* in the TimeQ2.
*
* Inputs : RZK_TCB_t *pInsertionPos.
*
* Outputs : None.
*
*
* Dependencies : None.
*
*/
void UpdateTQ2OnInsert(RZK_TCB_t *pInsertionPos)
{
RZK_TCB_t * pTemp;
TICK_t tTimeToAdd; /*This is the value of ticks to be added to all the other threads*/
pTemp = pInsertionPos -> pNext; /* pTemp points to the first valid thread in the queue.*/
/* Wait time for the second thread= it's current wait time - wait time of the new thread */
((RZK_TCB_t *)pTemp -> pNext) -> tWaitTime -= pTemp -> tWaitTime;
/* TimeQueue[TIME_Q2].tWaitTime = 0;*/
/* time to be added to the remaining threads = wait time of the old first thread.*/
tTimeToAdd = ((RZK_TCB_t *)pTemp->pNext)-> tWaitTime;
/* Loop the queue and update the wait time for all other threads.*/
pTemp = ((RZK_TCB_t *)pTemp -> pNext )-> pNext;
while( pTemp != pInsertionPos )
{
/* Wait time for a thread w.r.t. first thread = it's current wait time + wait time of the old first thread added*/
pTemp -> tWaitTime = pTemp -> tWaitTime + tTimeToAdd;
pTemp = pTemp -> pNext;
}
} /* end of UpdateTQ2OnInsert */
/*********************************************************************
*
* Function : UpdateTQ2OnRemove
*
* Description : This function updates all the other threads wait time residing in
* TQ2 when the first thread is removed from the queue, which results
* in adding the tWaitTime of the removed thread to all remaining
* threads in the queue. If the thread to be removed is other than
* first thread then simply remove the thread from the queue.
*
* Inputs : RZK_TCB_t * pThreadToRemove.
*
* Outputs : None.
*
* Dependencies : None.
************************************************************************/
void UpdateTQ2OnRemove(RZK_TCB_t * pThreadToRemove)
{
TICK_t tSecondThreadTime;
RZK_TCB_t *pTemp = (RZK_TCB_t *) &TimeQueue [ TIME_Q2 ] . pNext;
/* First check whether the thread to be Removed is the first thread in the queue.If not simply call the Remove function and return.
else Update the ticks for the remaining threads and remove the first thread.*/
if(( ( void *) (pThreadToRemove -> pPrevious) == ( void * ) &TimeQueue[TIME_Q2]) &&
( ( void * ) pThreadToRemove->pNext != ( void * ) &TimeQueue[TIME_Q2]))
{
pTemp = pThreadToRemove -> pNext;
/* The wait time for the second thread = remaining wait time of the out going thread + it's current wait time */
tSecondThreadTime = pTemp -> tWaitTime;
pTemp -> tWaitTime = tSecondThreadTime + pThreadToRemove-> tWaitTime ;
/* This is the reference for the remaining threads.*/
pTemp = pTemp -> pNext;
/*Loop through the queue and Update the wait time of each thread*/
while ( ( void * ) pTemp != ( void * ) &TimeQueue [ TIME_Q2] . pNext )
{
/* Wait time for the thread = it's current wait time - the new first thread wait time*/
pTemp -> tWaitTime = pTemp -> tWaitTime - tSecondThreadTime;
pTemp = pTemp -> pNext;
}
} /* end if */
/* After updating the tWaitTime Remove the first thread */
QueueNodeRemove( pThreadToRemove);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -