VirtualBox

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

Last change on this file since 85546 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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