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