X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Scheduler%2FFIFORunner.cc;h=446859c74ee8695b54f4320cc8e26b98f23bed16;hb=bcbf09e13c018a2bf2391d3484450f3be84931eb;hp=ffaf11edf081ef06ffdd89920adb73ed17b267f1;hpb=d785ab5820076da44b4a71b05cc231ef7e97c7bf;p=senf.git diff --git a/Scheduler/FIFORunner.cc b/Scheduler/FIFORunner.cc index ffaf11e..446859c 100644 --- a/Scheduler/FIFORunner.cc +++ b/Scheduler/FIFORunner.cc @@ -30,12 +30,13 @@ #include #include #include "../Utils/Exception.hh" +#include "../Utils/senfassert.hh" //#include "FIFORunner.mpp" #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -prefix_ senf::scheduler::FIFORunner::FIFORunner() +prefix_ senf::scheduler::detail::FIFORunner::FIFORunner() : tasks_ (), next_ (tasks_.end()), watchdogMs_ (1000), watchdogCount_(0), hangCount_ (0) { struct sigevent ev; @@ -58,9 +59,12 @@ prefix_ senf::scheduler::FIFORunner::FIFORunner() sigaddset(&mask, SIGURG); if (sigprocmask(SIG_UNBLOCK, &mask, 0) < 0) SENF_THROW_SYSTEM_EXCEPTION("sigprocmask()"); + + tasks_.push_back(highPriorityEnd_); + tasks_.push_back(normalPriorityEnd_); } -prefix_ senf::scheduler::FIFORunner::~FIFORunner() +prefix_ senf::scheduler::detail::FIFORunner::~FIFORunner() { timer_delete(watchdogId_); signal(SIGURG, SIG_DFL); @@ -88,7 +92,7 @@ prefix_ senf::scheduler::FIFORunner::~FIFORunner() // will mostly be localized to the end of the queue. only occasionally one of the dormant tasks will // be runnable. This additional traversal time will be amortized over a larger time. -prefix_ void senf::scheduler::FIFORunner::dequeue(TaskInfo * task) +prefix_ void senf::scheduler::detail::FIFORunner::dequeue(TaskInfo * task) { TaskList::iterator i (TaskList::current(*task)); if (next_ == i) @@ -96,65 +100,108 @@ prefix_ void senf::scheduler::FIFORunner::dequeue(TaskInfo * task) tasks_.erase(i); } -namespace { - struct NullTask - : public senf::scheduler::FIFORunner::TaskInfo - { - void run() {}; - }; +prefix_ void senf::scheduler::detail::FIFORunner::run() +{ + struct itimerspec timer; + timer.it_interval.tv_sec = watchdogMs_ / 1000; + timer.it_interval.tv_nsec = (watchdogMs_ % 1000) * 1000000ul; + timer.it_value.tv_sec = timer.it_interval.tv_sec; + timer.it_value.tv_nsec = timer.it_interval.tv_nsec; + + if (timer_settime(watchdogId_, 0, &timer, 0) < 0) + SENF_THROW_SYSTEM_EXCEPTION("timer_settime()"); + + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_nsec = 0; + timer.it_value.tv_sec = 0; + timer.it_value.tv_nsec = 0; + + try { + TaskList::iterator f (tasks_.begin()); + TaskList::iterator l (TaskList::current(highPriorityEnd_)); + run(f, l); + + f = l; ++f; + l = TaskList::current(normalPriorityEnd_); + run(f, l); + + f = l; ++f; + l = tasks_.end(); + run(f, l); + } + catch(...) { + timer_settime(watchdogId_, 0, &timer, 0); + throw; + } + + if (timer_settime(watchdogId_, 0, &timer, 0) < 0) + SENF_THROW_SYSTEM_EXCEPTION("timer_settime()"); } -prefix_ void senf::scheduler::FIFORunner::run() + +prefix_ void senf::scheduler::detail::FIFORunner::run(TaskList::iterator f, TaskList::iterator l) { + if (f == l) + // We'll have problems inserting NullTask between f and l below, so just explicitly bail out + return; + // This algorithm is carefully adjusted to make it work even when arbitrary tasks are removed // from the queue // - Before we begin, we add a NullTask to the queue. The only purpose of this node is, to mark // the current end of the queue. The iterator to this node becomes the end iterator of the // range to process - // - We update the TaskInfo and move it to the end of the queue before calling the callback so + // - We update the TaskInfo and move it to the next queue Element before calling the callback so // we don't access the TaskInfo if it is removed while the callback is running // - We keep the next to-be-processed node in a class variable which is checked and updated // whenever a node is removed. + NullTask null; - tasks_.push_back(null); + tasks_.insert(l, null); TaskList::iterator end (TaskList::current(null)); - next_ = tasks_.begin(); - struct itimerspec timer; - timer.it_interval.tv_sec = watchdogMs_ / 1000; - timer.it_interval.tv_nsec = (watchdogMs_ % 1000) * 1000000ul; - timer.it_value.tv_sec = timer.it_interval.tv_sec; - timer.it_value.tv_nsec = timer.it_interval.tv_nsec; - if (timer_settime(watchdogId_, 0, &timer, 0) < 0) - SENF_THROW_SYSTEM_EXCEPTION("timer_settime()"); - while (next_ != end) { - TaskInfo & task (*next_); - if (task.runnable) { - task.runnable = false; - runningName_ = task.name; -# ifdef SENF_DEBUG - runningBacktrace_ = task.backtrace; -# endif - TaskList::iterator i (next_); - ++ next_; - tasks_.splice(tasks_.end(), tasks_, i); - watchdogCount_ = 1; - task.run(); + next_ = f; + try { + while (next_ != end) { + TaskInfo & task (*next_); + if (task.runnable_) { + task.runnable_ = false; + runningName_ = task.name(); +# ifdef SENF_DEBUG + runningBacktrace_ = task.backtrace_; +# endif + TaskList::iterator i (next_); + ++ next_; + tasks_.splice(l, tasks_, i); + watchdogCount_ = 1; + task.run(); + } + else + ++ next_; } - else - ++ next_; + } + catch (...) { + watchdogCount_ = 0; + next_ = l; + throw; } watchdogCount_ = 0; - timer.it_interval.tv_sec = 0; - timer.it_interval.tv_nsec = 0; - timer.it_value.tv_sec = 0; - timer.it_value.tv_nsec = 0; - if (timer_settime(watchdogId_, 0, &timer, 0) < 0) - SENF_THROW_SYSTEM_EXCEPTION("timer_settime()"); - tasks_.erase(end); - next_ = tasks_.end(); + next_ = l; +} + +prefix_ senf::scheduler::detail::FIFORunner::TaskList::iterator +senf::scheduler::detail::FIFORunner::priorityEnd(TaskInfo::Priority p) +{ + switch (p) { + case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_LOW : + return tasks_.end(); + case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_NORMAL : + return TaskList::current(normalPriorityEnd_); + case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_HIGH : + return TaskList::current(highPriorityEnd_); + } + return tasks_.begin(); } -prefix_ void senf::scheduler::FIFORunner::watchdog(int, siginfo_t * si, void *) +prefix_ void senf::scheduler::detail::FIFORunner::watchdog(int, siginfo_t * si, void *) { FIFORunner & runner (*static_cast(si->si_value.sival_ptr)); if (runner.watchdogCount_ > 0) {