void Thread3(void){ Count3 = 0; for(;;){ Count3++; OS_Suspend(); // cooperative multitasking } }
// ******** OS_Kill ************ // kill the currently running thread, release its TCB memory // input: none // output: none void OS_Kill(void) { int id; tcbType *temp; // Starting critical section to delete TCB OS_DisableInterrupts(); NumThreads--; id = (*RunPt).id; // Make TCB invalid tcbs[id].valid = INVALID; // Remove TCB from linked list temp = tcbs[id].prev; (*temp).next = tcbs[id].next; temp = tcbs[id].next; (*temp).prev = tcbs[id].prev; // Check if thread is Head or Tail if(&tcbs[id] == Head) { Head = tcbs[id].next; } else if(&tcbs[id] == Tail) { Tail = tcbs[id].prev; } // Trigger threadswitch OS_Suspend(); while(1) { } // Never leave }
int OS_AddThread(void(*task)(void), unsigned long stackSize, unsigned long priority) { uint32_t status; status = OS_StartCritical(); //we will take the first thread from the dead pool if(DeadPt == '\0') { OS_DisableInterrupts(); while(1){} } (DeadPt)->id = uniqueId; //unique id (DeadPt)->active = 1; (DeadPt)->sleepState = 0; //flag (DeadPt)->priority = priority; (DeadPt)->blockedState = 0; //flag (DeadPt)->needToWakeUp = 0; //flag SetInitialStack(DeadPt, stackSize); (DeadPt)->stack[stackSize - 2] = (uint32_t)task; //push PC uniqueId++; addDeadToScheduler(&DeadPt); if(higherPriorityAdded == 1) { OS_Suspend(); } OS_EndCritical(status); return 1; }
// ******** OS_Sleep ************ // place this thread into a dormant state // input: number of msec to sleep // output: none // You are free to select the time resolution for this function // Sleep time is a multiple of context switch time period // OS_Sleep(0) implements cooperative multitasking void OS_Sleep(unsigned long sleepTime) { OS_DisableInterrupts(); threadRemover(&SleepPt, sleepTime * 2); sleepCount++; switched = 1; OS_Suspend(); OS_EnableInterrupts(); }
void Threader3(void){ Count3 = 0; for(;;){ Count3++; OS_Signal(&Ready); OS_Suspend(); } }
// ******** OS_Kill ************ // kill the currently running thread, release its TCB and stack // input: none // output: none void OS_Kill(void) { OS_DisableInterrupts(); threadRemover(&DeadPt, 0); //parameter 0 will be ignored deadCount++; switched = 1; OS_Suspend(); OS_EnableInterrupts(); }
void Threader1(void){ Count1 = 0; for(;;){ OS_Wait(&Ready); Count1++; OS_Signal(&Ready); OS_Suspend(); } }
// -------------------------- OS_Kill -------------------------------- void OS_Kill(void){ long status; status = StartCritical(); unlinkTCB(&Actives, RunPt); RunPt->id = 0; EndCritical(status); OS_Suspend(); }
// --------------------------- OS_Sleep ------------------------------ void OS_Sleep(unsigned long sleepTime){ DisableInterrupts(); RunPt->sleepSt = sleepTime; unlinkTCB(&Actives, RunPt); SleepingThreads[SleepingThreadNum] = RunPt; SleepingThreadNum++; OS_Suspend(); EnableInterrupts(); }
// ******** OS_bWait ************ // if the semaphore is 0 then spin/block // if the semaphore is 1, then clear semaphore to 0 // input: pointer to a binary semaphore // output: none void OS_bWait(Sema4Type *semaPt){ DisableInterrupts(); while( semaPt->Value != 1){ //TODO: implement Blocking EnableInterrupts(); OS_Suspend(); DisableInterrupts(); } semaPt->Value =0; EnableInterrupts(); return; }
// ******** OS_Wait ************ // decrement semaphore // Lab2 spinlock // Lab3 block if less than zero // input: pointer to a counting semaphore // output: none void OS_Wait(Sema4Type *semaPt) { uint32_t status; status = OS_StartCritical(); (*semaPt).Value--; //decrease count if((*semaPt).Value < 0) { OS_Block(semaPt); //block and put into semaphore blocked list OS_Suspend(); OS_EnableInterrupts(); OS_DisableInterrupts(); } OS_EndCritical(status); }
// ------------------------- OS_bWait -------------------------------- void OS_bWait(Sema4Type *semaPt){ DisableInterrupts(); while (semaPt->Value <= 0){ unlinkTCB(&Actives, RunPt); linkTCB(&(semaPt->Blocked), RunPt); RunPt->blockSt = semaPt; OS_Suspend(); EnableInterrupts(); } DisableInterrupts(); (*semaPt).Value = 0; EnableInterrupts(); }
// ******** OS_bWait ************ // Lab2 spinlock, set to 0 // Lab3 block if less than zero // input: pointer to a binary semaphore // output: none void OS_bWait(Sema4Type *semaPt) { int32_t status; status = OS_StartCritical(); while((*semaPt).Value == 0) { OS_Block(semaPt); //block and put into semaphore blocked list OS_Suspend(); OS_EnableInterrupts(); OS_DisableInterrupts(); } //while someone has the semaphor (*semaPt).Value = 0; //take the semaphore EndCritical(status ); }
// ******** OS_Wait ************ // decrement semaphore and spin/block if less than zero // input: pointer to a counting semaphore // output: none void OS_Wait(Sema4Type *semaPt) { long status; status = StartCritical(); (*semaPt).Value--; if((*semaPt).Value < 0) { (*RunPt).blockedState = semaPt; OS_Suspend(); } EndCritical(status); }
CANmsgType BCAN_get(void) { long sr; CANmsgType data; OS_Wait(&CANmessagesReceived); sr = OS_StartCritical(); while(!CAN_RX_FIFOFifo_Get(&data)) //keep checking if the FIFO has data { #ifdef OS_COOP_SPINLOCK //if the interrupt fires and stuffs the FIFO OS_Suspend(); //before the systick triggers a threadswitch #endif //it will get its data faster (but other threads will have to wait) } OS_EndCritical(sr); return data; }
void SH_Shell(void) { SH_Init(); while(1) { /* Show prompt */ printf("%s", _SH_getVar(SH_PROMPT_NAME)); /* Input command */ _SH_InCommand(input, SH_MAX_LENGTH); printf("\n"); /* Construct and execute command */ if(!input[0]) continue; _SH_Parse_Command(input); _SH_BeginRedirect(); // determine file redirection _SH_Execute(); _SH_EndRedirect(); // end redirect to file (if necessary) memset(input, 0, SH_MAX_LENGTH); OS_Suspend(); } }
void read_test(void) { int i; unsigned int then, now; eFile_Init(); eFile_Format(); OS_AddThread(&write_test, 128, 0); OS_Sleep(2000); then = OS_MsTime(); // read 10 blocks for(i = 0; i < 10; i++) { eDisk_ReadBlock(buffer, i); } now = OS_MsTime(); OS_AddThread(&SH_Shell, 128, 0); OS_Sleep(1000); OS_Suspend(); printf("Read test took %d ms", now - then); eFile_Format(); OS_Kill(); }
void traverseSleep(void) { struct TCB * temp; struct TCB * toRestore; temp = SleepPt; while(temp!= '\0') { (*temp).sleepState -= 2; if( (*temp).sleepState <=0 && (*temp).needToWakeUp != 1) //if sleep was given a time to wake up { toRestore = (*temp).nextTCB; //since we are taking node out, need to restore where we were addSleepToScheduler(temp); //take node out of sleep and put into scheduler temp = toRestore; //restore to be able to go to next thread } if(temp == '\0') break; temp = (*temp).nextTCB; } if(higherPriorityAdded == 1) { OS_Suspend(); } }
/********************************************************************* * * HPTask */ static void HPTask(void) { while (1) { OS_Suspend(NULL); // Suspend high priority task OS_Timing_End(&_Time); // Stop measurement } }
// ******** OS_Sleep ************ // place this thread into a dormant state // input: number of msec to sleep // output: none // You are free to select the time resolution for this function // OS_Sleep(0) implements cooperative multitasking void OS_Sleep(unsigned long sleepTime) { (*RunPt).sleepState = sleepTime; OS_Suspend(); return; }
/********************************************************************* * * HPTask */ static void HPTask(void) { while (1) { OS_Suspend(NULL); // Suspend high priority task BSP_ClrLED(0); // Stop measurement } }
//---------------------------------------------------------------------------// // // //函数:void ShowMenu_Item(unsigned char ,unsigned char,unsigned char) // //描述:显示菜单项子函数 // //参数:index ——菜单项索引 // // no ——显示位置序号 // // mode ——显示模式 // // // //---------------------------------------------------------------------------// void RunMenu(unsigned char index) { char key,i,Active_Item = 0x00 ; OS_U8 Event ; union { unsigned int pos[2] ; char byte[4] ; }TPoint ; Ini_Background: Color = Black ; Color_BK = 0xEF9F ; Clear_LCD(Color_BK) ; DrawRectFill(0,0,240,25,Blue) ; DrawRectFill(0,295,240,25,Blue) ; Color = 0xFFFF ; Color_BK = Blue ; PutStringCN16(4,4,(unsigned char *)Menus[index].MenuTitle) ; Color = Black ; Color_BK = WINDOW_BK_COLOR ; for(i=0;i<Menus[index].MenuConfig[0];i++) { if(i==Active_Item) ShowMenu_Item(Menus[index].ItemIndex[i],i,1) ; else ShowMenu_Item(Menus[index].ItemIndex[i],i,0) ; } for(;;) { // Event = OS_WaitEvent(EVENT_KEY_PRESSED+EVENT_TP_PRESSED) ; OS_WaitMail(&MBKey) ; // if(Event&EVENT_KEY_PRESSED) { OS_GetMail1(&MBKey, &key) ; switch(key) { case Right: InactivateItem(Active_Item) ; if(++Active_Item>=Menus[index].MenuConfig[0]) Active_Item = 0x00 ; ActivateItem(Active_Item) ; break ; case Left: InactivateItem(Active_Item) ; if(Active_Item==0) Active_Item = 0x08 ; else Active_Item-- ; ActivateItem(Active_Item) ; break ; case 8: InactivateItem(Active_Item) ; if(Active_Item==0) Active_Item = 0x08 ; else if(Active_Item==1||Active_Item==2) Active_Item = Active_Item+5 ; else Active_Item = Active_Item-3 ; ActivateItem(Active_Item) ; break ; case 0: InactivateItem(Active_Item) ; if(Active_Item==8) Active_Item = 0x00 ; else if(Active_Item==6||Active_Item==7) Active_Item = Active_Item-5 ; else Active_Item = Active_Item+3 ; ActivateItem(Active_Item) ; break ; case Enter: OS_PutMail1(&MBFunc, &Active_Item) ; OS_SignalEvent(EVENT_OP_STARTED,&MENU_OP_TASK_TCB) ; OS_Suspend(&LCD_TASK_TCB) ; goto Ini_Background ; default: break ; } } /* else { OS_GetMail(&MBTouch, TPoint.byte) ; for(i=0;i<Menus[index].MenuConfig[0];i++) { if( (TPoint.pos[0]<(60+i%3*80)&&TPoint.pos[0]>(20+i%3*80)) &&(TPoint.pos[1]<(95+i/3*84)&&TPoint.pos[1]>(55+i/3*82))) { Active_Item = i ; OS_PutMail1(&MBFunc, &Active_Item) ; OS_SignalEvent(EVENT_OP_STARTED,&MENU_OP_TASK_TCB) ; OS_Suspend(&LCD_TASK_TCB) ; goto Ini_Background ; } } } */ } }