Scheduler: Restructure signal blocking/unblocking
[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 prefix_ void senf::scheduler::process()
52 {
53     try {
54         detail::FIFORunner::instance().startWatchdog();
55         detail::SignalDispatcher::instance().unblockSignals();
56         detail::TimerDispatcher::instance().unblockSignals();
57         terminate_ = false;
58         while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
59                                  detail::TimerDispatcher::instance().empty() &&
60                                  detail::FileDispatcher::instance().empty())) {
61             detail::FdManager::instance().processOnce();
62             detail::FileDispatcher::instance().prepareRun();
63             detail::EventHookDispatcher::instance().prepareRun();
64             detail::FIFORunner::instance().run();
65         }
66     }
67     catch(...) {
68         detail::TimerDispatcher::instance().blockSignals();
69         detail::SignalDispatcher::instance().blockSignals();
70         detail::FIFORunner::instance().stopWatchdog();
71         throw;
72     }
73     detail::TimerDispatcher::instance().blockSignals();
74     detail::SignalDispatcher::instance().blockSignals();
75     detail::FIFORunner::instance().stopWatchdog();
76 }
77
78 prefix_ void senf::scheduler::restart()
79 {
80     detail::FdManager*            fdm (&detail::FdManager::instance());
81     detail::FIFORunner*           ffr (&detail::FIFORunner::instance());
82     detail::FdDispatcher*         fdd (&detail::FdDispatcher::instance());
83     detail::TimerDispatcher*      tdd (&detail::TimerDispatcher::instance());
84     detail::SignalDispatcher*     sdd (&detail::SignalDispatcher::instance());
85     detail::FileDispatcher*       fld (&detail::FileDispatcher::instance());
86     detail::EventHookDispatcher*  eed (&detail::EventHookDispatcher::instance());
87
88     eed->~EventHookDispatcher();
89     fld->~FileDispatcher();
90     sdd->~SignalDispatcher();
91     tdd->~TimerDispatcher();
92     fdd->~FdDispatcher();
93     ffr->~FIFORunner();
94     fdm->~FdManager();
95     
96     new (fdm) detail::FdManager();
97     new (ffr) detail::FIFORunner();
98     new (fdd) detail::FdDispatcher();
99     new (tdd) detail::TimerDispatcher();
100     new (sdd) detail::SignalDispatcher();
101     new (fld) detail::FileDispatcher();
102     new (eed) detail::EventHookDispatcher();
103 }
104
105 prefix_ bool senf::scheduler::empty()
106 {
107     return detail::FdDispatcher::instance().empty() 
108         && detail::TimerDispatcher::instance().empty()
109         && detail::FileDispatcher::instance().empty()
110         && detail::SignalDispatcher::instance().empty()
111         && detail::EventHookDispatcher::instance().empty();
112 }
113
114 ///////////////////////////////////////////////////////////////////////////
115 // senf::schedulerLogTimeSource
116
117 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
118     const
119 {
120     return eventTime();
121 }
122
123 ///////////////////////////////cc.e////////////////////////////////////////
124 #undef prefix_
125
126 \f
127 // Local Variables:
128 // mode: c++
129 // fill-column: 100
130 // c-file-style: "senf"
131 // indent-tabs-mode: nil
132 // ispell-local-dictionary: "american"
133 // compile-command: "scons -u test"
134 // comment-column: 40
135 // End: