blob: 9fed7c2302e95b4799cc98925c45ef2eb6ee1d33 (
plain) (
blame)
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
|
#include <memory>
#include <vector>
struct Executor;
struct Thread {
Thread(const Thread&) = delete;
Thread& operator=(const Thread&) = delete;
Thread();
virtual ~Thread() {}
virtual void threadFn() = 0;
protected:
void yield();
private:
static void entry(void* obj);
void* mStackPtr;
friend struct Executor;
const Executor* mExecutor;
};
struct Executor {
Executor(const Executor&) = delete;
Executor& operator=(const Executor&) = delete;
Executor() = default;
const void* getStackPtr() const { return mStackPtr; }
void spawn(std::unique_ptr<Thread> t) {
mThreads.push_back(std::move(t));
mThreads.back()->mExecutor = this;
}
void run() {
for (const std::unique_ptr<Thread>& t : mThreads) {
yield_to(t.get());
}
}
private:
void* mStackPtr;
std::vector<std::unique_ptr<Thread>> mThreads;
void yield_to(const Thread* t) const;
};
|