VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semmutex-r0drv-darwin.cpp

Last change on this file was 109128, checked in by vboxsync, 10 days ago

IPRT/r0drv: Build adjustments for darwin.arm64. jiraref:VBP-1653

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.7 KB
Line 
1/* $Id: semmutex-r0drv-darwin.cpp 109128 2025-05-01 01:31:56Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
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
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define RTSEMMUTEX_WITHOUT_REMAPPING
42#include "the-darwin-kernel.h"
43#include "internal/iprt.h"
44#include <iprt/semaphore.h>
45
46#include <iprt/asm.h>
47#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48# include <iprt/asm-amd64-x86.h>
49#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
50# include <iprt/asm-arm.h>
51#endif
52#include <iprt/assert.h>
53#include <iprt/err.h>
54#include <iprt/mem.h>
55#include <iprt/thread.h>
56
57#include "internal/magics.h"
58
59
60/*********************************************************************************************************************************
61* Structures and Typedefs *
62*********************************************************************************************************************************/
63/**
64 * Darwin mutex semaphore.
65 */
66typedef struct RTSEMMUTEXINTERNAL
67{
68 /** Magic value (RTSEMMUTEX_MAGIC). */
69 uint32_t volatile u32Magic;
70 /** The number of waiting threads. */
71 uint32_t cWaiters;
72 /** The number of references. */
73 uint32_t volatile cRefs;
74 /** The number of recursions. */
75 uint32_t cRecursions;
76 /** The handle of the owner thread. */
77 RTNATIVETHREAD hNativeOwner;
78 /** The spinlock protecting us. */
79 lck_spin_t *pSpinlock;
80} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
81
82
83
84RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
85{
86 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
87}
88
89
90RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
91 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
92{
93 RT_NOREF(hClass, uSubClass, pszNameFmt);
94 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
95 RT_ASSERT_PREEMPTIBLE();
96 IPRT_DARWIN_SAVE_EFL_AC();
97
98 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
99 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
100 if (pThis)
101 {
102 pThis->u32Magic = RTSEMMUTEX_MAGIC;
103 pThis->cWaiters = 0;
104 pThis->cRefs = 1;
105 pThis->cRecursions = 0;
106 pThis->hNativeOwner = NIL_RTNATIVETHREAD;
107 Assert(g_pDarwinLockGroup);
108 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
109 if (pThis->pSpinlock)
110 {
111 *phMutexSem = pThis;
112 IPRT_DARWIN_RESTORE_EFL_AC();
113 return VINF_SUCCESS;
114 }
115
116 RTMemFree(pThis);
117 }
118 IPRT_DARWIN_RESTORE_EFL_AC();
119 return VERR_NO_MEMORY;
120}
121
122
123/**
124 * Called when the refcount reaches zero.
125 */
126static void rtSemMutexDarwinFree(PRTSEMMUTEXINTERNAL pThis)
127{
128 IPRT_DARWIN_SAVE_EFL_AC();
129
130 lck_spin_unlock(pThis->pSpinlock);
131 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
132 RTMemFree(pThis);
133
134 IPRT_DARWIN_RESTORE_EFL_AC();
135}
136
137
138RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
139{
140 /*
141 * Validate input.
142 */
143 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
144 if (pThis == NIL_RTSEMMUTEX)
145 return VERR_INVALID_PARAMETER;
146 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
147 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
148 RT_ASSERT_INTS_ON();
149 IPRT_DARWIN_SAVE_EFL_AC();
150
151 /*
152 * Kill it, wake up all waiting threads and release the reference.
153 */
154 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMMUTEX_MAGIC, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
155 lck_spin_lock(pThis->pSpinlock);
156
157 if (pThis->cWaiters > 0)
158 thread_wakeup_prim((event_t)pThis, FALSE /* one_thread */, THREAD_RESTART);
159
160 if (ASMAtomicDecU32(&pThis->cRefs) == 0)
161 rtSemMutexDarwinFree(pThis);
162 else
163 lck_spin_unlock(pThis->pSpinlock);
164
165 IPRT_DARWIN_RESTORE_EFL_AC();
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Internal worker for the sleep scenario.
172 *
173 * Called owning the spinlock, returns without it.
174 *
175 * @returns IPRT status code.
176 * @param pThis The mutex instance.
177 * @param cMillies The timeout.
178 * @param fInterruptible Whether it's interruptible
179 * (RTSemMutexRequestNoResume) or not
180 * (RTSemMutexRequest).
181 * @param hNativeSelf The thread handle of the caller.
182 */
183static int rtR0SemMutexDarwinRequestSleep(PRTSEMMUTEXINTERNAL pThis, RTMSINTERVAL cMillies,
184 wait_interrupt_t fInterruptible, RTNATIVETHREAD hNativeSelf)
185{
186 /*
187 * Grab a reference and indicate that we're waiting.
188 */
189 pThis->cWaiters++;
190 ASMAtomicIncU32(&pThis->cRefs);
191
192 /*
193 * Go to sleep, use the address of the mutex instance as sleep/blocking/event id.
194 */
195 wait_result_t rcWait;
196 if (cMillies == RT_INDEFINITE_WAIT)
197 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
198 else
199 {
200 uint64_t u64AbsTime;
201 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
202 u64AbsTime += mach_absolute_time();
203
204 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
205 (event_t)pThis, fInterruptible, u64AbsTime);
206 }
207
208 /*
209 * Translate the rc.
210 */
211 int rc;
212 switch (rcWait)
213 {
214 case THREAD_AWAKENED:
215 if (RT_LIKELY(pThis->u32Magic == RTSEMMUTEX_MAGIC))
216 {
217 if (RT_LIKELY( pThis->cRecursions == 0
218 && pThis->hNativeOwner == NIL_RTNATIVETHREAD))
219 {
220 pThis->cRecursions = 1;
221 pThis->hNativeOwner = hNativeSelf;
222 rc = VINF_SUCCESS;
223 }
224 else
225 {
226 Assert(pThis->cRecursions == 0);
227 Assert(pThis->hNativeOwner == NIL_RTNATIVETHREAD);
228 rc = VERR_INTERNAL_ERROR_3;
229 }
230 }
231 else
232 rc = VERR_SEM_DESTROYED;
233 break;
234
235 case THREAD_TIMED_OUT:
236 Assert(cMillies != RT_INDEFINITE_WAIT);
237 rc = VERR_TIMEOUT;
238 break;
239
240 case THREAD_INTERRUPTED:
241 Assert(fInterruptible);
242 rc = VERR_INTERRUPTED;
243 break;
244
245 case THREAD_RESTART:
246 Assert(pThis->u32Magic == ~RTSEMMUTEX_MAGIC);
247 rc = VERR_SEM_DESTROYED;
248 break;
249
250 default:
251 AssertMsgFailed(("rcWait=%d\n", rcWait));
252 rc = VERR_GENERAL_FAILURE;
253 break;
254 }
255
256 /*
257 * Dereference it and quit the lock.
258 */
259 Assert(pThis->cWaiters > 0);
260 pThis->cWaiters--;
261
262 Assert(pThis->cRefs > 0);
263 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
264 rtSemMutexDarwinFree(pThis);
265 else
266 lck_spin_unlock(pThis->pSpinlock);
267 return rc;
268}
269
270
271/**
272 * Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
273 *
274 * @returns IPRT status code.
275 * @param hMutexSem The mutex handle.
276 * @param cMillies The timeout.
277 * @param fInterruptible Whether it's interruptible
278 * (RTSemMutexRequestNoResume) or not
279 * (RTSemMutexRequest).
280 */
281DECLINLINE(int) rtR0SemMutexDarwinRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
282{
283 /*
284 * Validate input.
285 */
286 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
287 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
288 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
289 RT_ASSERT_PREEMPTIBLE();
290 IPRT_DARWIN_SAVE_EFL_AC();
291
292 /*
293 * Grab the lock and check out the state.
294 */
295 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
296 int rc = VINF_SUCCESS;
297 lck_spin_lock(pThis->pSpinlock);
298
299 /* Recursive call? */
300 if (pThis->hNativeOwner == hNativeSelf)
301 {
302 Assert(pThis->cRecursions > 0);
303 Assert(pThis->cRecursions < 256);
304 pThis->cRecursions++;
305 }
306
307 /* Is it free and nobody ahead of us in the queue? */
308 else if ( pThis->hNativeOwner == NIL_RTNATIVETHREAD
309 && pThis->cWaiters == 0)
310 {
311 pThis->hNativeOwner = hNativeSelf;
312 pThis->cRecursions = 1;
313 }
314
315 /* Polling call? */
316 else if (cMillies == 0)
317 rc = VERR_TIMEOUT;
318
319 /* Yawn, time for a nap... */
320 else
321 {
322 rc = rtR0SemMutexDarwinRequestSleep(pThis, cMillies, fInterruptible, hNativeSelf);
323 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
324 return rc;
325 }
326
327 lck_spin_unlock(pThis->pSpinlock);
328 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
329 return rc;
330}
331
332
333RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
334{
335 return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_UNINT);
336}
337
338
339RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
340{
341 RT_SRC_POS_NOREF(); RT_NOREF(uId);
342 return RTSemMutexRequest(hMutexSem, cMillies);
343}
344
345
346RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
347{
348 return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_ABORTSAFE);
349}
350
351
352RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
353{
354 RT_SRC_POS_NOREF(); RT_NOREF(uId);
355 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
356}
357
358
359RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
360{
361 /*
362 * Validate input.
363 */
364 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
365 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
366 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
367 RT_ASSERT_PREEMPTIBLE();
368 IPRT_DARWIN_SAVE_EFL_AC();
369
370 /*
371 * Take the lock and do the job.
372 */
373 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
374 int rc = VINF_SUCCESS;
375 lck_spin_lock(pThis->pSpinlock);
376
377 if (pThis->hNativeOwner == hNativeSelf)
378 {
379 Assert(pThis->cRecursions > 0);
380 if (--pThis->cRecursions == 0)
381 {
382 pThis->hNativeOwner = NIL_RTNATIVETHREAD;
383 if (pThis->cWaiters > 0)
384 thread_wakeup_prim((event_t)pThis, TRUE /* one_thread */, THREAD_AWAKENED);
385
386 }
387 }
388 else
389 rc = VERR_NOT_OWNER;
390
391 lck_spin_unlock(pThis->pSpinlock);
392
393 AssertRC(rc);
394 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
395 return VINF_SUCCESS;
396}
397
398
399RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
400{
401 /*
402 * Validate.
403 */
404 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
405 AssertPtrReturn(pThis, false);
406 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
407 IPRT_DARWIN_SAVE_EFL_AC();
408
409 /*
410 * Take the lock and do the check.
411 */
412 lck_spin_lock(pThis->pSpinlock);
413 bool fRc = pThis->hNativeOwner != NIL_RTNATIVETHREAD;
414 lck_spin_unlock(pThis->pSpinlock);
415
416 IPRT_DARWIN_RESTORE_EFL_AC();
417 return fRc;
418}
419
Note: See TracBrowser for help on using the repository browser.

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