VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/sems-win.cpp@ 25240

Last change on this file since 25240 was 10839, checked in by vboxsync, 16 years ago

RTSemEventDestroy: Don't bitch on NIL_RTSEMEVENT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1/* $Id: sems-win.cpp 10839 2008-07-23 19:48:51Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, implementation for Windows host platform.
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* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_SEMAPHORE
36#include <Windows.h>
37
38#include <iprt/semaphore.h>
39#include <iprt/thread.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include "internal/strict.h"
43
44
45/*******************************************************************************
46* Defined Constants And Macros *
47*******************************************************************************/
48/** Converts semaphore to win32 handle. */
49#define SEM2HND(Sem) ((HANDLE)(uintptr_t)Sem)
50
51
52
53RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
54{
55 /*
56 * Create the semaphore.
57 * (Auto reset, not signaled, private event object.)
58 */
59 HANDLE hev = CreateEvent(NULL, FALSE, FALSE, NULL);
60 if (hev)
61 {
62 *pEventSem = (RTSEMEVENT)(void *)hev;
63 Assert(*pEventSem != NIL_RTSEMEVENT);
64 return VINF_SUCCESS;
65 }
66 return RTErrConvertFromWin32(GetLastError());
67}
68
69
70RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
71{
72 if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
73 return VERR_INVALID_HANDLE;
74
75 /*
76 * Close semaphore handle.
77 */
78 if (CloseHandle(SEM2HND(EventSem)))
79 return VINF_SUCCESS;
80 AssertMsgFailed(("Destroy EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
81 return RTErrConvertFromWin32(GetLastError());
82}
83
84
85RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
86{
87 /*
88 * Wait for condition.
89 */
90 int rc = WaitForSingleObjectEx(SEM2HND(EventSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
91 switch (rc)
92 {
93 case WAIT_OBJECT_0: return VINF_SUCCESS;
94 case WAIT_TIMEOUT: return VERR_TIMEOUT;
95 case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
96 case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
97 default:
98 {
99 AssertMsgFailed(("Wait on EventSem %p failed, rc=%d lasterr=%d\n", EventSem, rc, GetLastError()));
100 int rc2 = RTErrConvertFromWin32(GetLastError());
101 if (rc2)
102 return rc2;
103
104 AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
105 return VERR_INTERNAL_ERROR;
106 }
107 }
108}
109
110
111RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
112{
113 /*
114 * Signal the object.
115 */
116 if (SetEvent(SEM2HND(EventSem)))
117 return VINF_SUCCESS;
118 AssertMsgFailed(("Signaling EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
119 return RTErrConvertFromWin32(GetLastError());
120}
121
122
123
124
125RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
126{
127 /*
128 * Create the semaphore.
129 * (Manual reset, not signaled, private event object.)
130 */
131 HANDLE hev = CreateEvent(NULL, TRUE, FALSE, NULL);
132 if (hev)
133 {
134 *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
135 return VINF_SUCCESS;
136 }
137 return RTErrConvertFromWin32(GetLastError());
138}
139
140
141RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
142{
143 /*
144 * Close semaphore handle.
145 */
146 if (CloseHandle(SEM2HND(EventMultiSem)))
147 return VINF_SUCCESS;
148 AssertMsgFailed(("Destroy EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
149 return RTErrConvertFromWin32(GetLastError());
150}
151
152
153RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
154{
155 /*
156 * Signal the object.
157 */
158 if (SetEvent(SEM2HND(EventMultiSem)))
159 return VINF_SUCCESS;
160 AssertMsgFailed(("Signaling EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
161 return RTErrConvertFromWin32(GetLastError());
162}
163
164
165RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
166{
167 /*
168 * Reset the object.
169 */
170 if (ResetEvent(SEM2HND(EventMultiSem)))
171 return VINF_SUCCESS;
172 AssertMsgFailed(("Resetting EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
173 return RTErrConvertFromWin32(GetLastError());
174}
175
176
177RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
178{
179 /*
180 * Wait for condition.
181 */
182 int rc = WaitForSingleObjectEx(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
183 switch (rc)
184 {
185 case WAIT_OBJECT_0: return VINF_SUCCESS;
186 case WAIT_TIMEOUT: return VERR_TIMEOUT;
187 case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
188 case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
189 default:
190 {
191 AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d lasterr=%d\n", EventMultiSem, rc, GetLastError()));
192 int rc2 = RTErrConvertFromWin32(GetLastError());
193 if (rc2)
194 return rc2;
195
196 AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
197 return VERR_INTERNAL_ERROR;
198 }
199 }
200}
201
202
203
204
205RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
206{
207 /*
208 * Create the semaphore.
209 */
210 HANDLE hmtx = CreateMutex(NULL, FALSE, NULL);
211 if (hmtx)
212 {
213 *pMutexSem = (RTSEMMUTEX)(void *)hmtx;
214 return VINF_SUCCESS;
215 }
216
217 return RTErrConvertFromWin32(GetLastError());
218}
219
220
221RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
222{
223 /*
224 * Close semaphore handle.
225 */
226 if (CloseHandle(SEM2HND(MutexSem)))
227 return VINF_SUCCESS;
228 AssertMsgFailed(("Destroy MutexSem %p failed, lasterr=%d\n", MutexSem, GetLastError()));
229 return RTErrConvertFromWin32(GetLastError());
230}
231
232
233RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
234{
235 /*
236 * Lock mutex semaphore.
237 */
238 int rc = WaitForSingleObjectEx(SEM2HND(MutexSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
239 switch (rc)
240 {
241 case WAIT_OBJECT_0:
242 {
243#ifdef RTSEMMUTEX_STRICT
244 RTTHREAD Thread = RTThreadSelf();
245 if (Thread != NIL_RTTHREAD)
246 RTThreadWriteLockInc(Thread);
247#endif
248 return VINF_SUCCESS;
249 }
250
251 case WAIT_TIMEOUT: return VERR_TIMEOUT;
252 case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
253 case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
254 default:
255 {
256 AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
257 int rc2 = RTErrConvertFromWin32(GetLastError());
258 if (rc2 != 0)
259 return rc2;
260
261 AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
262 return VERR_INTERNAL_ERROR;
263 }
264 }
265}
266
267RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
268{
269 /*
270 * Unlock mutex semaphore.
271 */
272#ifdef RTSEMMUTEX_STRICT
273 RTTHREAD Thread = RTThreadSelf();
274 if (Thread != NIL_RTTHREAD)
275 RTThreadWriteLockDec(Thread);
276#endif
277 if (ReleaseMutex(SEM2HND(MutexSem)))
278 return VINF_SUCCESS;
279
280#ifdef RTSEMMUTEX_STRICT
281 if (Thread != NIL_RTTHREAD)
282 RTThreadWriteLockInc(Thread);
283#endif
284 AssertMsgFailed(("Release MutexSem %p failed, lasterr=%d\n", MutexSem, GetLastError()));
285 return RTErrConvertFromWin32(GetLastError());
286}
287
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