-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvireo.c
227 lines (197 loc) · 5.02 KB
/
vireo.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* vireo - Preemptible green threads in C for Linux
* https://github.com/geofft/vireo
*
* Copyright (c) 2014 Alexander Chernyakhovsky <achernya@mit.edu>
* Copyright (c) 2014 Geoffrey Thomas <geofft@ldpreload.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include <signal.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include "vireo.h"
#define UNUSED __attribute__((unused))
#define TIMERSIG SIGRTMIN
// Environment structure, containing a status and a jump buffer.
typedef struct Env {
int status;
ucontext_t state;
int state_reentered;
int ipc_sender;
int ipc_value;
} Env;
// Increase NENV to get more greenlets
#define NENV 1024
// Status codes for the Env
#define ENV_UNUSED 0
#define ENV_RUNNABLE 1
#define ENV_WAITING 2
#define ENV_STACK_SIZE 16384
static Env envs[NENV];
static int curenv;
void umain(void); /* provided by user */
/* Define a "successor context" for the purpose of calling env_exit */
static ucontext_t exiter = {0};
/* Preemption timer */
timer_t timer;
const struct itimerspec ts = {
{0, 0},
{0, 100000000},
};
static void
make_stack(ucontext_t *ucp)
{
// Reuse existing stack if any
if (ucp->uc_stack.ss_sp)
return;
ucp->uc_stack.ss_sp = mmap(
NULL, ENV_STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_PRIVATE,
-1, 0);
ucp->uc_stack.ss_size = ENV_STACK_SIZE;
}
int
vireo_create(vireo_entry entry)
{
// Find an available environment
int env;
for (env = 0; (env < NENV); env++) {
if (envs[env].status == ENV_UNUSED) {
// This one is usable!
break;
}
}
if (env == NENV) {
// No available environments found
return -1;
}
envs[env].status = ENV_RUNNABLE;
getcontext(&envs[env].state);
make_stack(&envs[env].state);
envs[env].state.uc_link = &exiter;
makecontext(&envs[env].state, entry, 0);
// Creation worked. Yay.
return env;
}
static void
vireo_schedule(void)
{
int attempts = 0;
int candidate;
while (attempts < NENV) {
candidate = (curenv + attempts + 1) % NENV;
if (envs[candidate].status == ENV_RUNNABLE) {
curenv = candidate;
/* Request delivery of TIMERSIG after 10 ms */
timer_settime(timer, 0, &ts, NULL);
setcontext(&envs[curenv].state);
}
attempts++;
}
exit(0);
}
void
vireo_yield(void)
{
envs[curenv].state_reentered = 0;
getcontext(&envs[curenv].state);
if (envs[curenv].state_reentered++ == 0) {
// Save successful, find the next process to run.
vireo_schedule();
}
// We've re-entered. Do nothing.
}
void
vireo_exit(void)
{
envs[curenv].status = ENV_UNUSED;
vireo_schedule();
}
void
vireo_destroy(int env)
{
envs[env].status = ENV_UNUSED;
}
int
vireo_getid(void)
{
return curenv;
}
int
vireo_recv(int *who)
{
envs[curenv].status = ENV_WAITING;
vireo_yield();
if (who)
*who = envs[curenv].ipc_sender;
return envs[curenv].ipc_value;
}
void
vireo_send(int toenv, int val)
{
while (envs[toenv].status != ENV_WAITING)
vireo_yield();
envs[toenv].ipc_sender = curenv;
envs[toenv].ipc_value = val;
envs[toenv].status = ENV_RUNNABLE;
}
static void
preempt(int signum UNUSED, siginfo_t *si UNUSED, void *context UNUSED)
{
vireo_yield();
}
static void
enable_preemption(void)
{
struct sigaction act = {
.sa_sigaction = preempt,
.sa_flags = SA_SIGINFO,
};
struct sigevent sigev = {
.sigev_notify = SIGEV_SIGNAL,
.sigev_signo = TIMERSIG,
.sigev_value.sival_int = 0,
};
sigemptyset(&act.sa_mask);
sigaction(TIMERSIG, &act, NULL);
timer_create(CLOCK_PROCESS_CPUTIME_ID, &sigev, &timer);
}
static void
initialize_threads(vireo_entry new_main)
{
curenv = 0;
getcontext(&exiter);
make_stack(&exiter);
makecontext(&exiter, vireo_exit, 0);
vireo_create(new_main);
setcontext(&envs[curenv].state);
}
int
main(int argc UNUSED, char* argv[] UNUSED)
{
enable_preemption();
initialize_threads(umain);
return 0;
}