1 | /* $Id: spinlock-r0drv-linux.c 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Linux.
|
---|
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 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-linux-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 | #include <linux/spinlock.h> /** @todo why is this here and not in the-linux-kernel.h? */
|
---|
45 |
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *******************************************************************************/
|
---|
50 | /**
|
---|
51 | * Wrapper for the spinlock_t structure.
|
---|
52 | */
|
---|
53 | typedef struct RTSPINLOCKINTERNAL
|
---|
54 | {
|
---|
55 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** The linux spinlock structure. */
|
---|
58 | spinlock_t Spinlock;
|
---|
59 | #if !defined(CONFIG_SMP) || defined(RT_ARCH_AMD64)
|
---|
60 | /** A placeholder on Uni systems so we won't piss off RTMemAlloc(). */
|
---|
61 | void *pvUniDummy;
|
---|
62 | #endif
|
---|
63 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
68 | {
|
---|
69 | /*
|
---|
70 | * Allocate.
|
---|
71 | */
|
---|
72 | PRTSPINLOCKINTERNAL pSpinlockInt;
|
---|
73 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
74 | pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
75 | if (!pSpinlockInt)
|
---|
76 | return VERR_NO_MEMORY;
|
---|
77 | /*
|
---|
78 | * Initialize and return.
|
---|
79 | */
|
---|
80 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
81 | spin_lock_init(&pSpinlockInt->Spinlock);
|
---|
82 |
|
---|
83 | *pSpinlock = pSpinlockInt;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(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 |
|
---|
108 | RTDECL(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 | NOREF(pSpinlockInt);
|
---|
114 |
|
---|
115 | spin_lock_irqsave(&pSpinlockInt->Spinlock, pTmp->flFlags);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
120 | {
|
---|
121 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
122 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
123 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
124 | NOREF(pSpinlockInt);
|
---|
125 |
|
---|
126 | spin_unlock_irqrestore(&pSpinlockInt->Spinlock, pTmp->flFlags);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
131 | {
|
---|
132 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
133 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
134 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
135 | NOREF(pSpinlockInt); NOREF(pTmp);
|
---|
136 |
|
---|
137 | spin_lock(&pSpinlockInt->Spinlock);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
142 | {
|
---|
143 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
144 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
145 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
146 | NOREF(pSpinlockInt); NOREF(pTmp);
|
---|
147 |
|
---|
148 | spin_unlock(&pSpinlockInt->Spinlock);
|
---|
149 | }
|
---|
150 |
|
---|