]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-condvar.c
SPL 0.6.0-rc13
[mirror_zfs.git] / module / spl / spl-condvar.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251
BB
6 * UCRL-CODE-235197
7 *
716154c5
BB
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251
BB
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Credential Implementation.
25\*****************************************************************************/
715f6251 26
4efd4118 27#include <sys/condvar.h>
55abb092 28#include <spl-debug.h>
4efd4118 29
b17edc10
BB
30#ifdef SS_DEBUG_SUBSYS
31#undef SS_DEBUG_SUBSYS
4efd4118
BB
32#endif
33
b17edc10 34#define SS_DEBUG_SUBSYS SS_CONDVAR
4efd4118
BB
35
36void
37__cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
38{
39 int flags = KM_SLEEP;
40
b17edc10 41 SENTRY;
4efd4118 42 ASSERT(cvp);
b29012b9 43 ASSERT(name == NULL);
4efd4118
BB
44 ASSERT(type == CV_DEFAULT);
45 ASSERT(arg == NULL);
46
47 cvp->cv_magic = CV_MAGIC;
48 init_waitqueue_head(&cvp->cv_event);
d599e4fa 49 init_waitqueue_head(&cvp->cv_destroy);
4efd4118 50 atomic_set(&cvp->cv_waiters, 0);
d2733258 51 atomic_set(&cvp->cv_refs, 1);
4efd4118 52 cvp->cv_mutex = NULL;
4efd4118
BB
53
54 /* We may be called when there is a non-zero preempt_count or
55 * interrupts are disabled is which case we must not sleep.
56 */
57 if (current_thread_info()->preempt_count || irqs_disabled())
58 flags = KM_NOSLEEP;
59
b17edc10 60 SEXIT;
4efd4118
BB
61}
62EXPORT_SYMBOL(__cv_init);
63
d599e4fa
BB
64static int
65cv_destroy_wakeup(kcondvar_t *cvp)
66{
d2733258
BB
67 if (!atomic_read(&cvp->cv_waiters) && !atomic_read(&cvp->cv_refs)) {
68 ASSERT(cvp->cv_mutex == NULL);
69 ASSERT(!waitqueue_active(&cvp->cv_event));
70 return 1;
71 }
d599e4fa 72
d2733258 73 return 0;
d599e4fa
BB
74}
75
4efd4118
BB
76void
77__cv_destroy(kcondvar_t *cvp)
78{
b17edc10 79 SENTRY;
4efd4118
BB
80 ASSERT(cvp);
81 ASSERT(cvp->cv_magic == CV_MAGIC);
d599e4fa 82
d2733258
BB
83 cvp->cv_magic = CV_DESTROY;
84 atomic_dec(&cvp->cv_refs);
85
86 /* Block until all waiters are woken and references dropped. */
d599e4fa
BB
87 while (cv_destroy_wakeup(cvp) == 0)
88 wait_event_timeout(cvp->cv_destroy, cv_destroy_wakeup(cvp), 1);
89
3c60f505 90 ASSERT3P(cvp->cv_mutex, ==, NULL);
d2733258 91 ASSERT3S(atomic_read(&cvp->cv_refs), ==, 0);
3c60f505
BB
92 ASSERT3S(atomic_read(&cvp->cv_waiters), ==, 0);
93 ASSERT3S(waitqueue_active(&cvp->cv_event), ==, 0);
4efd4118 94
b17edc10 95 SEXIT;
4efd4118
BB
96}
97EXPORT_SYMBOL(__cv_destroy);
98
f752b46e
BB
99static void
100cv_wait_common(kcondvar_t *cvp, kmutex_t *mp, int state)
4efd4118
BB
101{
102 DEFINE_WAIT(wait);
b17edc10 103 SENTRY;
4efd4118
BB
104
105 ASSERT(cvp);
106 ASSERT(mp);
107 ASSERT(cvp->cv_magic == CV_MAGIC);
4efd4118 108 ASSERT(mutex_owned(mp));
d2733258 109 atomic_inc(&cvp->cv_refs);
4efd4118
BB
110
111 if (cvp->cv_mutex == NULL)
112 cvp->cv_mutex = mp;
113
114 /* Ensure the same mutex is used by all callers */
115 ASSERT(cvp->cv_mutex == mp);
4efd4118 116
f752b46e 117 prepare_to_wait_exclusive(&cvp->cv_event, &wait, state);
4efd4118
BB
118 atomic_inc(&cvp->cv_waiters);
119
120 /* Mutex should be dropped after prepare_to_wait() this
121 * ensures we're linked in to the waiters list and avoids the
122 * race where 'cvp->cv_waiters > 0' but the list is empty. */
123 mutex_exit(mp);
124 schedule();
125 mutex_enter(mp);
126
058de03c 127 /* No more waiters a different mutex could be used */
d599e4fa 128 if (atomic_dec_and_test(&cvp->cv_waiters)) {
058de03c 129 cvp->cv_mutex = NULL;
d599e4fa
BB
130 wake_up(&cvp->cv_destroy);
131 }
058de03c 132
4efd4118 133 finish_wait(&cvp->cv_event, &wait);
d2733258 134 atomic_dec(&cvp->cv_refs);
d599e4fa 135
b17edc10 136 SEXIT;
4efd4118 137}
f752b46e
BB
138
139void
140__cv_wait(kcondvar_t *cvp, kmutex_t *mp)
141{
142 cv_wait_common(cvp, mp, TASK_UNINTERRUPTIBLE);
143}
4efd4118
BB
144EXPORT_SYMBOL(__cv_wait);
145
f752b46e
BB
146void
147__cv_wait_interruptible(kcondvar_t *cvp, kmutex_t *mp)
148{
149 cv_wait_common(cvp, mp, TASK_INTERRUPTIBLE);
150}
151EXPORT_SYMBOL(__cv_wait_interruptible);
152
4efd4118
BB
153/* 'expire_time' argument is an absolute wall clock time in jiffies.
154 * Return value is time left (expire_time - now) or -1 if timeout occurred.
155 */
3f688a8c
NK
156static clock_t
157__cv_timedwait_common(kcondvar_t *cvp, kmutex_t *mp,
158 clock_t expire_time, int state)
4efd4118
BB
159{
160 DEFINE_WAIT(wait);
161 clock_t time_left;
b17edc10 162 SENTRY;
4efd4118
BB
163
164 ASSERT(cvp);
165 ASSERT(mp);
166 ASSERT(cvp->cv_magic == CV_MAGIC);
4efd4118 167 ASSERT(mutex_owned(mp));
d2733258 168 atomic_inc(&cvp->cv_refs);
4efd4118
BB
169
170 if (cvp->cv_mutex == NULL)
171 cvp->cv_mutex = mp;
172
173 /* Ensure the same mutex is used by all callers */
174 ASSERT(cvp->cv_mutex == mp);
4efd4118
BB
175
176 /* XXX - Does not handle jiffie wrap properly */
177 time_left = expire_time - jiffies;
d2733258
BB
178 if (time_left <= 0) {
179 atomic_dec(&cvp->cv_refs);
b17edc10 180 SRETURN(-1);
d2733258 181 }
4efd4118 182
3f688a8c 183 prepare_to_wait_exclusive(&cvp->cv_event, &wait, state);
4efd4118
BB
184 atomic_inc(&cvp->cv_waiters);
185
186 /* Mutex should be dropped after prepare_to_wait() this
187 * ensures we're linked in to the waiters list and avoids the
188 * race where 'cvp->cv_waiters > 0' but the list is empty. */
189 mutex_exit(mp);
190 time_left = schedule_timeout(time_left);
191 mutex_enter(mp);
192
058de03c 193 /* No more waiters a different mutex could be used */
d599e4fa 194 if (atomic_dec_and_test(&cvp->cv_waiters)) {
058de03c 195 cvp->cv_mutex = NULL;
d599e4fa
BB
196 wake_up(&cvp->cv_destroy);
197 }
058de03c 198
4efd4118 199 finish_wait(&cvp->cv_event, &wait);
d2733258 200 atomic_dec(&cvp->cv_refs);
4efd4118 201
b17edc10 202 SRETURN(time_left > 0 ? time_left : -1);
4efd4118 203}
3f688a8c
NK
204
205clock_t
206__cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t exp_time)
207{
208 return __cv_timedwait_common(cvp, mp, exp_time, TASK_UNINTERRUPTIBLE);
209}
4efd4118
BB
210EXPORT_SYMBOL(__cv_timedwait);
211
3f688a8c
NK
212clock_t
213__cv_timedwait_interruptible(kcondvar_t *cvp, kmutex_t *mp, clock_t exp_time)
214{
215 return __cv_timedwait_common(cvp, mp, exp_time, TASK_INTERRUPTIBLE);
216}
217EXPORT_SYMBOL(__cv_timedwait_interruptible);
218
4efd4118
BB
219void
220__cv_signal(kcondvar_t *cvp)
221{
b17edc10 222 SENTRY;
4efd4118
BB
223 ASSERT(cvp);
224 ASSERT(cvp->cv_magic == CV_MAGIC);
d2733258 225 atomic_inc(&cvp->cv_refs);
4efd4118
BB
226
227 /* All waiters are added with WQ_FLAG_EXCLUSIVE so only one
228 * waiter will be set runable with each call to wake_up().
229 * Additionally wake_up() holds a spin_lock assoicated with
230 * the wait queue to ensure we don't race waking up processes. */
231 if (atomic_read(&cvp->cv_waiters) > 0)
232 wake_up(&cvp->cv_event);
233
d2733258 234 atomic_dec(&cvp->cv_refs);
b17edc10 235 SEXIT;
4efd4118
BB
236}
237EXPORT_SYMBOL(__cv_signal);
238
239void
240__cv_broadcast(kcondvar_t *cvp)
241{
d2733258 242 SENTRY;
4efd4118
BB
243 ASSERT(cvp);
244 ASSERT(cvp->cv_magic == CV_MAGIC);
d2733258 245 atomic_inc(&cvp->cv_refs);
4efd4118
BB
246
247 /* Wake_up_all() will wake up all waiters even those which
248 * have the WQ_FLAG_EXCLUSIVE flag set. */
249 if (atomic_read(&cvp->cv_waiters) > 0)
250 wake_up_all(&cvp->cv_event);
251
d2733258 252 atomic_dec(&cvp->cv_refs);
b17edc10 253 SEXIT;
4efd4118
BB
254}
255EXPORT_SYMBOL(__cv_broadcast);