/
E_System.hpp
181 lines (158 loc) · 4.32 KB
/
E_System.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @file E_System.hpp
* @Author leeopop (dlrmsghd@gmail.com)
* @date November, 2014
* @brief Header for E::System and E::Runnable
*/
#ifndef E_SYSTEM_HPP_
#define E_SYSTEM_HPP_
#include <E/E_Common.hpp>
#include <E/E_Log.hpp>
#include <E/E_Module.hpp>
namespace E
{
class TimerContainer;
class Runnable;
/**
* @brief System provides a virtual clock used during the simulation.
* Current virtual clock can be obtained by System::getCurrentTime.
* Every Module registered in the System share same virtual clock
* and the System manages the total ordering of every event.
*
* @see System::getCurrentTime
* @see Module
*/
class System : private Log
{
private:
std::unordered_set<Runnable*> runnableSet;
std::mutex mutex;
class TimerContainer
{
public:
Module* from;
Module* to;
bool canceled;
Time wakeup;
Module::Message* message;
UUID uuid;
};
class TimerContainerLess
{
public:
bool operator()(const TimerContainer* a , const TimerContainer* b )
{
if(a->wakeup != b->wakeup)
return a->wakeup > b->wakeup;
else
return a->uuid > b->uuid; //XXX if uuid returns to the start??
}
};
UUID currentID;
Time currentTime;
std::priority_queue<TimerContainer*, std::vector<TimerContainer*>, TimerContainerLess> timerQueue;
std::unordered_map<UUID, TimerContainer*> activeTimer;
std::unordered_set<UUID> activeUUID;
std::unordered_set<Module*> registeredModule;
UUID allocateUUID();
bool deallocateUUID(UUID uuid);
void registerModule(Module* module);
void unregisterModule(Module* module);
bool isRegistered(Module* module);
UUID sendMessage(Module* from, Module* to, Module::Message* message, Time timeAfter);
bool cancelMessage(UUID messageID);
public:
/**
* @brief Nothing is needed to construct a System
*/
System();
virtual ~System();
/**
* @brief Execute all registered Module. Virtual clock will move to the end of the simulation.
* @param till The System will operate until the given virtual clock.
* If all events are finished, it will immediately return even if it did not reached to the given termination time.
*/
void run(Time till);
/**
* @return Global lock object used in this System
*/
std::mutex& getSystemLock();
/**
* @return Returns current virtual clock of the System.
*/
Time getCurrentTime();
/**
* @brief Register a Runnable interface to this System.
*
* @param runnable Runnable interface to be added.
*
* @return nothing
*
* @note You cannot override this function.
*/
virtual void addRunnable(Runnable* runnable) final;
/**
* @brief Unregister a Runnable interface to this System.
*
* @param runnable Runnable interface to be removed.
* The interface must be registered before being removed.
*
* @return nothing
*
* @note You cannot override this function.
*/
virtual void delRunnable(Runnable* runnable) final;
friend Module::Module(System* system);
friend Module::~Module();
friend UUID Module::sendMessage(Module* to, Module::Message* message, Time timeAfter);
friend bool Module::cancelMessage(UUID timer);
};
/**
* @brief Runnable is executed via System.
* The total ordering of execution is guaranteed by the System.
*
* @note You cannot instantiate this class.
* You must inherit this class to use.
*
* @see System
*/
class Runnable
{
protected:
/**
* @brief Constructs a Runnable interface.
*
* @param system System to be registered.
* Runnable is automatically registered when it is created.
* @param initial_value Initial running state (true by default).
*/
Runnable(System* system, bool initial_value = true);
virtual ~Runnable();
/**
* @param value Set the running state
* @note The System would not terminate unless all Runnable becomes "stopped".
* You cannot override this function.
*/
virtual void setRunning(bool value) final;
public:
/**
* @return Current running state.
* @note You cannot override this function.
*/
virtual bool isRunning() final;
/**
* @brief Wait until current running state becomes the given value.
* @param value Value you are waiting for.
* @param lock Global lock of the system.
*
* @note You cannot override this function.
* @see System::getSystemLock
*/
virtual void waitForRunning(bool value, std::unique_lock<std::mutex>& lock) final;
private:
System* system;
bool running;
std::condition_variable cond;
};
}
#endif /* E_SYSTEM_HPP_ */