]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-condvar.c
Remove condition variable names
[mirror_spl.git] / module / spl / spl-condvar.c
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>.
6 * UCRL-CODE-235197
7 *
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.
15 *
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
19 * for more details.
20 *
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 \*****************************************************************************/
26
27 #include <sys/condvar.h>
28 #include <spl-debug.h>
29
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
32 #endif
33
34 #define SS_DEBUG_SUBSYS SS_CONDVAR
35
36 void
37 __cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
38 {
39 int flags = KM_SLEEP;
40
41 SENTRY;
42 ASSERT(cvp);
43 ASSERT(name == NULL);
44 ASSERT(type == CV_DEFAULT);
45 ASSERT(arg == NULL);
46
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 cvp->cv_mutex = NULL;
52
53 /* We may be called when there is a non-zero preempt_count or
54 * interrupts are disabled is which case we must not sleep.
55 */
56 if (current_thread_info()->preempt_count || irqs_disabled())
57 flags = KM_NOSLEEP;
58
59 SEXIT;
60 }
61 EXPORT_SYMBOL(__cv_init);
62
63 static int
64 cv_destroy_wakeup(kcondvar_t *cvp)
65 {
66 if ((waitqueue_active(&cvp->cv_event)) ||
67 (atomic_read(&cvp->cv_waiters) > 0))
68 return 0;
69
70 return 1;
71 }
72
73 void
74 __cv_destroy(kcondvar_t *cvp)
75 {
76 SENTRY;
77 ASSERT(cvp);
78 ASSERT(cvp->cv_magic == CV_MAGIC);
79
80 /* Block until all waiters have woken */
81 while (cv_destroy_wakeup(cvp) == 0)
82 wait_event_timeout(cvp->cv_destroy, cv_destroy_wakeup(cvp), 1);
83
84 ASSERT(cvp->cv_mutex == NULL);
85 ASSERT(atomic_read(&cvp->cv_waiters) == 0);
86 ASSERT(!waitqueue_active(&cvp->cv_event));
87
88 SEXIT;
89 }
90 EXPORT_SYMBOL(__cv_destroy);
91
92 static void
93 cv_wait_common(kcondvar_t *cvp, kmutex_t *mp, int state)
94 {
95 DEFINE_WAIT(wait);
96 SENTRY;
97
98 ASSERT(cvp);
99 ASSERT(mp);
100 ASSERT(cvp->cv_magic == CV_MAGIC);
101 ASSERT(mutex_owned(mp));
102
103 if (cvp->cv_mutex == NULL)
104 cvp->cv_mutex = mp;
105
106 /* Ensure the same mutex is used by all callers */
107 ASSERT(cvp->cv_mutex == mp);
108
109 prepare_to_wait_exclusive(&cvp->cv_event, &wait, state);
110 atomic_inc(&cvp->cv_waiters);
111
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. */
115 mutex_exit(mp);
116 schedule();
117 mutex_enter(mp);
118
119 /* No more waiters a different mutex could be used */
120 if (atomic_dec_and_test(&cvp->cv_waiters)) {
121 cvp->cv_mutex = NULL;
122 wake_up(&cvp->cv_destroy);
123 }
124
125 finish_wait(&cvp->cv_event, &wait);
126
127 SEXIT;
128 }
129
130 void
131 __cv_wait(kcondvar_t *cvp, kmutex_t *mp)
132 {
133 cv_wait_common(cvp, mp, TASK_UNINTERRUPTIBLE);
134 }
135 EXPORT_SYMBOL(__cv_wait);
136
137 void
138 __cv_wait_interruptible(kcondvar_t *cvp, kmutex_t *mp)
139 {
140 cv_wait_common(cvp, mp, TASK_INTERRUPTIBLE);
141 }
142 EXPORT_SYMBOL(__cv_wait_interruptible);
143
144 /* 'expire_time' argument is an absolute wall clock time in jiffies.
145 * Return value is time left (expire_time - now) or -1 if timeout occurred.
146 */
147 static clock_t
148 __cv_timedwait_common(kcondvar_t *cvp, kmutex_t *mp,
149 clock_t expire_time, int state)
150 {
151 DEFINE_WAIT(wait);
152 clock_t time_left;
153 SENTRY;
154
155 ASSERT(cvp);
156 ASSERT(mp);
157 ASSERT(cvp->cv_magic == CV_MAGIC);
158 ASSERT(mutex_owned(mp));
159
160 if (cvp->cv_mutex == NULL)
161 cvp->cv_mutex = mp;
162
163 /* Ensure the same mutex is used by all callers */
164 ASSERT(cvp->cv_mutex == mp);
165
166 /* XXX - Does not handle jiffie wrap properly */
167 time_left = expire_time - jiffies;
168 if (time_left <= 0)
169 SRETURN(-1);
170
171 prepare_to_wait_exclusive(&cvp->cv_event, &wait, state);
172 atomic_inc(&cvp->cv_waiters);
173
174 /* Mutex should be dropped after prepare_to_wait() this
175 * ensures we're linked in to the waiters list and avoids the
176 * race where 'cvp->cv_waiters > 0' but the list is empty. */
177 mutex_exit(mp);
178 time_left = schedule_timeout(time_left);
179 mutex_enter(mp);
180
181 /* No more waiters a different mutex could be used */
182 if (atomic_dec_and_test(&cvp->cv_waiters)) {
183 cvp->cv_mutex = NULL;
184 wake_up(&cvp->cv_destroy);
185 }
186
187 finish_wait(&cvp->cv_event, &wait);
188
189 SRETURN(time_left > 0 ? time_left : -1);
190 }
191
192 clock_t
193 __cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t exp_time)
194 {
195 return __cv_timedwait_common(cvp, mp, exp_time, TASK_UNINTERRUPTIBLE);
196 }
197 EXPORT_SYMBOL(__cv_timedwait);
198
199 clock_t
200 __cv_timedwait_interruptible(kcondvar_t *cvp, kmutex_t *mp, clock_t exp_time)
201 {
202 return __cv_timedwait_common(cvp, mp, exp_time, TASK_INTERRUPTIBLE);
203 }
204 EXPORT_SYMBOL(__cv_timedwait_interruptible);
205
206 void
207 __cv_signal(kcondvar_t *cvp)
208 {
209 SENTRY;
210 ASSERT(cvp);
211 ASSERT(cvp->cv_magic == CV_MAGIC);
212
213 /* All waiters are added with WQ_FLAG_EXCLUSIVE so only one
214 * waiter will be set runable with each call to wake_up().
215 * Additionally wake_up() holds a spin_lock assoicated with
216 * the wait queue to ensure we don't race waking up processes. */
217 if (atomic_read(&cvp->cv_waiters) > 0)
218 wake_up(&cvp->cv_event);
219
220 SEXIT;
221 }
222 EXPORT_SYMBOL(__cv_signal);
223
224 void
225 __cv_broadcast(kcondvar_t *cvp)
226 {
227 ASSERT(cvp);
228 ASSERT(cvp->cv_magic == CV_MAGIC);
229 SENTRY;
230
231 /* Wake_up_all() will wake up all waiters even those which
232 * have the WQ_FLAG_EXCLUSIVE flag set. */
233 if (atomic_read(&cvp->cv_waiters) > 0)
234 wake_up_all(&cvp->cv_event);
235
236 SEXIT;
237 }
238 EXPORT_SYMBOL(__cv_broadcast);