-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiber_test.cpp
More file actions
119 lines (88 loc) · 1.77 KB
/
fiber_test.cpp
File metadata and controls
119 lines (88 loc) · 1.77 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
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
#include <stdio.h>
#include <stdlib.h>
#include <emmintrin.h>
#include <functional>
struct
context
{
void *rip, *rsp;
void *rbx, *rbp, *r12, *r13, *r14, *r15, *rdi, *rsi;
// windows
__m128i xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15;
};
typedef void* job;
extern "C" void get_context(context *c);
extern "C" void set_context(context *c);
extern "C" void swap_context(context *a, context *b);
struct
parameters
{
int Id = 0;
int Name = 0;
int Value1 = 0;
int Value2 = 0;
};
bool workDone = false;
struct main_job
{
context AwakeContext = {};
context Context = {};
job Job;
};
struct
fiber
{
char Data[4096] = { 0 };
main_job Main = {};
static void
func(uintptr_t ptr)
{
main_job *MainJob = (main_job *)ptr;
void (*ff)() = (void(*)())MainJob->Job;
ff();
set_context(&MainJob->AwakeContext);
//swap_context(&MainJob.Context, &MainJob.AwakeContext);
}
fiber(job Job)
{
char* sp = (char*)(Data + sizeof(fiber::Data));
sp = (char*)((uintptr_t)sp & -16L);
Main.Job = Job;
uintptr_t* spPointer = (uintptr_t*)Data;
*spPointer = (uintptr_t)&Main;
Main.Context = { 0 };
Main.Context.rip = func;
Main.Context.rsp = (void*)sp;
}
void static
start(fiber* Fiber)
{
//set_context(&Fiber->Context);
swap_context(&Fiber->Main.AwakeContext, &Fiber->Main.Context);
}
};
//template<class... Args>
//void f(Args... args)
//{
// auto x = [args...] { return g(args...); };
// x();
//}
int id = 0;
void some_work()
{
id++;
}
fiber Fiber(some_work);
int main()
{
volatile bool InUse = false;
//get_context(&Fiber.Main.AwakeContext);
printf("hello, world!\n");
if (!InUse)
{
InUse = true;
fiber::start(&Fiber);
}
printf("END %d\n", id);
return 0;
}