VirtualBox

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

Last change on this file since 58591 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.2 KB
Line 
1/* $Id: semmutex-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
82 RT_ASSERT_PREEMPTIBLE();
83 IPRT_DARWIN_SAVE_EFL_AC();
84
85 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
86 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
87 if (pThis)
88 {
89 pThis->u32Magic = RTSEMMUTEX_MAGIC;
90 pThis->cWaiters = 0;
91 pThis->cRefs = 1;
92 pThis->cRecursions = 0;
93 pThis->hNativeOwner = NIL_RTNATIVETHREAD;
94 Assert(g_pDarwinLockGroup);
95 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
96 if (pThis->pSpinlock)
97 {
98 *phMutexSem = pThis;
99 IPRT_DARWIN_RESTORE_EFL_AC();
100 return VINF_SUCCESS;
101 }
102
103 RTMemFree(pThis);
104 }
105 IPRT_DARWIN_RESTORE_EFL_AC();
106 return VERR_NO_MEMORY;
107}
108
109
110/**
111 * Called when the refcount reaches zero.
112 */
113static void rtSemMutexDarwinFree(PRTSEMMUTEXINTERNAL pThis)
114{
115 IPRT_DARWIN_SAVE_EFL_AC();
116
117 lck_spin_unlock(pThis->pSpinlock);
118 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
119 RTMemFree(pThis);
120
121 IPRT_DARWIN_RESTORE_EFL_AC();
122}
123
124
125RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
126{
127 /*
128 * Validate input.
129 */
130 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
131 if (pThis == NIL_RTSEMMUTEX)
132 return VERR_INVALID_PARAMETER;
133 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
134 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
135 RT_ASSERT_INTS_ON();
136 IPRT_DARWIN_SAVE_EFL_AC();
137
138 /*
139 * Kill it, wake up all waiting threads and release the reference.
140 */
141 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMMUTEX_MAGIC, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
142 lck_spin_lock(pThis->pSpinlock);
143
144 if (pThis->cWaiters > 0)
145 thread_wakeup_prim((event_t)pThis, FALSE /* one_thread */, THREAD_RESTART);
146
147 if (ASMAtomicDecU32(&pThis->cRefs) == 0)
148 rtSemMutexDarwinFree(pThis);
149 else
150 lck_spin_unlock(pThis->pSpinlock);
151
152 IPRT_DARWIN_RESTORE_EFL_AC();
153 return VINF_SUCCESS;
154}
155
156
157/**
158 * Internal worker for the sleep scenario.
159 *
160 * Called owning the spinlock, returns without it.
161 *
162 * @returns IPRT status code.
163 * @param pThis The mutex instance.
164 * @param cMillies The timeout.
165 * @param fInterruptible Whether it's interruptible
166 * (RTSemMutexRequestNoResume) or not
167 * (RTSemMutexRequest).
168 * @param hNativeSelf The thread handle of the caller.
169 */
170static int rtR0SemMutexDarwinRequestSleep(PRTSEMMUTEXINTERNAL pThis, RTMSINTERVAL cMillies,
171 wait_interrupt_t fInterruptible, RTNATIVETHREAD hNativeSelf)
172{
173 /*
174 * Grab a reference and indicate that we're waiting.
175 */
176 pThis->cWaiters++;
177 ASMAtomicIncU32(&pThis->cRefs);
178
179 /*
180 * Go to sleep, use the address of the mutex instance as sleep/blocking/event id.
181 */
182 wait_result_t rcWait;
183 if (cMillies == RT_INDEFINITE_WAIT)
184 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
185 else
186 {
187 uint64_t u64AbsTime;
188 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
189 u64AbsTime += mach_absolute_time();
190
191 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
192 (event_t)pThis, fInterruptible, u64AbsTime);
193 }
194
195 /*
196 * Translate the rc.
197 */
198 int rc;
199 switch (rcWait)
200 {
201 case THREAD_AWAKENED:
202 if (RT_LIKELY(pThis->u32Magic == RTSEMMUTEX_MAGIC))
203 {
204 if (RT_LIKELY( pThis->cRecursions == 0
205 && pThis->hNativeOwner == NIL_RTNATIVETHREAD))
206 {
207 pThis->cRecursions = 1;
208 pThis->hNativeOwner = hNativeSelf;
209 rc = VINF_SUCCESS;
210 }
211 else
212 {
213 Assert(pThis->cRecursions == 0);
214 Assert(pThis->hNativeOwner == NIL_RTNATIVETHREAD);
215 rc = VERR_INTERNAL_ERROR_3;
216 }
217 }
218 else
219 rc = VERR_SEM_DESTROYED;
220 break;
221
222 case THREAD_TIMED_OUT:
223 Assert(cMillies != RT_INDEFINITE_WAIT);
224 rc = VERR_TIMEOUT;
225 break;
226
227 case THREAD_INTERRUPTED:
228 Assert(fInterruptible);
229 rc = VERR_INTERRUPTED;
230 break;
231
232 case THREAD_RESTART:
233 Assert(pThis->u32Magic == ~RTSEMMUTEX_MAGIC);
234 rc = VERR_SEM_DESTROYED;
235 break;
236
237 default:
238 AssertMsgFailed(("rcWait=%d\n", rcWait));
239 rc = VERR_GENERAL_FAILURE;
240 break;
241 }
242
243 /*
244 * Dereference it and quit the lock.
245 */
246 Assert(pThis->cWaiters > 0);
247 pThis->cWaiters--;
248
249 Assert(pThis->cRefs > 0);
250 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
251 rtSemMutexDarwinFree(pThis);
252 else
253 lck_spin_unlock(pThis->pSpinlock);
254 return rc;
255}
256
257
258/**
259 * Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
260 *
261 * @returns IPRT status code.
262 * @param hMutexSem The mutex handle.
263 * @param cMillies The timeout.
264 * @param fInterruptible Whether it's interruptible
265 * (RTSemMutexRequestNoResume) or not
266 * (RTSemMutexRequest).
267 */
268DECLINLINE(int) rtR0SemMutexDarwinRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
269{
270 /*
271 * Validate input.
272 */
273 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
274 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
275 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
276 RT_ASSERT_PREEMPTIBLE();
277 IPRT_DARWIN_SAVE_EFL_AC();
278
279 /*
280 * Grab the lock and check out the state.
281 */
282 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
283 int rc = VINF_SUCCESS;
284 lck_spin_lock(pThis->pSpinlock);
285
286 /* Recursive call? */
287 if (pThis->hNativeOwner == hNativeSelf)
288 {
289 Assert(pThis->cRecursions > 0);
290 Assert(pThis->cRecursions < 256);
291 pThis->cRecursions++;
292 }
293
294 /* Is it free and nobody ahead of us in the queue? */
295 else if ( pThis->hNativeOwner == NIL_RTNATIVETHREAD
296 && pThis->cWaiters == 0)
297 {
298 pThis->hNativeOwner = hNativeSelf;
299 pThis->cRecursions = 1;
300 }
301
302 /* Polling call? */
303 else if (cMillies == 0)
304 rc = VERR_TIMEOUT;
305
306 /* Yawn, time for a nap... */
307 else
308 {
309 rc = rtR0SemMutexDarwinRequestSleep(pThis, cMillies, fInterruptible, hNativeSelf);
310 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
311 return rc;
312 }
313
314 lck_spin_unlock(pThis->pSpinlock);
315 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
316 return rc;
317}
318
319
320RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
321{
322 return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_UNINT);
323}
324
325
326RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
327{
328 return RTSemMutexRequest(hMutexSem, cMillies);
329}
330
331
332RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
333{
334 return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_ABORTSAFE);
335}
336
337
338RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
339{
340 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
341}
342
343
344RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
345{
346 /*
347 * Validate input.
348 */
349 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
350 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
351 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
352 RT_ASSERT_PREEMPTIBLE();
353 IPRT_DARWIN_SAVE_EFL_AC();
354
355 /*
356 * Take the lock and do the job.
357 */
358 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
359 int rc = VINF_SUCCESS;
360 lck_spin_lock(pThis->pSpinlock);
361
362 if (pThis->hNativeOwner == hNativeSelf)
363 {
364 Assert(pThis->cRecursions > 0);
365 if (--pThis->cRecursions == 0)
366 {
367 pThis->hNativeOwner = NIL_RTNATIVETHREAD;
368 if (pThis->cWaiters > 0)
369 thread_wakeup_prim((event_t)pThis, TRUE /* one_thread */, THREAD_AWAKENED);
370
371 }
372 }
373 else
374 rc = VERR_NOT_OWNER;
375
376 lck_spin_unlock(pThis->pSpinlock);
377
378 AssertRC(rc);
379 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
380 return VINF_SUCCESS;
381}
382
383
384RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
385{
386 /*
387 * Validate.
388 */
389 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
390 AssertPtrReturn(pThis, false);
391 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
392 IPRT_DARWIN_SAVE_EFL_AC();
393
394 /*
395 * Take the lock and do the check.
396 */
397 lck_spin_lock(pThis->pSpinlock);
398 bool fRc = pThis->hNativeOwner != NIL_RTNATIVETHREAD;
399 lck_spin_unlock(pThis->pSpinlock);
400
401 IPRT_DARWIN_RESTORE_EFL_AC();
402 return fRc;
403}
404
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