-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathThreadManager.h
76 lines (64 loc) · 1.48 KB
/
ThreadManager.h
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
#pragma once
#include "Ctu.h"
class Thread : public Waitable {
public:
Thread(Ctu *_ctu, int _id);
void assignHandle(ghandle handle);
void terminate();
void suspend(function<void()> cb=nullptr);
void resume(function<void()> cb=nullptr);
void freeze();
void thaw();
void onWake(function<void()> cb);
int id;
bool started;
ghandle handle;
bool active;
ThreadRegisters regs;
gptr tlsBase;
private:
Ctu *ctu;
list<function<void()>> wakeCallbacks;
};
class NativeThread {
public:
NativeThread(Ctu *_ctu, function<void()> _runner, int _id);
void terminate();
void suspend();
void resume();
void run();
int id;
bool active;
private:
Ctu *ctu;
function<void()> runner;
};
class ThreadManager {
public:
ThreadManager(Ctu *_ctu);
void start();
shared_ptr<Thread> create(gptr pc=0, gptr sp=0);
shared_ptr<NativeThread> createNative(function<void()> runner);
void requeue();
void enqueue(int id);
void enqueue(shared_ptr<Thread> thread);
void next(bool force=false);
void terminate(int id);
shared_ptr<Thread> current();
shared_ptr<Thread> last();
bool setCurrent(int id);
bool switched;
vector<shared_ptr<Thread>> thread_list();
private:
void tryRunNative();
bool isNative(int id);
Ctu *ctu;
unordered_map<int, shared_ptr<Thread>> threads;
unordered_map<int, shared_ptr<NativeThread>> nativeThreads;
int threadId;
list<shared_ptr<Thread>> running;
list<shared_ptr<NativeThread>> runningNative;
shared_ptr<Thread> _current, _last;
bool wasNativeLast;
bool first;
};