VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/spinlock-r0drv-nt.cpp@ 1507

Last change on this file since 1507 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: spinlock-r0drv-nt.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Spinlocks, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include "the-nt-kernel.h"
27#include <iprt/spinlock.h>
28#include <iprt/err.h>
29#include <iprt/alloc.h>
30#include <iprt/assert.h>
31#include <iprt/asm.h>
32
33
34/*******************************************************************************
35* Structures and Typedefs *
36*******************************************************************************/
37/**
38 * Wrapper for the KSPIN_LOCK type.
39 */
40typedef struct RTSPINLOCKINTERNAL
41{
42 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
43 uint32_t volatile u32Magic;
44 /** The NT spinlock structure. */
45 KSPIN_LOCK Spinlock;
46} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
47
48/** Magic value for RTSPINLOCKINTERNAL::u32Magic. (Terry Pratchett) */
49#define RTSPINLOCK_MAGIC 0x19480428
50
51
52
53RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
54{
55 /*
56 * Allocate.
57 */
58 Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
59 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
60 if (!pSpinlockInt)
61 return VERR_NO_MEMORY;
62
63 /*
64 * Initialize & return.
65 */
66 pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
67 KeInitializeSpinLock(&pSpinlockInt->Spinlock);
68 Assert(sizeof(KIRQL) == sizeof(unsigned char));
69
70 *pSpinlock = pSpinlockInt;
71 return VINF_SUCCESS;
72}
73
74
75RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
76{
77 /*
78 * Validate input.
79 */
80 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
81 if (!pSpinlockInt)
82 return VERR_INVALID_PARAMETER;
83 if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
84 {
85 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
86 return VERR_INVALID_PARAMETER;
87 }
88
89 ASMAtomicIncU32(&pSpinlockInt->u32Magic);
90 RTMemFree(pSpinlockInt);
91 return VINF_SUCCESS;
92}
93
94
95RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
96{
97 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
98 Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
99
100 KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
101 pTmp->uFlags = ASMGetFlags();
102 ASMIntDisable();
103}
104
105
106RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
107{
108 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
109 Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
110
111 ASMSetFlags(pTmp->uFlags);
112 KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
113}
114
115
116RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
117{
118 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
119 Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
120
121 KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
122}
123
124
125RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
126{
127 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
128 Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
129
130 KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
131}
132
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