-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
72 lines (57 loc) · 1.92 KB
/
thread.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
#include <cassert>
#include <ucontext.h>
#include "thread.h"
#include "types.h"
using ThreadStateWeakPtr = std::weak_ptr<ThreadState>;
using JoinQueueWeakPtr = std::weak_ptr<std::queue<TcbPtr>>;
class thread::impl {
public:
ThreadStateWeakPtr state;
JoinQueueWeakPtr joinQueue;
};
thread::thread(thread_startfunc_t body, void *arg) {
RaiiLock l;
// TODO: MULTIPROCESSOR - switch invariant - acquire guard
impl_ptr = new impl();
// create tcb and put it onto ready queue
readyQueue.push(TcbPtr(new Tcb(READY, body, arg)));
impl_ptr->state = readyQueue.back()->state;
impl_ptr->joinQueue = readyQueue.back()->joinQueue;
// TODO: send IPI
send_ipi();
// TODO: MULTIPROCESSOR - free guard
}
thread::~thread() {
delete impl_ptr;
}
void thread::yield() {
RaiiLock l;
// TODO: MULTIPROCESSOR - acquire guard
yield_helper();
// TODO: MULTIPROCESSOR - free guard
}
void thread::join() {
RaiiLock l;
// TODO: MULTIPROCESSOR - acquire guard
// TODO: check currThread TCB is valid (state, joinQueue, stack)
assert(runningList.find(cpu::self()) != runningList.end());
TcbPtr &currThread = runningList[cpu::self()];
ThreadStatePtr state = impl_ptr->state.lock();
// case 1: thread is still running, block current thread
if (state && *state != FINISHED) {
JoinQueuePtr joinQueue = impl_ptr->joinQueue.lock();
assert(joinQueue);
// move current thread to join queue
*(currThread->state) = BLOCKED;
joinQueue->push(std::move(currThread));
// switch to next ready thread if there is one, else suspend
switch_to_next_or_suspend(&joinQueue->back()->ctx);
// if there are any threads on the finished list, clean them up
cleanup_finished_list();
}
// case 2 thread has finished, continue execution
else {
// do nothing
}
// TODO: MULTIPROCESSOR - free guard
}