1d7b28bfaea5beea4496ed3b0f032a96eb37a4d3
[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 #include "SignalEvent.hh"
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 namespace {
44     bool terminate_ (false);
45 }
46
47 prefix_ void senf::scheduler::terminate()
48 {
49     terminate_ = true;
50 }
51
52 prefix_ void senf::scheduler::process()
53 {
54     terminate_ = false;
55     while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
56                              detail::TimerDispatcher::instance().empty() &&
57                              detail::FileDispatcher::instance().empty())) {
58         detail::SignalDispatcher::instance().unblockSignals();
59         detail::TimerDispatcher::instance().unblockSignals();
60         detail::FdManager::instance().processOnce();
61         detail::TimerDispatcher::instance().blockSignals();
62         detail::SignalDispatcher::instance().blockSignals();
63         detail::FileDispatcher::instance().prepareRun();
64         detail::FIFORunner::instance().run();
65     }
66 }
67
68 prefix_ void senf::scheduler::restart()
69 {
70     detail::FdManager*        fdm (&detail::FdManager::instance());
71     detail::FIFORunner*       ffr (&detail::FIFORunner::instance());
72     detail::FdDispatcher*     fdd (&detail::FdDispatcher::instance());
73     detail::TimerDispatcher*  tdd (&detail::TimerDispatcher::instance());
74     detail::SignalDispatcher* sdd (&detail::SignalDispatcher::instance());
75     detail::FileDispatcher*   fld (&detail::FileDispatcher::instance());
76     
77     fld->~FileDispatcher();
78     sdd->~SignalDispatcher();
79     tdd->~TimerDispatcher();
80     fdd->~FdDispatcher();
81     ffr->~FIFORunner();
82     fdm->~FdManager();
83     
84     new (fdm) detail::FdManager();
85     new (ffr) detail::FIFORunner();
86     new (fdd) detail::FdDispatcher();
87     new (tdd) detail::TimerDispatcher();
88     new (sdd) detail::SignalDispatcher();
89     new (fld) detail::FileDispatcher();
90 }
91
92 ///////////////////////////////////////////////////////////////////////////
93 // senf::schedulerLogTimeSource
94
95 prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
96     const
97 {
98     return eventTime();
99 }
100
101 ///////////////////////////////cc.e////////////////////////////////////////
102 #undef prefix_
103
104 \f
105 // Local Variables:
106 // mode: c++
107 // fill-column: 100
108 // c-file-style: "senf"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -u test"
112 // comment-column: 40
113 // End: