]>
git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-condvar.c
2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/condvar.h>
29 #ifdef DEBUG_SUBSYSTEM
30 #undef DEBUG_SUBSYSTEM
33 #define DEBUG_SUBSYSTEM S_CONDVAR
36 __cv_init(kcondvar_t
*cvp
, char *name
, kcv_type_t type
, void *arg
)
43 ASSERT(type
== CV_DEFAULT
);
46 cvp
->cv_magic
= CV_MAGIC
;
47 init_waitqueue_head(&cvp
->cv_event
);
48 spin_lock_init(&cvp
->cv_lock
);
49 atomic_set(&cvp
->cv_waiters
, 0);
52 cvp
->cv_name_size
= strlen(name
) + 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())
60 cvp
->cv_name
= kmem_alloc(cvp
->cv_name_size
, flags
);
62 strcpy(cvp
->cv_name
, name
);
66 EXPORT_SYMBOL(__cv_init
);
69 __cv_destroy(kcondvar_t
*cvp
)
73 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
74 spin_lock(&cvp
->cv_lock
);
75 ASSERT(atomic_read(&cvp
->cv_waiters
) == 0);
76 ASSERT(!waitqueue_active(&cvp
->cv_event
));
79 kmem_free(cvp
->cv_name
, cvp
->cv_name_size
);
81 spin_unlock(&cvp
->cv_lock
);
82 memset(cvp
, CV_POISON
, sizeof(*cvp
));
85 EXPORT_SYMBOL(__cv_destroy
);
88 cv_wait_common(kcondvar_t
*cvp
, kmutex_t
*mp
, int state
)
95 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
96 spin_lock(&cvp
->cv_lock
);
97 ASSERT(mutex_owned(mp
));
99 if (cvp
->cv_mutex
== NULL
)
102 /* Ensure the same mutex is used by all callers */
103 ASSERT(cvp
->cv_mutex
== mp
);
104 spin_unlock(&cvp
->cv_lock
);
106 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
, state
);
107 atomic_inc(&cvp
->cv_waiters
);
109 /* Mutex should be dropped after prepare_to_wait() this
110 * ensures we're linked in to the waiters list and avoids the
111 * race where 'cvp->cv_waiters > 0' but the list is empty. */
116 atomic_dec(&cvp
->cv_waiters
);
117 finish_wait(&cvp
->cv_event
, &wait
);
122 __cv_wait(kcondvar_t
*cvp
, kmutex_t
*mp
)
124 cv_wait_common(cvp
, mp
, TASK_UNINTERRUPTIBLE
);
126 EXPORT_SYMBOL(__cv_wait
);
129 __cv_wait_interruptible(kcondvar_t
*cvp
, kmutex_t
*mp
)
131 cv_wait_common(cvp
, mp
, TASK_INTERRUPTIBLE
);
133 EXPORT_SYMBOL(__cv_wait_interruptible
);
135 /* 'expire_time' argument is an absolute wall clock time in jiffies.
136 * Return value is time left (expire_time - now) or -1 if timeout occurred.
139 __cv_timedwait(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t expire_time
)
147 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
148 spin_lock(&cvp
->cv_lock
);
149 ASSERT(mutex_owned(mp
));
151 if (cvp
->cv_mutex
== NULL
)
154 /* Ensure the same mutex is used by all callers */
155 ASSERT(cvp
->cv_mutex
== mp
);
156 spin_unlock(&cvp
->cv_lock
);
158 /* XXX - Does not handle jiffie wrap properly */
159 time_left
= expire_time
- jiffies
;
163 prepare_to_wait_exclusive(&cvp
->cv_event
, &wait
,
164 TASK_UNINTERRUPTIBLE
);
165 atomic_inc(&cvp
->cv_waiters
);
167 /* Mutex should be dropped after prepare_to_wait() this
168 * ensures we're linked in to the waiters list and avoids the
169 * race where 'cvp->cv_waiters > 0' but the list is empty. */
171 time_left
= schedule_timeout(time_left
);
174 atomic_dec(&cvp
->cv_waiters
);
175 finish_wait(&cvp
->cv_event
, &wait
);
177 RETURN(time_left
> 0 ? time_left
: -1);
179 EXPORT_SYMBOL(__cv_timedwait
);
182 __cv_signal(kcondvar_t
*cvp
)
186 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
188 /* All waiters are added with WQ_FLAG_EXCLUSIVE so only one
189 * waiter will be set runable with each call to wake_up().
190 * Additionally wake_up() holds a spin_lock assoicated with
191 * the wait queue to ensure we don't race waking up processes. */
192 if (atomic_read(&cvp
->cv_waiters
) > 0)
193 wake_up(&cvp
->cv_event
);
197 EXPORT_SYMBOL(__cv_signal
);
200 __cv_broadcast(kcondvar_t
*cvp
)
203 ASSERT(cvp
->cv_magic
== CV_MAGIC
);
206 /* Wake_up_all() will wake up all waiters even those which
207 * have the WQ_FLAG_EXCLUSIVE flag set. */
208 if (atomic_read(&cvp
->cv_waiters
) > 0)
209 wake_up_all(&cvp
->cv_event
);
213 EXPORT_SYMBOL(__cv_broadcast
);