]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/os/freebsd/spl/sys/condvar.h
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[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_sig_io cv_timedwait_sig
147
148 static inline int
149 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, hrtime_t res,
150     int flag)
151 {
152         hrtime_t hrtime;
153         int rc;
154
155         ASSERT(tim >= res);
156
157         hrtime = gethrtime();
158         if (flag == 0)
159                 tim += hrtime;
160
161         if (hrtime >= tim)
162                 return (-1);
163         rc = cv_timedwait_sbt(cvp, mp, zfs_nstosbt(tim),
164             zfs_nstosbt(res), C_ABSOLUTE);
165
166         if (rc == EWOULDBLOCK)
167                 return (-1);
168
169         KASSERT(rc == 0, ("unexpected rc value %d", rc));
170         return (1);
171 }
172
173 static inline int
174 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
175     hrtime_t res, int flag)
176 {
177         sbintime_t sbt;
178         hrtime_t hrtime;
179         int rc;
180
181         ASSERT(tim >= res);
182
183         hrtime = gethrtime();
184         if (flag == 0)
185                 tim += hrtime;
186
187         if (hrtime >= tim)
188                 return (-1);
189
190         sbt = zfs_nstosbt(tim);
191         rc = cv_timedwait_sig_sbt(cvp, mp, sbt, zfs_nstosbt(res), C_ABSOLUTE);
192
193         switch (rc) {
194         case EWOULDBLOCK:
195                 return (-1);
196         case EINTR:
197         case ERESTART:
198                 return (0);
199         default:
200                 KASSERT(rc == 0, ("unexpected rc value %d", rc));
201                 return (1);
202         }
203 }
204
205 #endif  /* _OPENSOLARIS_SYS_CONDVAR_H_ */