1 | /* $Id: hmevent.cpp,v 1.5 2001-06-19 10:50:24 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 Event Semaphore implementation
|
---|
5 | *
|
---|
6 | * TODO: Inheritance
|
---|
7 | * TODO: Does DCE_POSTONE work in Warp 3 or 4 with no FP applied?
|
---|
8 | * TODO: No inheritance when CreateEvent is called for existing named event semaphore?
|
---|
9 | * (see HMCreateEvent in handlemanager.cpp)
|
---|
10 | * TODO: Name collisions with files & mutex not allowed. Check if this can happen in OS/2
|
---|
11 | *
|
---|
12 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
13 | *
|
---|
14 | * Copyright 2001 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
15 | */
|
---|
16 |
|
---|
17 | #undef DEBUG_LOCAL
|
---|
18 | //#define DEBUG_LOCAL
|
---|
19 |
|
---|
20 |
|
---|
21 | /*****************************************************************************
|
---|
22 | * Remark *
|
---|
23 | *****************************************************************************
|
---|
24 |
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*****************************************************************************
|
---|
29 | * Includes *
|
---|
30 | *****************************************************************************/
|
---|
31 |
|
---|
32 | #ifdef USE_OS2SEMAPHORES
|
---|
33 | #define INCL_DOSSEMAPHORES
|
---|
34 | #include <os2wrap.h>
|
---|
35 | #include <win32type.h>
|
---|
36 | #include <win32api.h>
|
---|
37 | #include <winconst.h>
|
---|
38 | #else
|
---|
39 | #include <os2win.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <string.h>
|
---|
44 | #include "unicode.h"
|
---|
45 | #include "misc.h"
|
---|
46 |
|
---|
47 | #include "HandleManager.H"
|
---|
48 | #include "HMEvent.h"
|
---|
49 | #include "oslibdos.h"
|
---|
50 |
|
---|
51 | #define DBG_LOCALLOG DBG_hmevent
|
---|
52 | #include "dbglocal.h"
|
---|
53 |
|
---|
54 | #ifndef DCE_AUTORESET
|
---|
55 | #define DCE_AUTORESET 0x1000 /* DosCreateEventSem option to auto-reset */
|
---|
56 | /* event semaphore on post. */
|
---|
57 | #define DCE_POSTONE 0x0800 /* DosCreateEventSem option to post only */
|
---|
58 | /* waiter and auto-reset the semaphore when*/
|
---|
59 | /* there are multiple waiters. */
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | /*****************************************************************************
|
---|
63 | * Defines *
|
---|
64 | *****************************************************************************/
|
---|
65 |
|
---|
66 | /*****************************************************************************
|
---|
67 | * Structures *
|
---|
68 | *****************************************************************************/
|
---|
69 |
|
---|
70 | /*****************************************************************************
|
---|
71 | * Local Prototypes *
|
---|
72 | *****************************************************************************/
|
---|
73 |
|
---|
74 |
|
---|
75 | /*****************************************************************************
|
---|
76 | * Name : HMCreateEvent
|
---|
77 | * Purpose : router function for CreateEvent
|
---|
78 | * Parameters:
|
---|
79 | * Variables :
|
---|
80 | * Result :
|
---|
81 | * Remark :
|
---|
82 | * Status :
|
---|
83 | *
|
---|
84 | * Author : Patrick Haller [Tue, 1999/07/06 20:44]
|
---|
85 | *****************************************************************************/
|
---|
86 |
|
---|
87 | DWORD HMDeviceEventClass::CreateEvent(PHMHANDLEDATA pHMHandleData,
|
---|
88 | LPSECURITY_ATTRIBUTES lpsa,
|
---|
89 | BOOL fManualReset,
|
---|
90 | BOOL fInitialState,
|
---|
91 | LPCTSTR lpszEventName)
|
---|
92 | {
|
---|
93 | #ifdef USE_OS2SEMAPHORES
|
---|
94 | APIRET rc;
|
---|
95 | HEV hev;
|
---|
96 | char szSemName[CCHMAXPATH];
|
---|
97 |
|
---|
98 | dprintf(("KERNEL32: HandleManager::Event::CreateEvent(%08xh,%08xh,%08xh,%08xh,%s)\n",
|
---|
99 | pHMHandleData,
|
---|
100 | lpsa,
|
---|
101 | fManualReset,
|
---|
102 | fInitialState,
|
---|
103 | lpszEventName));
|
---|
104 |
|
---|
105 | if(lpszEventName) {
|
---|
106 | strcpy(szSemName, "\\SEM32\\");
|
---|
107 | strcat(szSemName, lpszEventName);
|
---|
108 | lpszEventName = szSemName;
|
---|
109 | }
|
---|
110 | //Manual reset means all threads waiting on the event semaphore will be
|
---|
111 | //unblocked and the app must manually reset the event semaphore
|
---|
112 | //Automatic reset -> only one waiting thread unblocked & state reset
|
---|
113 | rc = DosCreateEventSem(lpszEventName, &hev, (fManualReset) ? 0 : DCE_POSTONE, fInitialState);
|
---|
114 |
|
---|
115 | if(rc) {
|
---|
116 | dprintf(("DosCreateEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
117 | pHMHandleData->hHMHandle = 0;
|
---|
118 | return error2WinError(rc);
|
---|
119 | }
|
---|
120 | pHMHandleData->dwAccess = EVENT_ALL_ACCESS_W;
|
---|
121 | pHMHandleData->dwFlags = fManualReset;
|
---|
122 | pHMHandleData->hHMHandle = hev;
|
---|
123 | return ERROR_SUCCESS_W;
|
---|
124 | #else
|
---|
125 | HANDLE hOpen32;
|
---|
126 |
|
---|
127 | dprintf(("KERNEL32: HandleManager::Event::CreateEvent(%08xh,%08xh,%08xh,%08xh,%s)\n",
|
---|
128 | pHMHandleData,
|
---|
129 | lpsa,
|
---|
130 | fManualReset,
|
---|
131 | fInitialState,
|
---|
132 | lpszEventName));
|
---|
133 |
|
---|
134 | hOpen32 = O32_CreateEvent(lpsa, // call Open32
|
---|
135 | fManualReset,
|
---|
136 | fInitialState,
|
---|
137 | lpszEventName);
|
---|
138 |
|
---|
139 | if (0 != hOpen32) // check success
|
---|
140 | {
|
---|
141 | pHMHandleData->hHMHandle = hOpen32; // save handle
|
---|
142 | return (NO_ERROR);
|
---|
143 | }
|
---|
144 | else
|
---|
145 | return (O32_GetLastError());
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /*****************************************************************************
|
---|
151 | * Name : HMOpenEvent
|
---|
152 | * Purpose : router function for OpenEvent
|
---|
153 | * Parameters:
|
---|
154 | * Variables :
|
---|
155 | * Result :
|
---|
156 | * Remark :
|
---|
157 | * Status :
|
---|
158 | *
|
---|
159 | * Author : Patrick Haller [Tue, 1999/07/06 20:44]
|
---|
160 | *****************************************************************************/
|
---|
161 |
|
---|
162 | DWORD HMDeviceEventClass::OpenEvent(PHMHANDLEDATA pHMHandleData,
|
---|
163 | BOOL fInheritHandle,
|
---|
164 | LPCTSTR lpszEventName)
|
---|
165 | {
|
---|
166 | #ifdef USE_OS2SEMAPHORES
|
---|
167 | HEV hev;
|
---|
168 | APIRET rc;
|
---|
169 | char szSemName[CCHMAXPATH];
|
---|
170 |
|
---|
171 | dprintf(("KERNEL32: HandleManager::Event::OpenEvent(%08xh,%08xh,%s)\n",
|
---|
172 | pHMHandleData,
|
---|
173 | fInheritHandle,
|
---|
174 | lpszEventName));
|
---|
175 |
|
---|
176 | if(lpszEventName == NULL) {
|
---|
177 | pHMHandleData->hHMHandle = 0;
|
---|
178 | return ERROR_INVALID_PARAMETER_W;
|
---|
179 | }
|
---|
180 |
|
---|
181 | strcpy(szSemName, "\\SEM32\\");
|
---|
182 | strcat(szSemName, lpszEventName);
|
---|
183 | rc = DosOpenEventSem(szSemName, &hev);
|
---|
184 | if(rc) {
|
---|
185 | dprintf(("DosOpenEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
186 | pHMHandleData->hHMHandle = 0;
|
---|
187 | return error2WinError(rc);
|
---|
188 | }
|
---|
189 | pHMHandleData->hHMHandle = hev;
|
---|
190 | return ERROR_SUCCESS_W;
|
---|
191 | #else
|
---|
192 | HANDLE hOpen32;
|
---|
193 |
|
---|
194 | dprintf(("KERNEL32: HandleManager::Event::OpenEvent(%08xh,%08xh,%s)\n",
|
---|
195 | pHMHandleData,
|
---|
196 | fInheritHandle,
|
---|
197 | lpszEventName));
|
---|
198 |
|
---|
199 | hOpen32 = O32_OpenEvent(pHMHandleData->dwAccess, // call Open32
|
---|
200 | fInheritHandle,
|
---|
201 | lpszEventName);
|
---|
202 |
|
---|
203 | if (0 != hOpen32) // check success
|
---|
204 | {
|
---|
205 | pHMHandleData->hHMHandle = hOpen32; // save handle
|
---|
206 | return (NO_ERROR);
|
---|
207 | }
|
---|
208 | else
|
---|
209 | return (O32_GetLastError());
|
---|
210 | #endif
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*****************************************************************************
|
---|
214 | * Name : HMDeviceEventClass::CloseHandle
|
---|
215 | * Purpose : close the handle
|
---|
216 | * Parameters: PHMHANDLEDATA pHMHandleData
|
---|
217 | * Variables :
|
---|
218 | * Result : API returncode
|
---|
219 | * Remark :
|
---|
220 | * Status :
|
---|
221 | *
|
---|
222 | * Author :
|
---|
223 | *****************************************************************************/
|
---|
224 |
|
---|
225 | #ifdef USE_OS2SEMAPHORES
|
---|
226 | BOOL HMDeviceEventClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
|
---|
227 | {
|
---|
228 | APIRET rc;
|
---|
229 |
|
---|
230 | if(pHMHandleData->hHMHandle) {
|
---|
231 | rc = DosCloseEventSem((HEV)pHMHandleData->hHMHandle);
|
---|
232 | if(rc) {
|
---|
233 | dprintf(("DosCloseEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
234 | SetLastError(error2WinError(rc));
|
---|
235 | return FALSE;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | return TRUE;
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | /*****************************************************************************
|
---|
243 | * Name : HMDeviceEventClass::DuplicateHandle
|
---|
244 | * Purpose :
|
---|
245 | * Parameters:
|
---|
246 | * various parameters as required
|
---|
247 | * Variables :
|
---|
248 | * Result :
|
---|
249 | * Remark : the standard behaviour is to return an error code for non-
|
---|
250 | * existant request codes
|
---|
251 | * Status :
|
---|
252 | *
|
---|
253 | * Author :
|
---|
254 | *****************************************************************************/
|
---|
255 | #ifdef USE_OS2SEMAPHORES
|
---|
256 | BOOL HMDeviceEventClass::DuplicateHandle(PHMHANDLEDATA pHMHandleData, HANDLE srcprocess,
|
---|
257 | PHMHANDLEDATA pHMSrcHandle,
|
---|
258 | HANDLE destprocess,
|
---|
259 | PHANDLE desthandle,
|
---|
260 | DWORD fdwAccess,
|
---|
261 | BOOL fInherit,
|
---|
262 | DWORD fdwOptions,
|
---|
263 | DWORD fdwOdinOptions)
|
---|
264 | {
|
---|
265 | APIRET rc;
|
---|
266 | HEV hev;
|
---|
267 |
|
---|
268 | dprintf(("KERNEL32:HandleManager::DuplicateHandle %s(%08x,%08x,%08x,%08x,%08x) - NOT IMPLEMENTED!!!!!!!!\n",
|
---|
269 | lpHMDeviceName,
|
---|
270 | pHMHandleData,
|
---|
271 | srcprocess, pHMSrcHandle, destprocess, desthandle));
|
---|
272 |
|
---|
273 | if(srcprocess != destprocess) {
|
---|
274 | DebugInt3();
|
---|
275 | SetLastError(ERROR_ACCESS_DENIED_W);
|
---|
276 | return FALSE;
|
---|
277 | }
|
---|
278 | hev = (HEV)pHMSrcHandle->hHMHandle;
|
---|
279 | rc = DosOpenEventSem(NULL, &hev);
|
---|
280 | if(rc) {
|
---|
281 | dprintf(("DosOpenEventSem %x failed with rc %d", pHMSrcHandle->hHMHandle, rc));
|
---|
282 | pHMHandleData->hHMHandle = 0;
|
---|
283 | SetLastError(error2WinError(rc));
|
---|
284 | return FALSE;
|
---|
285 | }
|
---|
286 | pHMHandleData->dwAccess = fdwAccess;
|
---|
287 | pHMHandleData->dwFlags = pHMSrcHandle->dwFlags; //fManualReset
|
---|
288 | pHMHandleData->hHMHandle = hev;
|
---|
289 | SetLastError(ERROR_SUCCESS_W);
|
---|
290 | return TRUE;
|
---|
291 | }
|
---|
292 | #endif
|
---|
293 |
|
---|
294 | /*****************************************************************************
|
---|
295 | * Name : HMSetEvent
|
---|
296 | * Purpose : router function for SetEvent
|
---|
297 | * Parameters:
|
---|
298 | * Variables :
|
---|
299 | * Result :
|
---|
300 | * Remark :
|
---|
301 | * Status :
|
---|
302 | *
|
---|
303 | * Author : Patrick Haller [Tue, 1999/07/06 20:44]
|
---|
304 | *****************************************************************************/
|
---|
305 |
|
---|
306 | BOOL HMDeviceEventClass::SetEvent(PHMHANDLEDATA pHMHandleData)
|
---|
307 | {
|
---|
308 | #ifdef USE_OS2SEMAPHORES
|
---|
309 | APIRET rc;
|
---|
310 |
|
---|
311 | dprintf(("KERNEL32: HandleManager::Event::SetEvent(%08xh)\n",
|
---|
312 | pHMHandleData->hHMHandle));
|
---|
313 |
|
---|
314 | if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
|
---|
315 | {
|
---|
316 | dprintf(("ERROR: Access denied!!"));
|
---|
317 | SetLastError(ERROR_ACCESS_DENIED_W);
|
---|
318 | return FALSE;
|
---|
319 | }
|
---|
320 |
|
---|
321 | rc = DosPostEventSem(pHMHandleData->hHMHandle);
|
---|
322 | if(rc) {
|
---|
323 | dprintf(("DosPostEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
324 | SetLastError(error2WinError(rc));
|
---|
325 | return FALSE;
|
---|
326 | }
|
---|
327 | SetLastError(ERROR_SUCCESS_W);
|
---|
328 | return TRUE;
|
---|
329 | #else
|
---|
330 | dprintf(("KERNEL32: HandleManager::Event::SetEvent(%08xh)\n",
|
---|
331 | pHMHandleData->hHMHandle));
|
---|
332 |
|
---|
333 | return (O32_SetEvent(pHMHandleData->hHMHandle));
|
---|
334 | #endif
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | /*****************************************************************************
|
---|
339 | * Name : HMPulseEvent
|
---|
340 | * Purpose : router function for PulseEvent
|
---|
341 | * Parameters:
|
---|
342 | * Variables :
|
---|
343 | * Result :
|
---|
344 | * Remark :
|
---|
345 | * Status :
|
---|
346 | *
|
---|
347 | * Author : Patrick Haller [Tue, 1999/07/06 20:44]
|
---|
348 | *****************************************************************************/
|
---|
349 |
|
---|
350 | BOOL HMDeviceEventClass::PulseEvent(PHMHANDLEDATA pHMHandleData)
|
---|
351 | {
|
---|
352 | #ifdef USE_OS2SEMAPHORES
|
---|
353 | APIRET rc;
|
---|
354 | ULONG count;
|
---|
355 |
|
---|
356 | dprintf2(("KERNEL32: HandleManager::Event::PulseEvent(%08xh)\n",
|
---|
357 | pHMHandleData->hHMHandle));
|
---|
358 |
|
---|
359 | if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
|
---|
360 | {
|
---|
361 | dprintf(("ERROR: Access denied!!"));
|
---|
362 | SetLastError(ERROR_ACCESS_DENIED_W);
|
---|
363 | return FALSE;
|
---|
364 | }
|
---|
365 |
|
---|
366 | rc = DosPostEventSem(pHMHandleData->hHMHandle);
|
---|
367 | if(rc) {
|
---|
368 | dprintf(("DosPostEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
369 | SetLastError(error2WinError(rc));
|
---|
370 | return FALSE;
|
---|
371 | }
|
---|
372 | if(pHMHandleData->dwFlags == TRUE) {//fManualReset
|
---|
373 | rc = DosResetEventSem(pHMHandleData->hHMHandle, &count);
|
---|
374 | if(rc) {
|
---|
375 | dprintf(("DosResetEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
376 | SetLastError(error2WinError(rc));
|
---|
377 | return FALSE;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | SetLastError(ERROR_SUCCESS_W);
|
---|
381 | return TRUE;
|
---|
382 | #else
|
---|
383 | dprintf2(("KERNEL32: HandleManager::Event::PulseEvent(%08xh)\n",
|
---|
384 | pHMHandleData->hHMHandle));
|
---|
385 |
|
---|
386 | return (O32_PulseEvent(pHMHandleData->hHMHandle));
|
---|
387 | #endif
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /*****************************************************************************
|
---|
392 | * Name : HMResetEvent
|
---|
393 | * Purpose : router function for ResetEvent
|
---|
394 | * Parameters:
|
---|
395 | * Variables :
|
---|
396 | * Result :
|
---|
397 | * Remark :
|
---|
398 | * Status :
|
---|
399 | *
|
---|
400 | * Author : Patrick Haller [Tue, 1999/07/06 20:44]
|
---|
401 | *****************************************************************************/
|
---|
402 |
|
---|
403 | BOOL HMDeviceEventClass::ResetEvent(PHMHANDLEDATA pHMHandleData)
|
---|
404 | {
|
---|
405 | #ifdef USE_OS2SEMAPHORES
|
---|
406 | APIRET rc;
|
---|
407 | ULONG count;
|
---|
408 |
|
---|
409 | dprintf2(("KERNEL32: HandleManager::Event::ResetEvent(%08xh)\n",
|
---|
410 | pHMHandleData->hHMHandle));
|
---|
411 |
|
---|
412 | if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
|
---|
413 | {
|
---|
414 | dprintf(("ERROR: Access denied!!"));
|
---|
415 | SetLastError(ERROR_ACCESS_DENIED_W);
|
---|
416 | return FALSE;
|
---|
417 | }
|
---|
418 |
|
---|
419 | rc = DosResetEventSem(pHMHandleData->hHMHandle, &count);
|
---|
420 | if(rc) {
|
---|
421 | dprintf(("DosResetEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
|
---|
422 | SetLastError(error2WinError(rc));
|
---|
423 | return FALSE;
|
---|
424 | }
|
---|
425 | SetLastError(ERROR_SUCCESS_W);
|
---|
426 | return TRUE;
|
---|
427 | #else
|
---|
428 | dprintf2(("KERNEL32: HandleManager::Event::ResetEvent(%08xh)\n",
|
---|
429 | pHMHandleData->hHMHandle));
|
---|
430 |
|
---|
431 | return (O32_ResetEvent(pHMHandleData->hHMHandle));
|
---|
432 | #endif
|
---|
433 | }
|
---|
434 |
|
---|