IBR-DTN  1.0.0
EventSwitch.h
Go to the documentation of this file.
1 /*
2  * EventSwitch.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef EVENTSWITCH_H_
23 #define EVENTSWITCH_H_
24 
25 #include "Component.h"
26 #include "core/Event.h"
27 #include <ibrcommon/Exceptions.h>
28 #include <ibrcommon/thread/Mutex.h>
29 #include <ibrcommon/thread/Conditional.h>
30 #include <ibrcommon/TimeMeasurement.h>
31 
32 #include <list>
33 #include <queue>
34 #include <map>
35 
36 namespace dtn
37 {
38  namespace core
39  {
41  {
42  private:
43  EventSwitch();
44  virtual ~EventSwitch();
45 
46  bool _running;
47  bool _shutdown;
48 
52  virtual const std::string getName() const;
53 
54  class Task
55  {
56  public:
57  Task(EventProcessor &proc, dtn::core::Event *evt);
58  ~Task();
59 
60  EventProcessor &processor;
61  dtn::core::Event *event;
62  };
63 
64  class Worker : public ibrcommon::JoinableThread
65  {
66  public:
67  Worker(EventSwitch &sw, bool profiling);
68  ~Worker();
69 
70  bool isStalled();
71 
72  protected:
73  void run() throw ();
74  virtual void __cancellation() throw ();
75 
76  private:
77  EventSwitch &_switch;
78  bool _running;
79  ibrcommon::TimeMeasurement _tm;
80  bool _inprogress;
81  bool _profiling;
82  };
83 
84  class WatchDog : public ibrcommon::JoinableThread
85  {
86  public:
87  WatchDog(EventSwitch &sw, std::list<Worker*> &workers);
88  ~WatchDog();
89 
90  void up();
91  void down();
92 
93  protected:
94  void run() throw ();
95  virtual void __cancellation() throw ();
96 
97  private:
98  EventSwitch &_switch;
99  std::list<Worker*> &_workers;
100 
101  bool _running;
102  ibrcommon::Conditional _cond;
103  };
104 
105  ibrcommon::Conditional _queue_cond;
106  std::queue<Task*> _queue;
107  std::queue<Task*> _prio_queue;
108  std::queue<Task*> _low_queue;
109 
110  WatchDog _wd;
111  std::list<Worker*> _wlist;
112 
113  ibrcommon::TimeMeasurement _tm;
114  bool _inprogress;
115 
116  void process(ibrcommon::TimeMeasurement &tm, bool &inprogress, bool profiling);
117 
118  protected:
119  virtual void componentUp() throw ();
120  virtual void componentDown() throw ();
121 
122  bool isStalled();
123 
124  public:
125  static EventSwitch& getInstance();
126  void loop(size_t threads, bool profiling = false);
127 
131  bool empty() const;
132 
137  void shutdown();
138 
143  static void queue(EventProcessor &proc, Event *evt);
144 
145  friend class Worker;
146  };
147  }
148 }
149 
150 #endif /* EVENTSWITCH_H_ */
static EventSwitch & getInstance()
bool _running
Definition: dtninbox.cpp:122
virtual void componentUp()
Definition: EventSwitch.cpp:48
static void queue(EventProcessor &proc, Event *evt)
void loop(size_t threads, bool profiling=false)
virtual void componentDown()
Definition: EventSwitch.cpp:65