switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / FIFORunner.cci
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief FIFORunner inline non-template implementation */
30
31 //#include "FIFORunner.ih"
32
33 // Custom includes
34 #ifdef SENF_DEBUG
35 #include <sstream>
36 #include <senf/Utils/Backtrace.hh>
37 #endif
38
39 #define prefix_ inline
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 //  senf::scheduler::detail::FIFORunner::TaskInfo
44
45 prefix_ senf::scheduler::detail::FIFORunner::TaskInfo::TaskInfo(std::string const & name,
46                                                                 Priority priority)
47     : Event(name), runnable_ (false), priority_ (priority)
48 {
49 #ifdef SENF_BACKTRACE
50     std::stringstream ss;
51     senf::backtrace(ss, 32);
52     backtrace_ = ss.str();
53 #endif
54 }
55
56 prefix_ senf::scheduler::detail::FIFORunner::TaskInfo::~TaskInfo()
57 {}
58
59 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::setRunnable()
60 {
61     runnable_ = true;
62 }
63
64 prefix_ void senf::scheduler::detail::FIFORunner::TaskInfo::run()
65 {
66     countRun();
67     // Be sure to run v_run last since the callback may destroy this instance
68     v_run();
69 }
70
71 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::runnable()
72     const
73 {
74     return runnable_;
75 }
76
77 prefix_ bool senf::scheduler::detail::FIFORunner::TaskInfo::v_enabled()
78     const
79 {
80     return TaskListBase::linked();
81 }
82
83 //-/////////////////////////////////////////////////////////////////////////////////////////////////
84 // senf::scheduler::detail::FIFORunner::NullTask
85
86 prefix_ senf::scheduler::detail::FIFORunner::NullTask::NullTask()
87     : senf::scheduler::detail::FIFORunner::TaskInfo ("<null>")
88 {}
89
90 prefix_ senf::scheduler::detail::FIFORunner::NullTask::~NullTask()
91 {
92     if (TaskListBase::linked())
93         FIFORunner::instance().dequeue(this);
94 }
95
96 prefix_ void senf::scheduler::detail::FIFORunner::NullTask::v_run()
97 {}
98
99 prefix_ char const * senf::scheduler::detail::FIFORunner::NullTask::v_type()
100     const
101 {
102     return 0;
103 }
104
105 prefix_ std::string senf::scheduler::detail::FIFORunner::NullTask::v_info()
106     const
107 {
108     return "";
109 }
110
111 //-/////////////////////////////////////////////////////////////////////////////////////////////////
112 // senf::scheduler::detail::FIFORunner
113
114 prefix_ void senf::scheduler::detail::FIFORunner::enqueue(TaskInfo * task)
115 {
116     tasks_.insert(priorityEnd(task->priority_), *task);
117 }
118
119 prefix_ void senf::scheduler::detail::FIFORunner::taskTimeout(unsigned ms)
120 {
121     watchdogMs_ = ms;
122     if (watchdogRunning_)
123         startWatchdog();
124 }
125
126 prefix_ unsigned senf::scheduler::detail::FIFORunner::taskTimeout()
127     const
128 {
129     return watchdogMs_;
130 }
131
132 prefix_ void senf::scheduler::detail::FIFORunner::abortOnTimeout(bool flag)
133 {
134     watchdogAbort_ = flag;
135 }
136
137 prefix_ bool senf::scheduler::detail::FIFORunner::abortOnTimeout()
138     const
139 {
140     return watchdogAbort_;
141 }
142
143 prefix_ unsigned senf::scheduler::detail::FIFORunner::hangCount()
144 {
145     unsigned hc (hangCount_);
146     hangCount_ = 0;
147     return hc;
148 }
149
150 prefix_ senf::scheduler::detail::FIFORunner::iterator
151 senf::scheduler::detail::FIFORunner::begin()
152     const
153 {
154     // We need to filter out elements with e.type() == 0 ... the NullTask temporarily added is such
155     // an element and must be skipped.
156     return boost::make_filter_iterator(
157         EventManager::IteratorFilter(), tasks_.begin(), tasks_.end());
158 }
159
160 prefix_ senf::scheduler::detail::FIFORunner::iterator senf::scheduler::detail::FIFORunner::end()
161     const
162 {
163     return boost::make_filter_iterator(
164         EventManager::IteratorFilter(), tasks_.end(), tasks_.end());
165 }
166
167 prefix_ void senf::scheduler::detail::FIFORunner::yield()
168 {
169     yield_ = true;
170 }
171
172 //-/////////////////////////////////////////////////////////////////////////////////////////////////
173 #undef prefix_
174
175 \f
176 // Local Variables:
177 // mode: c++
178 // fill-column: 100
179 // comment-column: 40
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"
184 // End: