VirtualBox

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

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

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1/* $Id: sems-os2.cpp 25724 2010-01-11 14:45:34Z 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.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 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, RTMSINTERVAL 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 phEventMultiSem)
151{
152 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
153}
154
155
156RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
157 const char *pszNameFmt, ...)
158{
159 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
160
161 /*
162 * Create the semaphore.
163 * (Manual reset, not signaled, private event object.)
164 */
165 HEV hev;
166 int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
167 if (!rc)
168 {
169 *phEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
170 return VINF_SUCCESS;
171 }
172 return RTErrConvertFromOS2(rc);
173}
174
175
176RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
177{
178 if (hEventMultiSem == NIL_RTSEMEVENTMULTI)
179 return VINF_SUCCESS;
180
181 /*
182 * Close semaphore handle.
183 */
184 int rc = DosCloseEventSem(SEM2HND(hEventMultiSem));
185 if (!rc)
186 return VINF_SUCCESS;
187 AssertMsgFailed(("Destroy hEventMultiSem %p failed, rc=%d\n", hEventMultiSem, rc));
188 return RTErrConvertFromOS2(rc);
189}
190
191
192RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
193{
194 /*
195 * Signal the object.
196 */
197 int rc = DosPostEventSem(SEM2HND(hEventMultiSem));
198 switch (rc)
199 {
200 case NO_ERROR:
201 case ERROR_ALREADY_POSTED:
202 case ERROR_TOO_MANY_POSTS:
203 return VINF_SUCCESS;
204 default:
205 return RTErrConvertFromOS2(rc);
206 }
207}
208
209
210RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
211{
212 /*
213 * Reset the object.
214 */
215 ULONG ulIgnore;
216 int rc = DosResetEventSem(SEM2HND(hEventMultiSem), &ulIgnore);
217 switch (rc)
218 {
219 case NO_ERROR:
220 case ERROR_ALREADY_RESET:
221 return VINF_SUCCESS;
222 default:
223 return RTErrConvertFromOS2(rc);
224 }
225}
226
227
228RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
229{
230 /*
231 * Wait for condition.
232 */
233 int rc = DosWaitEventSem(SEM2HND(hEventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
234 switch (rc)
235 {
236 case NO_ERROR: return VINF_SUCCESS;
237 case ERROR_SEM_TIMEOUT:
238 case ERROR_TIMEOUT: return VERR_TIMEOUT;
239 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
240 default:
241 {
242 AssertMsgFailed(("Wait on hEventMultiSem %p failed, rc=%d\n", hEventMultiSem, rc));
243 return RTErrConvertFromOS2(rc);
244 }
245 }
246}
247
248
249RTDECL(void) RTSemEventMultiSetSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
250{
251 /** @todo implement RTSemEventMultiSetSignaller on OS/2 */
252}
253
254
255RTDECL(void) RTSemEventMultiAddSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
256{
257}
258
259
260RTDECL(void) RTSemEventMultiRemoveSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
261{
262}
263
264
265
266#undef RTSemMutexCreate
267RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
268{
269 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
270}
271
272
273RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
274 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
275{
276 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
277
278 /*
279 * Create the semaphore.
280 */
281 HMTX hmtx;
282 int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
283 if (!rc)
284 {
285 /** @todo implement lock validation of OS/2 mutex semaphores. */
286 *phMutexSem = (RTSEMMUTEX)(void *)hmtx;
287 return VINF_SUCCESS;
288 }
289
290 return RTErrConvertFromOS2(rc);
291}
292
293
294RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
295{
296 if (hMutexSem == NIL_RTSEMMUTEX)
297 return VINF_SUCCESS;
298
299 /*
300 * Close semaphore handle.
301 */
302 int rc = DosCloseMutexSem(SEM2HND(hMutexSem));
303 if (!rc)
304 return VINF_SUCCESS;
305 AssertMsgFailed(("Destroy hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
306 return RTErrConvertFromOS2(rc);
307}
308
309
310
311RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
312{
313#if 0 /** @todo def RTSEMMUTEX_STRICT */
314 /*
315 * Validate.
316 */
317 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
318 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
319 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
320
321 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
322#else
323 return RTLOCKVAL_SUB_CLASS_INVALID;
324#endif
325}
326
327
328#undef RTSemMutexRequestNoResume
329RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
330{
331 /*
332 * Lock mutex semaphore.
333 */
334 int rc = DosRequestMutexSem(SEM2HND(hMutexSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
335 switch (rc)
336 {
337 case NO_ERROR: return VINF_SUCCESS;
338 case ERROR_SEM_TIMEOUT:
339 case ERROR_TIMEOUT: return VERR_TIMEOUT;
340 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
341 case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
342 default:
343 {
344 AssertMsgFailed(("Wait on hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
345 return RTErrConvertFromOS2(rc);
346 }
347 }
348}
349
350RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
351{
352 /*
353 * Unlock mutex semaphore.
354 */
355 int rc = DosReleaseMutexSem(SEM2HND(hMutexSem));
356 if (!rc)
357 return VINF_SUCCESS;
358 AssertMsgFailed(("Release hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
359 return RTErrConvertFromOS2(rc);
360}
361
362
363RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem);
364{
365 /*
366 * Unlock mutex semaphore.
367 */
368 PID pid;
369 TID tid;
370 ULONG cRecursions;
371 int rc = DosQueryMutexSem(SEM2HND(hMutexSem), &pid, &tid, &cRecursions);
372 if (!rc)
373 return cRecursions != 0;
374 AssertMsgFailed(("DosQueryMutexSem %p failed, rc=%d\n", hMutexSem, rc));
375 return rc == ERROR_SEM_OWNER_DIED;
376}
377
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