VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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