VirtualBox

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

Last change on this file since 36190 was 36190, checked in by vboxsync, 14 years ago

IPRT,Drivers: Committed a modified version of the diff_linux_guest_host patch. This mangles the IPRT symbols in kernel space on linux and later other platforms.

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