2 * Copyright (c) 2005, Kohsuke Ohtani
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
31 * cond.c - condition variable object
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
)
50 if ((c
= kmem_alloc(sizeof(struct cond
))) == NULL
)
53 event_init(&c
->event
, "condition");
55 c
->magic
= COND_MAGIC
;
56 if (umem_copyout(&c
, cond
, sizeof(cond_t
))) {
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
)
74 if (umem_copyin(uc
, &c
, sizeof(cond_t
)))
84 * Destroy a condition variable.
85 * If there are any blocked thread waiting for the specified CV, it
88 __syscall
int cond_destroy(cond_t
*cond
)
94 if ((err
= cond_copyin(cond
, &c
))) {
98 if (event_waiting(&c
->event
)) {
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
)
122 if (umem_copyin(cond
, &c
, sizeof(cond_t
))) {
126 if (c
== COND_INITIALIZER
) {
127 if ((err
= cond_init(cond
))) {
131 umem_copyin(cond
, &c
, sizeof(cond_t
));
133 if (!cond_valid(c
)) {
138 if ((err
= mutex_unlock(mtx
))) {
142 result
= sched_tsleep(&c
->event
, timeout
);
143 if (result
== SLP_TIMEOUT
)
145 else if (result
== SLP_INTR
)
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
)
162 err
= cond_copyin(cond
, &c
);
164 sched_wakeone(&c
->event
);
170 * Unblock all threads that are blocked on the specified CV.
172 __syscall
int cond_broadcast(cond_t
*cond
)
178 err
= cond_copyin(cond
, &c
);
180 sched_wakeup(&c
->event
);