source: trunk/src/kernel32/hmevent.cpp@ 21302

Last change on this file since 21302 was 21302, checked in by ydario, 16 years ago

Kernel32 updates.

File size: 13.2 KB
Line 
1/* $Id: hmevent.cpp,v 1.9 2003-02-04 11:28:57 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
18/*****************************************************************************
19 * Remark *
20 *****************************************************************************
21
22 */
23
24
25/*****************************************************************************
26 * Includes *
27 *****************************************************************************/
28
29#include <os2win.h>
30
31#include <stdlib.h>
32#include <string.h>
33#include "unicode.h"
34#include "misc.h"
35
36#include "HandleManager.H"
37#include "HMEvent.h"
38#include "HMSemaphore.h"
39#include "oslibdos.h"
40
41#include "hmhandle.h"
42
43
44#define DBG_LOCALLOG DBG_hmevent
45#include "dbglocal.h"
46
47
48/*****************************************************************************
49 * Name : HANDLE HMCreateEvent
50 * Purpose : Wrapper for the CreateEvent() API
51 * Parameters:
52 * Variables :
53 * Result :
54 * Remark :
55 * Status :
56 *
57 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
58 *****************************************************************************/
59
60HANDLE WIN32API CreateEventA(LPSECURITY_ATTRIBUTES lpsa,
61 BOOL bManualReset,
62 BOOL bInitialState,
63 LPCTSTR lpName)
64{
65 PHMHANDLE pHandle;
66 DWORD rc; /* API return code */
67
68
69 if(lpName) { //check if shared event semaphore already exists
70 dprintf(("Event semaphore name %s", lpName));
71 //TODO: No inheritance??
72 HANDLE handle = OpenEventA(EVENT_ALL_ACCESS, FALSE, lpName);
73 if(handle) {
74 dprintf(("CreateEvent: return handle of existing event semaphore %x", handle));
75 SetLastError(ERROR_ALREADY_EXISTS);
76 return handle;
77 }
78 }
79
80 pHandle = HMHandleGetFreePtr(HMTYPE_EVENTSEM); /* get free handle */
81 if (pHandle == NULL) /* oops, no free handles ! */
82 {
83 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
84 return 0;
85 }
86
87 /* call the device handler */
88 rc = pHandle->pDeviceHandler->CreateEvent(&pHandle->hmHandleData,
89 lpsa,
90 bManualReset,
91 bInitialState,
92 lpName);
93 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
94 {
95 HMHandleFree(pHandle->hmHandleData.hWin32Handle);
96 SetLastError(rc);
97 return 0; /* signal error */
98 }
99 else
100 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
101
102 return pHandle->hmHandleData.hWin32Handle; /* return valid handle */
103}
104
105/*****************************************************************************
106 * Name : BOOL CreateEventW
107 * Purpose : forward call to Open32
108 * Parameters:
109 * Variables :
110 * Result :
111 * Remark : handle translation is done in CreateEventA
112 * Status :
113 *
114 * Author : Patrick Haller [Fri, 1998/06/12 03:44]
115 *****************************************************************************/
116
117HANDLE WIN32API CreateEventW(LPSECURITY_ATTRIBUTES arg1,
118 BOOL arg2, BOOL arg3,
119 LPCWSTR arg4)
120{
121 HANDLE rc;
122 char *astring;
123
124 if (arg4 != NULL) // support for unnamed semaphores
125 astring = UnicodeToAsciiString((LPWSTR)arg4);
126 else
127 astring = NULL;
128
129 rc = CreateEventA(arg1,
130 arg2,
131 arg3,
132 astring);
133
134 if (astring != NULL)
135 FreeAsciiString(astring);
136
137 return(rc);
138}
139
140/*****************************************************************************
141 * Name : HANDLE HMOpenEvent
142 * Purpose : Wrapper for the OpenEvent() API
143 * Parameters:
144 * Variables :
145 * Result :
146 * Remark :
147 * Status :
148 *
149 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
150 *****************************************************************************/
151
152HANDLE WIN32API OpenEventA(DWORD fdwAccess,
153 BOOL fInherit,
154 LPCTSTR lpName)
155{
156 PHMHANDLE pHandle;
157 DWORD rc; /* API return code */
158
159 if(lpName) {
160 dprintf(("Event semaphore name %s", lpName));
161 }
162
163 pHandle = HMHandleGetFreePtr(HMTYPE_EVENTSEM); /* get free handle */
164 if (pHandle == NULL) /* oops, no free handles ! */
165 {
166 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
167 return 0;
168 }
169 pHandle->hmHandleData.dwAccess = fdwAccess;
170 /* call the device handler */
171 rc = pHandle->pDeviceHandler->OpenEvent(&pHandle->hmHandleData,
172 fInherit,
173 lpName);
174 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
175 {
176 HMHandleFree(pHandle->hmHandleData.hWin32Handle);
177 SetLastError(rc);
178 return 0; /* signal error */
179 }
180 else
181 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
182
183 return pHandle->hmHandleData.hWin32Handle; /* return valid handle */
184}
185
186/*****************************************************************************
187 * Name : HMSetEvent
188 * Purpose : router function for SetEvent
189 * Parameters:
190 * Variables :
191 * Result :
192 * Remark :
193 * Status :
194 *
195 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
196 *****************************************************************************/
197
198BOOL WIN32API SetEvent(HANDLE hEvent)
199{
200 DWORD dwResult; /* result from the device handler's API */
201 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
202
203 /* validate handle */
204 pHMHandle = HMHandleQueryPtr(hEvent); /* get the index */
205 if (pHMHandle == NULL) /* error ? */
206 {
207 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
208 }
209
210 dwResult = pHMHandle->pDeviceHandler->SetEvent(&pHMHandle->hmHandleData);
211
212 return (dwResult); /* deliver return code */
213}
214
215
216/*****************************************************************************
217 * Name : HMPulseEvent
218 * Purpose : router function for PulseEvent
219 * Parameters:
220 * Variables :
221 * Result :
222 * Remark :
223 * Status :
224 *
225 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
226 *****************************************************************************/
227
228BOOL WIN32API PulseEvent(HANDLE hEvent)
229{
230 DWORD dwResult; /* result from the device handler's API */
231 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
232
233 /* validate handle */
234 pHMHandle = HMHandleQueryPtr(hEvent); /* get the index */
235 if (pHMHandle == NULL) /* error ? */
236 {
237 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
238 }
239
240 dwResult = pHMHandle->pDeviceHandler->PulseEvent(&pHMHandle->hmHandleData);
241
242 return (dwResult); /* deliver return code */
243}
244
245
246/*****************************************************************************
247 * Name : HMResetEvent
248 * Purpose : router function for ResetEvent
249 * Parameters:
250 * Variables :
251 * Result :
252 * Remark :
253 * Status :
254 *
255 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
256 *****************************************************************************/
257
258BOOL WIN32API ResetEvent(HANDLE hEvent)
259{
260 DWORD dwResult; /* result from the device handler's API */
261 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
262
263 /* validate handle */
264 pHMHandle = HMHandleQueryPtr(hEvent); /* get the index */
265 if (pHMHandle == NULL) /* error ? */
266 {
267 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
268 }
269
270 dwResult = pHMHandle->pDeviceHandler->ResetEvent(&pHMHandle->hmHandleData);
271
272 return (dwResult); /* deliver return code */
273}
274
275
276/*****************************************************************************
277 * Name : HMCreateEvent
278 * Purpose : router function for CreateEvent
279 * Parameters:
280 * Variables :
281 * Result :
282 * Remark :
283 * Status :
284 *
285 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
286 *****************************************************************************/
287
288DWORD HMDeviceEventClass::CreateEvent(PHMHANDLEDATA pHMHandleData,
289 LPSECURITY_ATTRIBUTES lpsa,
290 BOOL fManualReset,
291 BOOL fInitialState,
292 LPCTSTR lpszEventName)
293{
294 HANDLE hOpen32;
295
296 dprintf(("KERNEL32: HandleManager::Event::CreateEvent(%08xh,%08xh,%08xh,%08xh,%s)\n",
297 pHMHandleData,
298 lpsa,
299 fManualReset,
300 fInitialState,
301 lpszEventName));
302
303 hOpen32 = O32_CreateEvent(lpsa, // call Open32
304 fManualReset,
305 fInitialState,
306 lpszEventName);
307
308 dprintf(("KERNEL32: HandleManager::Semaphore::CreateEvent hOpen32 = %p, pHMHandleData->hHMHandle = %p\n", hOpen32, pHMHandleData->hHMHandle));
309
310 if (0 != hOpen32) // check success
311 {
312 pHMHandleData->hHMHandle = hOpen32; // save handle
313 return (NO_ERROR);
314 }
315 else
316 return (GetLastError());
317}
318
319
320/*****************************************************************************
321 * Name : HMOpenEvent
322 * Purpose : router function for OpenEvent
323 * Parameters:
324 * Variables :
325 * Result :
326 * Remark :
327 * Status :
328 *
329 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
330 *****************************************************************************/
331
332DWORD HMDeviceEventClass::OpenEvent(PHMHANDLEDATA pHMHandleData,
333 BOOL fInheritHandle,
334 LPCTSTR lpszEventName)
335{
336 HANDLE hOpen32;
337
338 dprintf(("KERNEL32: HandleManager::Event::OpenEvent(%08xh,%08xh,%s)\n",
339 pHMHandleData,
340 fInheritHandle,
341 lpszEventName));
342
343 hOpen32 = O32_OpenEvent(pHMHandleData->dwAccess, // call Open32
344 fInheritHandle,
345 lpszEventName);
346
347 if (0 != hOpen32) // check success
348 {
349 pHMHandleData->hHMHandle = hOpen32; // save handle
350 return (NO_ERROR);
351 }
352 else
353 return (GetLastError());
354}
355
356/*****************************************************************************
357 * Name : HMSetEvent
358 * Purpose : router function for SetEvent
359 * Parameters:
360 * Variables :
361 * Result :
362 * Remark :
363 * Status :
364 *
365 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
366 *****************************************************************************/
367
368BOOL HMDeviceEventClass::SetEvent(PHMHANDLEDATA pHMHandleData)
369{
370 dprintf(("KERNEL32: HandleManager::Event::SetEvent(%08xh)\n",
371 pHMHandleData->hHMHandle));
372
373 return (O32_SetEvent(pHMHandleData->hHMHandle));
374}
375
376
377/*****************************************************************************
378 * Name : HMPulseEvent
379 * Purpose : router function for PulseEvent
380 * Parameters:
381 * Variables :
382 * Result :
383 * Remark :
384 * Status :
385 *
386 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
387 *****************************************************************************/
388
389BOOL HMDeviceEventClass::PulseEvent(PHMHANDLEDATA pHMHandleData)
390{
391 dprintf2(("KERNEL32: HandleManager::Event::PulseEvent(%08xh)\n",
392 pHMHandleData->hHMHandle));
393
394 return (O32_PulseEvent(pHMHandleData->hHMHandle));
395}
396
397
398/*****************************************************************************
399 * Name : HMResetEvent
400 * Purpose : router function for ResetEvent
401 * Parameters:
402 * Variables :
403 * Result :
404 * Remark :
405 * Status :
406 *
407 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
408 *****************************************************************************/
409
410BOOL HMDeviceEventClass::ResetEvent(PHMHANDLEDATA pHMHandleData)
411{
412 dprintf2(("KERNEL32: HandleManager::Event::ResetEvent(%08xh)\n",
413 pHMHandleData->hHMHandle));
414
415 return (O32_ResetEvent(pHMHandleData->hHMHandle));
416}
417
Note: See TracBrowser for help on using the repository browser.