blob: 2b2742ddd9a22f8eb422f8e291b4a678ffa52c5b (
plain)
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
|
#pragma once
#include <stdint.h>
#ifndef _WIN32
#include <coro.h>
#endif
#include <ev++.h>
#include <ext/hash_set>
#include <queue>
#include <Async.h>
#include <Threads.h>
#include <Exceptions.h>
#include <Task.h>
namespace gnu = __gnu_cxx;
namespace Balau {
class TaskScheduler;
namespace Events {
class TaskEvent;
};
class TaskMan {
public:
class TaskManThread : public Thread {
public:
virtual ~TaskManThread();
virtual void * proc();
void stopMe(int code = 0) { m_taskMan->stopMe(code); }
private:
TaskMan * m_taskMan;
};
TaskMan();
~TaskMan();
int mainLoop();
static TaskMan * getDefaultTaskMan();
struct ev_loop * getLoop() { return m_loop; }
void signalTask(Task * t);
static void stop(int code);
void stopMe(int code = 0);
static TaskManThread * createThreadedTaskMan() {
TaskManThread * r = new TaskManThread();
r->threadStart();
return r;
}
static void stopThreadedTaskMan(TaskManThread * tmt) {
tmt->stopMe(0);
tmt->join();
delete tmt;
}
bool stopped() { return m_stopped; }
template<class T>
static T * registerTask(T * t, Task * stick = NULL) { TaskMan::iRegisterTask(t, stick, NULL); return t; }
template<class T>
static T * registerTask(T * t, Events::TaskEvent * event) { TaskMan::iRegisterTask(t, NULL, event); return t; }
private:
static void iRegisterTask(Task * t, Task * stick, Events::TaskEvent * event);
static void registerAsyncOp(AsyncOperation * op);
void * getStack();
void freeStack(void * stack);
void addToPending(Task * t);
static void asyncIdleReady(void * param) {
TaskMan * taskMan = (TaskMan *) param;
taskMan->asyncIdleReady();
}
void asyncIdleReady() {
m_evt.send();
}
#ifndef _WIN32
coro_context m_returnContext;
#else
void * m_fiber;
#endif
friend class Task;
friend class TaskScheduler;
template<class T>
friend T * createAsyncOp(T * op);
struct taskHasher { size_t operator()(const Task * t) const { return reinterpret_cast<uintptr_t>(t); } };
typedef gnu::hash_set<Task *, taskHasher> taskHash_t;
taskHash_t m_tasks, m_signaledTasks;
Queue<Task> m_pendingAdd;
struct ev_loop * m_loop;
ev::async m_evt;
std::queue<void *> m_stacks;
int m_nStacks;
int m_stopCode = 0;
bool m_stopped = false;
bool m_allowedToSignal = false;
};
template<class T>
T * createAsyncOp(T * op) { TaskMan::registerAsyncOp(op); return op; }
};
|