VirtualBox

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

Last change on this file since 7169 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: sems-os2.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Semaphores, OS/2.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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
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
45RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
46{
47 /*
48 * Create the semaphore.
49 * (Auto reset, not signaled, private event object.)
50 */
51 HEV hev;
52 int rc = DosCreateEventSem(NULL, &hev, DCE_AUTORESET | DCE_POSTONE, 0);
53 if (!rc)
54 {
55 *pEventSem = (RTSEMEVENT)(void *)hev;
56 return VINF_SUCCESS;
57 }
58 return RTErrConvertFromOS2(rc);
59}
60
61
62RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
63{
64 /*
65 * Close semaphore handle.
66 */
67 int rc = DosCloseEventSem(SEM2HND(EventSem));
68 if (!rc)
69 return VINF_SUCCESS;
70 AssertMsgFailed(("Destroy EventSem %p failed, rc=%d\n", EventSem, rc));
71 return RTErrConvertFromOS2(rc);
72}
73
74
75RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
76{
77 /*
78 * Wait for condition.
79 */
80 int rc = DosWaitEventSem(SEM2HND(EventSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
81 switch (rc)
82 {
83 case NO_ERROR: return VINF_SUCCESS;
84 case ERROR_SEM_TIMEOUT:
85 case ERROR_TIMEOUT: return VERR_TIMEOUT;
86 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
87 default:
88 {
89 AssertMsgFailed(("Wait on EventSem %p failed, rc=%d\n", EventSem, rc));
90 return RTErrConvertFromOS2(rc);
91 }
92 }
93}
94
95
96RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
97{
98 /*
99 * Signal the object.
100 */
101 int rc = DosPostEventSem(SEM2HND(EventSem));
102 switch (rc)
103 {
104 case NO_ERROR:
105 case ERROR_ALREADY_POSTED:
106 case ERROR_TOO_MANY_POSTS:
107 return VINF_SUCCESS;
108 default:
109 return RTErrConvertFromOS2(rc);
110 }
111}
112
113
114
115
116RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
117{
118 /*
119 * Create the semaphore.
120 * (Manual reset, not signaled, private event object.)
121 */
122 HEV hev;
123 int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
124 if (!rc)
125 {
126 *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
127 return VINF_SUCCESS;
128 }
129 return RTErrConvertFromOS2(rc);
130}
131
132
133RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
134{
135 /*
136 * Close semaphore handle.
137 */
138 int rc = DosCloseEventSem(SEM2HND(EventMultiSem));
139 if (!rc)
140 return VINF_SUCCESS;
141 AssertMsgFailed(("Destroy EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
142 return RTErrConvertFromOS2(rc);
143}
144
145
146RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
147{
148 /*
149 * Signal the object.
150 */
151 int rc = DosPostEventSem(SEM2HND(EventMultiSem));
152 switch (rc)
153 {
154 case NO_ERROR:
155 case ERROR_ALREADY_POSTED:
156 case ERROR_TOO_MANY_POSTS:
157 return VINF_SUCCESS;
158 default:
159 return RTErrConvertFromOS2(rc);
160 }
161}
162
163
164RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
165{
166 /*
167 * Reset the object.
168 */
169 ULONG ulIgnore;
170 int rc = DosResetEventSem(SEM2HND(EventMultiSem), &ulIgnore);
171 switch (rc)
172 {
173 case NO_ERROR:
174 case ERROR_ALREADY_RESET:
175 return VINF_SUCCESS;
176 default:
177 return RTErrConvertFromOS2(rc);
178 }
179}
180
181
182RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
183{
184 /*
185 * Wait for condition.
186 */
187 int rc = DosWaitEventSem(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
188 switch (rc)
189 {
190 case NO_ERROR: return VINF_SUCCESS;
191 case ERROR_SEM_TIMEOUT:
192 case ERROR_TIMEOUT: return VERR_TIMEOUT;
193 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
194 default:
195 {
196 AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
197 return RTErrConvertFromOS2(rc);
198 }
199 }
200}
201
202
203
204
205RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
206{
207 /*
208 * Create the semaphore.
209 */
210 HMTX hmtx;
211 int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
212 if (!rc)
213 {
214 *pMutexSem = (RTSEMMUTEX)(void *)hmtx;
215 return VINF_SUCCESS;
216 }
217
218 return RTErrConvertFromOS2(rc);
219}
220
221
222RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
223{
224 /*
225 * Close semaphore handle.
226 */
227 int rc = DosCloseMutexSem(SEM2HND(MutexSem));
228 if (!rc)
229 return VINF_SUCCESS;
230 AssertMsgFailed(("Destroy MutexSem %p failed, rc=%d\n", MutexSem, rc));
231 return RTErrConvertFromOS2(rc);
232}
233
234
235RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
236{
237 /*
238 * Lock mutex semaphore.
239 */
240 int rc = DosRequestMutexSem(SEM2HND(MutexSem), 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 case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
248 default:
249 {
250 AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d\n", MutexSem, rc));
251 return RTErrConvertFromOS2(rc);
252 }
253 }
254}
255
256RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
257{
258 /*
259 * Unlock mutex semaphore.
260 */
261 int rc = DosReleaseMutexSem(SEM2HND(MutexSem));
262 if (!rc)
263 return VINF_SUCCESS;
264 AssertMsgFailed(("Release MutexSem %p failed, rc=%d\n", MutexSem, rc));
265 return RTErrConvertFromOS2(rc);
266}
267
268
269
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