VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/spinlock-r0drv-os2.cpp@ 40806

最後變更 在這個檔案從40806是 40806,由 vboxsync 提交於 13 年 前

RTSpinlock: Redid the interface, eliminating NoInts and Tmp. Whether a spinlock is interrupt safe or not is now defined at creation time, preventing stupid bugs arrising from calling the wrong acquire and/or release methods somewhere. The saved flags are stored in the spinlock strucutre, eliminating the annoying Tmp variable. Needs testing on each platform before fixing the build burn.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 4.3 KB
 
1/* $Id: spinlock-r0drv-os2.cpp 40806 2012-04-06 21:05:19Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, OS/2.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-os2-kernel.h"
35
36#include <iprt/spinlock.h>
37#include <iprt/err.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41
42#include "internal/magics.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * Wrapper for the SpinLock_t type.
50 */
51typedef struct RTSPINLOCKINTERNAL
52{
53 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
54 uint32_t volatile u32Magic;
55 /** Spinlock creation flags. */
56 uint32_t fFlags;
57 /** The OS/2 spinlock structure. */
58 SpinLock_t Spinlock;
59} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
60
61
62RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
63{
64 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
65
66 /*
67 * Allocate.
68 */
69 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
70 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
71 if (!pThis)
72 return VERR_NO_MEMORY;
73
74 /*
75 * Initialize & return.
76 */
77 pThis->u32Magic = RTSPINLOCK_MAGIC;
78 pThis->fFlags = fFlags;
79 pThis->fIntSaved = 0;
80 KernAllocSpinLock(&pThis->Spinlock);
81 *pSpinlock = pThis;
82 return VINF_SUCCESS;
83}
84
85
86RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
87{
88 /*
89 * Validate input.
90 */
91 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
92 if (!pThis)
93 return VERR_INVALID_PARAMETER;
94 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
95 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
96 VERR_INVALID_PARAMETER);
97
98 /*
99 * Make the lock invalid and release the memory.
100 */
101 ASMAtomicIncU32(&pThis->u32Magic);
102 KernFreeSpinLock(&pThis->Spinlock);
103 RTMemFree(pThis);
104 return VINF_SUCCESS;
105}
106
107
108RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
109{
110 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
111 AssertPtr(pThis);
112 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
113
114 KernAcquireSpinLock(&pThis->Spinlock);
115 Assert(!ASMIntAreEnabled()); /** @todo verify that interrupts are disabled. */
116}
117
118
119RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
120{
121 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
122 AssertPtr(pThis);
123 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
124
125 KernReleaseSpinLock(&pThis->Spinlock);
126}
127
128
129RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
130{
131#if 1
132 if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
133 RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
134#else
135 AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
136#endif
137 RTSpinlockRelease(Spinlock);
138}
139
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette