VirtualBox

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

Last change on this file since 7348 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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