1 | /* $Id: spinlock-r0drv-darwin.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Spinlocks, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 <iprt/spinlock.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/alloc.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 |
|
---|
38 | #include "internal/magics.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | /**
|
---|
45 | * Wrapper for the KSPIN_LOCK type.
|
---|
46 | */
|
---|
47 | typedef struct RTSPINLOCKINTERNAL
|
---|
48 | {
|
---|
49 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
50 | uint32_t volatile u32Magic;
|
---|
51 | /** The Darwin spinlock structure. */
|
---|
52 | lck_spin_t *pSpinLock;
|
---|
53 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
58 | {
|
---|
59 | /*
|
---|
60 | * Allocate.
|
---|
61 | */
|
---|
62 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
63 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
64 | if (!pSpinlockInt)
|
---|
65 | return VERR_NO_MEMORY;
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Initialize & return.
|
---|
69 | */
|
---|
70 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
71 | Assert(g_pDarwinLockGroup);
|
---|
72 | pSpinlockInt->pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
73 | if (!pSpinlockInt->pSpinLock)
|
---|
74 | {
|
---|
75 | RTMemFree(pSpinlockInt);
|
---|
76 | return VERR_NO_MEMORY;
|
---|
77 | }
|
---|
78 |
|
---|
79 | *pSpinlock = pSpinlockInt;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | * Validate input.
|
---|
88 | */
|
---|
89 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
90 | if (!pSpinlockInt)
|
---|
91 | return VERR_INVALID_PARAMETER;
|
---|
92 | AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
93 | ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
|
---|
94 | VERR_INVALID_PARAMETER);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Make the lock invalid and release the memory.
|
---|
98 | */
|
---|
99 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
100 |
|
---|
101 | Assert(g_pDarwinLockGroup);
|
---|
102 | lck_spin_destroy(pSpinlockInt->pSpinLock, g_pDarwinLockGroup);
|
---|
103 | pSpinlockInt->pSpinLock = NULL;
|
---|
104 |
|
---|
105 | RTMemFree(pSpinlockInt);
|
---|
106 | return VINF_SUCCESS;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
111 | {
|
---|
112 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
113 | AssertPtr(pSpinlockInt);
|
---|
114 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
115 |
|
---|
116 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
117 |
|
---|
118 | pTmp->uFlags = ASMGetFlags();
|
---|
119 | ASMIntDisable();
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
124 | {
|
---|
125 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
126 | AssertPtr(pSpinlockInt);
|
---|
127 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
128 |
|
---|
129 | ASMSetFlags(pTmp->uFlags);
|
---|
130 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
135 | {
|
---|
136 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
137 | AssertPtr(pSpinlockInt);
|
---|
138 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
139 |
|
---|
140 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
145 | {
|
---|
146 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
147 | AssertPtr(pSpinlockInt);
|
---|
148 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
149 |
|
---|
150 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
151 | }
|
---|
152 |
|
---|