5273eb310a0fd2b6cfce1ec0c77e7162677fa90d
[senf.git] / senf / Scheduler / Scheduler.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Scheduler non-inline non-template implementation
25
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
31     threads)
32  */
33
34 #include "Scheduler.hh"
35 //#include "Scheduler.ih"
36
37 // Custom includes
38
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace {
43     bool terminate_ (false);
44     bool running_ (false);
45 }
46
47 prefix_ void senf::scheduler::terminate()
48 {
49     terminate_ = true;
50 }
51
52 prefix_ void senf::scheduler::yield()
53 {
54     detail::FIFORunner::instance().yield();
55 }
56
57 prefix_ bool senf::scheduler::running()
58 {
59     return running_;
60 }
61
62 prefix_ senf::ClockService::clock_type senf::scheduler::now()
63 {
64     return running() ? eventTime() : ClockService::now();
65 }
66
67 namespace {
68
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
72     {
73         SchedulerScopedInit()
74             {
75                 senf::scheduler::detail::FIFORunner::instance().startWatchdog();
76                 senf::scheduler::detail::SignalDispatcher::instance().unblockSignals();
77                 senf::scheduler::detail::TimerDispatcher::instance().enable();
78                 running_ = true;
79             }
80
81         ~SchedulerScopedInit()
82             {
83                 senf::scheduler::detail::TimerDispatcher::instance().disable();
84                 senf::scheduler::detail::SignalDispatcher::instance().blockSignals();
85                 senf::scheduler::detail::FIFORunner::instance().stopWatchdog();
86                 running_ = false;
87             }
88     };
89 }
90
91 prefix_ void senf::scheduler::process()
92 {
93     SchedulerScopedInit initScheduler;
94     terminate_ = false;
95     detail::TimerDispatcher::instance().reschedule();
96     while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
97                              detail::TimerDispatcher::instance().empty() &&
98                              detail::FileDispatcher::instance().empty() &&
99                              detail::IdleEventDispatcher::instance().empty()) ) {
100         detail::FdManager::instance().processOnce();
101         detail::FileDispatcher::instance().prepareRun();
102         detail::EventHookDispatcher::instance().prepareRun();
103         detail::TimerDispatcher::instance().prepareRun();
104         detail::IdleEventDispatcher::instance().prepareRun();
105         detail::FIFORunner::instance().run();
106         detail::TimerDispatcher::instance().reschedule();
107     }
108 }
109
110 prefix_ void senf::scheduler::restart()
111 {
112     detail::FdManager*            fdm (&detail::FdManager::instance());
113     detail::FIFORunner*           ffr (&detail::FIFORunner::instance());
114     detail::FdDispatcher*         fdd (&detail::FdDispatcher::instance());
115     detail::TimerDispatcher*      tdd (&detail::TimerDispatcher::instance());
116     detail::SignalDispatcher*     sdd (&detail::SignalDispatcher::instance());
117     detail::FileDispatcher*       fld (&detail::FileDispatcher::instance());
118     detail::IdleEventDispatcher*  ied (&detail::IdleEventDispatcher::instance());
119     detail::EventHookDispatcher*  eed (&detail::EventHookDispatcher::instance());
120
121     eed->~EventHookDispatcher();
122     ied->~IdleEventDispatcher();
123     fld->~FileDispatcher();
124     sdd->~SignalDispatcher();
125     tdd->~TimerDispatcher();
126     fdd->~FdDispatcher();
127     ffr->~FIFORunner();
128     fdm->~FdManager();
129
130     new (fdm) detail::FdManager();
131     new (ffr) detail::FIFORunner();
132     new (fdd) detail::FdDispatcher();
133     new (tdd) detail::TimerDispatcher();
134     new (sdd) detail::SignalDispatcher();
135     new (fld) detail::FileDispatcher();
136     new (ied) detail::IdleEventDispatcher();
137     new (eed) detail::EventHookDispatcher();
138 }
139
140 prefix_ bool senf::scheduler::empty()
141 {
142     return detail::FdDispatcher::instance().empty()
143         && detail::TimerDispatcher::instance().empty()
144         && detail::FileDispatcher::instance().empty()
145         && detail::SignalDispatcher::instance().empty()
146         && detail::IdleEventDispatcher::instance().empty()
147         && detail::EventHookDispatcher::instance().empty();
148 }
149
150 prefix_ void senf::scheduler::hiresTimers()
151 {
152 #ifdef HAVE_TIMERFD_CREATE
153     if (haveScalableHiresTimers())
154         detail::TimerDispatcher::instance().timerSource(
155             std::auto_ptr<detail::TimerSource>(new detail::TimerFDTimerSource()));
156     else
157 #endif
158         detail::TimerDispatcher::instance().timerSource(
159             std::auto_ptr<detail::TimerSource>(new detail::POSIXTimerSource()));
160 }
161
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 // senf::schedulerLogTimeSource
164
165 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
166     const
167 {
168     return scheduler::now();
169 }
170
171 //-/////////////////////////////////////////////////////////////////////////////////////////////////
172 // senf::scheduler::BlockSignals
173
174 prefix_ senf::scheduler::BlockSignals::BlockSignals(bool initiallyBlocked)
175     : blocked_ (false)
176 {
177     ::sigfillset(&allSigs_);
178     if (initiallyBlocked)
179         block();
180 }
181
182 prefix_ void senf::scheduler::BlockSignals::block()
183 {
184     if (blocked_)
185         return;
186     ::sigprocmask(SIG_BLOCK, &allSigs_, &savedSigs_);
187     blocked_ = true;
188 }
189
190 prefix_ void senf::scheduler::BlockSignals::unblock()
191 {
192     if (!blocked_)
193         return;
194     ::sigprocmask(SIG_SETMASK, &savedSigs_, 0);
195     blocked_ = false;
196 }
197
198 //-/////////////////////////////////////////////////////////////////////////////////////////////////
199 #undef prefix_
200
201 \f
202 // Local Variables:
203 // mode: c++
204 // fill-column: 100
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"
209 // comment-column: 40
210 // End: