VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/sleepqueue-r0drv-freebsd.h@ 96445

Last change on this file since 96445 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: sleepqueue-r0drv-freebsd.h 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - FreeBSD Ring-0 Driver Helpers for Abstracting Sleep Queues,
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_SRC_r0drv_freebsd_sleepqueue_r0drv_freebsd_h
38#define IPRT_INCLUDED_SRC_r0drv_freebsd_sleepqueue_r0drv_freebsd_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include "the-freebsd-kernel.h"
44
45#include <iprt/asm-math.h>
46#include <iprt/err.h>
47#include <iprt/string.h>
48#include <iprt/time.h>
49
50/**
51 * Kernel mode FreeBSD wait state structure.
52 */
53typedef struct RTR0SEMBSDSLEEP
54{
55 /** The absolute timeout given as nano seconds since the start of the
56 * monotonic clock. */
57 uint64_t uNsAbsTimeout;
58 /** The timeout in ticks. Updated after waiting. */
59 int iTimeout;
60 /** Set if it's an indefinite wait. */
61 bool fIndefinite;
62 /** Set if we've already timed out.
63 * Set by rtR0SemBsdWaitDoIt and read by rtR0SemBsdWaitHasTimedOut. */
64 bool fTimedOut;
65 /** Flag whether the wait was interrupted. */
66 bool fInterrupted;
67 /** flag whether the wait is interruptible or not. */
68 bool fInterruptible;
69 /** Opaque wait channel id. */
70 void *pvWaitChan;
71} RTR0SEMBSDSLEEP;
72/** Pointer to a FreeBSD wait state. */
73typedef RTR0SEMBSDSLEEP *PRTR0SEMBSDSLEEP;
74
75
76/**
77 * Updates the timeout of the FreeBSD wait.
78 *
79 * @returns RTSEMWAIT_FLAGS_INDEFINITE if the timeout value is too big.
80 * 0 otherwise
81 * @param pWait The wait structure.
82 * @param uTimeout The relative timeout in nanoseconds.
83 */
84DECLINLINE(uint32_t) rtR0SemBsdWaitUpdateTimeout(PRTR0SEMBSDSLEEP pWait, uint64_t uTimeout)
85{
86#if 0
87 struct timeval tv;
88
89 tv.tv_sec = uTimeout / UINT64_C(1000000000);
90 tv.tv_usec = (uTimeout % UINT64_C(1000000000)) / UINT64_C(1000);
91
92 pWait->iTimeout = tvtohz(&tv);
93#else
94 uint64_t cTicks = ASMMultU64ByU32DivByU32(uTimeout, hz, UINT32_C(1000000000));
95 if (cTicks >= INT_MAX)
96 return RTSEMWAIT_FLAGS_INDEFINITE;
97 else
98 pWait->iTimeout = (int)cTicks;
99#endif
100
101 return 0;
102}
103
104/**
105 * Initializes a wait.
106 *
107 * The caller MUST check the wait condition BEFORE calling this function or the
108 * timeout logic will be flawed.
109 *
110 * @returns VINF_SUCCESS or VERR_TIMEOUT.
111 * @param pWait The wait structure.
112 * @param fFlags The wait flags.
113 * @param uTimeout The timeout.
114 * @param pvWaitChan The opaque wait channel.
115 */
116DECLINLINE(int) rtR0SemBsdWaitInit(PRTR0SEMBSDSLEEP pWait, uint32_t fFlags, uint64_t uTimeout,
117 void *pvWaitChan)
118{
119 pWait->iTimeout = 0;
120 pWait->uNsAbsTimeout = 0; /* shut up gcc */
121
122 /*
123 * Process the flags and timeout.
124 */
125 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
126 {
127/** @todo optimize: millisecs -> nanosecs -> millisec -> jiffies */
128 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
129 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
130 ? uTimeout * UINT32_C(1000000)
131 : UINT64_MAX;
132 if (uTimeout == UINT64_MAX)
133 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
134 else
135 {
136 uint64_t u64Now;
137 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
138 {
139 if (uTimeout == 0)
140 return VERR_TIMEOUT;
141
142 u64Now = RTTimeSystemNanoTS();
143 if (u64Now + uTimeout < u64Now) /* overflow */
144 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
145 else
146 pWait->uNsAbsTimeout = u64Now + uTimeout;
147 }
148 else
149 {
150 u64Now = RTTimeSystemNanoTS();
151 if (u64Now >= uTimeout)
152 return VERR_TIMEOUT;
153
154 pWait->uNsAbsTimeout = uTimeout;
155 uTimeout -= u64Now; /* Get a relative value. */
156 }
157 }
158 }
159
160 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
161 {
162 pWait->fIndefinite = false;
163 fFlags |= rtR0SemBsdWaitUpdateTimeout(pWait, uTimeout);
164 }
165
166 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
167 {
168 pWait->fIndefinite = true;
169 pWait->iTimeout = INT_MAX;
170 pWait->uNsAbsTimeout = UINT64_MAX;
171 }
172
173 pWait->fTimedOut = false;
174
175 /*
176 * Initialize the wait queue related bits.
177 */
178 pWait->fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE
179 ? true : false;
180 pWait->pvWaitChan = pvWaitChan;
181 pWait->fInterrupted = false;
182
183 return VINF_SUCCESS;
184}
185
186/**
187 * Prepares the next wait.
188 *
189 * This must be called before rtR0SemBsdWaitDoIt, and the caller should check
190 * the exit conditions inbetween the two calls.
191 *
192 * @param pWait The wait structure.
193 */
194DECLINLINE(void) rtR0SemBsdWaitPrepare(PRTR0SEMBSDSLEEP pWait)
195{
196 /* Lock the queues. */
197 sleepq_lock(pWait->pvWaitChan);
198}
199
200/**
201 * Do the actual wait.
202 *
203 * @param pWait The wait structure.
204 */
205DECLINLINE(void) rtR0SemBsdWaitDoIt(PRTR0SEMBSDSLEEP pWait)
206{
207 int rcBsd;
208 int fSleepqFlags = SLEEPQ_CONDVAR;
209
210 if (pWait->fInterruptible)
211 fSleepqFlags |= SLEEPQ_INTERRUPTIBLE;
212
213 sleepq_add(pWait->pvWaitChan, NULL, "VBoxIS", fSleepqFlags, 0);
214
215 if (!pWait->fIndefinite)
216 {
217 sleepq_set_timeout(pWait->pvWaitChan, pWait->iTimeout);
218
219 if (pWait->fInterruptible)
220 rcBsd = SLEEPQ_TIMEDWAIT_SIG(pWait->pvWaitChan);
221 else
222 rcBsd = SLEEPQ_TIMEDWAIT(pWait->pvWaitChan);
223 }
224 else
225 {
226 if (pWait->fInterruptible)
227 rcBsd = SLEEPQ_WAIT_SIG(pWait->pvWaitChan);
228 else
229 {
230 rcBsd = 0;
231 SLEEPQ_WAIT(pWait->pvWaitChan);
232 }
233 }
234
235 switch (rcBsd)
236 {
237 case 0:
238 break;
239 case ERESTART:
240 {
241 if (!pWait->fIndefinite)
242 {
243 /* Recalc timeout. */
244 uint64_t u64Now = RTTimeSystemNanoTS();
245 if (u64Now >= pWait->uNsAbsTimeout)
246 pWait->fTimedOut = true;
247 else
248 {
249 u64Now = pWait->uNsAbsTimeout - u64Now;
250 rtR0SemBsdWaitUpdateTimeout(pWait, u64Now);
251 }
252 }
253 break;
254 }
255 case EWOULDBLOCK:
256 pWait->fTimedOut = true;
257 break;
258 case EINTR:
259 Assert(pWait->fInterruptible);
260 pWait->fInterrupted = true;
261 break;
262 default:
263 AssertMsgFailed(("sleepq_* -> %d\n", rcBsd));
264 break;
265 }
266}
267
268
269/**
270 * Checks if a FreeBSD wait was interrupted.
271 *
272 * @returns true / false
273 * @param pWait The wait structure.
274 * @remarks This shall be called before the first rtR0SemBsdWaitDoIt().
275 */
276DECLINLINE(bool) rtR0SemBsdWaitWasInterrupted(PRTR0SEMBSDSLEEP pWait)
277{
278 return pWait->fInterrupted;
279}
280
281
282/**
283 * Checks if a FreeBSD wait has timed out.
284 *
285 * @returns true / false
286 * @param pWait The wait structure.
287 */
288DECLINLINE(bool) rtR0SemBsdWaitHasTimedOut(PRTR0SEMBSDSLEEP pWait)
289{
290 return pWait->fTimedOut;
291}
292
293
294/**
295 * Deletes a FreeBSD wait.
296 *
297 * @param pWait The wait structure.
298 */
299DECLINLINE(void) rtR0SemBsdWaitDelete(PRTR0SEMBSDSLEEP pWait)
300{
301 sleepq_release(pWait->pvWaitChan);
302}
303
304
305/**
306 * Signals the wait channel.
307 *
308 * @param pvWaitChan The opaque wait channel handle.
309 */
310DECLINLINE(void) rtR0SemBsdSignal(void *pvWaitChan)
311{
312 sleepq_lock(pvWaitChan);
313 int fWakeupSwapProc = sleepq_signal(pvWaitChan, SLEEPQ_CONDVAR, 0, 0);
314 sleepq_release(pvWaitChan);
315 if (fWakeupSwapProc)
316 kick_proc0();
317}
318
319/**
320 * Wakes up all waiters on the wait channel.
321 *
322 * @param pvWaitChan The opaque wait channel handle.
323 */
324DECLINLINE(void) rtR0SemBsdBroadcast(void *pvWaitChan)
325{
326 sleepq_lock(pvWaitChan);
327 sleepq_broadcast(pvWaitChan, SLEEPQ_CONDVAR, 0, 0);
328#if __FreeBSD_version >= 800000 /* Broadcast releases the sleep queue lock on FreeBSD 7.x */
329 sleepq_release(pvWaitChan);
330#endif
331}
332
333/**
334 * Gets the max resolution of the timeout machinery.
335 *
336 * @returns Resolution specified in nanoseconds.
337 */
338DECLINLINE(uint32_t) rtR0SemBsdWaitGetResolution(void)
339{
340 return 1000000000 / hz; /* ns */
341}
342
343#endif /* !IPRT_INCLUDED_SRC_r0drv_freebsd_sleepqueue_r0drv_freebsd_h */
344
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette