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

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

semaphore updates

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