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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include "TaskMan.h"
#include "Task.h"
#include "Main.h"
#include "Local.h"
class Stopper : public Balau::Task {
virtual void Do();
virtual const char * getName();
};
void Stopper::Do() {
getMyTaskMan()->stopMe();
}
const char * Stopper::getName() {
return "Stopper";
}
static Balau::DefaultTmpl<Balau::TaskMan> defaultTaskMan(50);
static Balau::LocalTmpl<Balau::TaskMan> localTaskMan;
static const int TOO_MANY_STACKS = 1024;
namespace Balau {
class TaskScheduler : public Thread, public AtStart, public AtExit {
public:
TaskScheduler() : AtStart(100), m_stopping(false) { }
void registerTask(Task * t);
virtual void * proc();
virtual void doStart();
virtual void doExit();
void registerTaskMan(TaskMan * t);
void unregisterTaskMan(TaskMan * t);
void stopAll();
private:
Queue<Task *> m_queue;
std::queue<TaskMan *> m_taskManagers;
Lock m_lock;
volatile bool m_stopping;
};
};
static Balau::TaskScheduler s_scheduler;
void Balau::TaskScheduler::registerTask(Task * t) {
Printer::elog(E_TASK, "TaskScheduler::registerTask with t = %p", t);
m_queue.push(t);
}
void Balau::TaskScheduler::registerTaskMan(TaskMan * t) {
m_lock.enter();
m_taskManagers.push(t);
m_lock.leave();
}
void Balau::TaskScheduler::unregisterTaskMan(TaskMan * t) {
m_lock.enter();
TaskMan * p = NULL;
// yes, this is a potentially dangerous operation.
// But unregistering task managers shouldn't happen that often.
while (true) {
p = m_taskManagers.front();
m_taskManagers.pop();
if (p == t)
break;
m_taskManagers.push(p);
}
m_lock.leave();
}
void Balau::TaskScheduler::stopAll() {
m_stopping = true;
m_lock.enter();
std::queue<TaskMan *> altQueue;
TaskMan * tm;
while (!m_taskManagers.empty()) {
tm = m_taskManagers.front();
m_taskManagers.pop();
altQueue.push(tm);
tm->addToPending(new Stopper());
}
while (!altQueue.empty()) {
tm = altQueue.front();
altQueue.pop();
m_taskManagers.push(tm);
}
m_lock.leave();
}
void * Balau::TaskScheduler::proc() {
while (true) {
Printer::elog(E_TASK, "TaskScheduler waiting for a task to pop");
Task * t = m_queue.pop();
if (!t)
break;
if (dynamic_cast<Stopper *>(t) || m_stopping)
break;
m_lock.enter();
size_t s = m_taskManagers.size();
if (s == 0)
break;
TaskMan * tm = m_taskManagers.front();
if (s != 1) {
m_taskManagers.pop();
m_taskManagers.push(tm);
}
m_lock.leave();
Printer::elog(E_TASK, "TaskScheduler popped task %s at %p; adding to TaskMan %p", t->getName(), t, tm);
tm->addToPending(t);
tm->m_evt.send();
}
Printer::elog(E_TASK, "TaskScheduler stopping.");
return NULL;
}
void Balau::TaskScheduler::doStart() {
threadStart();
}
void Balau::TaskScheduler::doExit() {
Task * s = NULL;
m_queue.push(s);
join();
}
void asyncDummy(ev::async & w, int revents) { }
Balau::TaskMan::TaskMan() : m_stopped(false), m_allowedToSignal(false) {
#ifndef _WIN32
coro_create(&m_returnContext, 0, 0, 0, 0);
#else
m_fiber = ConvertThreadToFiber(NULL);
Assert(m_fiber);
#endif
TaskMan * global = localTaskMan.getGlobal();
if (!global) {
localTaskMan.setGlobal(this);
m_loop = ev_default_loop(EVFLAG_AUTO);
} else {
m_loop = ev_loop_new(EVFLAG_AUTO);
}
m_evt.set(m_loop);
m_evt.set<asyncDummy>();
m_evt.start();
s_scheduler.registerTaskMan(this);
m_nStacks = 0;
}
#ifdef _WIN32
class WinSocketStartup : public Balau::AtStart {
public:
WinSocketStartup() : AtStart(5) { }
virtual void doStart() {
WSADATA wsaData;
int r = WSAStartup(MAKEWORD(2, 0), &wsaData);
Assert(r == 0);
}
};
static WinSocketStartup wsa;
#endif
Balau::TaskMan * Balau::TaskMan::getDefaultTaskMan() { return localTaskMan.get(); }
Balau::TaskMan::~TaskMan() {
Assert(localTaskMan.getGlobal() != this);
while (m_stacks.size() != 0) {
free(m_stacks.front());
m_stacks.pop();
}
s_scheduler.unregisterTaskMan(this);
// probably way more work to do here in order to clean up tasks from that thread
ev_loop_destroy(m_loop);
}
void * Balau::TaskMan::getStack() {
void * r = NULL;
if (m_nStacks == 0) {
if (Task::needsStacks())
r = malloc(Task::stackSize());
} else {
r = m_stacks.front();
m_stacks.pop();
m_nStacks--;
}
return r;
}
void Balau::TaskMan::freeStack(void * stack) {
if (!stack)
return;
if (m_nStacks >= TOO_MANY_STACKS) {
free(stack);
} else {
m_stacks.push(stack);
m_nStacks++;
}
}
void Balau::TaskMan::mainLoop() {
do {
taskHash_t::iterator iH;
Task * t;
bool noWait = false;
Printer::elog(E_TASK, "TaskMan::mainLoop() at %p with m_tasks.size = %i", this, m_tasks.size());
// checking "STARTING" tasks, and running them once; also try to build the status of the noWait boolean.
for (iH = m_tasks.begin(); iH != m_tasks.end(); iH++) {
t = *iH;
if (t->getStatus() == Task::STARTING)
t->switchTo();
if ((t->getStatus() == Task::STOPPED) || (t->getStatus() == Task::FAULTED))
noWait = true;
}
// probably means we have pending tasks; or none at all, for some reason. Don't wait on it forever.
if (m_tasks.size() == 0)
noWait = true;
if (m_pendingAdd.size() != 0)
noWait = true;
// libev's event "loop". We always runs it once though.
m_allowedToSignal = true;
Printer::elog(E_TASK, "TaskMan at %p Going to libev main loop", this);
ev_run(m_loop, noWait || m_stopped ? EVRUN_NOWAIT : EVRUN_ONCE);
Printer::elog(E_TASK, "TaskMan at %p Getting out of libev main loop", this);
// let's check what task got stopped, and signal them
for (iH = m_tasks.begin(); iH != m_tasks.end(); iH++) {
t = *iH;
if (((t->getStatus() == Task::STOPPED) || (t->getStatus() == Task::FAULTED)) &&
(t->m_waitedBy.size() != 0)) {
Task::waitedByList_t::iterator i;
for (i = t->m_waitedBy.begin(); i != t->m_waitedBy.end(); i++) {
Events::TaskEvent * e = *i;
e->signal();
}
}
}
m_allowedToSignal = false;
// let's check who got signaled, and call them
for (iH = m_signaledTasks.begin(); iH != m_signaledTasks.end(); iH++) {
t = *iH;
Printer::elog(E_TASK, "TaskMan at %p Switching to task %p (%s - %s) that got signaled somehow.", this, t, t->getName(), ClassName(t).c_str());
Assert(t->getStatus() == Task::IDLE);
t->switchTo();
}
m_signaledTasks.clear();
// Adding tasks that were added, maybe from other threads
while (((m_pendingAdd.size() != 0) || (m_tasks.size() == 0)) && !m_stopped) {
t = m_pendingAdd.pop();
Assert(m_tasks.find(t) == m_tasks.end());
ev_now_update(m_loop);
t->setup(this, getStack());
m_tasks.insert(t);
}
// Finally, let's destroy tasks that no longer are necessary.
bool didDelete;
do {
didDelete = false;
for (iH = m_tasks.begin(); iH != m_tasks.end(); iH++) {
t = *iH;
if (((t->getStatus() == Task::STOPPED) || (t->getStatus() == Task::FAULTED)) &&
(t->m_waitedBy.size() == 0)) {
freeStack(t->m_stack);
delete t;
m_tasks.erase(iH);
didDelete = true;
break;
}
}
} while (didDelete);
} while (!m_stopped);
Printer::elog(E_TASK, "TaskManager at %p stopping.", this);
}
void Balau::TaskMan::registerTask(Balau::Task * t, Balau::Task * stick) {
if (stick) {
TaskMan * tm = stick->getMyTaskMan();
tm->addToPending(t);
tm->m_evt.send();
} else {
s_scheduler.registerTask(t);
}
}
void Balau::TaskMan::addToPending(Balau::Task * t) {
m_pendingAdd.push(t);
}
void Balau::TaskMan::signalTask(Task * t) {
Assert(m_tasks.find(t) != m_tasks.end());
Assert(m_allowedToSignal);
m_signaledTasks.insert(t);
}
void Balau::TaskMan::stop() {
s_scheduler.stopAll();
}
class ThreadedTaskMan : public Balau::Thread {
virtual void * proc() {
m_taskMan = new Balau::TaskMan();
m_taskMan->mainLoop();
return NULL;
}
Balau::TaskMan * m_taskMan;
};
Balau::Thread * Balau::TaskMan::createThreadedTaskMan() {
Thread * r = new ThreadedTaskMan();
r->threadStart();
return r;
}
|