VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/semmutex-r0drv-nt.cpp@ 25373

Last change on this file since 25373 was 25373, checked in by vboxsync, 15 years ago

IPRT,PDMCritSect: More validation changes. Validate posix and linux mutexes. Always update the thread state with critsects.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/* $Id: semmutex-r0drv-nt.cpp 25373 2009-12-14 19:20:27Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, NT.
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/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include "the-nt-kernel.h"
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * NT mutex semaphore.
51 */
52typedef struct RTSEMMUTEXINTERNAL
53{
54 /** Magic value (RTSEMMUTEX_MAGIC). */
55 uint32_t volatile u32Magic;
56#ifdef RT_USE_FAST_MUTEX
57 /** The fast mutex object. */
58 FAST_MUTEX Mutex;
59#else
60 /** The NT Mutex object. */
61 KMUTEX Mutex;
62#endif
63} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
64
65
66
67RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
68{
69 Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
70 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
71 if (pMutexInt)
72 {
73 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
74#ifdef RT_USE_FAST_MUTEX
75 ExInitializeFastMutex(&pMutexInt->Mutex);
76#else
77 KeInitializeMutex(&pMutexInt->Mutex, 0);
78#endif
79 *pMutexSem = pMutexInt;
80 return VINF_SUCCESS;
81 }
82 return VERR_NO_MEMORY;
83}
84
85
86RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
87{
88 /*
89 * Validate input.
90 */
91 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
92 if (!pMutexInt)
93 return VERR_INVALID_PARAMETER;
94 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
95 {
96 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
97 return VERR_INVALID_PARAMETER;
98 }
99
100 /*
101 * Invalidate it and signal the object just in case.
102 */
103 ASMAtomicIncU32(&pMutexInt->u32Magic);
104 RTMemFree(pMutexInt);
105 return VINF_SUCCESS;
106}
107
108
109RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
110{
111 /*
112 * Validate input.
113 */
114 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
115 if (!pMutexInt)
116 return VERR_INVALID_PARAMETER;
117 if ( !pMutexInt
118 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
119 {
120 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
121 return VERR_INVALID_PARAMETER;
122 }
123
124 /*
125 * Get the mutex.
126 */
127#ifdef RT_USE_FAST_MUTEX
128 AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
129 ExAcquireFastMutex(&pMutexInt->Mutex);
130#else
131 NTSTATUS rcNt;
132 if (cMillies == RT_INDEFINITE_WAIT)
133 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
134 else
135 {
136 LARGE_INTEGER Timeout;
137 Timeout.QuadPart = -(int64_t)cMillies * 10000;
138 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
139 }
140 switch (rcNt)
141 {
142 case STATUS_SUCCESS:
143 if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
144 return VINF_SUCCESS;
145 return VERR_SEM_DESTROYED;
146 case STATUS_ALERTED:
147 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
148 case STATUS_USER_APC:
149 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
150 case STATUS_TIMEOUT:
151 return VERR_TIMEOUT;
152 default:
153 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
154 pMutexInt->u32Magic, pMutexInt, (long)rcNt));
155 return VERR_INTERNAL_ERROR;
156 }
157#endif
158 return VINF_SUCCESS;
159}
160
161
162RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
163{
164 return RTSemMutexRequest(MutexSem, cMillies);
165}
166
167/** @todo implement the NoResume versions */
168
169RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
170{
171 /*
172 * Validate input.
173 */
174 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
175 if (!pMutexInt)
176 return VERR_INVALID_PARAMETER;
177 if ( !pMutexInt
178 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
179 {
180 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
181 return VERR_INVALID_PARAMETER;
182 }
183
184 /*
185 * Release the mutex.
186 */
187#ifdef RT_USE_FAST_MUTEX
188 ExReleaseFastMutex(&pMutexInt->Mutex);
189#else
190 KeReleaseMutex(&pMutexInt->Mutex, FALSE);
191#endif
192 return VINF_SUCCESS;
193}
194
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