]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-condvar.c
Split <sys/debug.h> header
[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 DEBUG_SUBSYSTEM
31 #undef DEBUG_SUBSYSTEM
32 #endif
33
34 #define DEBUG_SUBSYSTEM S_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 ENTRY;
42 ASSERT(cvp);
43 ASSERT(name);
44 ASSERT(type == CV_DEFAULT);
45 ASSERT(arg == NULL);
46
47 cvp->cv_magic = CV_MAGIC;
48 init_waitqueue_head(&cvp->cv_event);
49 spin_lock_init(&cvp->cv_lock);
50 atomic_set(&cvp->cv_waiters, 0);
51 cvp->cv_mutex = NULL;
52 cvp->cv_name = NULL;
53 cvp->cv_name_size = strlen(name) + 1;
54
55 /* We may be called when there is a non-zero preempt_count or
56 * interrupts are disabled is which case we must not sleep.
57 */
58 if (current_thread_info()->preempt_count || irqs_disabled())
59 flags = KM_NOSLEEP;
60
61 cvp->cv_name = kmem_alloc(cvp->cv_name_size, flags);
62 if (cvp->cv_name)
63 strcpy(cvp->cv_name, name);
64
65 EXIT;
66 }
67 EXPORT_SYMBOL(__cv_init);
68
69 void
70 __cv_destroy(kcondvar_t *cvp)
71 {
72 ENTRY;
73 ASSERT(cvp);
74 ASSERT(cvp->cv_magic == CV_MAGIC);
75 spin_lock(&cvp->cv_lock);
76 ASSERT(atomic_read(&cvp->cv_waiters) == 0);
77 ASSERT(!waitqueue_active(&cvp->cv_event));
78
79 if (cvp->cv_name)
80 kmem_free(cvp->cv_name, cvp->cv_name_size);
81
82 spin_unlock(&cvp->cv_lock);
83 memset(cvp, CV_POISON, sizeof(*cvp));
84 EXIT;
85 }
86 EXPORT_SYMBOL(__cv_destroy);
87
88 static void
89 cv_wait_common(kcondvar_t *cvp, kmutex_t *mp, int state)
90 {
91 DEFINE_WAIT(wait);
92 ENTRY;
93
94 ASSERT(cvp);
95 ASSERT(mp);
96 ASSERT(cvp->cv_magic == CV_MAGIC);
97 spin_lock(&cvp->cv_lock);
98 ASSERT(mutex_owned(mp));
99
100 if (cvp->cv_mutex == NULL)
101 cvp->cv_mutex = mp;
102
103 /* Ensure the same mutex is used by all callers */
104 ASSERT(cvp->cv_mutex == mp);
105 spin_unlock(&cvp->cv_lock);
106
107 prepare_to_wait_exclusive(&cvp->cv_event, &wait, state);
108 atomic_inc(&cvp->cv_waiters);
109
110 /* Mutex should be dropped after prepare_to_wait() this
111 * ensures we're linked in to the waiters list and avoids the
112 * race where 'cvp->cv_waiters > 0' but the list is empty. */
113 mutex_exit(mp);
114 schedule();
115 mutex_enter(mp);
116
117 atomic_dec(&cvp->cv_waiters);
118 finish_wait(&cvp->cv_event, &wait);
119 EXIT;
120 }
121
122 void
123 __cv_wait(kcondvar_t *cvp, kmutex_t *mp)
124 {
125 cv_wait_common(cvp, mp, TASK_UNINTERRUPTIBLE);
126 }
127 EXPORT_SYMBOL(__cv_wait);
128
129 void
130 __cv_wait_interruptible(kcondvar_t *cvp, kmutex_t *mp)
131 {
132 cv_wait_common(cvp, mp, TASK_INTERRUPTIBLE);
133 }
134 EXPORT_SYMBOL(__cv_wait_interruptible);
135
136 /* 'expire_time' argument is an absolute wall clock time in jiffies.
137 * Return value is time left (expire_time - now) or -1 if timeout occurred.
138 */
139 clock_t
140 __cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t expire_time)
141 {
142 DEFINE_WAIT(wait);
143 clock_t time_left;
144 ENTRY;
145
146 ASSERT(cvp);
147 ASSERT(mp);
148 ASSERT(cvp->cv_magic == CV_MAGIC);
149 spin_lock(&cvp->cv_lock);
150 ASSERT(mutex_owned(mp));
151
152 if (cvp->cv_mutex == NULL)
153 cvp->cv_mutex = mp;
154
155 /* Ensure the same mutex is used by all callers */
156 ASSERT(cvp->cv_mutex == mp);
157 spin_unlock(&cvp->cv_lock);
158
159 /* XXX - Does not handle jiffie wrap properly */
160 time_left = expire_time - jiffies;
161 if (time_left <= 0)
162 RETURN(-1);
163
164 prepare_to_wait_exclusive(&cvp->cv_event, &wait,
165 TASK_UNINTERRUPTIBLE);
166 atomic_inc(&cvp->cv_waiters);
167
168 /* Mutex should be dropped after prepare_to_wait() this
169 * ensures we're linked in to the waiters list and avoids the
170 * race where 'cvp->cv_waiters > 0' but the list is empty. */
171 mutex_exit(mp);
172 time_left = schedule_timeout(time_left);
173 mutex_enter(mp);
174
175 atomic_dec(&cvp->cv_waiters);
176 finish_wait(&cvp->cv_event, &wait);
177
178 RETURN(time_left > 0 ? time_left : -1);
179 }
180 EXPORT_SYMBOL(__cv_timedwait);
181
182 void
183 __cv_signal(kcondvar_t *cvp)
184 {
185 ENTRY;
186 ASSERT(cvp);
187 ASSERT(cvp->cv_magic == CV_MAGIC);
188
189 /* All waiters are added with WQ_FLAG_EXCLUSIVE so only one
190 * waiter will be set runable with each call to wake_up().
191 * Additionally wake_up() holds a spin_lock assoicated with
192 * the wait queue to ensure we don't race waking up processes. */
193 if (atomic_read(&cvp->cv_waiters) > 0)
194 wake_up(&cvp->cv_event);
195
196 EXIT;
197 }
198 EXPORT_SYMBOL(__cv_signal);
199
200 void
201 __cv_broadcast(kcondvar_t *cvp)
202 {
203 ASSERT(cvp);
204 ASSERT(cvp->cv_magic == CV_MAGIC);
205 ENTRY;
206
207 /* Wake_up_all() will wake up all waiters even those which
208 * have the WQ_FLAG_EXCLUSIVE flag set. */
209 if (atomic_read(&cvp->cv_waiters) > 0)
210 wake_up_all(&cvp->cv_event);
211
212 EXIT;
213 }
214 EXPORT_SYMBOL(__cv_broadcast);