-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_lib.cpp
More file actions
executable file
·365 lines (326 loc) · 8.6 KB
/
thread_lib.cpp
File metadata and controls
executable file
·365 lines (326 loc) · 8.6 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <iostream>
#include <queue>
#include <unordered_set>
#include <stdexcept>
#include <ucontext.h>
#include "thread.h"
#include "cpu.h"
// ************
// * README *
// ************
// context is the wrapper class of ucontext_t, stores all the info about a
// running ucontext_t. All the data structures in this project should store
// the context*, rather thant the raw ucontext_t*.
typedef class context {
public:
ucontext_t* ucontext_ptr;
std::queue<context*> exit_queue;
bool finish;
char* stack_address;
int id;
static int max_id;
context(){
ucontext_ptr = new ucontext_t;
finish = false;
stack_address = nullptr;
id = max_id++;
}
~context(){
delete[] stack_address;
stack_address = nullptr;
delete ucontext_ptr;
ucontext_ptr = nullptr;
}
} context;
int context::max_id = 0;
// ***********
// * cpu *
// ***********
class cpu::impl{
public:
static void interrupt_timer() {
thread::yield();
}
static void ready_queue_push_helper(context* t){
try {
cpu::impl::ready_queue.push(t);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
}
//static void release_context() {}
static std::queue<context*> ready_queue;
static context main_program;
static context* current_thread; // current running thread, should be set
// before setcontext in cpu_init.
static context* before_thread;
};
std::queue<context*> cpu::impl::ready_queue;
context cpu::impl::main_program;
context* cpu::impl::current_thread;
context* cpu::impl::before_thread = nullptr;
// ************
// * thread *
// ************
class thread::impl {
public:
impl(thread_startfunc_t func, void* arg) {
thread_context = new context;
make_context(thread_context, func, arg);
}
void join_impl() {
try {
thread_context->exit_queue.push(cpu::impl::current_thread);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
swapcontext(cpu::impl::current_thread->ucontext_ptr,
cpu::impl::main_program.ucontext_ptr);
thread::impl::release_context();
};
static void make_context(context* t, thread_startfunc_t func, void* arg) {
t->stack_address = new char[STACK_SIZE];
t->ucontext_ptr->uc_stack.ss_sp = t->stack_address;
t->ucontext_ptr->uc_stack.ss_size = STACK_SIZE;
t->ucontext_ptr->uc_stack.ss_flags = 0;
t->ucontext_ptr->uc_link = nullptr;
makecontext(t->ucontext_ptr,
(void (*)()) thread::impl::wrapper_func, 3, func, arg, t);
cpu::impl::ready_queue.push(t);
}
static void release_context() {
if (cpu::impl::before_thread) {
delete cpu::impl::before_thread;
cpu::impl::before_thread = nullptr;
}
}
static void wrapper_func(thread_startfunc_t func,
void* arg, context* context) {
//thread::impl::release_context();
cpu::interrupt_enable();
func(arg);
cpu::interrupt_disable();
cpu::impl::before_thread = context;
//dump exit queue
while (!context->exit_queue.empty()) {
cpu::impl::ready_queue_push_helper(context->exit_queue.front());
context->exit_queue.pop();
}
context->finish = true;
swapcontext(cpu::impl::current_thread->ucontext_ptr,
cpu::impl::main_program.ucontext_ptr);
}
context* thread_context; // Not destroy with the dtor of thread,
// Manually destroy when the cpu_init
// finds that before_thread is nullptr.
// Binded with the life span of ucontext,
// not the thread object.
};
thread::thread(thread_startfunc_t func, void* arg) {
cpu::interrupt_disable();
try {
this->impl_ptr = new impl(func, arg);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
cpu::interrupt_enable();
};
thread::~thread() {
cpu::interrupt_disable();
cpu::interrupt_enable();
};
void thread::join() {
cpu::interrupt_disable();
if (this->impl_ptr){
if (!this->impl_ptr->thread_context->finish) {
impl_ptr->join_impl();
}
}
cpu::interrupt_enable();
};
void thread::yield() {
cpu::interrupt_disable();
if (!cpu::impl::ready_queue.empty()) {
cpu::impl::ready_queue_push_helper(cpu::impl::current_thread);
swapcontext(cpu::impl::current_thread->ucontext_ptr,
cpu::impl::main_program.ucontext_ptr);
thread::impl::release_context();
}
cpu::interrupt_enable();
};
// ***************
// * cpu_inint *
// ***************
void cpu::init(thread_startfunc_t func, void* arg) {
//initialize the main_program
try {
context* init_context = new context;
getcontext(impl::main_program.ucontext_ptr);
thread::impl::make_context(init_context, func, arg);
impl_ptr = new impl;
}
catch (std::bad_alloc& ba) {
printf("bad alloc");
cpu::interrupt_enable();
throw ba;
}
interrupt_vector_table[TIMER] = &impl::interrupt_timer;
//run the program
while (!impl::ready_queue.empty()) {
impl::current_thread = impl::ready_queue.front();
impl::ready_queue.pop();
swapcontext(impl::main_program.ucontext_ptr,
impl::current_thread->ucontext_ptr);
thread::impl::release_context();
}
///release cpu resources
delete impl_ptr;
impl_ptr = nullptr;
cpu::interrupt_enable_suspend();
}
// ***********
// * mutex *
// ***********
class mutex::impl {
public:
bool status;
std::queue<context*> lock_queue;
std::unordered_set<int> func_with_lock;
impl() {
status = true;
}
void impl_lock() {
if (this->func_with_lock.find(cpu::impl::current_thread->id) ==
this->func_with_lock.end()) {
try {
func_with_lock.insert(cpu::impl::current_thread->id);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
}
if (status) {
status = false;
}
else {
try {
lock_queue.push(cpu::impl::current_thread);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
swapcontext(cpu::impl::current_thread->ucontext_ptr,
cpu::impl::main_program.ucontext_ptr);
thread::impl::release_context();
}
};
static void impl_unlock(mutex::impl* impl_ptr) {
if (impl_ptr->func_with_lock.find(cpu::impl::current_thread->id) ==
impl_ptr->func_with_lock.end()) {
cpu::interrupt_enable();
throw std::runtime_error("unlock without lock");
}
impl_ptr->func_with_lock.erase(cpu::impl::current_thread->id);
impl_ptr->status = true;
if (!impl_ptr->lock_queue.empty()) {
cpu::impl::ready_queue_push_helper(impl_ptr->lock_queue.front());
impl_ptr->lock_queue.pop();
impl_ptr->status = false;
}
}
};
mutex::mutex() {
cpu::interrupt_disable();
try {
this->impl_ptr = new impl;
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
cpu::interrupt_enable();
}
mutex::~mutex() {
cpu::interrupt_disable();
delete impl_ptr;
impl_ptr = nullptr;
cpu::interrupt_enable();
}
void mutex::lock() {
cpu::interrupt_disable();
impl_ptr->impl_lock();
cpu::interrupt_enable();
}
void mutex::unlock() {
cpu::interrupt_disable();
impl::impl_unlock(impl_ptr);
cpu::interrupt_enable();
}
// ********
// * cv *
// ********
class cv::impl {
private:
std::queue<context*> wait_queue;
public:
void impl_wait(mutex& m) {
try {
wait_queue.push(cpu::impl::current_thread);
}
catch (std::bad_alloc& ba) {
cpu::interrupt_enable();
throw ba;
}
mutex::impl::impl_unlock(m.impl_ptr);
swapcontext(cpu::impl::current_thread->ucontext_ptr,
cpu::impl::main_program.ucontext_ptr);
thread::impl::release_context();
m.impl_ptr->impl_lock();
}
void impl_signal() {
if (!wait_queue.empty()) {
cpu::impl::ready_queue_push_helper(wait_queue.front());
wait_queue.pop();
}
}
void impl_broadcast() {
while (!wait_queue.empty()) {
cpu::impl::ready_queue_push_helper(wait_queue.front());
wait_queue.pop();
}
}
};
cv::cv() {
cpu::interrupt_disable();
impl_ptr = new impl;
cpu::interrupt_enable();
}
cv::~cv() {
cpu::interrupt_disable();
delete impl_ptr;
impl_ptr = nullptr;
cpu::interrupt_enable();
}
void cv::broadcast() {
cpu::interrupt_disable();
impl_ptr->impl_broadcast();
cpu::interrupt_enable();
}
void cv::signal() {
cpu::interrupt_disable();
impl_ptr->impl_signal();
cpu::interrupt_enable();
}
void cv::wait(mutex& m) {
cpu::interrupt_disable();
impl_ptr->impl_wait(m);
cpu::interrupt_enable();
}