04afdcd049dcf38480a4cc54dd0f95cb93d2930c
[senf.git] / Scheduler / FIFORunner.cci
1 // $Id$
2 //
3 // Copyright (C) 2008 
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 FIFORunner inline non-template implementation */
25
26 //#include "FIFORunner.ih"
27
28 // Custom includes
29 #ifdef SENF_DEBUG
30 #include <sstream>
31 #include "../Utils/Backtrace.hh"
32 #endif
33
34 #define prefix_ inline
35 ///////////////////////////////cci.p///////////////////////////////////////
36
37 ///////////////////////////////////////////////////////////////////////////
38 //  senf::scheduler::detail::FIFORunner::TaskInfo
39
40 prefix_ senf::scheduler::detail::FIFORunner::TaskInfo::TaskInfo(std::string const & name,
41                                                                 Priority priority)
42     : Event(name), runnable_ (false), priority_ (priority)
43 {}
44
45 prefix_ senf::scheduler::detail::FIFORunner::TaskInfo::~TaskInfo()
46 {}
47
48 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::setRunnable()
49 {
50     runnable_ = true;
51 }
52
53 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::run()
54 {
55     countRun();
56     // Be sure to run v_run last since the callback may destroy this instance
57     v_run();
58 }
59
60 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::runnable()
61     const
62 {
63     return runnable_;
64 }
65
66 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::v_enabled()
67     const
68 {
69     return TaskListBase::linked();
70 }
71
72 ///////////////////////////////////////////////////////////////////////////
73 // senf::scheduler::detail::FIFORunner::NullTask
74
75 prefix_ senf::scheduler::detail::FIFORunner::NullTask::NullTask()
76     : senf::scheduler::detail::FIFORunner::TaskInfo ("<null>") 
77 {}
78
79 prefix_ senf::scheduler::detail::FIFORunner::NullTask::~NullTask()
80 {
81     if (TaskListBase::linked())
82         FIFORunner::instance().dequeue(this);
83 }
84
85 prefix_ void senf::scheduler::detail::FIFORunner::NullTask::v_run()
86 {}
87
88 prefix_ char const * senf::scheduler::detail::FIFORunner::NullTask::v_type()
89     const
90 {
91     return 0;
92 }
93
94 prefix_ std::string senf::scheduler::detail::FIFORunner::NullTask::v_info()
95     const
96 {
97     return "";
98 }
99
100 ///////////////////////////////////////////////////////////////////////////
101 // senf::scheduler::detail::FIFORunner
102
103 prefix_ void senf::scheduler::detail::FIFORunner::enqueue(TaskInfo * task)
104 {
105     tasks_.insert(priorityEnd(task->priority_), *task);
106 #ifdef SENF_DEBUG
107     std::stringstream ss;
108     backtrace(ss, 32);
109     task->backtrace_ = ss.str();
110 #endif
111 }
112
113 prefix_ void senf::scheduler::detail::FIFORunner::taskTimeout(unsigned ms)
114 {
115     watchdogMs_ = ms;
116     if (watchdogRunning_)
117         startWatchdog();
118 }
119
120 prefix_ unsigned senf::scheduler::detail::FIFORunner::taskTimeout()
121     const
122 {
123     return watchdogMs_;
124 }
125
126 prefix_ unsigned senf::scheduler::detail::FIFORunner::hangCount()
127 {
128     unsigned hc (hangCount_);
129     hangCount_ = 0;
130     return hc;
131 }
132
133 prefix_ senf::scheduler::detail::FIFORunner::iterator
134 senf::scheduler::detail::FIFORunner::begin()
135     const
136 {
137     // We need to filter out elements with e.type() == 0 ... the NullTask temporarily added is such
138     // an element and must be skipped.
139     return boost::make_filter_iterator(
140         EventManager::IteratorFilter(), tasks_.begin(), tasks_.end());
141 }
142
143 prefix_ senf::scheduler::detail::FIFORunner::iterator senf::scheduler::detail::FIFORunner::end()
144     const
145 {
146     return boost::make_filter_iterator(
147         EventManager::IteratorFilter(), tasks_.end(), tasks_.end());
148 }
149
150 ///////////////////////////////cci.e///////////////////////////////////////
151 #undef prefix_
152
153 \f
154 // Local Variables:
155 // mode: c++
156 // fill-column: 100
157 // comment-column: 40
158 // c-file-style: "senf"
159 // indent-tabs-mode: nil
160 // ispell-local-dictionary: "american"
161 // compile-command: "scons -u test"
162 // End: