Scheduler: Implement SignalDispatcher
[senf.git] / Scheduler / FIFORunner.cc
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 non-inline non-template implementation */
25
26 #include "FIFORunner.hh"
27 //#include "FIFORunner.ih"
28
29 // Custom includes
30
31 //#include "FIFORunner.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 prefix_ void senf::scheduler::FIFORunner::dequeue(TaskInfo * task)
36 {
37     TaskList::iterator i (TaskList::current(*task));
38     if (next_ == i)
39         ++next_;
40     tasks_.erase(i);
41 }
42
43 namespace {
44     struct NullTask 
45         : public senf::scheduler::FIFORunner::TaskInfo
46     {
47         void run() {};
48     };
49 }
50
51 prefix_ void senf::scheduler::FIFORunner::run()
52 {
53     // This algorithm is carefully adjusted to make it work even when arbitrary tasks are removed
54     // from the queue
55     // - Before we begin, we add a NullTask to the queue. The only purpose of this node is, to mark
56     //   the current end of the queue. The iterator to this node becomes the end iterator of the
57     //   range to process
58     // - We update the TaskInfo and move it to the end of the queue before calling the callback so
59     //   we don't access the TaskInfo if it is removed while the callback is running
60     // - We keep the next to-be-processed node in a class variable which is checked and updated
61     //   whenever a node is removed.
62     NullTask null;
63     tasks_.push_back(null);
64     TaskList::iterator end (TaskList::current(null));
65     next_ = tasks_.begin();
66     while (next_ != end) {
67         TaskInfo & task (*next_);
68         if (task.runnable) {
69             task.runnable = false;
70             TaskList::iterator i (next_);
71             ++ next_;
72             tasks_.splice(tasks_.end(), tasks_, i);
73             task.run();
74         }
75         else
76             ++ next_;
77     }
78     tasks_.erase(end);
79     next_ = tasks_.end();
80 }
81
82 ///////////////////////////////cc.e////////////////////////////////////////
83 #undef prefix_
84 //#include "FIFORunner.mpp"
85
86 \f
87 // Local Variables:
88 // mode: c++
89 // fill-column: 100
90 // comment-column: 40
91 // c-file-style: "senf"
92 // indent-tabs-mode: nil
93 // ispell-local-dictionary: "american"
94 // compile-command: "scons -u test"
95 // End: