-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_impl.cpp
More file actions
executable file
·81 lines (67 loc) · 2.02 KB
/
cpu_impl.cpp
File metadata and controls
executable file
·81 lines (67 loc) · 2.02 KB
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
/**
* cpu.cpp
*
* EECS 482: Project 2, Winter 2019
*
* Jiajun Peng, Youwei Chen, Xuetong Sun
* pjiajun, skylarch, xuetong
*
* cpu helper functions
*/
#include "cpu.h"
#include "cpu_impl.h"
#include "thread.h"
#include "thread_impl.h"
#include "ucontext.h"
#include <queue>
/**
* Modifies: current_thread, ready_queue.
* Effects: switch to the next ready thread and run.
*/
void cpu::impl::switch_thread() {
assert_interrupts_disabled();
if (!cpu::self()->impl_ptr->ready_queue.empty()) {
thread::impl * temp = cpu::self()->impl_ptr->current_thread;
cpu::self()->impl_ptr->current_thread = cpu::self()->impl_ptr->ready_queue.front();
cpu::self()->impl_ptr->ready_queue.pop();
swapcontext(temp->ucontext_ptr, cpu::self()->impl_ptr->current_thread->ucontext_ptr);
}
// If no next ready thread, suspend cpu
else {
cpu::interrupt_enable_suspend();
}
assert_interrupts_disabled();
}
/**
* Modifies: ready_queue.
* Effects: switch to the next ready thread and run.
*/
void cpu::impl::timer_handler() {
assert_interrupts_enabled();
cpu::interrupt_disable();
cpu::self()->impl_ptr->ready_queue.push(cpu::self()->impl_ptr->current_thread);
switch_thread();
assert_interrupts_disabled();
cpu::interrupt_enable();
}
/**
* Modifies: finished_queue.
* Effects: delete threads (object + context) on finish queue.
*/
void cpu::impl::delete_finished_queue() {
assert_interrupts_disabled();
while (!cpu::self()->impl_ptr->finished_queue.empty()) {
thread::impl * temp = cpu::self()->impl_ptr->finished_queue.front();
cpu::self()->impl_ptr->finished_queue.pop();
// Delete stack
delete[] (char *)temp->ucontext_ptr->uc_stack.ss_sp;
temp->ucontext_ptr->uc_stack.ss_sp = nullptr;
// Delete ucontext_ptr
delete temp->ucontext_ptr;
temp->ucontext_ptr = nullptr;
// Delete impl_ptr
delete temp;
temp = nullptr;
}
assert_interrupts_disabled();
}