ltsleep.c revision 1.3.4.3 1 1.3.4.3 yamt /* $NetBSD: ltsleep.c,v 1.3.4.3 2008/01/21 09:47:43 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.2 yamt int
49 1.3.4.2 yamt ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
50 1.3.4.2 yamt volatile struct simplelock *slock)
51 1.3.4.2 yamt {
52 1.3.4.2 yamt struct ltsleeper lts;
53 1.3.4.2 yamt int iplrecurse;
54 1.3.4.2 yamt
55 1.3.4.2 yamt lts.id = ident;
56 1.3.4.2 yamt cv_init(<s.cv, NULL);
57 1.3.4.2 yamt
58 1.3.4.2 yamt mutex_enter(&sleepermtx);
59 1.3.4.2 yamt LIST_INSERT_HEAD(&sleepers, <s, entries);
60 1.3.4.2 yamt
61 1.3.4.2 yamt /* release spl */
62 1.3.4.2 yamt iplrecurse = rumpuser_whatis_ipl();
63 1.3.4.2 yamt while (iplrecurse--)
64 1.3.4.2 yamt rumpuser_rw_exit(&rumpspl);
65 1.3.4.2 yamt
66 1.3.4.2 yamt /* protected by sleepermtx */
67 1.3.4.2 yamt if (slock)
68 1.3.4.2 yamt simple_unlock(slock);
69 1.3.4.2 yamt cv_wait(<s.cv, &sleepermtx);
70 1.3.4.2 yamt
71 1.3.4.2 yamt /* retake ipl */
72 1.3.4.2 yamt iplrecurse = rumpuser_whatis_ipl();
73 1.3.4.2 yamt while (iplrecurse--)
74 1.3.4.2 yamt rumpuser_rw_enter(&rumpspl, 0);
75 1.3.4.2 yamt
76 1.3.4.2 yamt LIST_REMOVE(<s, entries);
77 1.3.4.2 yamt mutex_exit(&sleepermtx);
78 1.3.4.2 yamt
79 1.3.4.2 yamt cv_destroy(<s.cv);
80 1.3.4.2 yamt
81 1.3.4.2 yamt if (slock && (prio & PNORELOCK) == 0)
82 1.3.4.2 yamt simple_lock(slock);
83 1.3.4.2 yamt
84 1.3.4.2 yamt return 0;
85 1.3.4.2 yamt }
86 1.3.4.2 yamt
87 1.3.4.3 yamt int
88 1.3.4.3 yamt mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
89 1.3.4.3 yamt kmutex_t *lock)
90 1.3.4.3 yamt {
91 1.3.4.3 yamt struct ltsleeper lts;
92 1.3.4.3 yamt int iplrecurse;
93 1.3.4.3 yamt
94 1.3.4.3 yamt lts.id = ident;
95 1.3.4.3 yamt cv_init(<s.cv, NULL);
96 1.3.4.3 yamt
97 1.3.4.3 yamt mutex_enter(&sleepermtx);
98 1.3.4.3 yamt LIST_INSERT_HEAD(&sleepers, <s, entries);
99 1.3.4.3 yamt
100 1.3.4.3 yamt /* release spl */
101 1.3.4.3 yamt iplrecurse = rumpuser_whatis_ipl();
102 1.3.4.3 yamt while (iplrecurse--)
103 1.3.4.3 yamt rumpuser_rw_exit(&rumpspl);
104 1.3.4.3 yamt
105 1.3.4.3 yamt /* protected by sleepermtx */
106 1.3.4.3 yamt mutex_exit(lock);
107 1.3.4.3 yamt cv_wait(<s.cv, &sleepermtx);
108 1.3.4.3 yamt
109 1.3.4.3 yamt /* retake ipl */
110 1.3.4.3 yamt iplrecurse = rumpuser_whatis_ipl();
111 1.3.4.3 yamt while (iplrecurse--)
112 1.3.4.3 yamt rumpuser_rw_enter(&rumpspl, 0);
113 1.3.4.3 yamt
114 1.3.4.3 yamt LIST_REMOVE(<s, entries);
115 1.3.4.3 yamt mutex_exit(&sleepermtx);
116 1.3.4.3 yamt
117 1.3.4.3 yamt cv_destroy(<s.cv);
118 1.3.4.3 yamt
119 1.3.4.3 yamt if ((prio & PNORELOCK) == 0)
120 1.3.4.3 yamt mutex_enter(lock);
121 1.3.4.3 yamt
122 1.3.4.3 yamt return 0;
123 1.3.4.3 yamt }
124 1.3.4.3 yamt
125 1.3.4.2 yamt void
126 1.3.4.2 yamt wakeup(wchan_t ident)
127 1.3.4.2 yamt {
128 1.3.4.2 yamt struct ltsleeper *ltsp;
129 1.3.4.2 yamt
130 1.3.4.2 yamt mutex_enter(&sleepermtx);
131 1.3.4.2 yamt LIST_FOREACH(ltsp, &sleepers, entries)
132 1.3.4.2 yamt if (ltsp->id == ident)
133 1.3.4.2 yamt cv_signal(<sp->cv);
134 1.3.4.2 yamt mutex_exit(&sleepermtx);
135 1.3.4.2 yamt }
136 1.3.4.2 yamt
137 1.3.4.2 yamt void
138 1.3.4.2 yamt rump_sleepers_init()
139 1.3.4.2 yamt {
140 1.3.4.2 yamt
141 1.3.4.2 yamt mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
142 1.3.4.2 yamt }
143