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