-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfiber.h
165 lines (136 loc) · 4.27 KB
/
fiber.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2018-2020 The EFramework Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef _FIBER_HEADER_
#define _FIBER_HEADER_
#include <sys/mman.h>
#define ERROR_FIBER_EXITED (-1)
#define ERROR_FIBER_NOT_INITED (-2)
#define FIBER_STATUS_EXITED 0
#define FIBER_STATUS_INITED 1
typedef struct _ef_fiber ef_fiber_t;
typedef struct _ef_fiber_sched ef_fiber_sched_t;
/*
the fiber layout
|----------------|
| ef_fiber_t and |
| other headers |
|----------------| <- stack_upper
| |
| |
| |
| |
| |
| really mapped |
| |
| |
| |
| | <- stack_ptr, used by user space context switch
| |
|----------------| <- stack_lower
| |
~ ~
~ ~
| |
| reserved area |
| |
| |
| |
|----------------|
| one page guard |
|----------------| <- stack_area
*/
struct _ef_fiber {
/*
* total memory area size
*/
size_t stack_size;
/*
* start address of reserved area
*/
void *stack_area;
/*
* fiber stack start from here
*/
void *stack_upper;
/*
* lower boundary of really mapped memory area
*/
void *stack_lower;
/*
* save or restore stack pointer on context switching
*/
void *stack_ptr;
/*
* currently initialized or exited
*/
long status;
/*
* when do yield or on exiting, return to parent
*/
ef_fiber_t *parent;
/*
* find the sched struct for the fiber
*/
ef_fiber_sched_t *sched;
};
struct _ef_fiber_sched {
/*
* current "running" fiber
*/
ef_fiber_t *current_fiber;
/*
* just save the stack_ptr of system thread
*/
ef_fiber_t thread_fiber;
};
typedef long (*ef_fiber_proc_t)(void *param);
#define ef_fiber_is_exited(fiber) ((fiber)->status == FIBER_STATUS_EXITED)
/*
* run a just initialized fiber or resume a fiber which doing yield
* sndval will be the return value of the yield function in the latter
*/
int ef_fiber_resume(ef_fiber_sched_t *rt, ef_fiber_t *to, long sndval, long *retval);
/*
* yield cpu and return to parent fiber (maybe system thread)
* the sndval will be the retval of the resume function in parent
*/
long ef_fiber_yield(ef_fiber_sched_t *rt, long sndval);
/*
* create a fiber with stack_size sized stack, and reserve header_size bytes
* to hold the header(s), init the fiber with fiber_proc and param
*/
ef_fiber_t *ef_fiber_create(ef_fiber_sched_t *rt, size_t stack_size, size_t header_size, ef_fiber_proc_t fiber_proc, void *param);
/*
* expand the lower boundary of the fiber stack to addr
*/
int ef_fiber_expand_stack(ef_fiber_t *fiber, void *addr);
/*
* init the sched rt, maybe you want to handle sigsegv yourself
*/
int ef_fiber_init_sched(ef_fiber_sched_t *rt, int handle_sigsegv);
/*
* init a fiber with fiber_proc and param
*/
void ef_fiber_init(ef_fiber_t *fiber, ef_fiber_proc_t fiber_proc, void *param);
/*
* delete a fiber always destroy its whole memory area
*/
void ef_fiber_delete(ef_fiber_t *fiber);
#endif