VirtualBox

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

Last change on this file since 8155 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

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