1 /* $NetBSD: ltsleep.c,v 1.24 2009/12/20 13:49:36 pooka Exp $ */
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Finnish Cultural Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Emulate the prehistoric tsleep-style kernel interfaces. We assume
33 * only code under the biglock will be using this type of synchronization
34 * and use the biglock as the cv interlock.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ltsleep.c,v 1.24 2009/12/20 13:49:36 pooka Exp $");
40 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/simplelock.h>
46 #include <rump/rumpuser.h>
48 #include "rump_private.h"
52 struct rumpuser_cv
*cv
;
53 LIST_ENTRY(ltsleeper
) entries
;
56 static LIST_HEAD(, ltsleeper
) sleepers
= LIST_HEAD_INITIALIZER(sleepers
);
59 sleeper(struct ltsleeper
*ltsp
, int timo
)
61 struct timespec ts
, ticks
;
64 LIST_INSERT_HEAD(&sleepers
, ltsp
, entries
);
65 kernel_unlock_allbutone(&nlocks
);
67 /* protected by biglock */
70 * Calculate wakeup-time.
71 * XXX: should assert nanotime() does not block,
72 * i.e. yield the cpu and/or biglock.
74 ticks
.tv_sec
= timo
/ hz
;
75 ticks
.tv_nsec
= (timo
% hz
) * (1000000000/hz
);
77 timespecadd(&ts
, &ticks
, &ts
);
79 if (rumpuser_cv_timedwait(ltsp
->cv
, rump_giantlock
,
80 ts
.tv_sec
, ts
.tv_nsec
) == 0)
85 rumpuser_cv_wait(ltsp
->cv
, rump_giantlock
);
89 LIST_REMOVE(ltsp
, entries
);
90 rumpuser_cv_destroy(ltsp
->cv
);
91 kernel_ununlock_allbutone(nlocks
);
97 ltsleep(wchan_t ident
, pri_t prio
, const char *wmesg
, int timo
,
98 volatile struct simplelock
*slock
)
100 struct ltsleeper lts
;
104 rumpuser_cv_init(<s
.cv
);
107 simple_unlock(slock
);
109 rv
= sleeper(<s
, timo
);
111 if (slock
&& (prio
& PNORELOCK
) == 0)
118 mtsleep(wchan_t ident
, pri_t prio
, const char *wmesg
, int timo
,
121 struct ltsleeper lts
;
125 rumpuser_cv_init(<s
.cv
);
129 rv
= sleeper(<s
, timo
);
131 if ((prio
& PNORELOCK
) == 0)
138 do_wakeup(wchan_t ident
, void (*wakeupfn
)(struct rumpuser_cv
*))
140 struct ltsleeper
*ltsp
;
142 KASSERT(kernel_biglocked());
143 LIST_FOREACH(ltsp
, &sleepers
, entries
) {
144 if (ltsp
->id
== ident
) {
151 wakeup(wchan_t ident
)
154 do_wakeup(ident
, rumpuser_cv_broadcast
);
158 wakeup_one(wchan_t ident
)
161 do_wakeup(ident
, rumpuser_cv_signal
);