1 | /* $Id: semfastmutex-r0drv-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fast Mutex Semaphores, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Header Files *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #include "the-nt-kernel.h"
|
---|
43 | #include <iprt/semaphore.h>
|
---|
44 | #include <iprt/alloc.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/asm.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 |
|
---|
49 | #include "internal/magics.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /**
|
---|
56 | * Wrapper for the linux semaphore structure.
|
---|
57 | */
|
---|
58 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
59 | {
|
---|
60 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
61 | uint32_t u32Magic;
|
---|
62 | /** the NT fast mutex. */
|
---|
63 | FAST_MUTEX Mutex;
|
---|
64 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
69 | {
|
---|
70 | /*
|
---|
71 | * Allocate.
|
---|
72 | */
|
---|
73 | PRTSEMFASTMUTEXINTERNAL pThis;
|
---|
74 | Assert(sizeof(*pThis) > sizeof(void *));
|
---|
75 | pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
76 | if (!pThis)
|
---|
77 | return VERR_NO_MEMORY;
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Initialize.
|
---|
81 | */
|
---|
82 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
83 | ExInitializeFastMutex(&pThis->Mutex);
|
---|
84 |
|
---|
85 | *phFastMtx = pThis;
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
91 | {
|
---|
92 | /*
|
---|
93 | * Validate.
|
---|
94 | */
|
---|
95 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
96 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
97 | return VINF_SUCCESS;
|
---|
98 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
99 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
100 |
|
---|
101 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
102 | Assert(pThis->Mutex.Count == 1);
|
---|
103 | RTMemFree(pThis);
|
---|
104 | return VINF_SUCCESS;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Validate.
|
---|
112 | */
|
---|
113 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
114 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
115 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
116 |
|
---|
117 | #if 1
|
---|
118 | /*
|
---|
119 | * ExAcquireFastMutex will set the IRQL to APC regardless of our current
|
---|
120 | * level. Lowering the IRQL may screw things up, so do not allow this.
|
---|
121 | */
|
---|
122 | # if 0 /** @todo enable this when the logger has been fixed. */
|
---|
123 | AssertMsg(KeGetCurrentIrql() <= APC_LEVEL, ("%d\n", KeGetCurrentIrql()), VERR_INVALID_STATE);
|
---|
124 | # else /* the gentler approach. */
|
---|
125 | KIRQL Irql = KeGetCurrentIrql();
|
---|
126 | if (Irql > APC_LEVEL)
|
---|
127 | return VERR_INVALID_STATE;
|
---|
128 | # endif
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | ExAcquireFastMutex(&pThis->Mutex);
|
---|
132 | return VINF_SUCCESS;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
137 | {
|
---|
138 | /*
|
---|
139 | * Validate.
|
---|
140 | */
|
---|
141 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
142 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
143 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
144 |
|
---|
145 | ExReleaseFastMutex(&pThis->Mutex);
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 |
|
---|