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 Scheduler non-inline non-template implementation
26 \idea Multithreading support: To support multithreading, the
27 static member Scheduler::instance() must return a thread-local
28 value (that is Scheduler::instance() must allocate one Scheduler
29 instance per thread). Another possibility would be to distribute
30 the async load unto several threads (one scheduler for multiple
34 #include "Scheduler.hh"
35 //#include "Scheduler.ih"
40 ///////////////////////////////cc.p////////////////////////////////////////
43 bool terminate_ (false);
44 bool running_ (false);
47 prefix_ void senf::scheduler::terminate()
52 prefix_ void senf::scheduler::yield()
54 senf::scheduler::detail::FIFORunner::instance().yield();
57 prefix_ bool senf::scheduler::running()
62 prefix_ senf::ClockService::clock_type senf::scheduler::now()
64 return running() ? eventTime() : ClockService::now();
69 // We don't want try { } catch(...) { ... throw; } since that will make debugging more
70 // difficult: the stack backtrace for an unexpected exception would always end here.
71 struct SchedulerScopedInit
75 senf::scheduler::detail::FIFORunner::instance().startWatchdog();
76 senf::scheduler::detail::SignalDispatcher::instance().unblockSignals();
77 senf::scheduler::detail::TimerDispatcher::instance().enable();
81 ~SchedulerScopedInit()
83 senf::scheduler::detail::TimerDispatcher::instance().disable();
84 senf::scheduler::detail::SignalDispatcher::instance().blockSignals();
85 senf::scheduler::detail::FIFORunner::instance().stopWatchdog();
91 prefix_ void senf::scheduler::process()
93 SchedulerScopedInit initScheduler;
96 detail::TimerDispatcher::instance().reschedule();
97 while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
98 detail::TimerDispatcher::instance().empty() &&
99 detail::FileDispatcher::instance().empty())) {
100 detail::FdManager::instance().processOnce();
101 detail::FileDispatcher::instance().prepareRun();
102 detail::EventHookDispatcher::instance().prepareRun();
103 detail::TimerDispatcher::instance().prepareRun();
104 detail::FIFORunner::instance().run();
105 detail::TimerDispatcher::instance().reschedule();
109 prefix_ void senf::scheduler::restart()
111 detail::FdManager* fdm (&detail::FdManager::instance());
112 detail::FIFORunner* ffr (&detail::FIFORunner::instance());
113 detail::FdDispatcher* fdd (&detail::FdDispatcher::instance());
114 detail::TimerDispatcher* tdd (&detail::TimerDispatcher::instance());
115 detail::SignalDispatcher* sdd (&detail::SignalDispatcher::instance());
116 detail::FileDispatcher* fld (&detail::FileDispatcher::instance());
117 detail::EventHookDispatcher* eed (&detail::EventHookDispatcher::instance());
119 eed->~EventHookDispatcher();
120 fld->~FileDispatcher();
121 sdd->~SignalDispatcher();
122 tdd->~TimerDispatcher();
123 fdd->~FdDispatcher();
127 new (fdm) detail::FdManager();
128 new (ffr) detail::FIFORunner();
129 new (fdd) detail::FdDispatcher();
130 new (tdd) detail::TimerDispatcher();
131 new (sdd) detail::SignalDispatcher();
132 new (fld) detail::FileDispatcher();
133 new (eed) detail::EventHookDispatcher();
136 prefix_ bool senf::scheduler::empty()
138 return detail::FdDispatcher::instance().empty()
139 && detail::TimerDispatcher::instance().empty()
140 && detail::FileDispatcher::instance().empty()
141 && detail::SignalDispatcher::instance().empty()
142 && detail::EventHookDispatcher::instance().empty();
145 prefix_ void senf::scheduler::hiresTimers()
148 if (haveScalableHiresTimers())
149 detail::TimerDispatcher::instance().timerSource(
150 std::auto_ptr<detail::TimerSource>(new detail::TimerFDTimerSource()));
153 detail::TimerDispatcher::instance().timerSource(
154 std::auto_ptr<detail::TimerSource>(new detail::POSIXTimerSource()));
157 ///////////////////////////////////////////////////////////////////////////
158 // senf::schedulerLogTimeSource
160 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
166 ///////////////////////////////////////////////////////////////////////////
167 // senf::scheduler::BlockSignals
169 prefix_ senf::scheduler::BlockSignals::BlockSignals(bool initiallyBlocked)
172 ::sigfillset(&allSigs_);
173 if (initiallyBlocked)
177 prefix_ void senf::scheduler::BlockSignals::block()
181 ::sigprocmask(SIG_BLOCK, &allSigs_, &savedSigs_);
185 prefix_ void senf::scheduler::BlockSignals::unblock()
189 ::sigprocmask(SIG_SETMASK, &savedSigs_, 0);
193 ///////////////////////////////cc.e////////////////////////////////////////
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"
204 // comment-column: 40