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