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://github.com/behlendorf/spl/>.
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
)
44 ASSERT(type
== CV_DEFAULT
);
47 cvp
->cv_magic
= CV_MAGIC
;
48 init_waitqueue_head(&cvp
->cv_event
);
49 init_waitqueue_head(&cvp
->cv_destroy
);
50 atomic_set(&cvp
->cv_waiters
, 0);
51 atomic_set(&cvp
->cv_refs
, 1);
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.
57 if (current_thread_info()->preempt_count
|| irqs_disabled())
62 EXPORT_SYMBOL(__cv_init
);
65 cv_destroy_wakeup(kcondvar_t
*cvp
)
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
));
77 __cv_destroy(kcondvar_t
*cvp
)
81 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
83 cvp
->cv_magic
= CV_DESTROY
;
84 atomic_dec(&cvp
->cv_refs
);
86 /* Block until all waiters are woken and references dropped. */
87 while (cv_destroy_wakeup(cvp
) == 0)
88 wait_event_timeout(cvp
->cv_destroy
, cv_destroy_wakeup(cvp
), 1);
90 ASSERT3P(cvp
->cv_mutex
, ==, NULL
);
91 ASSERT3S(atomic_read(&cvp
->cv_refs
), ==, 0);
92 ASSERT3S(atomic_read(&cvp
->cv_waiters
), ==, 0);
93 ASSERT3S(waitqueue_active(&cvp
->cv_event
), ==, 0);
97 EXPORT_SYMBOL(__cv_destroy
);
100 cv_wait_common(kcondvar_t
*cvp
, kmutex_t
*mp
, int state
)
107 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
108 ASSERT(mutex_owned(mp
));
109 atomic_inc(&cvp
->cv_refs
);
111 if (cvp
->cv_mutex
== NULL
)
114 /* Ensure the same mutex is used by all callers */
115 ASSERT(cvp
->cv_mutex
== mp
);
117 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
118 atomic_inc(&cvp
->cv_waiters
);
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. */
127 /* No more waiters a different mutex could be used */
128 if (atomic_dec_and_test(&cvp
->cv_waiters
)) {
129 cvp
->cv_mutex
= NULL
;
130 wake_up(&cvp
->cv_destroy
);
133 finish_wait(&cvp
->cv_event
, &wait
);
134 atomic_dec(&cvp
->cv_refs
);
140 __cv_wait(kcondvar_t
*cvp
, kmutex_t
*mp
)
142 cv_wait_common(cvp
, mp
, TASK_UNINTERRUPTIBLE
);
144 EXPORT_SYMBOL(__cv_wait
);
147 __cv_wait_interruptible(kcondvar_t
*cvp
, kmutex_t
*mp
)
149 cv_wait_common(cvp
, mp
, TASK_INTERRUPTIBLE
);
151 EXPORT_SYMBOL(__cv_wait_interruptible
);
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.
157 __cv_timedwait_common(kcondvar_t
*cvp
, kmutex_t
*mp
,
158 clock_t expire_time
, int state
)
166 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
167 ASSERT(mutex_owned(mp
));
168 atomic_inc(&cvp
->cv_refs
);
170 if (cvp
->cv_mutex
== NULL
)
173 /* Ensure the same mutex is used by all callers */
174 ASSERT(cvp
->cv_mutex
== mp
);
176 /* XXX - Does not handle jiffie wrap properly */
177 time_left
= expire_time
- jiffies
;
178 if (time_left
<= 0) {
179 atomic_dec(&cvp
->cv_refs
);
183 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
184 atomic_inc(&cvp
->cv_waiters
);
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. */
190 time_left
= schedule_timeout(time_left
);
193 /* No more waiters a different mutex could be used */
194 if (atomic_dec_and_test(&cvp
->cv_waiters
)) {
195 cvp
->cv_mutex
= NULL
;
196 wake_up(&cvp
->cv_destroy
);
199 finish_wait(&cvp
->cv_event
, &wait
);
200 atomic_dec(&cvp
->cv_refs
);
202 SRETURN(time_left
> 0 ? time_left
: -1);
206 __cv_timedwait(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t exp_time
)
208 return __cv_timedwait_common(cvp
, mp
, exp_time
, TASK_UNINTERRUPTIBLE
);
210 EXPORT_SYMBOL(__cv_timedwait
);
213 __cv_timedwait_interruptible(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t exp_time
)
215 return __cv_timedwait_common(cvp
, mp
, exp_time
, TASK_INTERRUPTIBLE
);
217 EXPORT_SYMBOL(__cv_timedwait_interruptible
);
220 __cv_signal(kcondvar_t
*cvp
)
224 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
225 atomic_inc(&cvp
->cv_refs
);
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
);
234 atomic_dec(&cvp
->cv_refs
);
237 EXPORT_SYMBOL(__cv_signal
);
240 __cv_broadcast(kcondvar_t
*cvp
)
244 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
245 atomic_inc(&cvp
->cv_refs
);
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
);
252 atomic_dec(&cvp
->cv_refs
);
255 EXPORT_SYMBOL(__cv_broadcast
);