-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
199 lines (164 loc) · 5.2 KB
/
types.cpp
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
#include <cassert>
#include "types.h"
std::queue<TcbPtr> readyQueue;
std::vector<TcbPtr> finishedList;
std::map<cpu *, TcbPtr> runningList;
ucontext_t *dummyCtx = new ucontext_t;
ucontext_t *suspendCtx = new ucontext_t;
Tcb::Tcb()
: ctx(ucontext_t())
, stackPtr(nullptr)
, state(ThreadStatePtr(new ThreadState(INITIALIZED)))
, joinQueue(JoinQueuePtr(nullptr))
{
}
Tcb::Tcb(ThreadState state, thread_startfunc_t body, void *arg)
: ctx(ucontext_t())
, stackPtr(new char[STACK_SIZE])
, state(ThreadStatePtr(new ThreadState(state)))
, joinQueue(JoinQueuePtr(new std::queue<TcbPtr>()))
{
ctx.uc_stack.ss_sp = stackPtr;
ctx.uc_stack.ss_size = STACK_SIZE;
ctx.uc_stack.ss_flags = 0;
ctx.uc_link = nullptr;
makecontext(&ctx, (void (*)()) os_wrapper, 2, body, arg);
}
void Tcb::freeStack() {
assert(stackPtr != nullptr);
delete[] (char *) stackPtr;
stackPtr = nullptr;
}
RaiiLock::RaiiLock() {
assert_interrupts_enabled();
cpu::interrupt_disable();
while (cpu::guard.exchange(true)) {
/* MT */
}
}
RaiiLock::~RaiiLock() {
assert_interrupts_disabled();
cpu::guard.store(false);
cpu::interrupt_enable();
}
void os_wrapper(thread_startfunc_t body, void *arg) {
assert_interrupts_disabled();
// do os stuff
// if there are any threads on the finished list, clean them up
cleanup_finished_list();
// enable interrupts - switch invariant
cpu::guard.store(false);
cpu::interrupt_enable();
// run thread to finish
body(arg);
// disable interrupts - switch invariant
cpu::interrupt_disable();
while (cpu::guard.exchange(true)) {
/* MT */
}
// get tcb of currently running thread
assert(runningList.find(cpu::self()) != runningList.end());
TcbPtr &currThread = runningList[cpu::self()];
// move all tcbs on join queue onto ready queue
while (!currThread->joinQueue->empty()) {
*(currThread->joinQueue->front()->state) = READY;
readyQueue.push(std::move(currThread->joinQueue->front()));
currThread->joinQueue->pop();
}
// TODO: send IPI
send_ipi();
// move tcb of currently running thread to finished list
*(currThread->state) = FINISHED;
finishedList.push_back(std::move(currThread));
// switch to next ready thread if there is one, else suspend
switch_to_next_or_suspend(dummyCtx);
}
void switch_to_next_or_suspend(ucontext_t *saveloc) {
assert_interrupts_disabled();
assert(runningList.find(cpu::self()) != runningList.end());
TcbPtr &currThread = runningList[cpu::self()];
// TODO: assert currThread points to an "empty" Tcb
// if another thread on ready queue, switch to it
if (!readyQueue.empty()) {
// move top tcb from ready queue onto running list
currThread = std::move(readyQueue.front());
readyQueue.pop();
*(currThread->state) = RUNNING;
// switch context to new current thread
swapcontext(saveloc, &currThread->ctx);
}
// else no other threads, save tcb to saveloc then suspend
else {
// switch to kernel suspend thread
makecontext(suspendCtx, (void (*)()) suspend_thread, 0);
swapcontext(saveloc, suspendCtx);
}
assert_interrupts_disabled();
}
void cleanup_finished_list() {
assert_interrupts_disabled();
while(!finishedList.empty()) {
finishedList.back()->freeStack();
finishedList.pop_back();
}
}
void yield_helper() {
// there is a next ready thread
if (!readyQueue.empty()) {
// put current thread on ready queue
assert(runningList.find(cpu::self()) != runningList.end());
TcbPtr &currThread = runningList[cpu::self()];
*(currThread->state) = READY;
readyQueue.push(std::move(currThread));
// TODO: send IPI: do we need to send an IPI here?
// switch to next ready thread
switch_to_next_or_suspend(&readyQueue.back()->ctx);
// if there are any threads on the finished list, clean them up
cleanup_finished_list();
}
}
void handle_timer() {
RaiiLock l;
// TODO: MULTIPROCESSOR - acquire guard
yield_helper();
// TODO: MULTIPROCESSOR - free guard
}
void send_ipi() {
assert_interrupts_disabled();
assert(cpu::guard);
// send IPIs if there's work to do
if (!readyQueue.empty()) {
// loop through list of CPUs
size_t i = 0;
for (auto& cpuTcbPair : runningList) {
if (i >= readyQueue.size()) {
break;
}
// send IPI if CPU is suspended
if (!cpuTcbPair.second) {
cpuTcbPair.first->interrupt_send();
++i;
}
}
}
}
void handle_ipi() {
RaiiLock l;
// if there's work to do, do it
if (!readyQueue.empty()) {
TcbPtr &threadToRun = runningList[cpu::self()];
*(readyQueue.front()->state) = RUNNING;
threadToRun = std::move(readyQueue.front());
readyQueue.pop();
setcontext(&threadToRun->ctx);
}
// else, suspend
else {
cpu::guard.store(false);
cpu::interrupt_enable_suspend();
}
}
void suspend_thread() {
cpu::guard.store(false);
cpu::interrupt_enable_suspend();
}