-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_thread_api.cpp
More file actions
94 lines (67 loc) · 1.89 KB
/
user_thread_api.cpp
File metadata and controls
94 lines (67 loc) · 1.89 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
/**
* @file user_thread.cpp
* @author d.deka (https://ddeka0.github.io/)
* @brief
* @version 0.1
* @date 2021-02-14
*
*/
#include "user_thread_api.hpp"
#include "threadCtx.hpp"
#include "logger.hpp"
#include "config.hpp"
#include <thread>
#include <unordered_map>
/** u_yield_to(ThreadCtx* nextThread)
* @brief
* Current pthread yields control to a user space thread
* @param nextThread
* next thread context to load from
* @return true
* Context Switch is done properly
* @return false
* Error while context switch
*/
bool u_yield_to(ThreadCtx* nextThread) {
PrintF
currContext[KTHREAD]->setState(uState::READY);
auto currRegCtx = currContext[KTHREAD]->getRegCtxPtr();
nextThread->setState(uState::RUNNING);
auto nextRegCtx = nextThread->getRegCtxPtr();
currContext[UTHREAD] = nextThread;
Log("Calling ContextSwitch");
ContextSwitch(currRegCtx,nextRegCtx);
Log("TID = ",THIS_THREAD_ID);
return true;
}
/** u_yield()
* @brief
* An user thread is going to yield to a pthread
* @return true
* @return false
*/
bool u_yield() {
PrintF
currContext[UTHREAD]->setState(uState::READY);
auto currRegCtx = currContext[UTHREAD]->getRegCtxPtr();
currContext[KTHREAD]->setState(uState::RUNNING);
auto nextRegCtx = currContext[KTHREAD]->getRegCtxPtr();
Log("Calling ContextSwitch");
ContextSwitch(currRegCtx,nextRegCtx);
Log("TID = ",THIS_THREAD_ID);
return true;
}
/** u_stop()
* @brief
* Marks the end of this user space thread and yield to a pthread
*/
void u_stop() {
PrintF;
currContext[UTHREAD]->setState(uState::COMPLETE);
auto currRegCtx = currContext[UTHREAD]->getRegCtxPtr();
currContext[KTHREAD]->setState(uState::RUNNING);
auto nextRegCtx = currContext[KTHREAD]->getRegCtxPtr();
Log("Calling ContextSwitch");
ContextSwitch(currRegCtx,nextRegCtx);
Log("TID = ",THIS_THREAD_ID);
}