1 | /* $Id: spinlock-r0drv-darwin.cpp 22052 2009-08-07 09:45:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-darwin-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/asm.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 | * Wrapper for the KSPIN_LOCK type.
|
---|
53 | */
|
---|
54 | typedef struct RTSPINLOCKINTERNAL
|
---|
55 | {
|
---|
56 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
57 | uint32_t volatile u32Magic;
|
---|
58 | /** The Darwin spinlock structure. */
|
---|
59 | lck_spin_t *pSpinLock;
|
---|
60 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
65 | {
|
---|
66 | RT_ASSERT_PREEMPTIBLE();
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Allocate.
|
---|
70 | */
|
---|
71 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
72 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
73 | if (!pSpinlockInt)
|
---|
74 | return VERR_NO_MEMORY;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Initialize & return.
|
---|
78 | */
|
---|
79 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
80 | Assert(g_pDarwinLockGroup);
|
---|
81 | pSpinlockInt->pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
82 | if (!pSpinlockInt->pSpinLock)
|
---|
83 | {
|
---|
84 | RTMemFree(pSpinlockInt);
|
---|
85 | return VERR_NO_MEMORY;
|
---|
86 | }
|
---|
87 |
|
---|
88 | *pSpinlock = pSpinlockInt;
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
94 | {
|
---|
95 | /*
|
---|
96 | * Validate input.
|
---|
97 | */
|
---|
98 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
99 | if (!pSpinlockInt)
|
---|
100 | return VERR_INVALID_PARAMETER;
|
---|
101 | AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
102 | ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
|
---|
103 | VERR_INVALID_PARAMETER);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Make the lock invalid and release the memory.
|
---|
107 | */
|
---|
108 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
109 |
|
---|
110 | Assert(g_pDarwinLockGroup);
|
---|
111 | lck_spin_destroy(pSpinlockInt->pSpinLock, g_pDarwinLockGroup);
|
---|
112 | pSpinlockInt->pSpinLock = NULL;
|
---|
113 |
|
---|
114 | RTMemFree(pSpinlockInt);
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
120 | {
|
---|
121 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
122 | AssertPtr(pSpinlockInt);
|
---|
123 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
124 |
|
---|
125 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
126 |
|
---|
127 | pTmp->uFlags = ASMGetFlags();
|
---|
128 | ASMIntDisable();
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
133 | {
|
---|
134 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
135 | AssertPtr(pSpinlockInt);
|
---|
136 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
137 |
|
---|
138 | ASMSetFlags(pTmp->uFlags);
|
---|
139 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
144 | {
|
---|
145 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
146 | AssertPtr(pSpinlockInt);
|
---|
147 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
148 |
|
---|
149 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
154 | {
|
---|
155 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
156 | AssertPtr(pSpinlockInt);
|
---|
157 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
158 |
|
---|
159 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
160 | }
|
---|
161 |
|
---|