-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_system.cpp
83 lines (72 loc) · 1.73 KB
/
task_system.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
73
74
75
76
77
78
79
80
81
82
/**
* @file task_system.cpp
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2021-02-14
*
*/
#include "config.hpp"
#include "task_system.hpp"
/**
* @brief
* Each Thread loop to get user space thread request in a queue
* @param i
* i = Thread Number
*/
void task_system::run(unsigned i) {
currContext[KTHREAD] = new ThreadCtx();
currContext[KTHREAD]->setState(uState::RUNNING);
currContext[UTHREAD] = nullptr;
while(true) {
ThreadCtx * ctx = nullptr;
if(!_q.pop(&ctx)) break;
if(ctx == nullptr) {
Log("ctx is nullptr");
}else if(ctx->getState() != uState::COMPLETE) {
u_yield_to(ctx);
if(ctx->getState() != uState::COMPLETE) {
// push tp queue again
_q.push(ctx);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}else {
Log("rsp = ",(uint64_t)(ctx->getRegCtxPtr()->rsp));
auto stackbase = (char*)(ctx->getRegCtxPtr()->rsp) - StackSize;
// delete stackbase;
delete ctx;
}
}
}
delete currContext[KTHREAD];
}
/**
* @brief Construct a new task system::task system object
*
*/
task_system::task_system() {
for(unsigned n = 0;n != this->_count ;++n) {
_threads.emplace_back([&,n]() {
run(n);
Log("work done!");
});
}
}
/**
* @brief Destroy the task system object
*
*/
task_system::~task_system() {
destruct();
}
/**
* @brief
*
*/
void task_system::destruct() {
this->_q.done(); // wake up the threads
for(auto& e : this->_threads) {
if(e.joinable()) {
e.join();
}
}
}