source: trunk/src/kernel32/hmmutex.cpp@ 6060

Last change on this file since 6060 was 6060, checked in by sandervl, 24 years ago

semaphore updates

File size: 14.1 KB
Line 
1/* $Id: hmmutex.cpp,v 1.5 2001-06-21 21:07:53 sandervl Exp $ */
2
3/*
4 * Win32 Mutex Semaphore implementation
5 *
6 * TODO: Inheritance
7 * TODO: No inheritance when CreateMutex is called for existing named event semaphore?
8 * (see HMCreateMutex in handlemanager.cpp)
9 * TODO: Name collisions with files & mutex not allowed. Check if this can happen in OS/2
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 * Copyright 2001 Sander van Leeuwen (sandervl@xs4all.nl)
14 */
15
16#undef DEBUG_LOCAL
17//#define DEBUG_LOCAL
18
19
20/*****************************************************************************
21 * Remark *
22 *****************************************************************************
23
24 */
25
26
27/*****************************************************************************
28 * Includes *
29 *****************************************************************************/
30
31#ifdef USE_OS2SEMAPHORES
32#define INCL_DOSSEMAPHORES
33#define INCL_DOSERRORS
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#include <stdlib.h>
42#include <string.h>
43#include "unicode.h"
44#include "misc.h"
45#include "oslibdos.h"
46
47#include "HandleManager.H"
48#include "HMMutex.h"
49
50#define DBG_LOCALLOG DBG_hmmutex
51#include "dbglocal.h"
52
53/*****************************************************************************
54 * Defines *
55 *****************************************************************************/
56
57/*****************************************************************************
58 * Structures *
59 *****************************************************************************/
60
61/*****************************************************************************
62 * Local Prototypes *
63 *****************************************************************************/
64
65
66/*****************************************************************************
67 * Name : HMCreateMutex
68 * Purpose : router function for CreateMutex
69 * Parameters:
70 * Variables :
71 * Result :
72 * Remark :
73 * Status :
74 *
75 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
76 *****************************************************************************/
77
78DWORD HMDeviceMutexClass::CreateMutex(PHMHANDLEDATA pHMHandleData,
79 LPSECURITY_ATTRIBUTES lpsa,
80 BOOL fInitialOwner,
81 LPCTSTR lpszMutexName)
82{
83#ifdef USE_OS2SEMAPHORES
84 APIRET rc;
85 HMTX htmx;
86 char szSemName[CCHMAXPATH];
87
88 dprintf(("KERNEL32: HandleManager::Mutex::CreateMutex(%08xh,%08xh,%08xh,%s)\n",
89 pHMHandleData,
90 lpsa,
91 fInitialOwner,
92 lpszMutexName));
93
94 if(lpszMutexName) {
95 strcpy(szSemName, "\\SEM32\\");
96 strcat(szSemName, lpszMutexName);
97 lpszMutexName = szSemName;
98 }
99 rc = DosCreateMutexSem(lpszMutexName, &htmx, 0, fInitialOwner);
100
101 if(rc) {
102 dprintf(("DosCreateMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
103 pHMHandleData->hHMHandle = 0;
104 return error2WinError(rc);
105 }
106 pHMHandleData->dwAccess = MUTEX_ALL_ACCESS_W;
107 pHMHandleData->hHMHandle = htmx;
108 return ERROR_SUCCESS_W;
109#else
110 HANDLE hOpen32;
111
112 dprintf(("KERNEL32: HandleManager::Mutex::CreateMutex(%08xh,%08xh,%08xh,%s)\n",
113 pHMHandleData,
114 lpsa,
115 fInitialOwner,
116 lpszMutexName));
117
118 hOpen32 = O32_CreateMutex(lpsa, // call Open32
119 fInitialOwner,
120 lpszMutexName);
121
122 if (0 != hOpen32) // check success
123 {
124 pHMHandleData->hHMHandle = hOpen32; // save handle
125 return (NO_ERROR);
126 }
127 else
128 return (O32_GetLastError());
129#endif
130}
131
132
133/*****************************************************************************
134 * Name : HMOpenMutex
135 * Purpose : router function for OpenMutex
136 * Parameters:
137 * Variables :
138 * Result :
139 * Remark :
140 * Status :
141 *
142 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
143 *****************************************************************************/
144
145DWORD HMDeviceMutexClass::OpenMutex(PHMHANDLEDATA pHMHandleData,
146 BOOL fInheritHandle,
147 LPCTSTR lpszMutexName)
148{
149#ifdef USE_OS2SEMAPHORES
150 HMTX hmtx;
151 APIRET rc;
152 char szSemName[CCHMAXPATH];
153
154 dprintf(("KERNEL32: HandleManager::Mutex::OpenMutex(%08xh,%08xh,%s)\n",
155 pHMHandleData,
156 fInheritHandle,
157 lpszMutexName));
158
159 if(lpszMutexName == NULL) {
160 pHMHandleData->hHMHandle = 0;
161 return ERROR_INVALID_PARAMETER_W;
162 }
163
164 strcpy(szSemName, "\\SEM32\\");
165 strcat(szSemName, lpszMutexName);
166 rc = DosOpenMutexSem(szSemName, &hmtx);
167 if(rc) {
168 dprintf(("DosOpenMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
169 pHMHandleData->hHMHandle = 0;
170 return error2WinError(rc);
171 }
172 pHMHandleData->hHMHandle = hmtx;
173 return ERROR_SUCCESS_W;
174#else
175 HANDLE hOpen32;
176
177 dprintf(("KERNEL32: HandleManager::Mutex::OpenMutex(%08xh,%08xh,%s)\n",
178 pHMHandleData,
179 fInheritHandle,
180 lpszMutexName));
181
182 hOpen32 = O32_OpenMutex(pHMHandleData->dwAccess, // call Open32
183 fInheritHandle,
184 lpszMutexName);
185
186 if (0 != hOpen32) // check success
187 {
188 pHMHandleData->hHMHandle = hOpen32; // save handle
189 return (NO_ERROR);
190 }
191 else
192 return (O32_GetLastError());
193#endif
194}
195
196/*****************************************************************************
197 * Name : HMDeviceMutexClass::CloseHandle
198 * Purpose : close the handle
199 * Parameters: PHMHANDLEDATA pHMHandleData
200 * Variables :
201 * Result : API returncode
202 * Remark :
203 * Status :
204 *
205 * Author :
206 *****************************************************************************/
207
208#ifdef USE_OS2SEMAPHORES
209BOOL HMDeviceMutexClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
210{
211 APIRET rc;
212
213 if(pHMHandleData->hHMHandle) {
214 rc = DosCloseMutexSem((HEV)pHMHandleData->hHMHandle);
215 if(rc) {
216 dprintf(("DosCloseMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
217 SetLastError(error2WinError(rc));
218 return FALSE;
219 }
220 }
221 return TRUE;
222}
223#endif
224
225/*****************************************************************************
226 * Name : HMDeviceMutexClass::DuplicateHandle
227 * Purpose :
228 * Parameters:
229 * various parameters as required
230 * Variables :
231 * Result :
232 * Remark : the standard behaviour is to return an error code for non-
233 * existant request codes
234 * Status :
235 *
236 * Author :
237 *****************************************************************************/
238#ifdef USE_OS2SEMAPHORES
239BOOL HMDeviceMutexClass::DuplicateHandle(PHMHANDLEDATA pHMHandleData, HANDLE srcprocess,
240 PHMHANDLEDATA pHMSrcHandle,
241 HANDLE destprocess,
242 PHANDLE desthandle,
243 DWORD fdwAccess,
244 BOOL fInherit,
245 DWORD fdwOptions,
246 DWORD fdwOdinOptions)
247{
248 APIRET rc;
249 HMTX hmtx;
250
251 dprintf(("KERNEL32:HandleManager::DuplicateHandle %s(%08x,%08x,%08x,%08x,%08x) - NOT IMPLEMENTED!!!!!!!!\n",
252 lpHMDeviceName,
253 pHMHandleData,
254 srcprocess, pHMSrcHandle, destprocess, desthandle));
255
256 if(srcprocess != destprocess) {
257 DebugInt3();
258 SetLastError(ERROR_ACCESS_DENIED_W);
259 return FALSE;
260 }
261 hmtx = (HMTX)pHMSrcHandle->hHMHandle;
262 rc = DosOpenMutexSem(NULL, &hmtx);
263 if(rc) {
264 dprintf(("DosOpenMutexSem %x failed with rc %d", pHMSrcHandle->hHMHandle, rc));
265 pHMHandleData->hHMHandle = 0;
266 SetLastError(error2WinError(rc));
267 return FALSE;
268 }
269 pHMHandleData->dwAccess = fdwAccess;
270 pHMHandleData->hHMHandle = hmtx;
271 SetLastError(ERROR_SUCCESS_W);
272 return TRUE;
273}
274#endif
275
276
277#ifdef USE_OS2SEMAPHORES
278/*****************************************************************************
279 * Name : DWORD HMDeviceOpen32Class::WaitForSingleObject
280 * Purpose : object synchronization
281 * Parameters: PHMHANDLEDATA pHMHandleData
282 * DWORD dwTimeout
283 * Variables :
284 * Result : API returncode
285 * Remark :
286 * Status :
287 *
288 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
289 *****************************************************************************/
290
291DWORD HMDeviceMutexClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
292 DWORD dwTimeout)
293{
294 DWORD rc;
295
296 dprintf2(("KERNEL32: HMDeviceMutexClass::WaitForSingleObject(%08xh %08xh)",
297 pHMHandleData->hHMHandle, dwTimeout));
298
299 if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
300 {
301 dprintf(("ERROR: Access denied!!"));
302 SetLastError(ERROR_ACCESS_DENIED_W);
303 return FALSE;
304 }
305
306 rc = DosRequestMutexSem(pHMHandleData->hHMHandle, dwTimeout);
307 if(rc && rc != ERROR_INTERRUPT && rc != ERROR_TIMEOUT && rc != ERROR_SEM_OWNER_DIED) {
308 dprintf(("DosWaitEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
309 SetLastError(error2WinError(rc));
310 return WAIT_FAILED_W;
311 }
312 SetLastError(ERROR_SUCCESS_W);
313 if(rc == ERROR_INTERRUPT || rc == ERROR_SEM_OWNER_DIED) {
314 return WAIT_ABANDONED_W;
315 }
316 else
317 if(rc == ERROR_TIMEOUT) {
318 return WAIT_TIMEOUT_W;
319 }
320 return WAIT_OBJECT_0_W;
321}
322#endif
323
324#ifdef USE_OS2SEMAPHORES
325/*****************************************************************************
326 * Name : DWORD HMDeviceOpen32Class::WaitForSingleObjectEx
327 * Purpose : object synchronization
328 * Parameters: PHMHANDLEDATA pHMHandleData
329 * DWORD dwTimeout
330 * BOOL fAlertable
331 * Variables :
332 * Result : API returncode
333 * Remark :
334 * Status :
335 *
336 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
337 *****************************************************************************/
338
339DWORD HMDeviceMutexClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData,
340 DWORD dwTimeout,
341 BOOL fAlertable)
342{
343 dprintf2(("KERNEL32: HMDeviceMutexClass::WaitForSingleObjectEx(%08xh,%08h,%08xh) not implemented correctly.\n",
344 pHMHandleData->hHMHandle, dwTimeout, fAlertable));
345
346 if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
347 {
348 dprintf(("ERROR: Access denied!!"));
349 SetLastError(ERROR_ACCESS_DENIED_W);
350 return WAIT_FAILED_W;
351 }
352
353 return WaitForSingleObject(pHMHandleData, dwTimeout);
354}
355#endif
356
357#ifdef USE_OS2SEMAPHORES
358/*****************************************************************************
359 * Name : BOOL HMDeviceMutexClass::MsgWaitForMultipleObjects
360 * Purpose :
361 * Variables :
362 * Result :
363 * Remark :
364 * Status :
365 *
366 * Author : SvL
367 *****************************************************************************/
368DWORD HMDeviceMutexClass::MsgWaitForMultipleObjects(PHMHANDLEDATA pHMHandleData,
369 DWORD nCount,
370 PHANDLE pHandles,
371 BOOL fWaitAll,
372 DWORD dwMilliseconds,
373 DWORD dwWakeMask)
374{
375 dprintf(("KERNEL32: ERROR: HandleManager::DeviceHandler::MsgWaitForMultipleObjects %08x %d %x %d %d %x",
376 pHMHandleData->hHMHandle, nCount, pHandles, fWaitAll, dwMilliseconds, dwWakeMask));
377
378 if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
379 {
380 dprintf(("ERROR: Access denied!!"));
381 SetLastError(ERROR_ACCESS_DENIED_W);
382 return WAIT_FAILED_W;
383 }
384 return WAIT_FAILED_W;
385}
386#endif
387
388#ifdef USE_OS2SEMAPHORES
389/*****************************************************************************
390 * Name : BOOL HMDeviceMutexClass::WaitForMultipleObjects
391 * Purpose :
392 * Variables :
393 * Result :
394 * Remark :
395 * Status :
396 *
397 * Author : SvL
398 *****************************************************************************/
399DWORD HMDeviceMutexClass::WaitForMultipleObjects(PHMHANDLEDATA pHMHandleData,
400 DWORD cObjects,
401 PHANDLE lphObjects,
402 BOOL fWaitAll,
403 DWORD dwTimeout)
404{
405 dprintf(("KERNEL32: ERROR: HandleManager::DeviceHandler::WaitForMultipleObjects %08x %d %x %d %x",
406 pHMHandleData->hHMHandle, cObjects, lphObjects, fWaitAll, dwTimeout));
407
408 if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
409 {
410 dprintf(("ERROR: Access denied!!"));
411 SetLastError(ERROR_ACCESS_DENIED_W);
412 return WAIT_FAILED_W;
413 }
414 return WAIT_FAILED_W;
415}
416#endif
417
418/*****************************************************************************
419 * Name : HMReleaseMutex
420 * Purpose : router function for ReleaseMutex
421 * Parameters:
422 * Variables :
423 * Result :
424 * Remark :
425 * Status :
426 *
427 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
428 *****************************************************************************/
429
430BOOL HMDeviceMutexClass::ReleaseMutex(PHMHANDLEDATA pHMHandleData)
431{
432#ifdef USE_OS2SEMAPHORES
433 APIRET rc;
434
435 dprintf(("KERNEL32: HandleManager::Mutex::ReleaseMutex(%08xh)\n",
436 pHMHandleData->hHMHandle));
437
438 rc = DosReleaseMutexSem(pHMHandleData->hHMHandle);
439 if(rc) {
440 dprintf(("DosReleaseMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
441 SetLastError(error2WinError(rc));
442 return FALSE;
443 }
444 SetLastError(ERROR_SUCCESS_W);
445 return TRUE;
446#else
447 dprintf(("KERNEL32: HandleManager::Mutex::ReleaseMutex(%08xh)\n",
448 pHMHandleData->hHMHandle));
449
450 return (O32_ReleaseMutex(pHMHandleData->hHMHandle));
451#endif
452}
453
Note: See TracBrowser for help on using the repository browser.