1 | /* $Id: spinlock-r0drv-os2.cpp 40871 2012-04-11 16:27:51Z 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 | */
|
---|
51 | typedef 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 |
|
---|
62 | RTDECL(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 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
80 | *pSpinlock = pThis;
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * Validate input.
|
---|
89 | */
|
---|
90 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
91 | if (!pThis)
|
---|
92 | return VERR_INVALID_PARAMETER;
|
---|
93 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
94 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
95 | VERR_INVALID_PARAMETER);
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Make the lock invalid and release the memory.
|
---|
99 | */
|
---|
100 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
101 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
102 | RTMemFree(pThis);
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
108 | {
|
---|
109 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
110 | AssertPtr(pThis);
|
---|
111 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
112 |
|
---|
113 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
114 | Assert(!ASMIntAreEnabled()); /** @todo verify that interrupts are disabled. */
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
119 | {
|
---|
120 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
121 | AssertPtr(pThis);
|
---|
122 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
123 |
|
---|
124 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
|
---|
129 | {
|
---|
130 | #if 1
|
---|
131 | if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
|
---|
132 | RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
|
---|
133 | #else
|
---|
134 | AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
|
---|
135 | #endif
|
---|
136 | RTSpinlockRelease(Spinlock);
|
---|
137 | }
|
---|
138 |
|
---|