1 | /* $Id: sems-os2.cpp 25624 2010-01-03 15:23:27Z 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 | /* Undefine debug mappings. */
|
---|
50 | #undef RTSemMutexRequest
|
---|
51 | #undef RTSemMutexRequestNoResume
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
|
---|
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 | *pEventSem = (RTSEMEVENT)(void *)hev;
|
---|
65 | return VINF_SUCCESS;
|
---|
66 | }
|
---|
67 | return RTErrConvertFromOS2(rc);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
72 | {
|
---|
73 | if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
|
---|
74 | return VERR_INVALID_HANDLE;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Close semaphore handle.
|
---|
78 | */
|
---|
79 | int rc = DosCloseEventSem(SEM2HND(EventSem));
|
---|
80 | if (!rc)
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | AssertMsgFailed(("Destroy EventSem %p failed, rc=%d\n", EventSem, rc));
|
---|
83 | return RTErrConvertFromOS2(rc);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
88 | {
|
---|
89 | /*
|
---|
90 | * Wait for condition.
|
---|
91 | */
|
---|
92 | int rc = DosWaitEventSem(SEM2HND(EventSem), 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 EventSem %p failed, rc=%d\n", EventSem, rc));
|
---|
102 | return RTErrConvertFromOS2(rc);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Signal the object.
|
---|
112 | */
|
---|
113 | int rc = DosPostEventSem(SEM2HND(EventSem));
|
---|
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 |
|
---|
127 |
|
---|
128 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
|
---|
129 | {
|
---|
130 | /*
|
---|
131 | * Create the semaphore.
|
---|
132 | * (Manual reset, not signaled, private event object.)
|
---|
133 | */
|
---|
134 | HEV hev;
|
---|
135 | int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
|
---|
136 | if (!rc)
|
---|
137 | {
|
---|
138 | *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 | return RTErrConvertFromOS2(rc);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Close semaphore handle.
|
---|
149 | */
|
---|
150 | int rc = DosCloseEventSem(SEM2HND(EventMultiSem));
|
---|
151 | if (!rc)
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | AssertMsgFailed(("Destroy EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
|
---|
154 | return RTErrConvertFromOS2(rc);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
159 | {
|
---|
160 | /*
|
---|
161 | * Signal the object.
|
---|
162 | */
|
---|
163 | int rc = DosPostEventSem(SEM2HND(EventMultiSem));
|
---|
164 | switch (rc)
|
---|
165 | {
|
---|
166 | case NO_ERROR:
|
---|
167 | case ERROR_ALREADY_POSTED:
|
---|
168 | case ERROR_TOO_MANY_POSTS:
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | default:
|
---|
171 | return RTErrConvertFromOS2(rc);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
177 | {
|
---|
178 | /*
|
---|
179 | * Reset the object.
|
---|
180 | */
|
---|
181 | ULONG ulIgnore;
|
---|
182 | int rc = DosResetEventSem(SEM2HND(EventMultiSem), &ulIgnore);
|
---|
183 | switch (rc)
|
---|
184 | {
|
---|
185 | case NO_ERROR:
|
---|
186 | case ERROR_ALREADY_RESET:
|
---|
187 | return VINF_SUCCESS;
|
---|
188 | default:
|
---|
189 | return RTErrConvertFromOS2(rc);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
195 | {
|
---|
196 | /*
|
---|
197 | * Wait for condition.
|
---|
198 | */
|
---|
199 | int rc = DosWaitEventSem(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
|
---|
200 | switch (rc)
|
---|
201 | {
|
---|
202 | case NO_ERROR: return VINF_SUCCESS;
|
---|
203 | case ERROR_SEM_TIMEOUT:
|
---|
204 | case ERROR_TIMEOUT: return VERR_TIMEOUT;
|
---|
205 | case ERROR_INTERRUPT: return VERR_INTERRUPTED;
|
---|
206 | default:
|
---|
207 | {
|
---|
208 | AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
|
---|
209 | return RTErrConvertFromOS2(rc);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
|
---|
218 | {
|
---|
219 | /*
|
---|
220 | * Create the semaphore.
|
---|
221 | */
|
---|
222 | HMTX hmtx;
|
---|
223 | int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
|
---|
224 | if (!rc)
|
---|
225 | {
|
---|
226 | *pMutexSem = (RTSEMMUTEX)(void *)hmtx;
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 | return RTErrConvertFromOS2(rc);
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
|
---|
235 | {
|
---|
236 | /*
|
---|
237 | * Close semaphore handle.
|
---|
238 | */
|
---|
239 | int rc = DosCloseMutexSem(SEM2HND(MutexSem));
|
---|
240 | if (!rc)
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | AssertMsgFailed(("Destroy MutexSem %p failed, rc=%d\n", MutexSem, rc));
|
---|
243 | return RTErrConvertFromOS2(rc);
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
|
---|
248 | {
|
---|
249 | /*
|
---|
250 | * Lock mutex semaphore.
|
---|
251 | */
|
---|
252 | int rc = DosRequestMutexSem(SEM2HND(MutexSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
|
---|
253 | switch (rc)
|
---|
254 | {
|
---|
255 | case NO_ERROR: return VINF_SUCCESS;
|
---|
256 | case ERROR_SEM_TIMEOUT:
|
---|
257 | case ERROR_TIMEOUT: return VERR_TIMEOUT;
|
---|
258 | case ERROR_INTERRUPT: return VERR_INTERRUPTED;
|
---|
259 | case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
|
---|
260 | default:
|
---|
261 | {
|
---|
262 | AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d\n", MutexSem, rc));
|
---|
263 | return RTErrConvertFromOS2(rc);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
|
---|
269 | {
|
---|
270 | /*
|
---|
271 | * Unlock mutex semaphore.
|
---|
272 | */
|
---|
273 | int rc = DosReleaseMutexSem(SEM2HND(MutexSem));
|
---|
274 | if (!rc)
|
---|
275 | return VINF_SUCCESS;
|
---|
276 | AssertMsgFailed(("Release MutexSem %p failed, rc=%d\n", MutexSem, rc));
|
---|
277 | return RTErrConvertFromOS2(rc);
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutex);
|
---|
282 | {
|
---|
283 | /*
|
---|
284 | * Unlock mutex semaphore.
|
---|
285 | */
|
---|
286 | PID pid;
|
---|
287 | TID tid;
|
---|
288 | ULONG cRecursions;
|
---|
289 | int rc = DosQueryMutexSem(SEM2HND(MutexSem), &pid, &tid, &cRecursions);
|
---|
290 | if (!rc)
|
---|
291 | return cRecursions != 0;
|
---|
292 | AssertMsgFailed(("DosQueryMutexSem %p failed, rc=%d\n", MutexSem, rc));
|
---|
293 | return rc == ERROR_SEM_OWNER_DIED;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|