VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/spinlock-r0drv-darwin.cpp@ 61572

Last change on this file since 61572 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: spinlock-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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/spinlock.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/thread.h>
43
44#include "internal/magics.h"
45
46
47/*********************************************************************************************************************************
48* Structures and Typedefs *
49*********************************************************************************************************************************/
50/**
51 * Wrapper for the KSPIN_LOCK type.
52 */
53typedef struct RTSPINLOCKINTERNAL
54{
55 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
56 uint32_t volatile u32Magic;
57 /** Saved interrupt flag. */
58 uint32_t volatile fIntSaved;
59 /** Creation flags. */
60 uint32_t fFlags;
61 /** The Darwin spinlock structure. */
62 lck_spin_t *pSpinLock;
63 /** The spinlock name. */
64 const char *pszName;
65} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
66
67
68
69RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
70{
71 RT_ASSERT_PREEMPTIBLE();
72 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
73 IPRT_DARWIN_SAVE_EFL_AC();
74
75 /*
76 * Allocate.
77 */
78 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
79 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
80 if (pThis)
81 {
82 /*
83 * Initialize & return.
84 */
85 pThis->u32Magic = RTSPINLOCK_MAGIC;
86 pThis->fIntSaved = 0;
87 pThis->fFlags = fFlags;
88 pThis->pszName = pszName;
89 Assert(g_pDarwinLockGroup);
90 pThis->pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
91 if (pThis->pSpinLock)
92 {
93 *pSpinlock = pThis;
94 IPRT_DARWIN_RESTORE_EFL_AC();
95 return VINF_SUCCESS;
96 }
97
98 RTMemFree(pThis);
99 }
100 IPRT_DARWIN_RESTORE_EFL_AC();
101 return VERR_NO_MEMORY;
102}
103
104
105RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
106{
107 /*
108 * Validate input.
109 */
110 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
111 if (!pThis)
112 return VERR_INVALID_PARAMETER;
113 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
114 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
115 VERR_INVALID_PARAMETER);
116
117 /*
118 * Make the lock invalid and release the memory.
119 */
120 ASMAtomicIncU32(&pThis->u32Magic);
121 IPRT_DARWIN_SAVE_EFL_AC();
122
123 Assert(g_pDarwinLockGroup);
124 lck_spin_free(pThis->pSpinLock, g_pDarwinLockGroup);
125 pThis->pSpinLock = NULL;
126
127 RTMemFree(pThis);
128
129 IPRT_DARWIN_RESTORE_EFL_AC();
130 return VINF_SUCCESS;
131}
132
133
134RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
135{
136 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
137 AssertPtr(pThis);
138 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
139
140 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
141 {
142 uint32_t fIntSaved = ASMGetFlags();
143 ASMIntDisable();
144 lck_spin_lock(pThis->pSpinLock);
145 pThis->fIntSaved = fIntSaved;
146 IPRT_DARWIN_RESTORE_EFL_ONLY_AC_EX(fIntSaved);
147 }
148 else
149 {
150 IPRT_DARWIN_SAVE_EFL_AC();
151 lck_spin_lock(pThis->pSpinLock);
152 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
153 }
154}
155
156
157RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
158{
159 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
160 AssertPtr(pThis);
161 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
162
163 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
164 {
165 uint32_t fIntSaved = pThis->fIntSaved;
166 pThis->fIntSaved = 0;
167 lck_spin_unlock(pThis->pSpinLock);
168 ASMSetFlags(fIntSaved);
169 }
170 else
171 {
172 IPRT_DARWIN_SAVE_EFL_AC();
173 lck_spin_unlock(pThis->pSpinLock);
174 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
175 }
176}
177
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