-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGreen.c
147 lines (135 loc) · 3.16 KB
/
Green.c
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
#include "Green.h"
#define MAX_THREAD 64
volatile CONTEXT Main;
volatile CONTEXT* Contexts[MAX_THREAD];
volatile UINTN RunningId = 0;
volatile EFI_EVENT* Schedule = NULL;
VOID EFIAPI SwitchContext(volatile CONTEXT*, volatile CONTEXT*);
VOID EFIAPI
CChangeContext(IN EFI_EVENT Event, IN VOID* Args)
{
UINTN NextRunning = (RunningId+1) % MAX_THREAD;
while(Contexts[NextRunning] == NULL ||
Contexts[NextRunning]->Stat != THREAD_RUNNABLE){
NextRunning = (NextRunning + 1) % MAX_THREAD;
}
if(NextRunning == RunningId){
return;
}
volatile CONTEXT* Old = Contexts[RunningId];
volatile CONTEXT* New = Contexts[NextRunning];
RunningId = NextRunning;
SwitchContext(Old, New);
}
VOID
ThreadInitialize(VOID)
{
if(Schedule){
return;
}
Schedule = AllocatePool(sizeof(EFI_EVENT));
if(Schedule == NULL){
Print(L"Cannot allocate pool!\n");
}
// Initialize the Contexts
Contexts[0] = &Main;
UINT8 i;
for(i = 1; i < MAX_THREAD; i++){
Contexts[i] = NULL;
}
EFI_STATUS Status = gBS->CreateEvent(EVT_TIMER |
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
CChangeContext,
NULL,
(EFI_EVENT*) Schedule);
if(EFI_ERROR(Status)){
Print(L"Error creating the event: %d\n", Status);
}
// 40000 (* 100 ns = 4 000 000 ns = 4 ms)
Status = gBS->SetTimer(*Schedule, TimerPeriodic, 40000);
if(EFI_ERROR(Status)){
Print(L"Error setting the timer : %d\n", Status);
}
}
UINTN
GetFreshId(VOID)
{
UINT8 i;
for(i = 1; i < MAX_THREAD; i++){
if(Contexts[i] == NULL ||
Contexts[i]->Stat == THREAD_JOINED){
return i;
}
}
return 0;
}
VOID
ThreadCreate(CONTEXT* Ctx, GREEN_FUNCTION Function, VOID* Param)
{
gBS->SetMem(Ctx, sizeof(CONTEXT), 0);
VOID* Stack = AllocatePool(STACKSIZE);
Ctx->StackBase = Stack;
// Put the return address at the top of the stack
Stack += STACKSIZE - 8;
*((UINTN*) Stack) = (UINTN) ThreadWrapper;
Ctx->Rsp = (UINTN) Stack;
Ctx->Rip = (UINTN) ThreadWrapper;
// Argument of the function
Ctx->Rdi = (UINTN) Ctx;
Ctx->Rsi = (UINTN) Function;
Ctx->Rdx = (UINTN) Param;
Ctx->Stat = THREAD_RUNNABLE;
// Context id
UINTN Id = GetFreshId();
if(Id == 0){
Print(L"Error getting a fresh id\n");
FreePool(Ctx->StackBase);
return;
}
Ctx->Id = Id;
Contexts[Id] = Ctx;
}
static inline
VOID
ThreadFinish(CONTEXT* Ctx, VOID** Return)
{
if(Return != NULL){
*Return = Ctx->Return;
}
Ctx->Stat = THREAD_JOINED;
// Free the space for another thread
Contexts[Ctx->Id] = NULL;
FreePool(Ctx->StackBase);
}
VOID
ThreadJoin(CONTEXT* Ctx, VOID** Return)
{
if(Ctx->Stat == THREAD_JOINED){
return;
} else if(Ctx->Stat == THREAD_FINISHED) {
ThreadFinish(Ctx, Return);
} else {
// Wait the thread to finish
while(Ctx->Stat != THREAD_FINISHED){
ThreadYield();
}
ThreadFinish(Ctx, Return);
}
}
VOID
ThreadWrapper(CONTEXT* Ctx, GREEN_FUNCTION Function, VOID* Param)
{
gBS->RestoreTPL(TPL_APPLICATION);
VOID* Return = Function(Param);
Ctx->Return = Return;
Ctx->Stat = THREAD_FINISHED;
// Wait for a thread to join this one
for(;;){
ThreadYield();
}
}
VOID
ThreadYield(VOID){
gBS->SignalEvent(*Schedule);
}