ltsleep.c revision 1.3.2.2 1 1.3.2.2 bouyer /* $NetBSD: ltsleep.c,v 1.3.2.2 2007/11/13 16:03:15 bouyer Exp $ */
2 1.3.2.2 bouyer
3 1.3.2.2 bouyer /*
4 1.3.2.2 bouyer * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.3.2.2 bouyer *
6 1.3.2.2 bouyer * Development of this software was supported by the
7 1.3.2.2 bouyer * Finnish Cultural Foundation.
8 1.3.2.2 bouyer *
9 1.3.2.2 bouyer * Redistribution and use in source and binary forms, with or without
10 1.3.2.2 bouyer * modification, are permitted provided that the following conditions
11 1.3.2.2 bouyer * are met:
12 1.3.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
13 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer.
14 1.3.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
15 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
16 1.3.2.2 bouyer * documentation and/or other materials provided with the distribution.
17 1.3.2.2 bouyer *
18 1.3.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.3.2.2 bouyer * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.3.2.2 bouyer * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.3.2.2 bouyer * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.3.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.3.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.3.2.2 bouyer * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.3.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.3.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.3.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.3.2.2 bouyer * SUCH DAMAGE.
29 1.3.2.2 bouyer */
30 1.3.2.2 bouyer
31 1.3.2.2 bouyer #include <sys/param.h>
32 1.3.2.2 bouyer #include <sys/proc.h>
33 1.3.2.2 bouyer #include <sys/queue.h>
34 1.3.2.2 bouyer
35 1.3.2.2 bouyer #include "rump_private.h"
36 1.3.2.2 bouyer #include "rumpuser.h"
37 1.3.2.2 bouyer
38 1.3.2.2 bouyer struct ltsleeper {
39 1.3.2.2 bouyer wchan_t id;
40 1.3.2.2 bouyer kcondvar_t cv;
41 1.3.2.2 bouyer LIST_ENTRY(ltsleeper) entries;
42 1.3.2.2 bouyer };
43 1.3.2.2 bouyer
44 1.3.2.2 bouyer static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
45 1.3.2.2 bouyer static kmutex_t sleepermtx;
46 1.3.2.2 bouyer
47 1.3.2.2 bouyer int
48 1.3.2.2 bouyer ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
49 1.3.2.2 bouyer volatile struct simplelock *slock)
50 1.3.2.2 bouyer {
51 1.3.2.2 bouyer struct ltsleeper lts;
52 1.3.2.2 bouyer int iplrecurse;
53 1.3.2.2 bouyer
54 1.3.2.2 bouyer lts.id = ident;
55 1.3.2.2 bouyer cv_init(<s.cv, NULL);
56 1.3.2.2 bouyer
57 1.3.2.2 bouyer mutex_enter(&sleepermtx);
58 1.3.2.2 bouyer LIST_INSERT_HEAD(&sleepers, <s, entries);
59 1.3.2.2 bouyer
60 1.3.2.2 bouyer /* release spl */
61 1.3.2.2 bouyer iplrecurse = rumpuser_whatis_ipl();
62 1.3.2.2 bouyer while (iplrecurse--)
63 1.3.2.2 bouyer rumpuser_rw_exit(&rumpspl);
64 1.3.2.2 bouyer
65 1.3.2.2 bouyer /* protected by sleepermtx */
66 1.3.2.2 bouyer if (slock)
67 1.3.2.2 bouyer simple_unlock(slock);
68 1.3.2.2 bouyer cv_wait(<s.cv, &sleepermtx);
69 1.3.2.2 bouyer
70 1.3.2.2 bouyer /* retake ipl */
71 1.3.2.2 bouyer iplrecurse = rumpuser_whatis_ipl();
72 1.3.2.2 bouyer while (iplrecurse--)
73 1.3.2.2 bouyer rumpuser_rw_enter(&rumpspl, 0);
74 1.3.2.2 bouyer
75 1.3.2.2 bouyer LIST_REMOVE(<s, entries);
76 1.3.2.2 bouyer mutex_exit(&sleepermtx);
77 1.3.2.2 bouyer
78 1.3.2.2 bouyer cv_destroy(<s.cv);
79 1.3.2.2 bouyer
80 1.3.2.2 bouyer if (slock && (prio & PNORELOCK) == 0)
81 1.3.2.2 bouyer simple_lock(slock);
82 1.3.2.2 bouyer
83 1.3.2.2 bouyer return 0;
84 1.3.2.2 bouyer }
85 1.3.2.2 bouyer
86 1.3.2.2 bouyer void
87 1.3.2.2 bouyer wakeup(wchan_t ident)
88 1.3.2.2 bouyer {
89 1.3.2.2 bouyer struct ltsleeper *ltsp;
90 1.3.2.2 bouyer
91 1.3.2.2 bouyer mutex_enter(&sleepermtx);
92 1.3.2.2 bouyer LIST_FOREACH(ltsp, &sleepers, entries)
93 1.3.2.2 bouyer if (ltsp->id == ident)
94 1.3.2.2 bouyer cv_signal(<sp->cv);
95 1.3.2.2 bouyer mutex_exit(&sleepermtx);
96 1.3.2.2 bouyer }
97 1.3.2.2 bouyer
98 1.3.2.2 bouyer void
99 1.3.2.2 bouyer rump_sleepers_init()
100 1.3.2.2 bouyer {
101 1.3.2.2 bouyer
102 1.3.2.2 bouyer mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
103 1.3.2.2 bouyer }
104