Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

scheduler.cpp

Go to the documentation of this file.
00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/core/scheduler.hpp> 00019 #include <ptf/core/timeinterval.hpp> 00020 #include <ptf/core/ptfcore.hpp> 00021 #include <ptf/environment/graphic.hpp> 00022 #include <stdexcept> 00023 00027 Scheduler::Scheduler() { 00028 quitFlag = false; 00029 } 00030 00034 Scheduler::~Scheduler() { 00035 // nothing to do here 00036 } 00037 00043 void Scheduler::schedule() { 00044 // get object for easier access 00045 PtfCore& ptf = PtfCore::getSingleton(); 00046 // print message 00047 ptf.getMessageLog() << MESSAGE("Running test.\n"); 00048 // time interval object 00049 TimeInterval time; 00050 time.measureA(); 00051 // main loop which runs as long as task are available 00052 while (!quitFlag && ptf.getSequenceIterator().hasNextTaskId()) { 00053 // get the id of the next task 00054 int taskId = ptf.getSequenceIterator().getNextTaskId(); 00055 // check task id 00056 if ((taskId > (int)ptf.getTasks().size()) || (ptf.getTasks()[taskId] == 0 )) { 00057 ptf.getMessageLog() << ERROR(" Bad task id in sequence file.\n"); 00058 throw std::runtime_error("Bad task id in sequence file."); 00059 } 00060 // create time interval objects used for storing execution times of 00061 // the three task methods being called 00062 TimeInterval* tprepare = new TimeInterval(); 00063 TimeInterval* trun = new TimeInterval(); 00064 TimeInterval* tfinish = new TimeInterval(); 00065 // run the prepare method of the task and get execution time 00066 tprepare->measureA(); 00067 ptf.getTasks()[taskId]->prepare(); 00068 tprepare->measureB(); 00069 wait(ptf.getTasks()[taskId]->getWaitPrepareExecute()); 00070 // run the execute method of the task and get execution time 00071 trun->measureA(); 00072 ptf.getTasks()[taskId]->run(); 00073 trun->measureB(); 00074 wait(ptf.getTasks()[taskId]->getWaitExecuteFinish()); 00075 // run the execute method of the task and get execution time 00076 tfinish->measureA(); 00077 ptf.getTasks()[taskId]->finish(); 00078 tfinish->measureB(); 00079 wait(ptf.getTasks()[taskId]->getWaitFinishNextTask()); 00080 // put gathered info in a task record (guarded by Mutex) 00081 TaskRecord* trec = new TaskRecord(taskId, tprepare, trun, tfinish); 00082 ptf.getTaskRecordsMutex().enterMutex(); 00083 ptf.getTaskRecords().push_back(trec); 00084 ptf.getTaskRecordsMutex().leaveMutex(); 00085 // print status 00086 timespec delta1, delta2, delta3; 00087 delta1 = tprepare->getDifference(); 00088 delta2 = trun->getDifference(); 00089 delta3 = tfinish->getDifference(); 00090 // task log 00091 ptf.getTaskLog() << "Task " << taskId << ":\n" 00092 << "\tPREPARE " << tprepare->toString() 00093 << "\tdelta " << delta1 << "\n" 00094 << "\tRUN " << trun->toString() 00095 << "\tdelta " << delta2 << "\n" 00096 << "\tFINISH " << tfinish->toString() 00097 << "\tdelta " << delta3 << "\n"; 00098 // call reaction handler 00099 ptf.getReactionHandler().handle(); 00100 } 00101 // print message 00102 ptf.getMessageLog() << MESSAGE("Finished test.\n"); 00103 } 00104 00109 void Scheduler::wait(timespec time) { 00110 TimeInterval duration; 00111 duration.measureA(); 00112 duration.measureB(); 00113 // perform wait 00114 while (duration.compareDifference(time) > 0) { 00115 duration.measureB(); 00116 } 00117 } 00118 00122 void Scheduler::quit() { 00123 PtfCore::getSingleton().getMessageLog() 00124 << MESSAGE("Scheduler caught quit message. Exiting as soon as possible.\n"); 00125 quitFlag = true; 00126 }

Generated on Fri Dec 17 14:54:23 2004 for Psychological Test Framework by doxygen 1.3.8