VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/semwait-linux.h@ 103795

Last change on this file since 103795 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: semwait-linux.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Common semaphore wait code, Linux.
4 */
5
6/*
7 * Copyright (C) 2021-2023 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_r3_linux_semwait_linux_h
38#define IPRT_INCLUDED_SRC_r3_linux_semwait_linux_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43
44/* With 2.6.17 futex.h has become C++ unfriendly, so define the bits we need. */
45#define FUTEX_WAIT 0
46#define FUTEX_WAKE 1
47#define FUTEX_WAIT_BITSET 9 /**< @since 2.6.25 - uses absolute timeout. */
48
49
50/**
51 * Wrapper for the futex syscall.
52 */
53DECLINLINE(long) sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
54{
55 errno = 0;
56 long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
57 if (rc < 0)
58 {
59 Assert(rc == -1);
60 rc = -errno;
61 }
62 return rc;
63}
64
65
66DECL_NO_INLINE(static, void) rtSemLinuxCheckForFutexWaitBitSetSlow(int volatile *pfCanUseWaitBitSet)
67{
68 uint32_t uTestVar = UINT32_MAX;
69 long rc = sys_futex(&uTestVar, FUTEX_WAIT_BITSET, UINT32_C(0xf0f0f0f0), NULL, NULL, UINT32_MAX);
70 *pfCanUseWaitBitSet = rc == -EAGAIN;
71 AssertMsg(rc == -ENOSYS || rc == -EAGAIN, ("%d\n", rc));
72}
73
74
75DECLINLINE(void) rtSemLinuxCheckForFutexWaitBitSet(int volatile *pfCanUseWaitBitSet)
76{
77 if (*pfCanUseWaitBitSet != -1)
78 { /* likely */ }
79 else
80 rtSemLinuxCheckForFutexWaitBitSetSlow(pfCanUseWaitBitSet);
81}
82
83
84/**
85 * Converts a extended wait timeout specification to an timespec and
86 * corresponding futex operation, as well as an approximate relative nanosecond
87 * interval.
88 *
89 * @note This does not check for RTSEMWAIT_FLAGS_INDEFINITE, caller should've
90 * done that already.
91 *
92 * @returns The relative wait in nanoseconds. 0 for a poll call, UINT64_MAX for
93 * an effectively indefinite wait.
94 * @param fFlags RTSEMWAIT_FLAGS_XXX.
95 * @param fCanUseWaitBitSet Whether we can use FUTEX_WAIT_BITMSET or not.
96 * @param uTimeout The timeout.
97 * @param pDeadline Where to return the deadline.
98 * @param piWaitOp Where to return the FUTEX wait operation number.
99 * @param puWaitVal3 Where to return the FUTEX wait value 3.
100 * @param pnsAbsTimeout Where to return the absolute timeout in case of
101 * a resuming relative call (i.e. FUTEX_WAIT).
102 */
103DECL_FORCE_INLINE(uint64_t)
104rtSemLinuxCalcDeadline(uint32_t fFlags, uint64_t uTimeout, int fCanUseWaitBitSet,
105 struct timespec *pDeadline, int *piWaitOp, uint32_t *puWaitVal3, uint64_t *pnsAbsTimeout)
106{
107 Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
108
109 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
110 {
111 Assert(!(fFlags & RTSEMWAIT_FLAGS_ABSOLUTE));
112
113 /*
114 * Polling call?
115 */
116 if (uTimeout == 0)
117 return 0;
118
119 /*
120 * We use FUTEX_WAIT here as it takes a relative timespec.
121 *
122 * Note! For non-resuming waits, we can skip calculating the absolute
123 * time ASSUMING it is only needed for timeout adjustments
124 * after an -EINTR return.
125 */
126 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
127 {
128 if ( sizeof(pDeadline->tv_sec) >= sizeof(uint64_t)
129 || uTimeout < (uint64_t)UINT32_MAX * RT_MS_1SEC)
130 {
131 pDeadline->tv_sec = uTimeout / RT_MS_1SEC;
132 pDeadline->tv_nsec = (uTimeout % RT_MS_1SEC) & RT_NS_1MS;
133 uTimeout *= RT_NS_1MS;
134 }
135 else
136 return UINT64_MAX;
137 }
138 else
139 {
140 Assert(fFlags & RTSEMWAIT_FLAGS_NANOSECS);
141 if ( sizeof(pDeadline->tv_sec) >= sizeof(uint64_t)
142 || uTimeout < (uint64_t)UINT32_MAX * RT_NS_1SEC)
143 {
144 pDeadline->tv_sec = uTimeout / RT_NS_1SEC;
145 pDeadline->tv_nsec = uTimeout % RT_NS_1SEC;
146 }
147 else
148 return UINT64_MAX;
149 }
150
151#ifdef RT_STRICT
152 if (!(fFlags & RTSEMWAIT_FLAGS_RESUME))
153 *pnsAbsTimeout = uTimeout;
154 else
155#endif
156 *pnsAbsTimeout = RTTimeNanoTS() + uTimeout; /* Note! only relevant for relative waits (FUTEX_WAIT). */
157 }
158 else
159 {
160 /* Absolute deadline: */
161 Assert(fFlags & RTSEMWAIT_FLAGS_ABSOLUTE);
162 if (fCanUseWaitBitSet == true)
163 {
164 /*
165 * Use FUTEX_WAIT_BITSET as it takes an absolute deadline.
166 */
167 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
168 {
169 if ( sizeof(pDeadline->tv_sec) >= sizeof(uint64_t)
170 || uTimeout < (uint64_t)UINT32_MAX * RT_MS_1SEC)
171 {
172 pDeadline->tv_sec = uTimeout / RT_MS_1SEC;
173 pDeadline->tv_nsec = (uTimeout % RT_MS_1SEC) & RT_NS_1MS;
174 }
175 else
176 return UINT64_MAX;
177 }
178 else
179 {
180 Assert(fFlags & RTSEMWAIT_FLAGS_NANOSECS);
181 if ( sizeof(pDeadline->tv_sec) >= sizeof(uint64_t)
182 || uTimeout < (uint64_t)UINT32_MAX * RT_NS_1SEC)
183 {
184 pDeadline->tv_sec = uTimeout / RT_NS_1SEC;
185 pDeadline->tv_nsec = uTimeout % RT_NS_1SEC;
186 }
187 else
188 return UINT64_MAX;
189 }
190 *pnsAbsTimeout = uTimeout;
191 *piWaitOp = FUTEX_WAIT_BITSET;
192 *puWaitVal3 = UINT32_MAX;
193 return RT_MS_1SEC; /* Whatever non-zero; Whole point is not calling RTTimeNanoTS() in this path. */
194 }
195
196 /*
197 * FUTEX_WAIT_BITSET is not available, so use FUTEX_WAIT with a
198 * relative timeout.
199 */
200 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
201 {
202 if (uTimeout < UINT64_MAX / RT_NS_1MS)
203 uTimeout *= RT_NS_1MS;
204 else
205 return UINT64_MAX;
206 }
207
208 uint64_t const u64Now = RTTimeNanoTS();
209 if (u64Now < uTimeout)
210 {
211 *pnsAbsTimeout = uTimeout;
212 uTimeout -= u64Now;
213 }
214 else
215 return 0;
216
217 if ( sizeof(pDeadline->tv_sec) >= sizeof(uint64_t)
218 || uTimeout < (uint64_t)UINT32_MAX * RT_NS_1SEC)
219 {
220 pDeadline->tv_sec = uTimeout / RT_NS_1SEC;
221 pDeadline->tv_nsec = uTimeout % RT_NS_1SEC;
222 }
223 else
224 return UINT64_MAX;
225 }
226
227 *piWaitOp = FUTEX_WAIT;
228 *puWaitVal3 = 0;
229 return uTimeout;
230}
231
232#endif /* !IPRT_INCLUDED_SRC_r3_linux_semwait_linux_h */
233
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