prex: const correctness for device_open()
[prex.git] / sys / sync / cond.c
blobf1f556ad1e5e7e5ce00846e22b0359fe0f8ded18
1 /*-
2 * Copyright (c) 2005, Kohsuke Ohtani
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * cond.c - condition variable object
34 #include <kernel.h>
35 #include <sched.h>
36 #include <kmem.h>
37 #include <thread.h>
38 #include <sync.h>
41 * Create and initialize a condition variable.
43 * If an initialized condition variable is reinitialized,
44 * undefined behavior results.
46 __syscall int cond_init(cond_t *cond)
48 cond_t c;
50 if ((c = kmem_alloc(sizeof(struct cond))) == NULL)
51 return ENOMEM;
53 event_init(&c->event, "condition");
54 c->task = cur_task();
55 c->magic = COND_MAGIC;
56 if (umem_copyout(&c, cond, sizeof(cond_t))) {
57 kmem_free(c);
58 return EFAULT;
60 return 0;
64 * Copy a condition variable from user space.
65 * It also checks if the passed cv is valid.
67 * @us: Pointer to cv in user space.
68 * @ks: Pointer to cv in kernel space.
70 static int cond_copyin(cond_t *uc, cond_t *kc)
72 cond_t c;
74 if (umem_copyin(uc, &c, sizeof(cond_t)))
75 return EFAULT;
77 if (!cond_valid(c))
78 return EINVAL;
79 *kc = c;
80 return 0;
84 * Destroy a condition variable.
85 * If there are any blocked thread waiting for the specified CV, it
86 * returns EBUSY.
88 __syscall int cond_destroy(cond_t *cond)
90 cond_t c;
91 int err;
93 sched_lock();
94 if ((err = cond_copyin(cond, &c))) {
95 sched_unlock();
96 return err;
98 if (event_waiting(&c->event)) {
99 sched_unlock();
100 return EBUSY;
102 c->magic = 0;
103 kmem_free(c);
104 sched_unlock();
105 return 0;
109 * Wait on a condition.
111 * If waiting thread receives any exception, this routine returns
112 * with EINTR in order to invoke exception handler. But, an application
113 * assumes this call does NOT return with error. So, the stub routine
114 * in system call library must call cond_wait() again if it gets EINTR.
116 __syscall int cond_wait(cond_t *cond, mutex_t *mtx, u_long timeout)
118 cond_t c;
119 int err, result;
121 sched_lock();
122 if (umem_copyin(cond, &c, sizeof(cond_t))) {
123 sched_unlock();
124 return EFAULT;
126 if (c == COND_INITIALIZER) {
127 if ((err = cond_init(cond))) {
128 sched_unlock();
129 return err;
131 umem_copyin(cond, &c, sizeof(cond_t));
132 } else {
133 if (!cond_valid(c)) {
134 sched_unlock();
135 return EINVAL;
138 if ((err = mutex_unlock(mtx))) {
139 sched_unlock();
140 return err;
142 result = sched_tsleep(&c->event, timeout);
143 if (result == SLP_TIMEOUT)
144 err = ETIMEDOUT;
145 else if (result == SLP_INTR)
146 err = EINTR;
147 sched_unlock();
148 mutex_lock(mtx);
149 return err;
153 * Unblock one thread that is blocked on the specified CV.
154 * The thread which has highest priority will be unblocked.
156 __syscall int cond_signal(cond_t *cond)
158 cond_t c;
159 int err;
161 sched_lock();
162 err = cond_copyin(cond, &c);
163 if (err == 0)
164 sched_wakeone(&c->event);
165 sched_unlock();
166 return err;
170 * Unblock all threads that are blocked on the specified CV.
172 __syscall int cond_broadcast(cond_t *cond)
174 cond_t c;
175 int err;
177 sched_lock();
178 err = cond_copyin(cond, &c);
179 if (err == 0)
180 sched_wakeup(&c->event);
181 sched_unlock();
182 return err;