-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread.cc
More file actions
44 lines (32 loc) · 848 Bytes
/
thread.cc
File metadata and controls
44 lines (32 loc) · 848 Bytes
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
#include "thread.h"
#include "frame.h"
namespace tinyco {
bool Stack::Init(size_t size) {
if (stack_) delete stack_;
size_ = size;
stack_ = new char[size_];
return true;
}
char *Stack::GetStackBeginPoint() { return stack_; }
size_t Stack::Size() { return size_; }
Thread::Thread() {}
Thread::~Thread() {}
bool Thread::Init() {
if (!stack_.Init(Stack::kMaxStackSize)) {
return false;
}
state_ = TS_STOP;
return true;
}
void Thread::RestoreContext() { setcontext(&uc_); }
void Thread::Schedule() { getcontext(&uc_); }
void Thread::SetContext(BeginFrom func, void *arg) {
fun_ = func;
getcontext(&uc_);
uc_.uc_stack.ss_sp = stack_.GetStackBeginPoint();
uc_.uc_stack.ss_size = stack_.Size();
uc_.uc_link = NULL;
uc_.uc_stack.ss_flags = 0;
makecontext(&uc_, reinterpret_cast<ContextFun>(func), 1, arg);
}
}