-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.hpp
207 lines (166 loc) · 5.78 KB
/
proto.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef PROTO_HPP
#define PROTO_HPP
#include "os.hpp"
#include <cstdint>
#include <vector>
#include <queue>
namespace Proto {
struct Object {
Object() {
ObjectList().push_back(this);
}
virtual void Run() = 0;
static std::vector<Object *>& ObjectList() {
static std::vector<Object *> ObjectList;
return ObjectList;
}
};
template <class T>
struct Worker : public Object {
void Push(T Value) {
m_Queue.push(Value);
}
T Pop() {
T Value = m_Queue.front();
m_Queue.pop();
return Value;
}
bool QueueIsEmpty() {
return m_Queue.empty();
}
private:
std::queue<T> m_Queue;
};
struct Timeout {
Timeout() {
m_StartUS = 0;
m_TimeoutUS = 0;
}
void TimeoutInUS(uint32_t TimeoutUS) {
m_StartUS = OS::GetFreeRunningUS();
m_TimeoutUS = TimeoutUS;
}
void TimeoutInMS(uint32_t TimeoutMS) {
TimeoutInUS(TimeoutMS * 1000);
}
void TimeoutInSeconds(uint32_t TimeoutSeconds) {
TimeoutInMS(TimeoutSeconds * 1000);
}
bool HasExpired() const {
const uint32_t NowUS = OS::GetFreeRunningUS();
return (NowUS - m_StartUS) >= m_TimeoutUS;
}
uint32_t m_StartUS;
uint32_t m_TimeoutUS;
};
struct Thread {
#define PROTO_THREAD_CONTEXT_BEGIN (-1)
#define PROTO_THREAD_CONTEXT_FINALLY (-2)
#define PROTO_THREAD_CONTEXT_END (-3)
Thread() : m_Timeout() {
m_Context = PROTO_THREAD_CONTEXT_BEGIN;
m_FirstEntry = true;
}
bool Join() {
return m_Context == PROTO_THREAD_CONTEXT_END;
}
bool PROTO_ThreadDelayUS(uint32_t US) {
if (m_FirstEntry) {
m_Timeout.TimeoutInUS(US);
m_FirstEntry = false;
return false;
}
if (m_Timeout.HasExpired()) {
m_FirstEntry = true;
return true;
}
return false;
}
bool PROTO_ThreadDelayMS(uint32_t MS) {
return PROTO_ThreadDelayUS(MS * 1000);
}
bool PROTO_ThreadDelaySeconds(uint32_t Seconds) {
return PROTO_ThreadDelayUS(Seconds * 1000 * 1000);
}
bool PROTO_ThreadWaitCond_TimeoutMS(bool Condition, uint32_t TimeoutMS, bool * pTimeoutStatus) {
if (m_FirstEntry) {
m_Timeout.TimeoutInMS(TimeoutMS);
m_FirstEntry = false;
return false;
}
if (Condition) {
*pTimeoutStatus = false;
m_FirstEntry = true;
return true;
}
if (m_Timeout.HasExpired()) {
*pTimeoutStatus = true;
m_FirstEntry = true;
return true;
}
return false;
}
bool PROTO_ThreadYield() {
if (m_FirstEntry) {
m_FirstEntry = false;
return false;
}
m_FirstEntry = true;
return true;
}
int32_t m_Context;
Timeout m_Timeout;
bool m_FirstEntry;
// The switch/case/__LINE__ approach is based on protothreads by Adam Dunkels.
#define ThreadBegin() \
switch (rThread.m_Context) { \
case PROTO_THREAD_CONTEXT_BEGIN:
#define ThreadDelayUS(US) \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!rThread.PROTO_ThreadDelayUS(US)) \
return; \
} while (0)
#define ThreadDelayMS(MS) \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!rThread.PROTO_ThreadDelayMS(MS)) \
return; \
} while (0)
#define ThreadDelaySeconds(Seconds) \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!rThread.PROTO_ThreadDelaySeconds(Seconds)) \
return; \
} while (0)
#define ThreadYield() \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!rThread.PROTO_ThreadYield()) \
return; \
} while (0)
#define ThreadWaitCond(Cond) \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!(Cond)) \
return; \
} while (0)
#define ThreadWaitCond_TimeoutMS(Cond, TimeoutMS, pTimeoutStatus) \
do { case __LINE__: rThread.m_Context = __LINE__; \
if (!rThread.PROTO_ThreadWaitCond_TimeoutMS(Cond, TimeoutMS, pTimeoutStatus)) \
return; \
} while(0)
#define ThreadExit() \
do { rThread.m_Context = PROTO_THREAD_CONTEXT_FINALLY; \
return; \
} while (0)
#define ThreadFinally() \
do { case PROTO_THREAD_CONTEXT_FINALLY: rThread.m_Context = PROTO_THREAD_CONTEXT_END; \
} while (0)
#define ThreadEnd() \
default: \
rThread.m_Context = PROTO_THREAD_CONTEXT_END; \
} return
};
inline void Run() {
for (auto pObject: Object::ObjectList()) {
pObject->Run();
}
}
}
#endif