]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/os/freebsd/spl/sys/condvar.h
Update openzfs to 2.0.0-rc2-g4ce06f
[FreeBSD/FreeBSD.git] / include / os / freebsd / spl / sys / condvar.h
1 /*
2  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2013 iXsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #ifndef _OPENSOLARIS_SYS_CONDVAR_H_
31 #define _OPENSOLARIS_SYS_CONDVAR_H_
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35
36 #include <sys/spl_condvar.h>
37 #include <sys/mutex.h>
38 #include <sys/time.h>
39
40 /*
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
44  * returned.
45  *
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.
49  *
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.
54  *
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.
58  *
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.
63  */
64
65 static __inline sbintime_t
66 zfs_nstosbt(int64_t _ns)
67 {
68         sbintime_t sb = 0;
69
70 #ifdef KASSERT
71         KASSERT(_ns >= 0, ("Negative values illegal for nstosbt: %jd", _ns));
72 #endif
73         if (_ns >= SBT_1S) {
74                 sb = (_ns / 1000000000) * SBT_1S;
75                 _ns = _ns % 1000000000;
76         }
77         /* 9223372037 = ceil(2^63 / 1000000000) */
78         sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
79         return (sb);
80 }
81
82
83 typedef struct cv       kcondvar_t;
84 #define CALLOUT_FLAG_ABSOLUTE C_ABSOLUTE
85
86 typedef enum {
87         CV_DEFAULT,
88         CV_DRIVER
89 } kcv_type_t;
90
91 #define zfs_cv_init(cv, name, type, arg)        do {                    \
92         const char *_name;                                              \
93         ASSERT((type) == CV_DEFAULT);                                   \
94         for (_name = #cv; *_name != '\0'; _name++) {                    \
95                 if (*_name >= 'a' && *_name <= 'z')                     \
96                         break;                                          \
97         }                                                               \
98         if (*_name == '\0')                                             \
99                 _name = #cv;                                            \
100         cv_init((cv), _name);                                           \
101 } while (0)
102 #define cv_init(cv, name, type, arg)    zfs_cv_init(cv, name, type, arg)
103
104
105 static inline int
106 cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
107 {
108
109         return (_cv_wait_sig(cvp, &(mp)->lock_object) == 0);
110 }
111
112 static inline int
113 cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t timo)
114 {
115         int rc;
116
117         timo -= ddi_get_lbolt();
118         if (timo <= 0)
119                 return (-1);
120         rc = _cv_timedwait_sbt((cvp), &(mp)->lock_object, \
121             tick_sbt * (timo), 0, C_HARDCLOCK);
122         if (rc == EWOULDBLOCK)
123                 return (-1);
124         return (1);
125 }
126
127 static inline int
128 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t timo)
129 {
130         int rc;
131
132         timo -= ddi_get_lbolt();
133         if (timo <= 0)
134                 return (-1);
135         rc = _cv_timedwait_sig_sbt(cvp, &(mp)->lock_object, \
136             tick_sbt * (timo), 0, C_HARDCLOCK);
137         if (rc == EWOULDBLOCK)
138                 return (-1);
139         if (rc == EINTR || rc == ERESTART)
140                 return (0);
141
142         return (1);
143 }
144
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
153
154 static inline int
155 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, hrtime_t res,
156     int flag)
157 {
158         hrtime_t hrtime;
159         int rc;
160
161         ASSERT(tim >= res);
162
163         hrtime = gethrtime();
164         if (flag == 0)
165                 tim += hrtime;
166
167         if (hrtime >= tim)
168                 return (-1);
169         rc = cv_timedwait_sbt(cvp, mp, zfs_nstosbt(tim),
170             zfs_nstosbt(res), C_ABSOLUTE);
171
172         if (rc == EWOULDBLOCK)
173                 return (-1);
174
175         KASSERT(rc == 0, ("unexpected rc value %d", rc));
176         return (1);
177 }
178
179 static inline int
180 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
181     hrtime_t res, int flag)
182 {
183         sbintime_t sbt;
184         hrtime_t hrtime;
185         int rc;
186
187         ASSERT(tim >= res);
188
189         hrtime = gethrtime();
190         if (flag == 0)
191                 tim += hrtime;
192
193         if (hrtime >= tim)
194                 return (-1);
195
196         sbt = zfs_nstosbt(tim);
197         rc = cv_timedwait_sig_sbt(cvp, mp, sbt, zfs_nstosbt(res), C_ABSOLUTE);
198
199         switch (rc) {
200         case EWOULDBLOCK:
201                 return (-1);
202         case EINTR:
203         case ERESTART:
204                 return (0);
205         default:
206                 KASSERT(rc == 0, ("unexpected rc value %d", rc));
207                 return (1);
208         }
209 }
210
211 #endif  /* _OPENSOLARIS_SYS_CONDVAR_H_ */