1 | /* $Id: semfastmutex-r0drv-darwin.cpp 29255 2010-05-09 18:11:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fast 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 | #include "the-darwin-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
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/err.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 | #include <iprt/mp.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /**
|
---|
52 | * Wrapper for the darwin semaphore structure.
|
---|
53 | */
|
---|
54 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
55 | {
|
---|
56 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
57 | uint32_t u32Magic;
|
---|
58 | /** The mutex. */
|
---|
59 | lck_mtx_t *pMtx;
|
---|
60 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
65 | {
|
---|
66 | AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
|
---|
67 | AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
|
---|
68 | RT_ASSERT_PREEMPTIBLE();
|
---|
69 |
|
---|
70 | PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
71 | if (pThis)
|
---|
72 | {
|
---|
73 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
74 | Assert(g_pDarwinLockGroup);
|
---|
75 | pThis->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
76 | if (pThis->pMtx)
|
---|
77 | {
|
---|
78 | *phFastMtx = pThis;
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 | RTMemFree(pThis);
|
---|
83 | }
|
---|
84 | return VERR_NO_MEMORY;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
89 | {
|
---|
90 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
91 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
94 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
95 | RT_ASSERT_INTS_ON();
|
---|
96 |
|
---|
97 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
98 | Assert(g_pDarwinLockGroup);
|
---|
99 | lck_mtx_free(pThis->pMtx, g_pDarwinLockGroup);
|
---|
100 | pThis->pMtx = NULL;
|
---|
101 | RTMemFree(pThis);
|
---|
102 |
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
108 | {
|
---|
109 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
110 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
111 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
112 | RT_ASSERT_PREEMPTIBLE();
|
---|
113 |
|
---|
114 | lck_mtx_lock(pThis->pMtx);
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
120 | {
|
---|
121 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
122 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
123 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
124 | RT_ASSERT_PREEMPTIBLE();
|
---|
125 |
|
---|
126 | lck_mtx_unlock(pThis->pMtx);
|
---|
127 | return VINF_SUCCESS;
|
---|
128 | }
|
---|
129 |
|
---|