replaced all BOOST_CHECK_NO_THROW with SENF_CHECK_NO_THROW
[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 }
45
46 prefix_ void senf::scheduler::terminate()
47 {
48     terminate_ = true;
49 }
50
51 namespace {
52     
53     // We don't want try { } catch(...) { ... throw; } since that will make debugging more
54     // difficult: the stack backtrace for an unexpected exception would always end here.
55     struct SchedulerScopedInit
56     {
57         SchedulerScopedInit() 
58             {
59                 senf::scheduler::detail::FIFORunner::instance().startWatchdog();
60                 senf::scheduler::detail::SignalDispatcher::instance().unblockSignals();
61                 senf::scheduler::detail::TimerDispatcher::instance().enable();
62             }
63
64         ~SchedulerScopedInit()
65             {
66                 senf::scheduler::detail::TimerDispatcher::instance().disable();
67                 senf::scheduler::detail::SignalDispatcher::instance().blockSignals();
68                 senf::scheduler::detail::FIFORunner::instance().stopWatchdog();
69             }
70     };
71 }
72
73 prefix_ void senf::scheduler::process()
74 {
75     SchedulerScopedInit initScheduler;
76     terminate_ = false;
77     detail::TimerDispatcher::instance().reschedule();
78     while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
79                              detail::TimerDispatcher::instance().empty() &&
80                              detail::FileDispatcher::instance().empty())) {
81         detail::FdManager::instance().processOnce();
82         detail::FileDispatcher::instance().prepareRun();
83         detail::EventHookDispatcher::instance().prepareRun();
84         detail::TimerDispatcher::instance().prepareRun();
85         detail::FIFORunner::instance().run();
86         detail::TimerDispatcher::instance().reschedule();
87     }
88 }
89
90 prefix_ void senf::scheduler::restart()
91 {
92     detail::FdManager*            fdm (&detail::FdManager::instance());
93     detail::FIFORunner*           ffr (&detail::FIFORunner::instance());
94     detail::FdDispatcher*         fdd (&detail::FdDispatcher::instance());
95     detail::TimerDispatcher*      tdd (&detail::TimerDispatcher::instance());
96     detail::SignalDispatcher*     sdd (&detail::SignalDispatcher::instance());
97     detail::FileDispatcher*       fld (&detail::FileDispatcher::instance());
98     detail::EventHookDispatcher*  eed (&detail::EventHookDispatcher::instance());
99
100     eed->~EventHookDispatcher();
101     fld->~FileDispatcher();
102     sdd->~SignalDispatcher();
103     tdd->~TimerDispatcher();
104     fdd->~FdDispatcher();
105     ffr->~FIFORunner();
106     fdm->~FdManager();
107     
108     new (fdm) detail::FdManager();
109     new (ffr) detail::FIFORunner();
110     new (fdd) detail::FdDispatcher();
111     new (tdd) detail::TimerDispatcher();
112     new (sdd) detail::SignalDispatcher();
113     new (fld) detail::FileDispatcher();
114     new (eed) detail::EventHookDispatcher();
115 }
116
117 prefix_ bool senf::scheduler::empty()
118 {
119     return detail::FdDispatcher::instance().empty() 
120         && detail::TimerDispatcher::instance().empty()
121         && detail::FileDispatcher::instance().empty()
122         && detail::SignalDispatcher::instance().empty()
123         && detail::EventHookDispatcher::instance().empty();
124 }
125
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::schedulerLogTimeSource
128
129 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
130     const
131 {
132     return eventTime();
133 }
134
135 ///////////////////////////////////////////////////////////////////////////
136 // senf::scheduler::BlockSignals
137
138 prefix_ senf::scheduler::BlockSignals::BlockSignals(bool initiallyBlocked)
139     : blocked_ (false)
140 {
141     ::sigfillset(&allSigs_);
142     if (initiallyBlocked)
143         block();
144 }
145
146 prefix_ void senf::scheduler::BlockSignals::block()
147 {
148     if (blocked_)
149         return;
150     ::sigprocmask(SIG_BLOCK, &allSigs_, &savedSigs_);
151     blocked_ = true;
152 }
153
154 prefix_ void senf::scheduler::BlockSignals::unblock()
155 {
156     if (!blocked_)
157         return;
158     ::sigprocmask(SIG_SETMASK, &savedSigs_, 0);
159     blocked_ = false;
160 }
161
162 ///////////////////////////////cc.e////////////////////////////////////////
163 #undef prefix_
164
165 \f
166 // Local Variables:
167 // mode: c++
168 // fill-column: 100
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"
173 // comment-column: 40
174 // End: