ltsleep.c revision 1.2 1 /* $NetBSD: ltsleep.c,v 1.2 2007/11/04 18:46:29 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Finnish Cultural Foundation.
8 *
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.
17 *
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.
29 */
30
31 #include <sys/param.h>
32 #include <sys/proc.h>
33 #include <sys/queue.h>
34
35 #include "rump_private.h"
36
37 struct ltsleeper {
38 wchan_t id;
39 kcondvar_t cv;
40 LIST_ENTRY(ltsleeper) entries;
41 };
42
43 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
44 static kmutex_t sleepermtx;
45
46 int
47 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
48 volatile struct simplelock *slock)
49 {
50 struct ltsleeper lts;
51
52 lts.id = ident;
53 cv_init(<s.cv, NULL);
54
55 mutex_enter(&sleepermtx);
56 LIST_INSERT_HEAD(&sleepers, <s, entries);
57 /* protected by sleepermtx */
58 if (slock)
59 simple_unlock(slock);
60 cv_wait(<s.cv, &sleepermtx);
61 LIST_REMOVE(<s, entries);
62 mutex_exit(&sleepermtx);
63
64 cv_destroy(<s.cv);
65
66 if (slock && (prio & PNORELOCK) == 0)
67 simple_lock(slock);
68
69 return 0;
70 }
71
72 void
73 wakeup(wchan_t ident)
74 {
75 struct ltsleeper *ltsp;
76
77 mutex_enter(&sleepermtx);
78 LIST_FOREACH(ltsp, &sleepers, entries)
79 if (ltsp->id == ident)
80 cv_signal(<sp->cv);
81 mutex_exit(&sleepermtx);
82 }
83
84 void
85 rump_sleepers_init()
86 {
87
88 mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
89 }
90