📄 tskmng.c
字号:
////////////////////////////////////////////////////////////////////////////////
// File: Tskmng.c
// Date: 2001-6-15 pm 03:36:19
// Written by: CYu
// Decription: Task Id equals to index of aTcb
// Priority equals to index of aReadyQueue
// both of aTcb and aReadyQueue has +1 quantity
// Modification record
//------------------------------------------------------------------------------
// Copyright: CNASIC Proprietary Material
// Copyright (c) 2001, All Rights Reserved By:
// National ASIC System Engineering Research Center (CNASIC)
// SouthEast University
//
// DISTRIBUTION PROHIBITED without written authorization from CNASIC
//------------------------------------------------------------------------------
// Modification History
//
// 2001/10/31 by longn_qi revise.
//
////////////////////////////////////////////////////////////////////////////////
/* System and Standard Header */
#include <sys\sysdebug.h>
//#include <stdio.h>
#include <kernel\ros33\itron.h>
#include <kernel\ros33\ros33.h>
#define _SIZE_T_DEFINED
#include <windows.h>
/* Application Header */
#include "internal.h"
/* Local Marco Definition */
#define ROS33_VERSION 0x0300 // version3.0
//--- static variables ---------------------------------------------------------
//--- external variables -------------------------------------------------------
//--- shared variables ---------------------------------------------------------
/* Local Function Declaration */
static DWORD WINAPI TaskTrunkThread( LPVOID pTaskEntry );
static void EndTask( ID idTask );
static ER iContextSwitch( void );
//--- external functions -------------------------------------------------------
int threadTemp[TSK_NUM +1];
/* Function Definition */
//------------------------------------------------------------------------------
// Function name : vcre_tsk
// Description :
// Return type : ER
// Argument : ID tskid
// Argument : FP task
// Argument : PRI tskpri
// Argument : UW istkadr - ignored in the simulator
// Remarks :
// So also :
//------------------------------------------------------------------------------
ER vcre_tsk( ID tskid,
FP task,
PRI tskpri,
UW istkadr )
{
if( tskid > TSK_NUM )
return E_NOEXS;
else if( tskid<=0 || aTcb[tskid].fpPC )
return E_ID;
aTcb[tskid].ubStatus = TTS_DMT;// the initialized status of task is dormant
aTcb[tskid].bIniPriority = tskpri;
aTcb[tskid].fpPC = task;
return E_OK;
}
//------------------------------------------------------------------------------
// Function name : sta_tsk
// Description :
// Return type : ER
// Argument : ID tskid
// Argument : INT stacd
// Remarks :
// So also :
//------------------------------------------------------------------------------
ER sta_tsk( ID tskid,
INT stacd )
{
HANDLE hThread;
#if ENABLE_SYSCALL_STATISTIC
gSyscallSta[0].sta++;
#endif
if( tskid>TSK_NUM )
return E_NOEXS;
if( tskid<=0 )
return E_ID;
if( !aTcb[tskid].fpPC )
return E_NOEXS;
if( !( aTcb[tskid].ubStatus & TTS_DMT) )
return E_OBJ;
hThread = CreateThread(
NULL,
0,
TaskTrunkThread,
aTcb[tskid].fpPC,
CREATE_SUSPENDED,
&aTcb[tskid].nThread );
if( !hThread )
return E_SYS;
aTcb[tskid].hThread = hThread;
threadTemp[tskid] = aTcb[tskid].nThread;
aTcb[tskid].ubStatus = TTS_RDY;
aTcb[tskid].bPriority = aTcb[tskid].bIniPriority;
AppendTaskToReadyQueue( tskid );
ContextSwitch( );
return E_OK;
}
//------------------------------------------------------------------------------
// Function name : ter_tsk
// Description :
// Return type : ER
// Argument : ID tskid
// Remarks :
// So also :
//------------------------------------------------------------------------------
ER ter_tsk( ID tskid )
{
#if ENABLE_SYSCALL_STATISTIC
gSyscallSta[2].sta++;
#endif
if( tskid>TSK_NUM )
return E_NOEXS;
if( tskid<=0 )
return E_ID;
if( !aTcb[tskid].fpPC )
return E_NOEXS;
if( tskid == idCurTask || ( aTcb[tskid].ubStatus & TTS_DMT ) )
return E_OBJ;
TerminateThread( aTcb[tskid].hThread, -1 );
EndTask( tskid );
// CloseHandle( aTcb[tskid].hThread );
return E_OK;
}
//------------------------------------------------------------------------------
// Function name : ext_tsk
// Description :
// No return value
// No argument
// Remarks :
// So also :
//------------------------------------------------------------------------------
void ext_tsk( void )
{
#if ENABLE_SYSCALL_STATISTIC
gSyscallSta[1].sta++;
#endif
EndTask( idCurTask );
idCurTask = 0;
ContextSwitch( );
ExitThread( -1 );
}
//------------------------------------------------------------------------------
// Function name : TaskTrunkThread
// Description :
// Return type : DWORD WINAPI
// Argument : LPVOID pTaskEntry
// Remarks :
// So also :
//------------------------------------------------------------------------------
DWORD WINAPI TaskTrunkThread( LPVOID pTaskEntry )
{
SetThreadPriority( GetCurrentThread( ), THREAD_PRIORITY_NORMAL );
((FP)pTaskEntry)();
EndTask( idCurTask );
idCurTask = 0;
ContextSwitch( );
return 0;
}
//------------------------------------------------------------------------------
// Function name : EndTask
// Description :
// No return value
// Argument : ID idTask
// Remarks :
// So also :
//------------------------------------------------------------------------------
void EndTask( ID idTask )
{
RemoveTaskFromReadyQueue( aTcb[idTask].bPriority, idTask );
aTcb[idTask].ubStatus = TTS_DMT;
CloseHandle( aTcb[idTask].hThread );
}
//------------------------------------------------------------------------------
// Function name : AppendTaskToReadyQueue
// Description :
// Return type : BOOL
// Argument : ID tskid
// Remarks : 1. the queue is empty, first = 0; last = 0
// fill both of them to tskid
// 2. the queue is not empty
// modify last
// tskid must be corrected by caller
// So also :
//------------------------------------------------------------------------------
void AppendTaskToReadyQueue( ID tskid )
{
PTCB pCurTcb;
PREADYQUEUE pCurRQ;
B bPriority;
pCurTcb = &aTcb[tskid];
bPriority = pCurTcb->bPriority;
// ready queue of current priority
pCurRQ = &aReadyQueue[bPriority];
if( !pCurRQ->idFirstTask )
{
pCurTcb->idNextTask = 0;
pCurTcb->idPrevTask = 0;
pCurRQ->idFirstTask = pCurRQ->idLastTask = tskid;
}
else
{
aTcb[pCurRQ->idLastTask].idNextTask = tskid;
pCurTcb->idPrevTask = pCurRQ->idLastTask;
pCurTcb->idNextTask = 0;
pCurRQ->idLastTask = tskid;
}
}
//------------------------------------------------------------------------------
// Function name : RemoveFirstTaskFromReadyQueue
// Description :
// Return type : ID
// Argument : B bPriority
// Remarks : 1. empty queue
// return 0
// 2. only one task in ready queue
// empty the queue
// 3. many task
// remove first one
// So also :
//------------------------------------------------------------------------------
ID RemoveFirstTaskFromReadyQueue( B bPriority )
{
ID idTsk;
// empty queue
if( !aReadyQueue[bPriority].idFirstTask )
idTsk = 0;
// only one task in the queue
//else if( aReadyQueue[bPriority].idFirstTask == aReadyQueue[bPriority].idLastTask )
else if( aTcb[aReadyQueue[bPriority].idFirstTask].idNextTask == 0 )
{
idTsk = aReadyQueue[bPriority].idFirstTask;
aTcb[idTsk].idNextTask = 0;
aTcb[idTsk].idPrevTask = 0;
aReadyQueue[bPriority].idFirstTask = aReadyQueue[bPriority].idLastTask = 0;
}
else
{
idTsk = aReadyQueue[bPriority].idFirstTask;
aReadyQueue[bPriority].idFirstTask = aTcb[idTsk].idNextTask;
aTcb[aTcb[idTsk].idNextTask].idPrevTask = 0;
aTcb[idTsk].idNextTask = 0;
}
return idTsk;
}
//------------------------------------------------------------------------------
// Function name : RemoveTaskFromReadyQueue
// Description :
// No return value
// Argument : B bPriority
// Argument : ID idTsk
// Remarks : 1. empty queue
// return directly
// 2. first task in the queue
// call RemoveFirstTaskFromReadyQueue()
// 3. find it
// 3.1 last one
// So also :
//------------------------------------------------------------------------------
void RemoveTaskFromReadyQueue( B bPriority,
ID idTsk )
{
ID i;
// empty queue
if( !aReadyQueue[bPriority].idFirstTask )
return;
// only one task in the queue
else if( aReadyQueue[bPriority].idFirstTask == idTsk )
RemoveFirstTaskFromReadyQueue( bPriority );
else
{
i = aReadyQueue[bPriority].idFirstTask;
while( i )
{
if( aTcb[i].idNextTask==idTsk )
{
if( idTsk==aReadyQueue[bPriority].idLastTask )
{
aTcb[i].idNextTask = 0;
aReadyQueue[bPriority].idLastTask = i;
}
else
{
aTcb[i].idNextTask = aTcb[idTsk].idNextTask;
aTcb[aTcb[idTsk].idNextTask].idPrevTask = i;
}
aTcb[idTsk].idPrevTask = 0;
aTcb[idTsk].idNextTask = 0;
}
i = aTcb[i].idNextTask;
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -