change for selective Backtrace (SENF_BACKTRACE). Included via autoconf if SENF_DEBUG...
[senf.git] / senf / 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 <senf/Utils/Backtrace.hh>
32 #endif
33
34 #define prefix_ inline
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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 #ifdef SENF_BACKTRACE
45     std::stringstream ss;
46     backtrace(ss, 32);
47     backtrace_ = ss.str();
48 #endif
49 }
50
51 prefix_ senf::scheduler::detail::FIFORunner::TaskInfo::~TaskInfo()
52 {}
53
54 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::setRunnable()
55 {
56     runnable_ = true;
57 }
58
59 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::run()
60 {
61     countRun();
62     // Be sure to run v_run last since the callback may destroy this instance
63     v_run();
64 }
65
66 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::runnable()
67     const
68 {
69     return runnable_;
70 }
71
72 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::v_enabled()
73     const
74 {
75     return TaskListBase::linked();
76 }
77
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 // senf::scheduler::detail::FIFORunner::NullTask
80
81 prefix_ senf::scheduler::detail::FIFORunner::NullTask::NullTask()
82     : senf::scheduler::detail::FIFORunner::TaskInfo ("<null>")
83 {}
84
85 prefix_ senf::scheduler::detail::FIFORunner::NullTask::~NullTask()
86 {
87     if (TaskListBase::linked())
88         FIFORunner::instance().dequeue(this);
89 }
90
91 prefix_ void senf::scheduler::detail::FIFORunner::NullTask::v_run()
92 {}
93
94 prefix_ char const * senf::scheduler::detail::FIFORunner::NullTask::v_type()
95     const
96 {
97     return 0;
98 }
99
100 prefix_ std::string senf::scheduler::detail::FIFORunner::NullTask::v_info()
101     const
102 {
103     return "";
104 }
105
106 //-/////////////////////////////////////////////////////////////////////////////////////////////////
107 // senf::scheduler::detail::FIFORunner
108
109 prefix_ void senf::scheduler::detail::FIFORunner::enqueue(TaskInfo * task)
110 {
111     tasks_.insert(priorityEnd(task->priority_), *task);
112 }
113
114 prefix_ void senf::scheduler::detail::FIFORunner::taskTimeout(unsigned ms)
115 {
116     watchdogMs_ = ms;
117     if (watchdogRunning_)
118         startWatchdog();
119 }
120
121 prefix_ unsigned senf::scheduler::detail::FIFORunner::taskTimeout()
122     const
123 {
124     return watchdogMs_;
125 }
126
127 prefix_ void senf::scheduler::detail::FIFORunner::abortOnTimeout(bool flag)
128 {
129     watchdogAbort_ = flag;
130 }
131
132 prefix_ bool senf::scheduler::detail::FIFORunner::abortOnTimeout()
133     const
134 {
135     return watchdogAbort_;
136 }
137
138 prefix_ unsigned senf::scheduler::detail::FIFORunner::hangCount()
139 {
140     unsigned hc (hangCount_);
141     hangCount_ = 0;
142     return hc;
143 }
144
145 prefix_ senf::scheduler::detail::FIFORunner::iterator
146 senf::scheduler::detail::FIFORunner::begin()
147     const
148 {
149     // We need to filter out elements with e.type() == 0 ... the NullTask temporarily added is such
150     // an element and must be skipped.
151     return boost::make_filter_iterator(
152         EventManager::IteratorFilter(), tasks_.begin(), tasks_.end());
153 }
154
155 prefix_ senf::scheduler::detail::FIFORunner::iterator senf::scheduler::detail::FIFORunner::end()
156     const
157 {
158     return boost::make_filter_iterator(
159         EventManager::IteratorFilter(), tasks_.end(), tasks_.end());
160 }
161
162 prefix_ void senf::scheduler::detail::FIFORunner::yield()
163 {
164     yield_ = true;
165 }
166
167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
168 #undef prefix_
169
170 \f
171 // Local Variables:
172 // mode: c++
173 // fill-column: 100
174 // comment-column: 40
175 // c-file-style: "senf"
176 // indent-tabs-mode: nil
177 // ispell-local-dictionary: "american"
178 // compile-command: "scons -u test"
179 // End: