1 | /* $Id: semfastmutex-r0drv-nt.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fast Mutex Semaphores, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include "the-nt-kernel.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 | #include <iprt/alloc.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 |
|
---|
39 | #include "internal/magics.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /**
|
---|
46 | * Wrapper for the linux semaphore structure.
|
---|
47 | */
|
---|
48 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
49 | {
|
---|
50 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
51 | uint32_t u32Magic;
|
---|
52 | /** the NT fast mutex. */
|
---|
53 | FAST_MUTEX Mutex;
|
---|
54 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Allocate.
|
---|
62 | */
|
---|
63 | PRTSEMFASTMUTEXINTERNAL pThis;
|
---|
64 | Assert(sizeof(*pThis) > sizeof(void *));
|
---|
65 | pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
66 | if (!pThis)
|
---|
67 | return VERR_NO_MEMORY;
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Initialize.
|
---|
71 | */
|
---|
72 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
73 | ExInitializeFastMutex(&pThis->Mutex);
|
---|
74 |
|
---|
75 | *phFastMtx = pThis;
|
---|
76 | return VINF_SUCCESS;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Validate.
|
---|
84 | */
|
---|
85 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
86 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
89 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
90 |
|
---|
91 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
92 | Assert(pThis->Mutex.Count == 1);
|
---|
93 | RTMemFree(pThis);
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * Validate.
|
---|
102 | */
|
---|
103 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
104 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
105 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
106 |
|
---|
107 | #if 1
|
---|
108 | /*
|
---|
109 | * ExAcquireFastMutex will set the IRQL to APC regardless of our current
|
---|
110 | * level. Lowering the IRQL may screw things up, so do not allow this.
|
---|
111 | */
|
---|
112 | # if 0 /** @todo enable this when the logger has been fixed. */
|
---|
113 | AssertMsg(KeGetCurrentIrql() <= APC_LEVEL, ("%d\n", KeGetCurrentIrql()), VERR_INVALID_STATE);
|
---|
114 | # else /* the gentler approach. */
|
---|
115 | KIRQL Irql = KeGetCurrentIrql();
|
---|
116 | if (Irql > APC_LEVEL)
|
---|
117 | return VERR_INVALID_STATE;
|
---|
118 | # endif
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | ExAcquireFastMutex(&pThis->Mutex);
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
127 | {
|
---|
128 | /*
|
---|
129 | * Validate.
|
---|
130 | */
|
---|
131 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
132 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
133 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
134 |
|
---|
135 | ExReleaseFastMutex(&pThis->Mutex);
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 |
|
---|