-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
76 lines (68 loc) · 1.43 KB
/
Copy pathThread.cpp
File metadata and controls
76 lines (68 loc) · 1.43 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
#include "Thread.h"
#include <windows.h>
extern "C" void thread_func();
void Scheduler::Loop()
{
while (true)
{
auto next = GetNextThread();
if (next)
{
this->next = next;
yield(this, ThreadStatus::READY);
if (next->status == ThreadStatus::ZOMBIE)
{
free_list_.emplace_back(next);
}
}
else
{
//TODO: wait for IOCP
printf("No thread to schedule\n");
return;
}
}
}
Thread* Scheduler::GetNextThread()
{
for (int i = 0, len = thread_list_.size(); i < len; ++i)
{
auto ret = thread_list_[last_pos_++];
last_pos_ = last_pos_ % len;
if (ret->status == ThreadStatus::READY)
return ret;
}
return nullptr;
}
Scheduler::Scheduler() :
monitor_(this, GetCurrentThreadId())
{
switch_count_ = 0;
monitor_.Start();
}
Thread* Scheduler::CreateThread(Func func, void* arg)
{
constexpr int DEFAULT_STACK_SIZE = 4 << 20;
Thread* new_thread;
if (free_list_.empty())
{
new_thread = new Thread;
new_thread->stack_data = VirtualAlloc(0, DEFAULT_STACK_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
new_thread->next = this;
thread_list_.push_back(new_thread);
}
else
{
new_thread = free_list_.back();
free_list_.pop_back();
}
void** p = (void**)(int(new_thread->stack_data) + DEFAULT_STACK_SIZE);
*--p = new_thread;
*--p = arg;
*--p = func;
*--p = &thread_func;
new_thread->stack = (void*)p;
new_thread->status = ThreadStatus::READY;
new_thread->switch_count_ = 0;
return new_thread;
}