2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2013 iXsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifndef _OPENSOLARIS_SYS_CONDVAR_H_
31 #define _OPENSOLARIS_SYS_CONDVAR_H_
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/spl_condvar.h>
37 #include <sys/mutex.h>
41 * cv_timedwait() is similar to cv_wait() except that it additionally expects
42 * a timeout value specified in ticks. When woken by cv_signal() or
43 * cv_broadcast() it returns 1, otherwise when the timeout is reached -1 is
46 * cv_timedwait_sig() behaves the same as cv_timedwait() but blocks
47 * interruptibly and can be woken by a signal (EINTR, ERESTART). When
48 * this occurs 0 is returned.
50 * cv_timedwait_io() and cv_timedwait_sig_io() are variants of cv_timedwait()
51 * and cv_timedwait_sig() which should be used when waiting for outstanding
52 * IO to complete. They are responsible for updating the iowait accounting
53 * when this is supported by the platform.
55 * cv_timedwait_hires() and cv_timedwait_sig_hires() are high resolution
56 * versions of cv_timedwait() and cv_timedwait_sig(). They expect the timeout
57 * to be specified as a hrtime_t allowing for timeouts of less than a tick.
59 * N.B. The return values differ slightly from the illumos implementation
60 * which returns the time remaining, instead of 1, when woken. They both
61 * return -1 on timeout. Consumers which need to know the time remaining
62 * are responsible for tracking it themselves.
65 static __inline sbintime_t
66 zfs_nstosbt(int64_t _ns)
71 KASSERT(_ns >= 0, ("Negative values illegal for nstosbt: %jd", _ns));
74 sb = (_ns / 1000000000) * SBT_1S;
75 _ns = _ns % 1000000000;
77 /* 9223372037 = ceil(2^63 / 1000000000) */
78 sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
83 typedef struct cv kcondvar_t;
84 #define CALLOUT_FLAG_ABSOLUTE C_ABSOLUTE
91 #define zfs_cv_init(cv, name, type, arg) do { \
93 ASSERT((type) == CV_DEFAULT); \
94 for (_name = #cv; *_name != '\0'; _name++) { \
95 if (*_name >= 'a' && *_name <= 'z') \
100 cv_init((cv), _name); \
102 #define cv_init(cv, name, type, arg) zfs_cv_init(cv, name, type, arg)
106 cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
109 return (_cv_wait_sig(cvp, &(mp)->lock_object) == 0);
113 cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t timo)
117 timo -= ddi_get_lbolt();
120 rc = _cv_timedwait_sbt((cvp), &(mp)->lock_object, \
121 tick_sbt * (timo), 0, C_HARDCLOCK);
122 if (rc == EWOULDBLOCK)
128 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t timo)
132 timo -= ddi_get_lbolt();
135 rc = _cv_timedwait_sig_sbt(cvp, &(mp)->lock_object, \
136 tick_sbt * (timo), 0, C_HARDCLOCK);
137 if (rc == EWOULDBLOCK)
139 if (rc == EINTR || rc == ERESTART)
145 #define cv_timedwait_io cv_timedwait
146 #define cv_timedwait_idle cv_timedwait
147 #define cv_timedwait_sig_io cv_timedwait_sig
148 #define cv_wait_io cv_wait
149 #define cv_wait_io_sig cv_wait_sig
150 #define cv_wait_idle cv_wait
151 #define cv_timedwait_io_hires cv_timedwait_hires
152 #define cv_timedwait_idle_hires cv_timedwait_hires
155 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, hrtime_t res,
163 hrtime = gethrtime();
169 rc = cv_timedwait_sbt(cvp, mp, zfs_nstosbt(tim),
170 zfs_nstosbt(res), C_ABSOLUTE);
172 if (rc == EWOULDBLOCK)
175 KASSERT(rc == 0, ("unexpected rc value %d", rc));
180 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
181 hrtime_t res, int flag)
189 hrtime = gethrtime();
196 sbt = zfs_nstosbt(tim);
197 rc = cv_timedwait_sig_sbt(cvp, mp, sbt, zfs_nstosbt(res), C_ABSOLUTE);
206 KASSERT(rc == 0, ("unexpected rc value %d", rc));
211 #endif /* _OPENSOLARIS_SYS_CONDVAR_H_ */