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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
/* $NetBSD: rumpfiber.c,v 1.13 2017/12/27 09:01:53 ozaki-r Exp $ */
/*
* Copyright (c) 2007-2013 Antti Kantee. All Rights Reserved.
* Copyright (c) 2014 Justin Cormack. 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.
*/
/* Based partly on code from Xen Minios with the following license */
/*
****************************************************************************
* (C) 2005 - Grzegorz Milos - Intel Research Cambridge
****************************************************************************
*
* File: sched.c
* Author: Grzegorz Milos
* Changes: Robert Kaiser
*
* Date: Aug 2005
*
* Environment: Xen Minimal OS
* Description: simple scheduler for Mini-Os
*
* The scheduler is non-preemptive (cooperative), and schedules according
* to Round Robin algorithm.
*
****************************************************************************
* 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.
*/
#include "rumpuser_port.h"
#if !defined(lint)
__RCSID("$NetBSD: rumpfiber.c,v 1.13 2017/12/27 09:01:53 ozaki-r Exp $");
#endif /* !lint */
#include <sys/mman.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <rump/rumpuser.h>
#include "rumpuser_int.h"
#include "rumpfiber.h"
static void init_sched(void);
static void join_thread(struct thread *);
static void switch_threads(struct thread *prev, struct thread *next);
static struct thread *get_current(void);
static int64_t now(void);
static void msleep(uint64_t millisecs);
static void abssleep(uint64_t millisecs);
TAILQ_HEAD(thread_list, thread);
static struct thread_list exited_threads = TAILQ_HEAD_INITIALIZER(exited_threads);
static struct thread_list thread_list = TAILQ_HEAD_INITIALIZER(thread_list);
static struct thread *current_thread = NULL;
static void (*scheduler_hook)(void *, void *);
static void printk(const char *s);
static void
printk(const char *msg)
{
int ret __attribute__((unused));
ret = write(2, msg, strlen(msg));
}
static struct thread *
get_current(void)
{
return current_thread;
}
static int64_t
now(void)
{
struct timespec ts;
int rv;
rv = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(rv == 0);
return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
}
void
schedule(void)
{
struct thread *prev, *next, *thread, *tmp;
int64_t tm, wakeup;
struct timespec sl;
prev = get_current();
do {
tm = now();
wakeup = tm + 1000; /* wake up in 1s max */
next = NULL;
TAILQ_FOREACH_SAFE(thread, &thread_list, thread_list, tmp) {
if (!is_runnable(thread) && thread->wakeup_time >= 0) {
if (thread->wakeup_time <= tm) {
thread->flags |= THREAD_TIMEDOUT;
wake(thread);
} else if (thread->wakeup_time < wakeup)
wakeup = thread->wakeup_time;
}
if (is_runnable(thread)) {
next = thread;
/* Put this thread on the end of the list */
TAILQ_REMOVE(&thread_list, thread, thread_list);
TAILQ_INSERT_TAIL(&thread_list, thread, thread_list);
break;
}
}
if (next)
break;
sl.tv_sec = (wakeup - tm) / 1000;
sl.tv_nsec = ((wakeup - tm) - 1000 * sl.tv_sec) * 1000000;
#ifdef HAVE_CLOCK_NANOSLEEP
clock_nanosleep(CLOCK_MONOTONIC, 0, &sl, NULL);
#else
nanosleep(&sl, NULL);
#endif
} while (1);
if (prev != next)
switch_threads(prev, next);
TAILQ_FOREACH_SAFE(thread, &exited_threads, thread_list, tmp) {
if (thread != prev) {
TAILQ_REMOVE(&exited_threads, thread, thread_list);
if ((thread->flags & THREAD_EXTSTACK) == 0)
munmap(thread->ctx.uc_stack.ss_sp, STACKSIZE);
free(thread->name);
free(thread);
}
}
}
static void
create_ctx(ucontext_t *ctx, void *stack, size_t stack_size,
void (*f)(void *), void *data)
{
getcontext(ctx);
ctx->uc_stack.ss_sp = stack;
ctx->uc_stack.ss_size = stack_size;
ctx->uc_stack.ss_flags = 0;
ctx->uc_link = NULL; /* TODO may link to main thread */
/* may have to do bounce function to call, if args to makecontext are ints */
makecontext(ctx, (void (*)(void))f, 1, data);
}
/* TODO see notes in rumpuser_thread_create, have flags here */
struct thread *
create_thread(const char *name, void *cookie, void (*f)(void *), void *data,
void *stack, size_t stack_size)
{
struct thread *thread = calloc(1, sizeof(struct thread));
if (!thread) {
return NULL;
}
if (!stack) {
assert(stack_size == 0);
stack = mmap(NULL, STACKSIZE, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
if (stack == MAP_FAILED) {
free(thread);
return NULL;
}
stack_size = STACKSIZE;
} else {
thread->flags = THREAD_EXTSTACK;
}
create_ctx(&thread->ctx, stack, stack_size, f, data);
thread->name = strdup(name);
thread->cookie = cookie;
/* Not runnable, not exited, not sleeping */
thread->wakeup_time = -1;
thread->lwp = NULL;
set_runnable(thread);
TAILQ_INSERT_TAIL(&thread_list, thread, thread_list);
return thread;
}
static void
switch_threads(struct thread *prev, struct thread *next)
{
int ret;
current_thread = next;
if (scheduler_hook)
scheduler_hook(prev->cookie, next->cookie);
ret = swapcontext(&prev->ctx, &next->ctx);
if (ret < 0) {
printk("swapcontext failed\n");
abort();
}
}
struct join_waiter {
struct thread *jw_thread;
struct thread *jw_wanted;
TAILQ_ENTRY(join_waiter) jw_entries;
};
static TAILQ_HEAD(, join_waiter) joinwq = TAILQ_HEAD_INITIALIZER(joinwq);
void
exit_thread(void)
{
struct thread *thread = get_current();
struct join_waiter *jw_iter;
/* if joinable, gate until we are allowed to exit */
while (thread->flags & THREAD_MUSTJOIN) {
thread->flags |= THREAD_JOINED;
/* see if the joiner is already there */
TAILQ_FOREACH(jw_iter, &joinwq, jw_entries) {
if (jw_iter->jw_wanted == thread) {
wake(jw_iter->jw_thread);
break;
}
}
block(thread);
schedule();
}
/* Remove from the thread list */
TAILQ_REMOVE(&thread_list, thread, thread_list);
clear_runnable(thread);
/* Put onto exited list */
TAILQ_INSERT_HEAD(&exited_threads, thread, thread_list);
/* Schedule will free the resources */
while (1) {
schedule();
printk("schedule() returned! Trying again\n");
}
}
static void
join_thread(struct thread *joinable)
{
struct join_waiter jw;
struct thread *thread = get_current();
assert(joinable->flags & THREAD_MUSTJOIN);
/* wait for exiting thread to hit thread_exit() */
while (! (joinable->flags & THREAD_JOINED)) {
jw.jw_thread = thread;
jw.jw_wanted = joinable;
TAILQ_INSERT_TAIL(&joinwq, &jw, jw_entries);
block(thread);
schedule();
TAILQ_REMOVE(&joinwq, &jw, jw_entries);
}
/* signal exiting thread that we have seen it and it may now exit */
assert(joinable->flags & THREAD_JOINED);
joinable->flags &= ~THREAD_MUSTJOIN;
wake(joinable);
}
static void msleep(uint64_t millisecs)
{
struct thread *thread = get_current();
thread->wakeup_time = now() + millisecs;
clear_runnable(thread);
schedule();
}
static void abssleep(uint64_t millisecs)
{
struct thread *thread = get_current();
thread->wakeup_time = millisecs;
clear_runnable(thread);
schedule();
}
/* like abssleep, except against realtime clock instead of monotonic clock */
int abssleep_real(uint64_t millisecs)
{
struct thread *thread = get_current();
struct timespec ts;
uint64_t real_now;
int rv;
clock_gettime(CLOCK_REALTIME, &ts);
real_now = 1000*ts.tv_sec + ts.tv_nsec/(1000*1000);
thread->wakeup_time = now() + (millisecs - real_now);
clear_runnable(thread);
schedule();
rv = !!(thread->flags & THREAD_TIMEDOUT);
thread->flags &= ~THREAD_TIMEDOUT;
return rv;
}
void wake(struct thread *thread)
{
thread->wakeup_time = -1;
set_runnable(thread);
}
void block(struct thread *thread)
{
thread->wakeup_time = -1;
clear_runnable(thread);
}
int is_runnable(struct thread *thread)
{
return thread->flags & RUNNABLE_FLAG;
}
void set_runnable(struct thread *thread)
{
thread->flags |= RUNNABLE_FLAG;
}
void clear_runnable(struct thread *thread)
{
thread->flags &= ~RUNNABLE_FLAG;
}
static void
init_sched(void)
{
struct thread *thread = calloc(1, sizeof(struct thread));
if (!thread) {
abort();
}
thread->name = strdup("init");
thread->flags = 0;
thread->wakeup_time = -1;
thread->lwp = NULL;
set_runnable(thread);
TAILQ_INSERT_TAIL(&thread_list, thread, thread_list);
current_thread = thread;
}
void
set_sched_hook(void (*f)(void *, void *))
{
scheduler_hook = f;
}
struct thread *
init_mainthread(void *cookie)
{
current_thread->cookie = cookie;
return current_thread;
}
/* rump functions below */
struct rumpuser_hyperup rumpuser__hyp;
int
rumpuser_init(int version, const struct rumpuser_hyperup *hyp)
{
int rv;
if (version != RUMPUSER_VERSION) {
printk("rumpuser version mismatch\n");
abort();
}
rv = rumpuser__random_init();
if (rv != 0) {
ET(rv);
}
rumpuser__hyp = *hyp;
init_sched();
return 0;
}
int
rumpuser_clock_gettime(int enum_rumpclock, int64_t *sec, long *nsec)
{
enum rumpclock rclk = enum_rumpclock;
struct timespec ts;
clockid_t clk;
int rv;
switch (rclk) {
case RUMPUSER_CLOCK_RELWALL:
clk = CLOCK_REALTIME;
break;
case RUMPUSER_CLOCK_ABSMONO:
clk = CLOCK_MONOTONIC;
break;
default:
abort();
}
if (clock_gettime(clk, &ts) == -1) {
rv = errno;
} else {
*sec = ts.tv_sec;
*nsec = ts.tv_nsec;
rv = 0;
}
ET(rv);
}
int
rumpuser_clock_sleep(int enum_rumpclock, int64_t sec, long nsec)
{
enum rumpclock rclk = enum_rumpclock;
uint64_t msec;
int nlocks;
rumpkern_unsched(&nlocks, NULL);
switch (rclk) {
case RUMPUSER_CLOCK_RELWALL:
msec = sec * 1000 + nsec / (1000*1000UL);
msleep(msec);
break;
case RUMPUSER_CLOCK_ABSMONO:
msec = sec * 1000 + nsec / (1000*1000UL);
abssleep(msec);
break;
}
rumpkern_sched(nlocks, NULL);
return 0;
}
int
rumpuser_getparam(const char *name, void *buf, size_t blen)
{
int rv;
const char *ncpu = "1";
if (strcmp(name, RUMPUSER_PARAM_NCPU) == 0) {
strncpy(buf, ncpu, blen);
rv = 0;
} else if (strcmp(name, RUMPUSER_PARAM_HOSTNAME) == 0) {
char tmp[MAXHOSTNAMELEN];
if (gethostname(tmp, sizeof(tmp)) == -1) {
snprintf(buf, blen, "rump-%05d", (int)getpid());
} else {
snprintf(buf, blen, "rump-%05d.%s",
(int)getpid(), tmp);
}
rv = 0;
} else if (*name == '_') {
rv = EINVAL;
} else {
if (getenv_r(name, buf, blen) == -1)
rv = errno;
else
rv = 0;
}
ET(rv);
}
void
rumpuser_putchar(int c)
{
putchar(c);
}
__dead void
rumpuser_exit(int rv)
{
if (rv == RUMPUSER_PANIC)
abort();
else
exit(rv);
}
void
rumpuser_seterrno(int error)
{
errno = error;
}
/*
* This is meant for safe debugging prints from the kernel.
*/
void
rumpuser_dprintf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
int
rumpuser_kill(int64_t pid, int rumpsig)
{
int sig;
sig = rumpuser__sig_rump2host(rumpsig);
if (sig > 0)
raise(sig);
return 0;
}
/* thread functions */
TAILQ_HEAD(waithead, waiter);
struct waiter {
struct thread *who;
TAILQ_ENTRY(waiter) entries;
int onlist;
};
static int
wait(struct waithead *wh, uint64_t msec)
{
struct waiter w;
w.who = get_current();
TAILQ_INSERT_TAIL(wh, &w, entries);
w.onlist = 1;
block(w.who);
if (msec)
w.who->wakeup_time = now() + msec;
schedule();
/* woken up by timeout? */
if (w.onlist)
TAILQ_REMOVE(wh, &w, entries);
return w.onlist ? ETIMEDOUT : 0;
}
static void
wakeup_one(struct waithead *wh)
{
struct waiter *w;
if ((w = TAILQ_FIRST(wh)) != NULL) {
TAILQ_REMOVE(wh, w, entries);
w->onlist = 0;
wake(w->who);
}
}
static void
wakeup_all(struct waithead *wh)
{
struct waiter *w;
while ((w = TAILQ_FIRST(wh)) != NULL) {
TAILQ_REMOVE(wh, w, entries);
w->onlist = 0;
wake(w->who);
}
}
int
rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
int joinable, int pri, int cpuidx, void **tptr)
{
struct thread *thr;
thr = create_thread(thrname, NULL, (void (*)(void *))f, arg, NULL, 0);
if (!thr)
return EINVAL;
/*
* XXX: should be supplied as a flag to create_thread() so as to
* _ensure_ it's set before the thread runs (and could exit).
* now we're trusting unclear semantics of create_thread()
*/
if (thr && joinable)
thr->flags |= THREAD_MUSTJOIN;
*tptr = thr;
return 0;
}
void
rumpuser_thread_exit(void)
{
exit_thread();
}
int
rumpuser_thread_join(void *p)
{
join_thread(p);
return 0;
}
struct rumpuser_mtx {
struct waithead waiters;
int v;
int flags;
struct lwp *o;
};
void
rumpuser_mutex_init(struct rumpuser_mtx **mtxp, int flags)
{
struct rumpuser_mtx *mtx;
mtx = malloc(sizeof(*mtx));
memset(mtx, 0, sizeof(*mtx));
mtx->flags = flags;
TAILQ_INIT(&mtx->waiters);
*mtxp = mtx;
}
int
rumpuser_mutex_spin_p(struct rumpuser_mtx *mtx)
{
return (mtx->flags & RUMPUSER_MTX_SPIN) != 0;
}
void
rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
{
int nlocks;
if (rumpuser_mutex_tryenter(mtx) != 0) {
rumpkern_unsched(&nlocks, NULL);
while (rumpuser_mutex_tryenter(mtx) != 0)
wait(&mtx->waiters, 0);
rumpkern_sched(nlocks, NULL);
}
}
void
rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
{
int rv;
rv = rumpuser_mutex_tryenter(mtx);
/* one VCPU supported, no preemption => must succeed */
if (rv != 0) {
printk("no voi ei\n");
}
}
int
rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
{
struct lwp *l = get_current()->lwp;
if (mtx->v && mtx->o != l)
return EBUSY;
mtx->v++;
mtx->o = l;
return 0;
}
void
rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
{
assert(mtx->v > 0);
if (--mtx->v == 0) {
mtx->o = NULL;
wakeup_one(&mtx->waiters);
}
}
void
rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
{
assert(TAILQ_EMPTY(&mtx->waiters) && mtx->o == NULL);
free(mtx);
}
void
rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp)
{
*lp = mtx->o;
}
struct rumpuser_rw {
struct waithead rwait;
struct waithead wwait;
int v;
struct lwp *o;
};
void
rumpuser_rw_init(struct rumpuser_rw **rwp)
{
struct rumpuser_rw *rw;
rw = malloc(sizeof(*rw));
memset(rw, 0, sizeof(*rw));
TAILQ_INIT(&rw->rwait);
TAILQ_INIT(&rw->wwait);
*rwp = rw;
}
void
rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw)
{
enum rumprwlock lk = enum_rumprwlock;
struct waithead *w = NULL;
int nlocks;
switch (lk) {
case RUMPUSER_RW_WRITER:
w = &rw->wwait;
break;
case RUMPUSER_RW_READER:
w = &rw->rwait;
break;
}
if (rumpuser_rw_tryenter(enum_rumprwlock, rw) != 0) {
rumpkern_unsched(&nlocks, NULL);
while (rumpuser_rw_tryenter(enum_rumprwlock, rw) != 0)
wait(w, 0);
rumpkern_sched(nlocks, NULL);
}
}
int
rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw)
{
enum rumprwlock lk = enum_rumprwlock;
int rv;
switch (lk) {
case RUMPUSER_RW_WRITER:
if (rw->o == NULL) {
rw->o = rumpuser_curlwp();
rv = 0;
} else {
rv = EBUSY;
}
break;
case RUMPUSER_RW_READER:
if (rw->o == NULL && TAILQ_EMPTY(&rw->wwait)) {
rw->v++;
rv = 0;
} else {
rv = EBUSY;
}
break;
default:
rv = EINVAL;
}
return rv;
}
void
rumpuser_rw_exit(struct rumpuser_rw *rw)
{
if (rw->o) {
rw->o = NULL;
} else {
rw->v--;
}
/* standard procedure, don't let readers starve out writers */
if (!TAILQ_EMPTY(&rw->wwait)) {
if (rw->o == NULL)
wakeup_one(&rw->wwait);
} else if (!TAILQ_EMPTY(&rw->rwait) && rw->o == NULL) {
wakeup_all(&rw->rwait);
}
}
void
rumpuser_rw_destroy(struct rumpuser_rw *rw)
{
free(rw);
}
void
rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rvp)
{
enum rumprwlock lk = enum_rumprwlock;
switch (lk) {
case RUMPUSER_RW_WRITER:
*rvp = rw->o == rumpuser_curlwp();
break;
case RUMPUSER_RW_READER:
*rvp = rw->v > 0;
break;
}
}
void
rumpuser_rw_downgrade(struct rumpuser_rw *rw)
{
assert(rw->o == rumpuser_curlwp());
rw->v = -1;
}
int
rumpuser_rw_tryupgrade(struct rumpuser_rw *rw)
{
if (rw->v == -1) {
rw->v = 1;
rw->o = rumpuser_curlwp();
return 0;
}
return EBUSY;
}
struct rumpuser_cv {
struct waithead waiters;
int nwaiters;
};
void
rumpuser_cv_init(struct rumpuser_cv **cvp)
{
struct rumpuser_cv *cv;
cv = malloc(sizeof(*cv));
memset(cv, 0, sizeof(*cv));
TAILQ_INIT(&cv->waiters);
*cvp = cv;
}
void
rumpuser_cv_destroy(struct rumpuser_cv *cv)
{
assert(cv->nwaiters == 0);
free(cv);
}
static void
cv_unsched(struct rumpuser_mtx *mtx, int *nlocks)
{
rumpkern_unsched(nlocks, mtx);
rumpuser_mutex_exit(mtx);
}
static void
cv_resched(struct rumpuser_mtx *mtx, int nlocks)
{
/* see rumpuser(3) */
if ((mtx->flags & (RUMPUSER_MTX_KMUTEX | RUMPUSER_MTX_SPIN)) ==
(RUMPUSER_MTX_KMUTEX | RUMPUSER_MTX_SPIN)) {
rumpkern_sched(nlocks, mtx);
rumpuser_mutex_enter_nowrap(mtx);
} else {
rumpuser_mutex_enter_nowrap(mtx);
rumpkern_sched(nlocks, mtx);
}
}
void
rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
{
int nlocks;
cv->nwaiters++;
cv_unsched(mtx, &nlocks);
wait(&cv->waiters, 0);
cv_resched(mtx, nlocks);
cv->nwaiters--;
}
void
rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
{
cv->nwaiters++;
rumpuser_mutex_exit(mtx);
wait(&cv->waiters, 0);
rumpuser_mutex_enter_nowrap(mtx);
cv->nwaiters--;
}
int
rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
int64_t sec, int64_t nsec)
{
int nlocks;
int rv;
cv->nwaiters++;
cv_unsched(mtx, &nlocks);
rv = wait(&cv->waiters, sec * 1000 + nsec / (1000*1000));
cv_resched(mtx, nlocks);
cv->nwaiters--;
return rv;
}
void
rumpuser_cv_signal(struct rumpuser_cv *cv)
{
wakeup_one(&cv->waiters);
}
void
rumpuser_cv_broadcast(struct rumpuser_cv *cv)
{
wakeup_all(&cv->waiters);
}
void
rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rvp)
{
*rvp = cv->nwaiters != 0;
}
/*
* curlwp
*/
void
rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
{
struct thread *thread;
enum rumplwpop op = enum_rumplwpop;
switch (op) {
case RUMPUSER_LWP_CREATE:
case RUMPUSER_LWP_DESTROY:
break;
case RUMPUSER_LWP_SET:
thread = get_current();
thread->lwp = l;
break;
case RUMPUSER_LWP_CLEAR:
thread = get_current();
assert(thread->lwp == l);
thread->lwp = NULL;
break;
}
}
struct lwp *
rumpuser_curlwp(void)
{
return get_current()->lwp;
}
|