因?yàn)槲依靡粋€(gè)排序的鏈表來(lái)保存就緒列表,所以調(diào)度程序容易實(shí)現(xiàn)。它只是檢測(cè)正在運(yùn)行的任務(wù)與最高優(yōu)先級(jí)的就緒任務(wù)是否是一個(gè)任務(wù)和同樣的優(yōu)先級(jí)。如果是,調(diào)度任務(wù)完成。否則,它會(huì)啟動(dòng)一個(gè)從前者到后者的設(shè)備場(chǎng)景的切換。這里是用C++實(shí)現(xiàn)的代碼。
/**************************************************************
*
* Method : schedule()
*
* Description : Select a new task to be run.
*
* Notes : If this routine is called from within an ISR, the
* schedule will be postponed until the nesting level
* returns to zero.
*
* The caller is responsible for disabling interrupts.
*
* Returns : None defined.
*
**************************************************************/
void
Sched::schedule(void)
{
Task * pOldTask;
Task * pNewTask;
if(state != Started) return;
//
// Postpone rescheduling until all interrupts are completed.
//
if(interruptLevel != 0)
{
bSchedule = 1;
return;
}
//
// If there is a higher-priority ready task, switch to it.
//
if(pRunningTask != readyList.pTop)
{
pOldTask = pRunningTask;
pNewTask = readyList.pTop;
pNewTask->state = Running;
pRunningTask = pNewTask;
if(pOldTask == NULL)
{
contextSwitch(NULL, &pNewTask->context);
}
else
{
pOldTask->state = Ready;
contextSwitch(&pOldTask->context, &pNewTask->context);
}
}
} /* schedule() */
正如你從代碼中看到的一樣,有兩種情況調(diào)度程序不啟動(dòng)設(shè)備場(chǎng)景的切換。第一種情況是如果多任務(wù)沒(méi)有被啟用的時(shí)候。這是必要的,因?yàn)橛袝r(shí)應(yīng)用程序員想要在調(diào)度程序真正啟動(dòng)之前創(chuàng)建其任務(wù)的一些或者全部。在那種情況下,應(yīng)用程序的main 例程會(huì)類(lèi)似于下面這段tb程序。每次一個(gè)Task 對(duì)象被創(chuàng)建的時(shí)候,調(diào)度程序就被調(diào)用(注1)。然而,因?yàn)檎{(diào)度程序?yàn)榱藱z查多任務(wù)是否已被啟動(dòng)而檢測(cè)變量state 的值,所以直到start()被調(diào)用以后才會(huì)發(fā)生設(shè)備場(chǎng)景的轉(zhuǎn)換。
#include "adeos.h"
void taskAfunction(void);
void taskBfunction(void);
/*
* Create two tasks, each with its own unique function and priority.
*/
Task taskA(taskAfunction, 150, 256);
Task taskB(taskBfunction, 200, 256);
/**************************************************************
*
* Method : main()
——————————————————————————————————
注 1:記住,任務(wù)的創(chuàng)建是我們調(diào)度點(diǎn)其中的一個(gè)。如果調(diào)度程序已經(jīng)啟動(dòng)了,新任務(wù)仍然可能是最高優(yōu)先級(jí)的就緒任務(wù)。
* Description : This is what an application program might look like
* if ADEOS were used as the operating system. This
* function is responsible for starting the operating system only.
*
* Notes : Any code placed after the call to os.start() will never
* be executed. This is because main() is not a task,
* so it does not get a chance to run once the scheduler is started.
*
**************************************************************/
void
main(void)
{
os.start();
// This point will never be reached.
} /* main() */
因?yàn)檫@是一段重要的代碼,所以讓我重新講解你正在看的東西。這是一個(gè)你可能作為ADEOS 用戶寫(xiě)出的應(yīng)用代碼的例子。你在開(kāi)始加入頭文件adeos.h 和聲明你的任務(wù)。在你聲明了你的任務(wù)和調(diào)用os.start 之后, tbw任務(wù)函數(shù)taskAfunction 和taskBfunction 開(kāi)始執(zhí)行(以偽并行的方式)。當(dāng)然,taskB 在這兩個(gè)任務(wù)中具有最高的優(yōu)先級(jí)(200),因此它將先運(yùn)行。然而,只要它由于任何原因放棄對(duì)處理器的控制,其他的任務(wù)就有機(jī)會(huì)運(yùn)行。另一種 ADEOS 調(diào)度程序不進(jìn)行設(shè)備場(chǎng)景切換的情況是在中斷進(jìn)行期間。操作系統(tǒng)跟蹤目前運(yùn)行的中斷服務(wù)例程的嵌套級(jí)別,井且只有在嵌套級(jí)是零的時(shí)候才允許設(shè)備場(chǎng)景切換。如果調(diào)度程序是從一個(gè)ISR 調(diào)用的(就像它在時(shí)鐘節(jié)拍期間一樣),bSchedule 標(biāo)志被置位,表明只要最外層的中斷處理程序退出,調(diào)度程序就應(yīng)該再次被調(diào)用。這個(gè)被延遲的調(diào)度加快了整個(gè)系統(tǒng)中斷響應(yīng)的時(shí)間。
/**************************************************************
*
* Method : schedule()
*
* Description : Select a new task to be run.
*
* Notes : If this routine is called from within an ISR, the
* schedule will be postponed until the nesting level
* returns to zero.
*
* The caller is responsible for disabling interrupts.
*
* Returns : None defined.
*
**************************************************************/
void
Sched::schedule(void)
{
Task * pOldTask;
Task * pNewTask;
if(state != Started) return;
//
// Postpone rescheduling until all interrupts are completed.
//
if(interruptLevel != 0)
{
bSchedule = 1;
return;
}
//
// If there is a higher-priority ready task, switch to it.
//
if(pRunningTask != readyList.pTop)
{
pOldTask = pRunningTask;
pNewTask = readyList.pTop;
pNewTask->state = Running;
pRunningTask = pNewTask;
if(pOldTask == NULL)
{
contextSwitch(NULL, &pNewTask->context);
}
else
{
pOldTask->state = Ready;
contextSwitch(&pOldTask->context, &pNewTask->context);
}
}
} /* schedule() */
正如你從代碼中看到的一樣,有兩種情況調(diào)度程序不啟動(dòng)設(shè)備場(chǎng)景的切換。第一種情況是如果多任務(wù)沒(méi)有被啟用的時(shí)候。這是必要的,因?yàn)橛袝r(shí)應(yīng)用程序員想要在調(diào)度程序真正啟動(dòng)之前創(chuàng)建其任務(wù)的一些或者全部。在那種情況下,應(yīng)用程序的main 例程會(huì)類(lèi)似于下面這段tb程序。每次一個(gè)Task 對(duì)象被創(chuàng)建的時(shí)候,調(diào)度程序就被調(diào)用(注1)。然而,因?yàn)檎{(diào)度程序?yàn)榱藱z查多任務(wù)是否已被啟動(dòng)而檢測(cè)變量state 的值,所以直到start()被調(diào)用以后才會(huì)發(fā)生設(shè)備場(chǎng)景的轉(zhuǎn)換。
#include "adeos.h"
void taskAfunction(void);
void taskBfunction(void);
/*
* Create two tasks, each with its own unique function and priority.
*/
Task taskA(taskAfunction, 150, 256);
Task taskB(taskBfunction, 200, 256);
/**************************************************************
*
* Method : main()
——————————————————————————————————
注 1:記住,任務(wù)的創(chuàng)建是我們調(diào)度點(diǎn)其中的一個(gè)。如果調(diào)度程序已經(jīng)啟動(dòng)了,新任務(wù)仍然可能是最高優(yōu)先級(jí)的就緒任務(wù)。
* Description : This is what an application program might look like
* if ADEOS were used as the operating system. This
* function is responsible for starting the operating system only.
*
* Notes : Any code placed after the call to os.start() will never
* be executed. This is because main() is not a task,
* so it does not get a chance to run once the scheduler is started.
*
**************************************************************/
void
main(void)
{
os.start();
// This point will never be reached.
} /* main() */
因?yàn)檫@是一段重要的代碼,所以讓我重新講解你正在看的東西。這是一個(gè)你可能作為ADEOS 用戶寫(xiě)出的應(yīng)用代碼的例子。你在開(kāi)始加入頭文件adeos.h 和聲明你的任務(wù)。在你聲明了你的任務(wù)和調(diào)用os.start 之后, tbw任務(wù)函數(shù)taskAfunction 和taskBfunction 開(kāi)始執(zhí)行(以偽并行的方式)。當(dāng)然,taskB 在這兩個(gè)任務(wù)中具有最高的優(yōu)先級(jí)(200),因此它將先運(yùn)行。然而,只要它由于任何原因放棄對(duì)處理器的控制,其他的任務(wù)就有機(jī)會(huì)運(yùn)行。另一種 ADEOS 調(diào)度程序不進(jìn)行設(shè)備場(chǎng)景切換的情況是在中斷進(jìn)行期間。操作系統(tǒng)跟蹤目前運(yùn)行的中斷服務(wù)例程的嵌套級(jí)別,井且只有在嵌套級(jí)是零的時(shí)候才允許設(shè)備場(chǎng)景切換。如果調(diào)度程序是從一個(gè)ISR 調(diào)用的(就像它在時(shí)鐘節(jié)拍期間一樣),bSchedule 標(biāo)志被置位,表明只要最外層的中斷處理程序退出,調(diào)度程序就應(yīng)該再次被調(diào)用。這個(gè)被延遲的調(diào)度加快了整個(gè)系統(tǒng)中斷響應(yīng)的時(shí)間。