1 | /* $Id: spinlock-r0drv-darwin.cpp 1816 2007-03-29 18:59:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Spinlocks, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include "the-darwin-kernel.h"
|
---|
27 | #include <iprt/spinlock.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 | #include <iprt/alloc.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 |
|
---|
33 | #include "internal/magics.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Structures and Typedefs *
|
---|
38 | *******************************************************************************/
|
---|
39 | /**
|
---|
40 | * Wrapper for the KSPIN_LOCK type.
|
---|
41 | */
|
---|
42 | typedef struct RTSPINLOCKINTERNAL
|
---|
43 | {
|
---|
44 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
45 | uint32_t volatile u32Magic;
|
---|
46 | /** The Darwin spinlock structure. */
|
---|
47 | lck_spin_t *pSpinLock;
|
---|
48 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Allocate.
|
---|
56 | */
|
---|
57 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
58 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
59 | if (!pSpinlockInt)
|
---|
60 | return VERR_NO_MEMORY;
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Initialize & return.
|
---|
64 | */
|
---|
65 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
66 | Assert(g_pDarwinLockGroup);
|
---|
67 | pSpinlockInt->pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
68 | if (!pSpinlockInt->pSpinLock)
|
---|
69 | {
|
---|
70 | RTMemFree(pSpinlockInt);
|
---|
71 | return VERR_NO_MEMORY;
|
---|
72 | }
|
---|
73 |
|
---|
74 | *pSpinlock = pSpinlockInt;
|
---|
75 | return VINF_SUCCESS;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Validate input.
|
---|
83 | */
|
---|
84 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
85 | if (!pSpinlockInt)
|
---|
86 | return VERR_INVALID_PARAMETER;
|
---|
87 | AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
88 | ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
|
---|
89 | VERR_INVALID_PARAMETER);
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Make the lock invalid and release the memory.
|
---|
93 | */
|
---|
94 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
95 |
|
---|
96 | Assert(g_pDarwinLockGroup);
|
---|
97 | lck_spin_destroy(pSpinlockInt->pSpinLock, g_pDarwinLockGroup);
|
---|
98 | pSpinlockInt->pSpinLock = NULL;
|
---|
99 |
|
---|
100 | RTMemFree(pSpinlockInt);
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
106 | {
|
---|
107 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
108 | AssertPtr(pSpinlockInt);
|
---|
109 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
110 |
|
---|
111 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
112 |
|
---|
113 | pTmp->uFlags = ASMGetFlags();
|
---|
114 | ASMIntDisable();
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
119 | {
|
---|
120 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
121 | AssertPtr(pSpinlockInt);
|
---|
122 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
123 |
|
---|
124 | ASMSetFlags(pTmp->uFlags);
|
---|
125 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
130 | {
|
---|
131 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
132 | AssertPtr(pSpinlockInt);
|
---|
133 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
134 |
|
---|
135 | lck_spin_lock(pSpinlockInt->pSpinLock);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
140 | {
|
---|
141 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
142 | AssertPtr(pSpinlockInt);
|
---|
143 | Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
144 |
|
---|
145 | lck_spin_unlock(pSpinlockInt->pSpinLock);
|
---|
146 | }
|
---|
147 |
|
---|