PPI: Add missing TargetDgramWriter doku
[senf.git] / 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 ///////////////////////////////cc.p////////////////////////////////////////
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_ bool senf::scheduler::running()
53 {
54     return running_;
55 }
56
57 prefix_ senf::ClockService::clock_type senf::scheduler::now()
58 {
59     return running() ? eventTime() : ClockService::now();
60 }
61
62 namespace {
63     
64     // We don't want try { } catch(...) { ... throw; } since that will make debugging more
65     // difficult: the stack backtrace for an unexpected exception would always end here.
66     struct SchedulerScopedInit
67     {
68         SchedulerScopedInit() 
69             {
70                 senf::scheduler::detail::FIFORunner::instance().startWatchdog();
71                 senf::scheduler::detail::SignalDispatcher::instance().unblockSignals();
72                 senf::scheduler::detail::TimerDispatcher::instance().enable();
73                 running_ = true;
74             }
75
76         ~SchedulerScopedInit()
77             {
78                 senf::scheduler::detail::TimerDispatcher::instance().disable();
79                 senf::scheduler::detail::SignalDispatcher::instance().blockSignals();
80                 senf::scheduler::detail::FIFORunner::instance().stopWatchdog();
81                 running_ = false;
82             }
83     };
84 }
85
86 prefix_ void senf::scheduler::process()
87 {
88     SchedulerScopedInit initScheduler;
89     terminate_ = false;
90     running_ = true;
91     detail::TimerDispatcher::instance().reschedule();
92     while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
93                              detail::TimerDispatcher::instance().empty() &&
94                              detail::FileDispatcher::instance().empty())) {
95         detail::FdManager::instance().processOnce();
96         detail::FileDispatcher::instance().prepareRun();
97         detail::EventHookDispatcher::instance().prepareRun();
98         detail::TimerDispatcher::instance().prepareRun();
99         detail::FIFORunner::instance().run();
100         detail::TimerDispatcher::instance().reschedule();
101     }
102 }
103
104 prefix_ void senf::scheduler::restart()
105 {
106     detail::FdManager*            fdm (&detail::FdManager::instance());
107     detail::FIFORunner*           ffr (&detail::FIFORunner::instance());
108     detail::FdDispatcher*         fdd (&detail::FdDispatcher::instance());
109     detail::TimerDispatcher*      tdd (&detail::TimerDispatcher::instance());
110     detail::SignalDispatcher*     sdd (&detail::SignalDispatcher::instance());
111     detail::FileDispatcher*       fld (&detail::FileDispatcher::instance());
112     detail::EventHookDispatcher*  eed (&detail::EventHookDispatcher::instance());
113
114     eed->~EventHookDispatcher();
115     fld->~FileDispatcher();
116     sdd->~SignalDispatcher();
117     tdd->~TimerDispatcher();
118     fdd->~FdDispatcher();
119     ffr->~FIFORunner();
120     fdm->~FdManager();
121     
122     new (fdm) detail::FdManager();
123     new (ffr) detail::FIFORunner();
124     new (fdd) detail::FdDispatcher();
125     new (tdd) detail::TimerDispatcher();
126     new (sdd) detail::SignalDispatcher();
127     new (fld) detail::FileDispatcher();
128     new (eed) detail::EventHookDispatcher();
129 }
130
131 prefix_ bool senf::scheduler::empty()
132 {
133     return detail::FdDispatcher::instance().empty() 
134         && detail::TimerDispatcher::instance().empty()
135         && detail::FileDispatcher::instance().empty()
136         && detail::SignalDispatcher::instance().empty()
137         && detail::EventHookDispatcher::instance().empty();
138 }
139
140 prefix_ void senf::scheduler::hiresTimers()
141 {
142 #ifdef HAVE_TIMERFD
143     if (haveScalableHiresTimers())
144         detail::TimerDispatcher::instance().timerSource(
145             std::auto_ptr<detail::TimerSource>(new detail::TimerFDTimerSource()));
146     else
147 #endif
148         detail::TimerDispatcher::instance().timerSource(
149             std::auto_ptr<detail::TimerSource>(new detail::POSIXTimerSource()));
150 }
151
152 ///////////////////////////////////////////////////////////////////////////
153 // senf::schedulerLogTimeSource
154
155 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
156     const
157 {
158     return eventTime();
159 }
160
161 ///////////////////////////////////////////////////////////////////////////
162 // senf::scheduler::BlockSignals
163
164 prefix_ senf::scheduler::BlockSignals::BlockSignals(bool initiallyBlocked)
165     : blocked_ (false)
166 {
167     ::sigfillset(&allSigs_);
168     if (initiallyBlocked)
169         block();
170 }
171
172 prefix_ void senf::scheduler::BlockSignals::block()
173 {
174     if (blocked_)
175         return;
176     ::sigprocmask(SIG_BLOCK, &allSigs_, &savedSigs_);
177     blocked_ = true;
178 }
179
180 prefix_ void senf::scheduler::BlockSignals::unblock()
181 {
182     if (!blocked_)
183         return;
184     ::sigprocmask(SIG_SETMASK, &savedSigs_, 0);
185     blocked_ = false;
186 }
187
188 ///////////////////////////////cc.e////////////////////////////////////////
189 #undef prefix_
190
191 \f
192 // Local Variables:
193 // mode: c++
194 // fill-column: 100
195 // c-file-style: "senf"
196 // indent-tabs-mode: nil
197 // ispell-local-dictionary: "american"
198 // compile-command: "scons -u test"
199 // comment-column: 40
200 // End: