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>
39 #include <sys/errno.h>
42 * cv_timedwait() is similar to cv_wait() except that it additionally expects
43 * a timeout value specified in ticks. When woken by cv_signal() or
44 * cv_broadcast() it returns 1, otherwise when the timeout is reached -1 is
47 * cv_timedwait_sig() behaves the same as cv_timedwait() but blocks
48 * interruptibly and can be woken by a signal (EINTR, ERESTART). When
49 * this occurs 0 is returned.
51 * cv_timedwait_io() and cv_timedwait_sig_io() are variants of cv_timedwait()
52 * and cv_timedwait_sig() which should be used when waiting for outstanding
53 * IO to complete. They are responsible for updating the iowait accounting
54 * when this is supported by the platform.
56 * cv_timedwait_hires() and cv_timedwait_sig_hires() are high resolution
57 * versions of cv_timedwait() and cv_timedwait_sig(). They expect the timeout
58 * to be specified as a hrtime_t allowing for timeouts of less than a tick.
60 * N.B. The return values differ slightly from the illumos implementation
61 * which returns the time remaining, instead of 1, when woken. They both
62 * return -1 on timeout. Consumers which need to know the time remaining
63 * are responsible for tracking it themselves.
66 static __inline sbintime_t
67 zfs_nstosbt(int64_t _ns
)
72 KASSERT(_ns
>= 0, ("Negative values illegal for nstosbt: %jd", _ns
));
75 sb
= (_ns
/ 1000000000) * SBT_1S
;
76 _ns
= _ns
% 1000000000;
78 /* 9223372037 = ceil(2^63 / 1000000000) */
79 sb
+= ((_ns
* 9223372037ull) + 0x7fffffff) >> 31;
84 typedef struct cv kcondvar_t
;
85 #define CALLOUT_FLAG_ABSOLUTE C_ABSOLUTE
92 #define zfs_cv_init(cv, name, type, arg) do { \
94 ASSERT((type) == CV_DEFAULT); \
95 for (_name = #cv; *_name != '\0'; _name++) { \
96 if (*_name >= 'a' && *_name <= 'z') \
101 cv_init((cv), _name); \
103 #define cv_init(cv, name, type, arg) zfs_cv_init(cv, name, type, arg)
107 cv_wait_sig(kcondvar_t
*cvp
, kmutex_t
*mp
)
110 return (_cv_wait_sig(cvp
, &(mp
)->lock_object
) == 0);
114 cv_timedwait(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t timo
)
118 timo
-= ddi_get_lbolt();
121 rc
= _cv_timedwait_sbt((cvp
), &(mp
)->lock_object
, \
122 tick_sbt
* (timo
), 0, C_HARDCLOCK
);
123 if (rc
== EWOULDBLOCK
)
129 cv_timedwait_sig(kcondvar_t
*cvp
, kmutex_t
*mp
, clock_t timo
)
133 timo
-= ddi_get_lbolt();
136 rc
= _cv_timedwait_sig_sbt(cvp
, &(mp
)->lock_object
, \
137 tick_sbt
* (timo
), 0, C_HARDCLOCK
);
138 if (rc
== EWOULDBLOCK
)
140 if (rc
== EINTR
|| rc
== ERESTART
)
146 #define cv_timedwait_io cv_timedwait
147 #define cv_timedwait_idle cv_timedwait
148 #define cv_timedwait_sig_io cv_timedwait_sig
149 #define cv_wait_io cv_wait
150 #define cv_wait_io_sig cv_wait_sig
151 #define cv_wait_idle cv_wait
152 #define cv_timedwait_io_hires cv_timedwait_hires
153 #define cv_timedwait_idle_hires cv_timedwait_hires
156 cv_timedwait_hires(kcondvar_t
*cvp
, kmutex_t
*mp
, hrtime_t tim
, hrtime_t res
,
164 hrtime
= gethrtime();
170 rc
= cv_timedwait_sbt(cvp
, mp
, zfs_nstosbt(tim
),
171 zfs_nstosbt(res
), C_ABSOLUTE
);
173 if (rc
== EWOULDBLOCK
)
176 KASSERT(rc
== 0, ("unexpected rc value %d", rc
));
181 cv_timedwait_sig_hires(kcondvar_t
*cvp
, kmutex_t
*mp
, hrtime_t tim
,
182 hrtime_t res
, int flag
)
190 hrtime
= gethrtime();
197 sbt
= zfs_nstosbt(tim
);
198 rc
= cv_timedwait_sig_sbt(cvp
, mp
, sbt
, zfs_nstosbt(res
), C_ABSOLUTE
);
207 KASSERT(rc
== 0, ("unexpected rc value %d", rc
));
212 #endif /* _OPENSOLARIS_SYS_CONDVAR_H_ */