VirtualBox

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

Last change on this file since 4787 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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