VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/os2/sems-os2.cpp@ 25717

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

iprt: Added RTSemEventCreateEx and did some cleanups of the RTSemEventDestroy behavior wrt NIL handles.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1/* $Id: sems-os2.cpp 25717 2010-01-11 13:24:09Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, OS/2.
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.215389.xyz. 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 INCL_DOSSEMAPHORES
36#define INCL_ERRORS
37#include <os2.h>
38#undef RT_MAX
39
40#include <iprt/semaphore.h>
41#include <iprt/assert.h>
42#include <iprt/err.h>
43
44
45/** Converts semaphore to OS/2 handle. */
46#define SEM2HND(Sem) ((LHANDLE)(uintptr_t)Sem)
47
48
49
50RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
51{
52 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
53}
54
55
56RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
57{
58 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
59
60 /*
61 * Create the semaphore.
62 * (Auto reset, not signaled, private event object.)
63 */
64 HEV hev;
65 int rc = DosCreateEventSem(NULL, &hev, DCE_AUTORESET | DCE_POSTONE, 0);
66 if (!rc)
67 {
68 *phEventSem = (RTSEMEVENT)(void *)hev;
69 return VINF_SUCCESS;
70 }
71 return RTErrConvertFromOS2(rc);
72}
73
74
75RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
76{
77 if (hEventSem == NIL_RTSEMEVENT)
78 return VINF_SUCCESS;
79
80 /*
81 * Close semaphore handle.
82 */
83 int rc = DosCloseEventSem(SEM2HND(hEventSem));
84 if (!rc)
85 return VINF_SUCCESS;
86 AssertMsgFailed(("Destroy hEventSem %p failed, rc=%d\n", hEventSem, rc));
87 return RTErrConvertFromOS2(rc);
88}
89
90
91RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, unsigned cMillies)
92{
93 /*
94 * Wait for condition.
95 */
96 int rc = DosWaitEventSem(SEM2HND(hEventSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
97 switch (rc)
98 {
99 case NO_ERROR: return VINF_SUCCESS;
100 case ERROR_SEM_TIMEOUT:
101 case ERROR_TIMEOUT: return VERR_TIMEOUT;
102 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
103 default:
104 {
105 AssertMsgFailed(("Wait on hEventSem %p failed, rc=%d\n", hEventSem, rc));
106 return RTErrConvertFromOS2(rc);
107 }
108 }
109}
110
111
112RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
113{
114 /*
115 * Signal the object.
116 */
117 int rc = DosPostEventSem(SEM2HND(hEventSem));
118 switch (rc)
119 {
120 case NO_ERROR:
121 case ERROR_ALREADY_POSTED:
122 case ERROR_TOO_MANY_POSTS:
123 return VINF_SUCCESS;
124 default:
125 return RTErrConvertFromOS2(rc);
126 }
127}
128
129
130RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
131{
132/** @todo implement RTSemEventSetSignaller and friends for OS/2 */
133}
134
135
136RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
137{
138
139}
140
141
142RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
143{
144
145}
146
147
148
149
150RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
151{
152 /*
153 * Create the semaphore.
154 * (Manual reset, not signaled, private event object.)
155 */
156 HEV hev;
157 int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
158 if (!rc)
159 {
160 *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
161 return VINF_SUCCESS;
162 }
163 return RTErrConvertFromOS2(rc);
164}
165
166
167RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
168{
169 /*
170 * Close semaphore handle.
171 */
172 int rc = DosCloseEventSem(SEM2HND(EventMultiSem));
173 if (!rc)
174 return VINF_SUCCESS;
175 AssertMsgFailed(("Destroy EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
176 return RTErrConvertFromOS2(rc);
177}
178
179
180RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
181{
182 /*
183 * Signal the object.
184 */
185 int rc = DosPostEventSem(SEM2HND(EventMultiSem));
186 switch (rc)
187 {
188 case NO_ERROR:
189 case ERROR_ALREADY_POSTED:
190 case ERROR_TOO_MANY_POSTS:
191 return VINF_SUCCESS;
192 default:
193 return RTErrConvertFromOS2(rc);
194 }
195}
196
197
198RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
199{
200 /*
201 * Reset the object.
202 */
203 ULONG ulIgnore;
204 int rc = DosResetEventSem(SEM2HND(EventMultiSem), &ulIgnore);
205 switch (rc)
206 {
207 case NO_ERROR:
208 case ERROR_ALREADY_RESET:
209 return VINF_SUCCESS;
210 default:
211 return RTErrConvertFromOS2(rc);
212 }
213}
214
215
216RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
217{
218 /*
219 * Wait for condition.
220 */
221 int rc = DosWaitEventSem(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
222 switch (rc)
223 {
224 case NO_ERROR: return VINF_SUCCESS;
225 case ERROR_SEM_TIMEOUT:
226 case ERROR_TIMEOUT: return VERR_TIMEOUT;
227 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
228 default:
229 {
230 AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
231 return RTErrConvertFromOS2(rc);
232 }
233 }
234}
235
236
237
238
239#undef RTSemMutexCreate
240RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
241{
242 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
243}
244
245
246RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
247 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
248{
249 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
250
251 /*
252 * Create the semaphore.
253 */
254 HMTX hmtx;
255 int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
256 if (!rc)
257 {
258 /** @todo implement lock validation of OS/2 mutex semaphores. */
259 *phMutexSem = (RTSEMMUTEX)(void *)hmtx;
260 return VINF_SUCCESS;
261 }
262
263 return RTErrConvertFromOS2(rc);
264}
265
266
267RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
268{
269 /*
270 * Close semaphore handle.
271 */
272 int rc = DosCloseMutexSem(SEM2HND(MutexSem));
273 if (!rc)
274 return VINF_SUCCESS;
275 AssertMsgFailed(("Destroy MutexSem %p failed, rc=%d\n", MutexSem, rc));
276 return RTErrConvertFromOS2(rc);
277}
278
279
280
281RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
282{
283#if 0 /** @todo def RTSEMMUTEX_STRICT */
284 /*
285 * Validate.
286 */
287 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
288 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
289 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
290
291 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
292#else
293 return RTLOCKVAL_SUB_CLASS_INVALID;
294#endif
295}
296
297
298#undef RTSemMutexRequestNoResume
299RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
300{
301 /*
302 * Lock mutex semaphore.
303 */
304 int rc = DosRequestMutexSem(SEM2HND(MutexSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
305 switch (rc)
306 {
307 case NO_ERROR: return VINF_SUCCESS;
308 case ERROR_SEM_TIMEOUT:
309 case ERROR_TIMEOUT: return VERR_TIMEOUT;
310 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
311 case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
312 default:
313 {
314 AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d\n", MutexSem, rc));
315 return RTErrConvertFromOS2(rc);
316 }
317 }
318}
319
320RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
321{
322 /*
323 * Unlock mutex semaphore.
324 */
325 int rc = DosReleaseMutexSem(SEM2HND(MutexSem));
326 if (!rc)
327 return VINF_SUCCESS;
328 AssertMsgFailed(("Release MutexSem %p failed, rc=%d\n", MutexSem, rc));
329 return RTErrConvertFromOS2(rc);
330}
331
332
333RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutex);
334{
335 /*
336 * Unlock mutex semaphore.
337 */
338 PID pid;
339 TID tid;
340 ULONG cRecursions;
341 int rc = DosQueryMutexSem(SEM2HND(MutexSem), &pid, &tid, &cRecursions);
342 if (!rc)
343 return cRecursions != 0;
344 AssertMsgFailed(("DosQueryMutexSem %p failed, rc=%d\n", MutexSem, rc));
345 return rc == ERROR_SEM_OWNER_DIED;
346}
347
348
349RTDECL(void) RTSemEventMultiSetSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
350{
351 /** @todo implement RTSemEventMultiSetSignaller on OS/2 */
352}
353
354
355RTDECL(void) RTSemEventMultiAddSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
356{
357}
358
359
360RTDECL(void) RTSemEventMultiRemoveSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
361{
362}
363
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