int init_sys(void) { SCB->CCR |= SCB_CCR_STKALIGN_Msk; // enable double word stack alignment PSP_array[0] = ((unsigned int)task0_stack) + (sizeof task0_stack) - 16*4; // top of stack HW32_REG((PSP_array[0] + (14<<2))) = (unsigned long)task0; // initial Program Counter HW32_REG((PSP_array[0] + (15<<2))) = 0x01000000; // initial PSR PSP_array[1] = ((unsigned int)task1_stack) + (sizeof task1_stack) - 16*4; HW32_REG((PSP_array[1] + (14<<2))) = (unsigned long)task1; HW32_REG((PSP_array[1] + (15<<2))) = 0x01000000; curr_task = 0; __set_PSP((PSP_array[curr_task] + 16*4)); // set PSP to top of stack NVIC_SetPriority(PendSV_IRQn, 0xFF); SysTick_Config(168000); __set_CONTROL(0x3); __ISB(); task0(); /*while(1) { stop_cpu; }; */ }
void PriorityTapeTest::testPriority() { boost::posix_time::ptime begin; boost::posix_time::ptime current; int duration; CPPUNIT_ASSERT( true == priority_->Enable(true) ); PriorityTapeTask task0(priority_.get(),100,0,10); PriorityTapeTask task1(priority_.get(),100,1,10); PriorityTapeTask task2(priority_.get(),100,2,10); PriorityTapeTask task3(priority_.get(),100,3,10); PriorityTapeTask task4(priority_.get(),100,4,10); PriorityTapeTask task5(priority_.get(),100,5,10); task0.Start(); boost::this_thread::sleep( boost::posix_time::milliseconds(5) ); task1.Start(); task2.Start(); task3.Start(); task4.Start(); task5.Start(); CPPUNIT_ASSERT( false == task0.Finished() ); CPPUNIT_ASSERT( false == task1.Finished() ); CPPUNIT_ASSERT( false == task2.Finished() ); CPPUNIT_ASSERT( false == task3.Finished() ); CPPUNIT_ASSERT( false == task4.Finished() ); CPPUNIT_ASSERT( false == task5.Finished() ); boost::this_thread::sleep( boost::posix_time::milliseconds(100) ); CPPUNIT_ASSERT( true == task0.Finished() ); CPPUNIT_ASSERT( true == task5.Finished() ); CPPUNIT_ASSERT( true == task4.Finished() ); CPPUNIT_ASSERT( true == task3.Finished() ); CPPUNIT_ASSERT( true == task2.Finished() ); CPPUNIT_ASSERT( true == task1.Finished() ); duration = (task0.End() - task0.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); duration = (task1.End() - task1.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); duration = (task2.End() - task2.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); duration = (task3.End() - task3.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); duration = (task4.End() - task4.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); duration = (task5.End() - task5.Begin()).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 ); CPPUNIT_ASSERT( task0.End() <= task5.Begin() ); CPPUNIT_ASSERT( task5.End() <= task4.Begin() ); CPPUNIT_ASSERT( task4.End() <= task3.Begin() ); CPPUNIT_ASSERT( task3.End() <= task2.Begin() ); CPPUNIT_ASSERT( task2.End() <= task1.Begin() ); }
// // a task gets dispatched on every tick_flag tick (10ms) // void task_dispatch(void) { /* scan the task bits for an active task and execute it */ char task; /* take care of the task timers. if the value ==0 skip it else decrement it. If it decrements to zero, activate the task associated with it */ task=0; while (task < NUM_TASKS ) { if (task_timers[task]) { task_timers[task]--; /* dec the timer */ if (task_timers[task] == 0 ) { set_task(task); /* if ==0 activate the task bit */ } } task++; } task = 0; /* start at the most significant task */ while (task <= NUM_TASKS ) { if ((task_bits & pgm_read_byte(&bit_mask[task]))) { break; /* if activate task found..*/ } task++; /* else try the next one */ } switch(task) /* if task bit is active..execute the task */ { case 0: task0(); break; case 1: task1(); break; case 2: task2(); break; case 3: task3(); break; case 4: task4(); break; case 5: task5(); break; case 6: task6(); break; case 7: task7(); break; default: break; /* no task was active!! */ } }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { int priority = (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO) + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2; // Enable FIFO scheduling, e.g., RT scheduling class on Solaris. if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO, priority, ACE_SCOPE_PROCESS)) != 0) { if (ACE_OS::last_error () == EPERM) { ACE_DEBUG ((LM_DEBUG, "client (%P|%t): user is not superuser, " "test runs in time-shared class\n")); } else ACE_ERROR ((LM_ERROR, "client (%P|%t): sched_params failed\n")); } try { CORBA::ORB_var orb = CORBA::ORB_init (argc, argv); if (parse_args (argc, argv) != 0) return 1; CORBA::Object_var object = orb->string_to_object (ior); Test::Roundtrip_var roundtrip = Test::Roundtrip::_narrow (object.in ()); if (CORBA::is_nil (roundtrip.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Nil Test::Roundtrip reference <%s>\n", ior), 1); } /// Begin the test ACE_DEBUG ((LM_DEBUG, "Starting threads\n")); Client_Task task0 (data_type, sz, roundtrip.in (), niterations); Client_Task task1 (data_type, sz, roundtrip.in (), niterations); Client_Task task2 (data_type, sz, roundtrip.in (), niterations); Client_Task task3 (data_type, sz, roundtrip.in (), niterations); ACE_hrtime_t test_start = ACE_OS::gethrtime (); task0.activate (THR_NEW_LWP | THR_JOINABLE); task1.activate (THR_NEW_LWP | THR_JOINABLE); task2.activate (THR_NEW_LWP | THR_JOINABLE); task3.activate (THR_NEW_LWP | THR_JOINABLE); task0.thr_mgr()->wait (); ACE_hrtime_t test_end = ACE_OS::gethrtime (); ACE_DEBUG ((LM_DEBUG, "Threads finished\n")); ACE_DEBUG ((LM_DEBUG, "High resolution timer calibration....")); ACE_High_Res_Timer::global_scale_factor_type gsf = ACE_High_Res_Timer::global_scale_factor (); ACE_DEBUG ((LM_DEBUG, "done\n")); ACE_Basic_Stats totals; task0.accumulate_and_dump (totals, ACE_TEXT("Task[0]"), gsf); task1.accumulate_and_dump (totals, ACE_TEXT("Task[1]"), gsf); task2.accumulate_and_dump (totals, ACE_TEXT("Task[2]"), gsf); task3.accumulate_and_dump (totals, ACE_TEXT("Task[3]"), gsf); totals.dump_results (ACE_TEXT("Total"), gsf); ACE_Throughput_Stats::dump_throughput (ACE_TEXT("Total"), gsf, test_end - test_start, totals.samples_count ()); if (do_shutdown) { roundtrip->shutdown (); } } catch (const CORBA::Exception& ex) { ex._tao_print_exception ("Exception caught:"); return 1; } return 0; }
/*! \fn void thread0 (void const *argument) \brief Thread definition for thread 0. \param argument A pointer to the list of arguments. */ void thread0 (void const *argument) { if (addTraceProtected("thread0 start run") != TRACE_OK) { stop_cpu; } while (1) { if (addTraceProtected("thread0 take sem0 attempt") != TRACE_OK) { stop_cpu; } if ( osSemaphoreWait (sid_Semaphore0, 0) != -1 ) // no wait { if (addTraceProtected("thread0 take sem0 success") != TRACE_OK) { stop_cpu; } task0(); // thread code count1Sec(); task0(); if (addTraceProtected("thread0 set priority to osPriorityLow") != TRACE_OK) { stop_cpu; } osThreadSetPriority(osThreadGetId(), osPriorityLow); if (addTraceProtected("thread0 yields") != TRACE_OK) { stop_cpu; } osThreadYield(); // suspend thread if (addTraceProtected("thread0 release sem0 attempt") != TRACE_OK) { stop_cpu; } if (osSemaphoreRelease (sid_Semaphore0) != osOK) { if (addTraceProtected("thread0 release sem0 fail") != TRACE_OK) { stop_cpu; } } else { if (addTraceProtected("thread0 release sem0 success") != TRACE_OK) { stop_cpu; } } } else { if (addTraceProtected("thread0 take sem0 fail") != TRACE_OK) { stop_cpu; } } if (addTraceProtected("thread0 set priority to osPriorityLow") != TRACE_OK) { stop_cpu; } osThreadSetPriority(osThreadGetId(), osPriorityLow); if (addTraceProtected("thread0 yields") != TRACE_OK) { stop_cpu; } osThreadYield(); // suspend thread if (addTraceProtected("thread0 back from yield") != TRACE_OK) { stop_cpu; } if (addTraceProtected("thread0 delete sem0") != TRACE_OK) { stop_cpu; } if (Delete_Semaphore0() != 0) { stop_cpu; } // This should terminate the current thread0 thread if (Terminate_thread0() != 0) { stop_cpu; } } }
void PriorityTapeGroupTest::testSchedule() { auto_ptr<PriorityTape> priority0(new PriorityTape()); auto_ptr<PriorityTape> priority1(new PriorityTape()); auto_ptr<PriorityTape> priority2(new PriorityTape()); auto_ptr<PriorityTape> priority3(new PriorityTape()); PriorityTapeTask task0(priority0.get(),10,0,1); PriorityTapeTask task1(priority1.get(),10,0,1); PriorityTapeTask task2(priority2.get(),10,0,1); PriorityTapeTask task3(priority3.get(),10,0,1); vector<string> tapes; tapes.push_back("01000001"); tapes.push_back("01000002"); tapes.push_back("02000001"); tapes.push_back("02000002"); vector<PriorityTape *> priorities; priorities.push_back(priority0.get()); priorities.push_back(priority1.get()); priorities.push_back(priority2.get()); priorities.push_back(priority3.get()); auto_ptr<PriorityTapeGroup> group(new PriorityTapeGroup(tapes,priorities)); task0.Start(); task1.Start(); task2.Start(); task3.Start(); boost::this_thread::sleep( boost::posix_time::milliseconds(10) ); CPPUNIT_ASSERT( false == task0.Finished() ); CPPUNIT_ASSERT( false == task1.Finished() ); CPPUNIT_ASSERT( false == task2.Finished() ); CPPUNIT_ASSERT( false == task3.Finished() ); group->Enable(true); task0.Start(); task1.Start(); task2.Start(); task3.Start(); boost::this_thread::sleep( boost::posix_time::milliseconds(10) ); CPPUNIT_ASSERT( true == task0.Finished() ); CPPUNIT_ASSERT( true == task1.Finished() ); CPPUNIT_ASSERT( true == task2.Finished() ); CPPUNIT_ASSERT( true == task3.Finished() ); group->Enable(false); task0.Start(); task1.Start(); task2.Start(); task3.Start(); boost::this_thread::sleep( boost::posix_time::milliseconds(10) ); CPPUNIT_ASSERT( false == task0.Finished() ); CPPUNIT_ASSERT( false == task1.Finished() ); CPPUNIT_ASSERT( false == task2.Finished() ); CPPUNIT_ASSERT( false == task3.Finished() ); boost::posix_time::ptime begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( false == group->Request(false,10,0) ); boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time(); int duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 && duration <= 12 ); group->Enable(true); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( false == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT_MESSAGE( boost::lexical_cast<string>(duration), duration >= 10 && duration <= 12 ); CPPUNIT_ASSERT( duration >= 10 && duration <= 12 ); group->Release(false); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); group->Release(false); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(true,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(true,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( false == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT_MESSAGE( boost::lexical_cast<string>(duration), duration >= 10 && duration <= 12 ); CPPUNIT_ASSERT( duration >= 10 && duration <= 12 ); group->Release(true); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( false == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration >= 10 && duration <= 12 ); group->Release(false); begin = boost::posix_time::microsec_clock::local_time(); CPPUNIT_ASSERT( true == group->Request(false,10,0) ); end = boost::posix_time::microsec_clock::local_time(); duration = (end - begin).total_milliseconds(); CPPUNIT_ASSERT( duration <= 5 ); group->Release(true); group->Release(false); }