-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy path_lwp.c
More file actions
397 lines (316 loc) · 7.84 KB
/
_lwp.c
File metadata and controls
397 lines (316 loc) · 7.84 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*-
* Copyright (c) 2014, 2015 Antti Kantee. 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 ``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 _lwp_park ___lwp_park60
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/lwpctl.h>
#include <sys/lwp.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/tls.h>
#include <assert.h>
#include <errno.h>
#include <lwp.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rump/rump.h>
#include <bmk-core/core.h>
#include <bmk-core/sched.h>
#include <rumprun-base/makelwp.h>
#include "rumprun-private.h"
struct rumprun_lwp {
struct bmk_thread *rl_thread;
int rl_lwpid;
char rl_name[MAXCOMLEN+1];
void (*rl_start)(void *);
void *rl_arg;
struct lwpctl rl_lwpctl;
int rl_no_parking_hare; /* a looney tunes reference ... finally! */
TAILQ_ENTRY(rumprun_lwp) rl_entries;
};
static TAILQ_HEAD(, rumprun_lwp) all_lwp = TAILQ_HEAD_INITIALIZER(all_lwp);
static __thread struct rumprun_lwp *me;
#define FIRST_LWPID 1
static int curlwpid = FIRST_LWPID;
static struct rumprun_lwp mainthread = {
.rl_lwpid = FIRST_LWPID,
};
static void rumprun_makelwp_tramp(void *);
static ptrdiff_t meoff;
static void
assignme(void *tcb, struct rumprun_lwp *value)
{
struct rumprun_lwp **dst = (void *)((uintptr_t)tcb + meoff);
*dst = value;
}
int
_lwp_ctl(int ctl, struct lwpctl **data)
{
*data = (struct lwpctl *)&me->rl_lwpctl;
return 0;
}
int
rumprun_makelwp(void (*start)(void *), void *arg, void *private,
void *stack_base, size_t stack_size, unsigned long flag, lwpid_t *lid)
{
struct rumprun_lwp *rl;
struct lwp *curlwp, *newlwp;
rl = calloc(1, sizeof(*rl));
if (rl == NULL)
return errno;
assignme(private, rl);
curlwp = rump_pub_lwproc_curlwp();
if ((errno = rump_pub_lwproc_newlwp(getpid())) != 0) {
free(rl);
return errno;
}
newlwp = rump_pub_lwproc_curlwp();
rl->rl_start = start;
rl->rl_arg = arg;
rl->rl_lwpid = ++curlwpid;
rl->rl_thread = bmk_sched_create_withtls("lwp", rl, 0,
rumprun_makelwp_tramp, newlwp, stack_base, stack_size, private);
if (rl->rl_thread == NULL) {
free(rl);
rump_pub_lwproc_releaselwp();
rump_pub_lwproc_switch(curlwp);
return EBUSY; /* ??? */
}
rump_pub_lwproc_switch(curlwp);
*lid = rl->rl_lwpid;
TAILQ_INSERT_TAIL(&all_lwp, rl, rl_entries);
return 0;
}
static void
rumprun_makelwp_tramp(void *arg)
{
rump_pub_lwproc_switch(arg);
(me->rl_start)(me->rl_arg);
}
static struct rumprun_lwp *
lwpid2rl(lwpid_t lid)
{
struct rumprun_lwp *rl;
if (lid == 0)
return &mainthread;
TAILQ_FOREACH(rl, &all_lwp, rl_entries) {
if (rl->rl_lwpid == lid)
return rl;
}
return NULL;
}
int
_lwp_unpark(lwpid_t lid, const void *hint)
{
struct rumprun_lwp *rl;
if ((rl = lwpid2rl(lid)) == NULL) {
return -1;
}
bmk_sched_wake(rl->rl_thread);
return 0;
}
ssize_t
_lwp_unpark_all(const lwpid_t *targets, size_t ntargets, const void *hint)
{
ssize_t rv;
if (targets == NULL)
return 1024;
rv = ntargets;
while (ntargets--) {
if (_lwp_unpark(*targets, NULL) != 0)
rv--;
targets++;
}
//assert(rv >= 0);
return rv;
}
/*
* called by the scheduler when a context switch is made
* nb. cookie is null when non-lwp threads are being run
*/
static void
schedhook(void *prevcookie, void *nextcookie)
{
struct rumprun_lwp *prev, *next;
prev = prevcookie;
next = nextcookie;
if (prev && prev->rl_lwpctl.lc_curcpu != LWPCTL_CPU_EXITED) {
prev->rl_lwpctl.lc_curcpu = LWPCTL_CPU_NONE;
}
if (next) {
next->rl_lwpctl.lc_curcpu = 0;
next->rl_lwpctl.lc_pctr++;
}
}
void
rumprun_lwp_init(void)
{
void *tcb = bmk_sched_gettcb();
bmk_sched_set_hook(schedhook);
meoff = (uintptr_t)&me - (uintptr_t)tcb;
assignme(tcb, &mainthread);
mainthread.rl_thread = bmk_sched_init_mainlwp(&mainthread);
TAILQ_INSERT_TAIL(&all_lwp, me, rl_entries);
}
int
_lwp_park(clockid_t clock_id, int flags, struct timespec *ts,
lwpid_t unpark, const void *hint, const void *unparkhint)
{
int rv;
if (unpark)
_lwp_unpark(unpark, unparkhint);
if (me->rl_no_parking_hare) {
me->rl_no_parking_hare = 0;
return 0;
}
if (ts) {
bmk_time_t nsecs = ts->tv_sec*1000*1000*1000 + ts->tv_nsec;
if (flags & TIMER_ABSTIME) {
nsecs -= bmk_platform_cpu_clock_epochoffset();
} else {
nsecs += bmk_platform_cpu_clock_monotonic();
}
bmk_sched_blockprepare_timeout(nsecs);
} else {
bmk_sched_blockprepare();
}
rv = bmk_sched_block();
bmk_assert(rv == 0 || rv == ETIMEDOUT);
if (rv) {
errno = rv;
rv = -1;
}
return rv;
}
int
_lwp_exit(void)
{
me->rl_lwpctl.lc_curcpu = LWPCTL_CPU_EXITED;
rump_pub_lwproc_releaselwp();
TAILQ_REMOVE(&all_lwp, me, rl_entries);
/* could just assign it here, but for symmetry! */
assignme(bmk_sched_gettcb(), NULL);
bmk_sched_exit_withtls();
return 0;
}
int
_lwp_continue(lwpid_t lid)
{
struct rumprun_lwp *rl;
if ((rl = lwpid2rl(lid)) == NULL) {
return ESRCH;
}
bmk_sched_unsuspend(rl->rl_thread);
return 0;
}
int
_lwp_suspend(lwpid_t lid)
{
struct rumprun_lwp *rl;
if ((rl = lwpid2rl(lid)) == NULL) {
return ESRCH;
}
bmk_sched_suspend(rl->rl_thread);
return 0;
}
int
_lwp_wakeup(lwpid_t lid)
{
struct rumprun_lwp *rl;
if ((rl = lwpid2rl(lid)) == NULL)
return ESRCH;
bmk_sched_wake(rl->rl_thread);
return 0;
}
int
_lwp_setname(lwpid_t lid, const char *name)
{
struct rumprun_lwp *rl;
if ((rl = lwpid2rl(lid)) == NULL)
return ESRCH;
strlcpy(rl->rl_name, name, sizeof(rl->rl_name));
return 0;
}
lwpid_t
_lwp_self(void)
{
return me->rl_lwpid;
}
/* XXX: messy. see sched.h, libc, libpthread, and all over */
int _sys_sched_yield(void);
int
_sys_sched_yield(void)
{
bmk_sched_yield();
return 0;
}
__weak_alias(sched_yield,_sys_sched_yield);
struct tls_tcb *
_rtld_tls_allocate(void)
{
return bmk_sched_tls_alloc();
}
void
_rtld_tls_free(struct tls_tcb *arg)
{
return bmk_sched_tls_free(arg);
}
void *
_lwp_getprivate(void)
{
return bmk_sched_gettcb();
}
void _lwpnullop(void);
void _lwpnullop(void) { }
int _lwpsuccess(void);
int _lwpsuccess(void) { return 0; }
void _lwpabort(void);
void __dead
_lwpabort(void)
{
printf("_lwpabort() called\n");
_exit(1);
}
__strong_alias(_sys_setcontext,_lwpabort);
__strong_alias(_lwp_kill,_lwpabort);
__strong_alias(__libc_static_tls_setup,_lwpnullop);
int rasctl(void);
int rasctl(void) { return ENOSYS; }
/*
* There is ongoing work to support these in the rump kernel,
* so I will just stub them out for now.
*/
__strong_alias(_sched_getaffinity,_lwpnullop);
__strong_alias(_sched_getparam,_lwpnullop);
__strong_alias(_sched_setaffinity,_lwpnullop);
__strong_alias(_sched_setparam,_lwpnullop);
/*
* Technically, specifying a lower >0 protection level is an error,
* but we don't flag that error for now.
*/
__strong_alias(_sched_protect,_lwpsuccess);