Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / rump / librump / rumpkern / ltsleep.c
blobfd0a91fcb4d14d31b1625426665c601b1418541d
1 /* $NetBSD: ltsleep.c,v 1.24 2009/12/20 13:49:36 pooka Exp $ */
3 /*
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
11 * are met:
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
28 * SUCH DAMAGE.
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>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 #include <sys/simplelock.h>
46 #include <rump/rumpuser.h>
48 #include "rump_private.h"
50 struct ltsleeper {
51 wchan_t id;
52 struct rumpuser_cv *cv;
53 LIST_ENTRY(ltsleeper) entries;
56 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
58 static int
59 sleeper(struct ltsleeper *ltsp, int timo)
61 struct timespec ts, ticks;
62 int rv, nlocks;
64 LIST_INSERT_HEAD(&sleepers, ltsp, entries);
65 kernel_unlock_allbutone(&nlocks);
67 /* protected by biglock */
68 if (timo) {
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);
76 nanotime(&ts);
77 timespecadd(&ts, &ticks, &ts);
79 if (rumpuser_cv_timedwait(ltsp->cv, rump_giantlock,
80 ts.tv_sec, ts.tv_nsec) == 0)
81 rv = 0;
82 else
83 rv = EWOULDBLOCK;
84 } else {
85 rumpuser_cv_wait(ltsp->cv, rump_giantlock);
86 rv = 0;
89 LIST_REMOVE(ltsp, entries);
90 rumpuser_cv_destroy(ltsp->cv);
91 kernel_ununlock_allbutone(nlocks);
93 return rv;
96 int
97 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
98 volatile struct simplelock *slock)
100 struct ltsleeper lts;
101 int rv;
103 lts.id = ident;
104 rumpuser_cv_init(&lts.cv);
106 if (slock)
107 simple_unlock(slock);
109 rv = sleeper(&lts, timo);
111 if (slock && (prio & PNORELOCK) == 0)
112 simple_lock(slock);
114 return rv;
118 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
119 kmutex_t *lock)
121 struct ltsleeper lts;
122 int rv;
124 lts.id = ident;
125 rumpuser_cv_init(&lts.cv);
127 mutex_exit(lock);
129 rv = sleeper(&lts, timo);
131 if ((prio & PNORELOCK) == 0)
132 mutex_enter(lock);
134 return rv;
137 static void
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) {
145 wakeupfn(ltsp->cv);
150 void
151 wakeup(wchan_t ident)
154 do_wakeup(ident, rumpuser_cv_broadcast);
157 void
158 wakeup_one(wchan_t ident)
161 do_wakeup(ident, rumpuser_cv_signal);