VirtualBox

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

Last change on this file since 105589 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.7 KB
Line 
1/* $Id: sems-os2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, OS/2.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define INCL_DOSSEMAPHORES
42#define INCL_ERRORS
43#include <os2.h>
44#undef RT_MAX
45
46#include <iprt/semaphore.h>
47#include <iprt/assert.h>
48#include <iprt/err.h>
49
50
51/** Converts semaphore to OS/2 handle. */
52#define SEM2HND(Sem) ((LHANDLE)(uintptr_t)Sem)
53
54
55
56RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
57{
58 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
59}
60
61
62RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
63{
64 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
65 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
66
67 /*
68 * Create the semaphore.
69 * (Auto reset, not signaled, private event object.)
70 */
71 HEV hev;
72 int rc = DosCreateEventSem(NULL, &hev, DCE_AUTORESET | DCE_POSTONE, 0);
73 if (!rc)
74 {
75 *phEventSem = (RTSEMEVENT)(void *)hev;
76 return VINF_SUCCESS;
77 }
78 return RTErrConvertFromOS2(rc);
79}
80
81
82RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
83{
84 if (hEventSem == NIL_RTSEMEVENT)
85 return VINF_SUCCESS;
86
87 /*
88 * Close semaphore handle.
89 */
90 int rc = DosCloseEventSem(SEM2HND(hEventSem));
91 if (!rc)
92 return VINF_SUCCESS;
93 AssertMsgFailed(("Destroy hEventSem %p failed, rc=%d\n", hEventSem, rc));
94 return RTErrConvertFromOS2(rc);
95}
96
97
98RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
99{
100 /*
101 * Wait for condition.
102 */
103 int rc = DosWaitEventSem(SEM2HND(hEventSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
104 switch (rc)
105 {
106 case NO_ERROR: return VINF_SUCCESS;
107 case ERROR_SEM_TIMEOUT:
108 case ERROR_TIMEOUT: return VERR_TIMEOUT;
109 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
110 default:
111 {
112 AssertMsgFailed(("Wait on hEventSem %p failed, rc=%d\n", hEventSem, rc));
113 return RTErrConvertFromOS2(rc);
114 }
115 }
116}
117
118
119RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
120{
121 /*
122 * Signal the object.
123 */
124 int rc = DosPostEventSem(SEM2HND(hEventSem));
125 switch (rc)
126 {
127 case NO_ERROR:
128 case ERROR_ALREADY_POSTED:
129 case ERROR_TOO_MANY_POSTS:
130 return VINF_SUCCESS;
131 default:
132 return RTErrConvertFromOS2(rc);
133 }
134}
135
136
137RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
138{
139/** @todo implement RTSemEventSetSignaller and friends for OS/2 */
140}
141
142
143RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
144{
145
146}
147
148
149RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
150{
151
152}
153
154
155
156
157RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
158{
159 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
160}
161
162
163RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
164 const char *pszNameFmt, ...)
165{
166 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
167
168 /*
169 * Create the semaphore.
170 * (Manual reset, not signaled, private event object.)
171 */
172 HEV hev;
173 int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
174 if (!rc)
175 {
176 *phEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
177 return VINF_SUCCESS;
178 }
179 return RTErrConvertFromOS2(rc);
180}
181
182
183RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
184{
185 if (hEventMultiSem == NIL_RTSEMEVENTMULTI)
186 return VINF_SUCCESS;
187
188 /*
189 * Close semaphore handle.
190 */
191 int rc = DosCloseEventSem(SEM2HND(hEventMultiSem));
192 if (!rc)
193 return VINF_SUCCESS;
194 AssertMsgFailed(("Destroy hEventMultiSem %p failed, rc=%d\n", hEventMultiSem, rc));
195 return RTErrConvertFromOS2(rc);
196}
197
198
199RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
200{
201 /*
202 * Signal the object.
203 */
204 int rc = DosPostEventSem(SEM2HND(hEventMultiSem));
205 switch (rc)
206 {
207 case NO_ERROR:
208 case ERROR_ALREADY_POSTED:
209 case ERROR_TOO_MANY_POSTS:
210 return VINF_SUCCESS;
211 default:
212 return RTErrConvertFromOS2(rc);
213 }
214}
215
216
217RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
218{
219 /*
220 * Reset the object.
221 */
222 ULONG ulIgnore;
223 int rc = DosResetEventSem(SEM2HND(hEventMultiSem), &ulIgnore);
224 switch (rc)
225 {
226 case NO_ERROR:
227 case ERROR_ALREADY_RESET:
228 return VINF_SUCCESS;
229 default:
230 return RTErrConvertFromOS2(rc);
231 }
232}
233
234
235RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
236{
237 /*
238 * Wait for condition.
239 */
240 int rc = DosWaitEventSem(SEM2HND(hEventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
241 switch (rc)
242 {
243 case NO_ERROR: return VINF_SUCCESS;
244 case ERROR_SEM_TIMEOUT:
245 case ERROR_TIMEOUT: return VERR_TIMEOUT;
246 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
247 default:
248 {
249 AssertMsgFailed(("Wait on hEventMultiSem %p failed, rc=%d\n", hEventMultiSem, rc));
250 return RTErrConvertFromOS2(rc);
251 }
252 }
253}
254
255
256RTDECL(void) RTSemEventMultiSetSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
257{
258 /** @todo implement RTSemEventMultiSetSignaller on OS/2 */
259}
260
261
262RTDECL(void) RTSemEventMultiAddSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
263{
264}
265
266
267RTDECL(void) RTSemEventMultiRemoveSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread)
268{
269}
270
271
272
273#undef RTSemMutexCreate
274RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
275{
276 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
277}
278
279
280RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
281 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
282{
283 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
284
285 /*
286 * Create the semaphore.
287 */
288 HMTX hmtx;
289 int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
290 if (!rc)
291 {
292 /** @todo implement lock validation of OS/2 mutex semaphores. */
293 *phMutexSem = (RTSEMMUTEX)(void *)hmtx;
294 return VINF_SUCCESS;
295 }
296
297 return RTErrConvertFromOS2(rc);
298}
299
300
301RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
302{
303 if (hMutexSem == NIL_RTSEMMUTEX)
304 return VINF_SUCCESS;
305
306 /*
307 * Close semaphore handle.
308 */
309 int rc = DosCloseMutexSem(SEM2HND(hMutexSem));
310 if (!rc)
311 return VINF_SUCCESS;
312 AssertMsgFailed(("Destroy hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
313 return RTErrConvertFromOS2(rc);
314}
315
316
317
318RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
319{
320#if 0 /** @todo def RTSEMMUTEX_STRICT */
321 /*
322 * Validate.
323 */
324 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
325 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
326 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
327
328 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
329#else
330 return RTLOCKVAL_SUB_CLASS_INVALID;
331#endif
332}
333
334
335#undef RTSemMutexRequestNoResume
336RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
337{
338 /*
339 * Lock mutex semaphore.
340 */
341 int rc = DosRequestMutexSem(SEM2HND(hMutexSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
342 switch (rc)
343 {
344 case NO_ERROR: return VINF_SUCCESS;
345 case ERROR_SEM_TIMEOUT:
346 case ERROR_TIMEOUT: return VERR_TIMEOUT;
347 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
348 case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
349 default:
350 {
351 AssertMsgFailed(("Wait on hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
352 return RTErrConvertFromOS2(rc);
353 }
354 }
355}
356
357RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
358{
359// RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
360// return rtSemMutexRequestNoResume(hMutexSem, cMillies, &SrcPos);
361 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
362}
363
364
365RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
366{
367 /*
368 * Unlock mutex semaphore.
369 */
370 int rc = DosReleaseMutexSem(SEM2HND(hMutexSem));
371 if (!rc)
372 return VINF_SUCCESS;
373 AssertMsgFailed(("Release hMutexSem %p failed, rc=%d\n", hMutexSem, rc));
374 return RTErrConvertFromOS2(rc);
375}
376
377
378RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
379{
380 /*
381 * Unlock mutex semaphore.
382 */
383 PID pid;
384 TID tid;
385 ULONG cRecursions;
386 int rc = DosQueryMutexSem(SEM2HND(hMutexSem), &pid, &tid, &cRecursions);
387 if (!rc)
388 return cRecursions != 0;
389 AssertMsgFailed(("DosQueryMutexSem %p failed, rc=%d\n", hMutexSem, rc));
390 return rc == ERROR_SEM_OWNER_DIED;
391}
392
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