VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/spinlock-generic.cpp@ 12559

Last change on this file since 12559 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/* $Id: spinlock-generic.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Spinlock, generic implementation.
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* Defined Constants And Macros *
34*******************************************************************************/
35/** @def RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
36 * Force cpu yields after spinning the number of times indicated by the define.
37 * If 0 we will spin forever. */
38#define RT_CFG_SPINLOCK_GENERIC_DO_SLEEP 100000
39
40
41/*******************************************************************************
42* Header Files *
43*******************************************************************************/
44#include <iprt/spinlock.h>
45#include <iprt/alloc.h>
46#include <iprt/asm.h>
47#include <iprt/err.h>
48#include <iprt/assert.h>
49#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
50# include <iprt/thread.h>
51#endif
52
53#include "internal/magics.h"
54
55
56/*******************************************************************************
57* Structures and Typedefs *
58*******************************************************************************/
59/**
60 * Generic spinlock structure.
61 */
62typedef struct RTSPINLOCKINTERNAL
63{
64 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
65 uint32_t u32Magic;
66 /** The spinlock. */
67 uint32_t volatile fLocked;
68} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
69
70
71RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
72{
73 /*
74 * Allocate.
75 */
76 PRTSPINLOCKINTERNAL pSpinlockInt;
77 pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
78 if (!pSpinlockInt)
79 return VERR_NO_MEMORY;
80
81 /*
82 * Initialize and return.
83 */
84 pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
85 ASMAtomicXchgU32(&pSpinlockInt->fLocked, 0);
86
87 *pSpinlock = pSpinlockInt;
88 return VINF_SUCCESS;
89}
90
91
92RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
93{
94 /*
95 * Validate input.
96 */
97 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
98 if (!pSpinlockInt)
99 return VERR_INVALID_PARAMETER;
100 if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
101 {
102 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
103 return VERR_INVALID_PARAMETER;
104 }
105
106 ASMAtomicIncU32(&pSpinlockInt->u32Magic);
107 RTMemFree(pSpinlockInt);
108 return VINF_SUCCESS;
109}
110
111
112RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
113{
114 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
115 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
116 ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
117
118 pTmp->uFlags = ASMGetFlags();
119#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
120 for (;;)
121 {
122 ASMIntDisable();
123 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
124 if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
125 return;
126 RTThreadYield();
127 }
128#else
129 ASMIntDisable();
130 while (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
131 /*nothing */;
132#endif
133}
134
135
136RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
137{
138 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
139 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
140 ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
141 NOREF(pSpinlockInt);
142
143 if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
144 AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
145 ASMSetFlags(pTmp->uFlags);
146}
147
148
149RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
150{
151 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
152 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
153 ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
154 NOREF(pTmp);
155
156#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
157 for (;;)
158 {
159 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
160 if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
161 return;
162 RTThreadYield();
163 }
164#else
165 while (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
166 /*nothing */;
167#endif
168}
169
170
171RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
172{
173 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
174 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
175 ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
176 NOREF(pTmp);
177
178 if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
179 AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
180}
181
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