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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Credential Implementation.
25 \*****************************************************************************/
27 #include <sys/condvar.h>
28 #include <spl-debug.h>
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
34 #define SS_DEBUG_SUBSYS SS_CONDVAR
37 __cv_init(kcondvar_t
*cvp
, char *name
, kcv_type_t type
, void *arg
)
42 ASSERT(type
== CV_DEFAULT
);
45 cvp
->cv_magic
= CV_MAGIC
;
46 init_waitqueue_head(&cvp
->cv_event
);
47 init_waitqueue_head(&cvp
->cv_destroy
);
48 atomic_set(&cvp
->cv_waiters
, 0);
49 atomic_set(&cvp
->cv_refs
, 1);
54 EXPORT_SYMBOL(__cv_init
);
57 cv_destroy_wakeup(kcondvar_t
*cvp
)
59 if (!atomic_read(&cvp
->cv_waiters
) && !atomic_read(&cvp
->cv_refs
)) {
60 ASSERT(cvp
->cv_mutex
== NULL
);
61 ASSERT(!waitqueue_active(&cvp
->cv_event
));
69 __cv_destroy(kcondvar_t
*cvp
)
73 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
75 cvp
->cv_magic
= CV_DESTROY
;
76 atomic_dec(&cvp
->cv_refs
);
78 /* Block until all waiters are woken and references dropped. */
79 while (cv_destroy_wakeup(cvp
) == 0)
80 wait_event_timeout(cvp
->cv_destroy
, cv_destroy_wakeup(cvp
), 1);
82 ASSERT3P(cvp
->cv_mutex
, ==, NULL
);
83 ASSERT3S(atomic_read(&cvp
->cv_refs
), ==, 0);
84 ASSERT3S(atomic_read(&cvp
->cv_waiters
), ==, 0);
85 ASSERT3S(waitqueue_active(&cvp
->cv_event
), ==, 0);
89 EXPORT_SYMBOL(__cv_destroy
);
92 cv_wait_common(kcondvar_t
*cvp
, kmutex_t
*mp
, int state
, int io
)
99 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
100 ASSERT(mutex_owned(mp
));
101 atomic_inc(&cvp
->cv_refs
);
103 if (cvp
->cv_mutex
== NULL
)
106 /* Ensure the same mutex is used by all callers */
107 ASSERT(cvp
->cv_mutex
== mp
);
109 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
110 atomic_inc(&cvp
->cv_waiters
);
112 /* Mutex should be dropped after prepare_to_wait() this
113 * ensures we're linked in to the waiters list and avoids the
114 * race where 'cvp->cv_waiters > 0' but the list is empty. */
122 /* No more waiters a different mutex could be used */
123 if (atomic_dec_and_test(&cvp
->cv_waiters
)) {
124 cvp
->cv_mutex
= NULL
;
125 wake_up(&cvp
->cv_destroy
);
128 finish_wait(&cvp
->cv_event
, &wait
);
129 atomic_dec(&cvp
->cv_refs
);
135 __cv_wait(kcondvar_t
*cvp
, kmutex_t
*mp
)
137 cv_wait_common(cvp
, mp
, TASK_UNINTERRUPTIBLE
, 0);
139 EXPORT_SYMBOL(__cv_wait
);
142 __cv_wait_interruptible(kcondvar_t
*cvp
, kmutex_t
*mp
)
144 cv_wait_common(cvp
, mp
, TASK_INTERRUPTIBLE
, 0);
146 EXPORT_SYMBOL(__cv_wait_interruptible
);
149 __cv_wait_io(kcondvar_t
*cvp
, kmutex_t
*mp
)
151 cv_wait_common(cvp
, mp
, TASK_UNINTERRUPTIBLE
, 1);
153 EXPORT_SYMBOL(__cv_wait_io
);
155 /* 'expire_time' argument is an absolute wall clock time in jiffies.
156 * Return value is time left (expire_time - now) or -1 if timeout occurred.
159 __cv_timedwait_common(kcondvar_t
*cvp
, kmutex_t
*mp
,
160 clock_t expire_time
, int state
)
168 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
169 ASSERT(mutex_owned(mp
));
170 atomic_inc(&cvp
->cv_refs
);
172 if (cvp
->cv_mutex
== NULL
)
175 /* Ensure the same mutex is used by all callers */
176 ASSERT(cvp
->cv_mutex
== mp
);
178 /* XXX - Does not handle jiffie wrap properly */
179 time_left
= expire_time
- jiffies
;
180 if (time_left
<= 0) {
181 atomic_dec(&cvp
->cv_refs
);
185 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
186 atomic_inc(&cvp
->cv_waiters
);
188 /* Mutex should be dropped after prepare_to_wait() this
189 * ensures we're linked in to the waiters list and avoids the
190 * race where 'cvp->cv_waiters > 0' but the list is empty. */
192 time_left
= schedule_timeout(time_left
);
195 /* No more waiters a different mutex could be used */
196 if (atomic_dec_and_test(&cvp
->cv_waiters
)) {
197 cvp
->cv_mutex
= NULL
;
198 wake_up(&cvp
->cv_destroy
);
201 finish_wait(&cvp
->cv_event
, &wait
);
202 atomic_dec(&cvp
->cv_refs
);
204 SRETURN(time_left
> 0 ? time_left
: -1);
208 __cv_timedwait(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t exp_time
)
210 return __cv_timedwait_common(cvp
, mp
, exp_time
, TASK_UNINTERRUPTIBLE
);
212 EXPORT_SYMBOL(__cv_timedwait
);
215 __cv_timedwait_interruptible(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t exp_time
)
217 return __cv_timedwait_common(cvp
, mp
, exp_time
, TASK_INTERRUPTIBLE
);
219 EXPORT_SYMBOL(__cv_timedwait_interruptible
);
222 *'expire_time' argument is an absolute clock time in nanoseconds.
223 * Return value is time left (expire_time - now) or -1 if timeout occurred.
226 __cv_timedwait_hires(kcondvar_t
*cvp
, kmutex_t
*mp
,
227 hrtime_t expire_time
, int state
)
230 hrtime_t time_left
, now
;
231 unsigned long time_left_us
;
236 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
237 ASSERT(mutex_owned(mp
));
238 atomic_inc(&cvp
->cv_refs
);
240 if (cvp
->cv_mutex
== NULL
)
243 /* Ensure the same mutex is used by all callers */
244 ASSERT(cvp
->cv_mutex
== mp
);
247 time_left
= expire_time
- now
;
248 if (time_left
<= 0) {
249 atomic_dec(&cvp
->cv_refs
);
252 time_left_us
= time_left
/ NSEC_PER_USEC
;
254 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
255 atomic_inc(&cvp
->cv_waiters
);
257 /* Mutex should be dropped after prepare_to_wait() this
258 * ensures we're linked in to the waiters list and avoids the
259 * race where 'cvp->cv_waiters > 0' but the list is empty. */
261 /* Allow a 100 us range to give kernel an opportunity to coalesce
263 usleep_range(time_left_us
, time_left_us
+ 100);
266 /* No more waiters a different mutex could be used */
267 if (atomic_dec_and_test(&cvp
->cv_waiters
)) {
268 cvp
->cv_mutex
= NULL
;
269 wake_up(&cvp
->cv_destroy
);
272 finish_wait(&cvp
->cv_event
, &wait
);
273 atomic_dec(&cvp
->cv_refs
);
275 time_left
= expire_time
- gethrtime();
276 SRETURN(time_left
> 0 ? time_left
: -1);
280 * Compatibility wrapper for the cv_timedwait_hires() Illumos interface.
283 cv_timedwait_hires(kcondvar_t
*cvp
, kmutex_t
*mp
, hrtime_t tim
,
284 hrtime_t res
, int flag
)
288 * Align expiration to the specified resolution.
290 if (flag
& CALLOUT_FLAG_ROUNDUP
)
292 tim
= (tim
/ res
) * res
;
295 if (!(flag
& CALLOUT_FLAG_ABSOLUTE
))
298 return __cv_timedwait_hires(cvp
, mp
, tim
, TASK_UNINTERRUPTIBLE
);
300 EXPORT_SYMBOL(cv_timedwait_hires
);
303 __cv_signal(kcondvar_t
*cvp
)
307 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
308 atomic_inc(&cvp
->cv_refs
);
310 /* All waiters are added with WQ_FLAG_EXCLUSIVE so only one
311 * waiter will be set runable with each call to wake_up().
312 * Additionally wake_up() holds a spin_lock assoicated with
313 * the wait queue to ensure we don't race waking up processes. */
314 if (atomic_read(&cvp
->cv_waiters
) > 0)
315 wake_up(&cvp
->cv_event
);
317 atomic_dec(&cvp
->cv_refs
);
320 EXPORT_SYMBOL(__cv_signal
);
323 __cv_broadcast(kcondvar_t
*cvp
)
327 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
328 atomic_inc(&cvp
->cv_refs
);
330 /* Wake_up_all() will wake up all waiters even those which
331 * have the WQ_FLAG_EXCLUSIVE flag set. */
332 if (atomic_read(&cvp
->cv_waiters
) > 0)
333 wake_up_all(&cvp
->cv_event
);
335 atomic_dec(&cvp
->cv_refs
);
338 EXPORT_SYMBOL(__cv_broadcast
);