4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief FIFORunner non-inline non-template implementation */
26 #include "FIFORunner.hh"
27 //#include "FIFORunner.ih"
32 #include "../Utils/Exception.hh"
33 #include "../Utils/senfassert.hh"
35 //#include "FIFORunner.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 prefix_ senf::scheduler::detail::FIFORunner::FIFORunner()
40 : tasks_ (), next_ (tasks_.end()), watchdogMs_ (1000), watchdogCount_(0), hangCount_ (0)
43 ::memset(&ev, 0, sizeof(ev));
44 ev.sigev_notify = SIGEV_SIGNAL;
45 ev.sigev_signo = SIGURG;
46 ev.sigev_value.sival_ptr = this;
47 if (timer_create(CLOCK_MONOTONIC, &ev, &watchdogId_) < 0)
48 SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
51 ::memset(&sa, 0, sizeof(sa));
52 sa.sa_sigaction = &watchdog;
53 sa.sa_flags = SA_SIGINFO;
54 if (sigaction(SIGURG, &sa, 0) < 0)
55 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
59 sigaddset(&mask, SIGURG);
60 if (sigprocmask(SIG_UNBLOCK, &mask, 0) < 0)
61 SENF_THROW_SYSTEM_EXCEPTION("sigprocmask()");
63 tasks_.push_back(highPriorityEnd_);
64 tasks_.push_back(normalPriorityEnd_);
67 prefix_ senf::scheduler::detail::FIFORunner::~FIFORunner()
69 timer_delete(watchdogId_);
70 signal(SIGURG, SIG_DFL);
73 // At the moment, the FIFORunner is not very efficient with many non-runnable tasks since the
74 // complete list of tasks is traversed on each run().
76 // To optimize this, we woould need a way to find the relative ordering of two tasks in O(1) (at the
77 // moment, this is an O(N) operation by traversing the list).
79 // One idea is, to give each task an 'order' value. Whenever a task is added at the end, it's order
80 // value is set to the order value of the last task + 1. Whenever the order value such added exceeds
81 // some threshold (e.g. 2^31 -1 or some such), the task list is traversed from beginning to end to
82 // assign new consecutive order values. This O(N) operation is so seldom, that it is amortized over
85 // With this value at hand, we can do several optimizations: One idea would be the following: The
86 // runnable set always has two types of tasks: There are tasks, which are heavily active and are
87 // signaled constantly and other tasks which lie dormant most of the time. Those dormant tasks will
88 // end up at the beginning of the task queue.
90 // With the above defined 'ordering' field available, we can manage an iterator pointing to the
91 // first and the last runnable task. This will often help a lot since the group of runnable tasks
92 // will mostly be localized to the end of the queue. only occasionally one of the dormant tasks will
93 // be runnable. This additional traversal time will be amortized over a larger time.
95 prefix_ void senf::scheduler::detail::FIFORunner::dequeue(TaskInfo * task)
97 TaskList::iterator i (TaskList::current(*task));
103 prefix_ void senf::scheduler::detail::FIFORunner::run()
105 struct itimerspec timer;
106 timer.it_interval.tv_sec = watchdogMs_ / 1000;
107 timer.it_interval.tv_nsec = (watchdogMs_ % 1000) * 1000000ul;
108 timer.it_value.tv_sec = timer.it_interval.tv_sec;
109 timer.it_value.tv_nsec = timer.it_interval.tv_nsec;
111 if (timer_settime(watchdogId_, 0, &timer, 0) < 0)
112 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
114 timer.it_interval.tv_sec = 0;
115 timer.it_interval.tv_nsec = 0;
116 timer.it_value.tv_sec = 0;
117 timer.it_value.tv_nsec = 0;
120 TaskList::iterator f (tasks_.begin());
121 TaskList::iterator l (TaskList::current(highPriorityEnd_));
125 l = TaskList::current(normalPriorityEnd_);
133 timer_settime(watchdogId_, 0, &timer, 0);
137 if (timer_settime(watchdogId_, 0, &timer, 0) < 0)
138 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
142 prefix_ void senf::scheduler::detail::FIFORunner::run(TaskList::iterator f, TaskList::iterator l)
145 // We'll have problems inserting NullTask between f and l below, so just explicitly bail out
148 // This algorithm is carefully adjusted to make it work even when arbitrary tasks are removed
150 // - Before we begin, we add a NullTask to the queue. The only purpose of this node is, to mark
151 // the current end of the queue. The iterator to this node becomes the end iterator of the
153 // - We update the TaskInfo and move it to the next queue Element before calling the callback so
154 // we don't access the TaskInfo if it is removed while the callback is running
155 // - We keep the next to-be-processed node in a class variable which is checked and updated
156 // whenever a node is removed.
159 tasks_.insert(l, null);
160 TaskList::iterator end (TaskList::current(null));
163 while (next_ != end) {
164 TaskInfo & task (*next_);
165 if (task.runnable_) {
166 task.runnable_ = false;
167 runningName_ = task.name();
169 runningBacktrace_ = task.backtrace_;
171 TaskList::iterator i (next_);
173 tasks_.splice(l, tasks_, i);
190 prefix_ senf::scheduler::detail::FIFORunner::TaskList::iterator
191 senf::scheduler::detail::FIFORunner::priorityEnd(TaskInfo::Priority p)
194 case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_LOW :
196 case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_NORMAL :
197 return TaskList::current(normalPriorityEnd_);
198 case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_HIGH :
199 return TaskList::current(highPriorityEnd_);
201 return tasks_.begin();
204 prefix_ void senf::scheduler::detail::FIFORunner::watchdog(int, siginfo_t * si, void *)
206 FIFORunner & runner (*static_cast<FIFORunner *>(si->si_value.sival_ptr));
207 if (runner.watchdogCount_ > 0) {
208 ++ runner.watchdogCount_;
209 if (runner.watchdogCount_ > 2) {
210 ++ runner.hangCount_;
211 write(1, "\n\n*** Scheduler task hanging: ", 30);
212 write(1, runner.runningName_.c_str(), runner.runningName_.size());
215 write(1, "Task was initialized at\n", 24);
216 write(1, runner.runningBacktrace_.c_str(), runner.runningBacktrace_.size());
223 ///////////////////////////////cc.e////////////////////////////////////////
225 //#include "FIFORunner.mpp"
231 // comment-column: 40
232 // c-file-style: "senf"
233 // indent-tabs-mode: nil
234 // ispell-local-dictionary: "american"
235 // compile-command: "scons -u test"