VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/semmutex-win.cpp@ 26626

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

iprt/lockvalidation: give better names to anonymous locks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1/* $Id: semmutex-win.cpp 25831 2010-01-14 15:12:53Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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#define LOG_GROUP RTLOGGROUP_SEMAPHORE
36#include <Windows.h>
37
38#include <iprt/semaphore.h>
39#include "internal/iprt.h"
40
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/err.h>
44#include <iprt/lockvalidator.h>
45#include <iprt/mem.h>
46#include <iprt/thread.h>
47#include "internal/magics.h"
48#include "internal/strict.h"
49
50
51/*******************************************************************************
52* Defined Constants And Macros *
53*******************************************************************************/
54/** Posix internal representation of a Mutex semaphore. */
55struct RTSEMMUTEXINTERNAL
56{
57 /** Magic value (RTSEMMUTEX_MAGIC). */
58 uint32_t u32Magic;
59 /** Recursion count. */
60 uint32_t volatile cRecursions;
61 /** The owner thread. */
62 RTNATIVETHREAD volatile hNativeOwner;
63 /** The mutex handle. */
64 HANDLE hMtx;
65#ifdef RTSEMMUTEX_STRICT
66 /** Lock validator record associated with this mutex. */
67 RTLOCKVALRECEXCL ValidatorRec;
68#endif
69};
70
71
72
73#undef RTSemMutexCreate
74RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
75{
76 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
77}
78
79
80RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
81 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
82{
83 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
84
85 /*
86 * Create the semaphore.
87 */
88 int rc;
89 HANDLE hMtx = CreateMutex(NULL, FALSE, NULL);
90 if (hMtx)
91 {
92 RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
93 if (pThis)
94 {
95 pThis->u32Magic = RTSEMMUTEX_MAGIC;
96 pThis->hMtx = hMtx;
97 pThis->hNativeOwner = NIL_RTNATIVETHREAD;
98 pThis->cRecursions = 0;
99#ifdef RTSEMMUTEX_STRICT
100 if (!pszNameFmt)
101 {
102 static uint32_t volatile s_iMutexAnon = 0;
103 RTLockValidatorRecExclInit(&pThis->ValidatorRec, hClass, uSubClass, pThis,
104 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL),
105 "RTSemMutex-%u", ASMAtomicIncU32(&s_iMutexAnon) - 1);
106 }
107 else
108 {
109 va_list va;
110 va_start(va, pszNameFmt);
111 RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
112 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
113 va_end(va);
114 }
115#endif
116 *phMutexSem = pThis;
117 return VINF_SUCCESS;
118 }
119
120 rc = VERR_NO_MEMORY;
121 }
122 else
123 rc = RTErrConvertFromWin32(GetLastError());
124 return rc;
125}
126
127
128RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
129{
130 /*
131 * Validate.
132 */
133 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
134 if (pThis == NIL_RTSEMMUTEX)
135 return VINF_SUCCESS;
136 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
137 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
138
139 /*
140 * Close semaphore handle.
141 */
142 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
143 HANDLE hMtx = pThis->hMtx;
144 ASMAtomicWritePtr((void * volatile *)&pThis->hMtx, (void *)INVALID_HANDLE_VALUE);
145
146 int rc = VINF_SUCCESS;
147 if (!CloseHandle(hMtx))
148 {
149 rc = RTErrConvertFromWin32(GetLastError());
150 AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
151 }
152
153#ifdef RTSEMMUTEX_STRICT
154 RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
155#endif
156 RTMemFree(pThis);
157 return rc;
158}
159
160
161RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
162{
163#ifdef RTSEMMUTEX_STRICT
164 /*
165 * Validate.
166 */
167 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
168 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
169 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
170
171 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
172#else
173 return RTLOCKVAL_SUB_CLASS_INVALID;
174#endif
175}
176
177
178/**
179 * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
180 *
181 * @returns Same as RTSEmMutexRequestNoResume
182 * @param hMutexSem The mutex handle.
183 * @param cMillies The number of milliseconds to wait.
184 * @param pSrcPos The source position of the caller.
185 */
186DECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
187{
188 /*
189 * Validate.
190 */
191 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
192 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
193 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
194
195 /*
196 * Check for recursive entry.
197 */
198 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
199 RTNATIVETHREAD hNativeOwner;
200 ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
201 if (hNativeOwner == hNativeSelf)
202 {
203#ifdef RTSEMMUTEX_STRICT
204 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
205 if (RT_FAILURE(rc9))
206 return rc9;
207#endif
208 ASMAtomicIncU32(&pThis->cRecursions);
209 return VINF_SUCCESS;
210 }
211
212 /*
213 * Lock mutex semaphore.
214 */
215 RTTHREAD hThreadSelf = NIL_RTTHREAD;
216 if (cMillies > 0)
217 {
218#ifdef RTSEMMUTEX_STRICT
219 hThreadSelf = RTThreadSelfAutoAdopt();
220 int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
221 cMillies, RTTHREADSTATE_MUTEX, true);
222 if (RT_FAILURE(rc9))
223 return rc9;
224#else
225 hThreadSelf = RTThreadSelf();
226 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
227#endif
228 }
229 DWORD rc = WaitForSingleObjectEx(pThis->hMtx,
230 cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
231 TRUE /*fAlertable*/);
232 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
233 switch (rc)
234 {
235 case WAIT_OBJECT_0:
236#ifdef RTSEMMUTEX_STRICT
237 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
238#endif
239 ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
240 ASMAtomicWriteU32(&pThis->cRecursions, 1);
241 return VINF_SUCCESS;
242
243 case WAIT_TIMEOUT: return VERR_TIMEOUT;
244 case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
245 case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
246 default:
247 AssertMsgFailed(("%u\n", rc));
248 case WAIT_FAILED:
249 {
250 int rc2 = RTErrConvertFromWin32(GetLastError());
251 AssertMsgFailed(("Wait on hMutexSem %p failed, rc=%d lasterr=%d\n", hMutexSem, rc, GetLastError()));
252 if (rc2 != VINF_SUCCESS)
253 return rc2;
254
255 AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
256 return VERR_INTERNAL_ERROR;
257 }
258 }
259}
260
261
262#undef RTSemMutexRequestNoResume
263RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
264{
265#ifndef RTSEMMUTEX_STRICT
266 return rtSemMutexRequestNoResume(hMutexSem, cMillies, NULL);
267#else
268 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
269 return rtSemMutexRequestNoResume(hMutexSem, cMillies, &SrcPos);
270#endif
271}
272
273
274RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
275{
276 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
277 return rtSemMutexRequestNoResume(hMutexSem, cMillies, &SrcPos);
278}
279
280
281RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
282{
283 /*
284 * Validate.
285 */
286 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
287 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
288 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
289
290 /*
291 * Check ownership and recursions.
292 */
293 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
294 RTNATIVETHREAD hNativeOwner;
295 ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
296 if (RT_UNLIKELY(hNativeOwner != hNativeSelf))
297 {
298 AssertMsgFailed(("Not owner of mutex %p!! hNativeSelf=%RTntrd Owner=%RTntrd cRecursions=%d\n",
299 pThis, hNativeSelf, hNativeOwner, pThis->cRecursions));
300 return VERR_NOT_OWNER;
301 }
302 if (pThis->cRecursions > 1)
303 {
304#ifdef RTSEMMUTEX_STRICT
305 int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorRec);
306 if (RT_FAILURE(rc9))
307 return rc9;
308#endif
309 ASMAtomicDecU32(&pThis->cRecursions);
310 return VINF_SUCCESS;
311 }
312
313 /*
314 * Unlock mutex semaphore.
315 */
316#ifdef RTSEMMUTEX_STRICT
317 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, false);
318 if (RT_FAILURE(rc9))
319 return rc9;
320#endif
321 ASMAtomicWriteU32(&pThis->cRecursions, 0);
322 ASMAtomicWriteHandle(&pThis->hNativeOwner, NIL_RTNATIVETHREAD);
323
324 if (ReleaseMutex(pThis->hMtx))
325 return VINF_SUCCESS;
326
327 int rc = RTErrConvertFromWin32(GetLastError());
328 AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));
329 return rc;
330}
331
332
333RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
334{
335 /*
336 * Validate.
337 */
338 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
339 AssertPtrReturn(pThis, false);
340 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
341
342 RTNATIVETHREAD hNativeOwner;
343 ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
344 return hNativeOwner != NIL_RTNATIVETHREAD;
345}
346
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette