VirtualBox

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

Last change on this file since 5938 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* $Id: sems-os2.cpp 4071 2007-08-07 17:07:59Z 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.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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define INCL_DOSSEMAPHORES
23#define INCL_ERRORS
24#include <os2.h>
25#undef RT_MAX
26
27#include <iprt/semaphore.h>
28#include <iprt/assert.h>
29#include <iprt/err.h>
30
31
32/** Converts semaphore to OS/2 handle. */
33#define SEM2HND(Sem) ((LHANDLE)(uintptr_t)Sem)
34
35
36RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
37{
38 /*
39 * Create the semaphore.
40 * (Auto reset, not signaled, private event object.)
41 */
42 HEV hev;
43 int rc = DosCreateEventSem(NULL, &hev, DCE_AUTORESET | DCE_POSTONE, 0);
44 if (!rc)
45 {
46 *pEventSem = (RTSEMEVENT)(void *)hev;
47 return VINF_SUCCESS;
48 }
49 return RTErrConvertFromOS2(rc);
50}
51
52
53RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
54{
55 /*
56 * Close semaphore handle.
57 */
58 int rc = DosCloseEventSem(SEM2HND(EventSem));
59 if (!rc)
60 return VINF_SUCCESS;
61 AssertMsgFailed(("Destroy EventSem %p failed, rc=%d\n", EventSem, rc));
62 return RTErrConvertFromOS2(rc);
63}
64
65
66RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
67{
68 /*
69 * Wait for condition.
70 */
71 int rc = DosWaitEventSem(SEM2HND(EventSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
72 switch (rc)
73 {
74 case NO_ERROR: return VINF_SUCCESS;
75 case ERROR_SEM_TIMEOUT:
76 case ERROR_TIMEOUT: return VERR_TIMEOUT;
77 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
78 default:
79 {
80 AssertMsgFailed(("Wait on EventSem %p failed, rc=%d\n", EventSem, rc));
81 return RTErrConvertFromOS2(rc);
82 }
83 }
84}
85
86
87RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
88{
89 /*
90 * Signal the object.
91 */
92 int rc = DosPostEventSem(SEM2HND(EventSem));
93 switch (rc)
94 {
95 case NO_ERROR:
96 case ERROR_ALREADY_POSTED:
97 case ERROR_TOO_MANY_POSTS:
98 return VINF_SUCCESS;
99 default:
100 return RTErrConvertFromOS2(rc);
101 }
102}
103
104
105
106
107RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
108{
109 /*
110 * Create the semaphore.
111 * (Manual reset, not signaled, private event object.)
112 */
113 HEV hev;
114 int rc = DosCreateEventSem(NULL, &hev, 0, FALSE);
115 if (!rc)
116 {
117 *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
118 return VINF_SUCCESS;
119 }
120 return RTErrConvertFromOS2(rc);
121}
122
123
124RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
125{
126 /*
127 * Close semaphore handle.
128 */
129 int rc = DosCloseEventSem(SEM2HND(EventMultiSem));
130 if (!rc)
131 return VINF_SUCCESS;
132 AssertMsgFailed(("Destroy EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
133 return RTErrConvertFromOS2(rc);
134}
135
136
137RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
138{
139 /*
140 * Signal the object.
141 */
142 int rc = DosPostEventSem(SEM2HND(EventMultiSem));
143 switch (rc)
144 {
145 case NO_ERROR:
146 case ERROR_ALREADY_POSTED:
147 case ERROR_TOO_MANY_POSTS:
148 return VINF_SUCCESS;
149 default:
150 return RTErrConvertFromOS2(rc);
151 }
152}
153
154
155RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
156{
157 /*
158 * Reset the object.
159 */
160 ULONG ulIgnore;
161 int rc = DosResetEventSem(SEM2HND(EventMultiSem), &ulIgnore);
162 switch (rc)
163 {
164 case NO_ERROR:
165 case ERROR_ALREADY_RESET:
166 return VINF_SUCCESS;
167 default:
168 return RTErrConvertFromOS2(rc);
169 }
170}
171
172
173RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
174{
175 /*
176 * Wait for condition.
177 */
178 int rc = DosWaitEventSem(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
179 switch (rc)
180 {
181 case NO_ERROR: return VINF_SUCCESS;
182 case ERROR_SEM_TIMEOUT:
183 case ERROR_TIMEOUT: return VERR_TIMEOUT;
184 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
185 default:
186 {
187 AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d\n", EventMultiSem, rc));
188 return RTErrConvertFromOS2(rc);
189 }
190 }
191}
192
193
194
195
196RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
197{
198 /*
199 * Create the semaphore.
200 */
201 HMTX hmtx;
202 int rc = DosCreateMutexSem(NULL, &hmtx, 0, FALSE);
203 if (!rc)
204 {
205 *pMutexSem = (RTSEMMUTEX)(void *)hmtx;
206 return VINF_SUCCESS;
207 }
208
209 return RTErrConvertFromOS2(rc);
210}
211
212
213RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
214{
215 /*
216 * Close semaphore handle.
217 */
218 int rc = DosCloseMutexSem(SEM2HND(MutexSem));
219 if (!rc)
220 return VINF_SUCCESS;
221 AssertMsgFailed(("Destroy MutexSem %p failed, rc=%d\n", MutexSem, rc));
222 return RTErrConvertFromOS2(rc);
223}
224
225
226RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
227{
228 /*
229 * Lock mutex semaphore.
230 */
231 int rc = DosRequestMutexSem(SEM2HND(MutexSem), cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies);
232 switch (rc)
233 {
234 case NO_ERROR: return VINF_SUCCESS;
235 case ERROR_SEM_TIMEOUT:
236 case ERROR_TIMEOUT: return VERR_TIMEOUT;
237 case ERROR_INTERRUPT: return VERR_INTERRUPTED;
238 case ERROR_SEM_OWNER_DIED: return VERR_SEM_OWNER_DIED;
239 default:
240 {
241 AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d\n", MutexSem, rc));
242 return RTErrConvertFromOS2(rc);
243 }
244 }
245}
246
247RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
248{
249 /*
250 * Unlock mutex semaphore.
251 */
252 int rc = DosReleaseMutexSem(SEM2HND(MutexSem));
253 if (!rc)
254 return VINF_SUCCESS;
255 AssertMsgFailed(("Release MutexSem %p failed, rc=%d\n", MutexSem, rc));
256 return RTErrConvertFromOS2(rc);
257}
258
259
260
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