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

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

semaphore rewrite (not activated)

File size: 9.3 KB
Line 
1/* $Id: hmmutex.cpp,v 1.4 2001-06-19 10:50:25 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#include <os2wrap.h>
34#include <win32type.h>
35#include <win32api.h>
36#include <winconst.h>
37#else
38#include <os2win.h>
39#endif
40#include <stdlib.h>
41#include <string.h>
42#include "unicode.h"
43#include "misc.h"
44#include "oslibdos.h"
45
46#include "HandleManager.H"
47#include "HMMutex.h"
48
49#define DBG_LOCALLOG DBG_hmmutex
50#include "dbglocal.h"
51
52/*****************************************************************************
53 * Defines *
54 *****************************************************************************/
55
56/*****************************************************************************
57 * Structures *
58 *****************************************************************************/
59
60/*****************************************************************************
61 * Local Prototypes *
62 *****************************************************************************/
63
64
65/*****************************************************************************
66 * Name : HMCreateMutex
67 * Purpose : router function for CreateMutex
68 * Parameters:
69 * Variables :
70 * Result :
71 * Remark :
72 * Status :
73 *
74 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
75 *****************************************************************************/
76
77DWORD HMDeviceMutexClass::CreateMutex(PHMHANDLEDATA pHMHandleData,
78 LPSECURITY_ATTRIBUTES lpsa,
79 BOOL fInitialOwner,
80 LPCTSTR lpszMutexName)
81{
82#ifdef USE_OS2SEMAPHORES
83 APIRET rc;
84 HMTX htmx;
85 char szSemName[CCHMAXPATH];
86
87 dprintf(("KERNEL32: HandleManager::Mutex::CreateMutex(%08xh,%08xh,%08xh,%s)\n",
88 pHMHandleData,
89 lpsa,
90 fInitialOwner,
91 lpszMutexName));
92
93 if(lpszMutexName) {
94 strcpy(szSemName, "\\SEM32\\");
95 strcat(szSemName, lpszMutexName);
96 lpszMutexName = szSemName;
97 }
98 rc = DosCreateMutexSem(lpszMutexName, &htmx, 0, fInitialOwner);
99
100 if(rc) {
101 dprintf(("DosCreateMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
102 pHMHandleData->hHMHandle = 0;
103 return error2WinError(rc);
104 }
105 pHMHandleData->dwAccess = MUTEX_ALL_ACCESS_W;
106 pHMHandleData->hHMHandle = htmx;
107 return ERROR_SUCCESS_W;
108#else
109 HANDLE hOpen32;
110
111 dprintf(("KERNEL32: HandleManager::Mutex::CreateMutex(%08xh,%08xh,%08xh,%s)\n",
112 pHMHandleData,
113 lpsa,
114 fInitialOwner,
115 lpszMutexName));
116
117 hOpen32 = O32_CreateMutex(lpsa, // call Open32
118 fInitialOwner,
119 lpszMutexName);
120
121 if (0 != hOpen32) // check success
122 {
123 pHMHandleData->hHMHandle = hOpen32; // save handle
124 return (NO_ERROR);
125 }
126 else
127 return (O32_GetLastError());
128#endif
129}
130
131
132/*****************************************************************************
133 * Name : HMOpenMutex
134 * Purpose : router function for OpenMutex
135 * Parameters:
136 * Variables :
137 * Result :
138 * Remark :
139 * Status :
140 *
141 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
142 *****************************************************************************/
143
144DWORD HMDeviceMutexClass::OpenMutex(PHMHANDLEDATA pHMHandleData,
145 BOOL fInheritHandle,
146 LPCTSTR lpszMutexName)
147{
148#ifdef USE_OS2SEMAPHORES
149 HMTX hmtx;
150 APIRET rc;
151 char szSemName[CCHMAXPATH];
152
153 dprintf(("KERNEL32: HandleManager::Mutex::OpenMutex(%08xh,%08xh,%s)\n",
154 pHMHandleData,
155 fInheritHandle,
156 lpszMutexName));
157
158 if(lpszMutexName == NULL) {
159 pHMHandleData->hHMHandle = 0;
160 return ERROR_INVALID_PARAMETER_W;
161 }
162
163 strcpy(szSemName, "\\SEM32\\");
164 strcat(szSemName, lpszMutexName);
165 rc = DosOpenMutexSem(szSemName, &hmtx);
166 if(rc) {
167 dprintf(("DosOpenMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
168 pHMHandleData->hHMHandle = 0;
169 return error2WinError(rc);
170 }
171 pHMHandleData->hHMHandle = hmtx;
172 return ERROR_SUCCESS_W;
173#else
174 HANDLE hOpen32;
175
176 dprintf(("KERNEL32: HandleManager::Mutex::OpenMutex(%08xh,%08xh,%s)\n",
177 pHMHandleData,
178 fInheritHandle,
179 lpszMutexName));
180
181 hOpen32 = O32_OpenMutex(pHMHandleData->dwAccess, // call Open32
182 fInheritHandle,
183 lpszMutexName);
184
185 if (0 != hOpen32) // check success
186 {
187 pHMHandleData->hHMHandle = hOpen32; // save handle
188 return (NO_ERROR);
189 }
190 else
191 return (O32_GetLastError());
192#endif
193}
194
195/*****************************************************************************
196 * Name : HMDeviceMutexClass::CloseHandle
197 * Purpose : close the handle
198 * Parameters: PHMHANDLEDATA pHMHandleData
199 * Variables :
200 * Result : API returncode
201 * Remark :
202 * Status :
203 *
204 * Author :
205 *****************************************************************************/
206
207#ifdef USE_OS2SEMAPHORES
208BOOL HMDeviceMutexClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
209{
210 APIRET rc;
211
212 if(pHMHandleData->hHMHandle) {
213 rc = DosCloseMutexSem((HEV)pHMHandleData->hHMHandle);
214 if(rc) {
215 dprintf(("DosCloseMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
216 SetLastError(error2WinError(rc));
217 return FALSE;
218 }
219 }
220 return TRUE;
221}
222#endif
223
224/*****************************************************************************
225 * Name : HMDeviceMutexClass::DuplicateHandle
226 * Purpose :
227 * Parameters:
228 * various parameters as required
229 * Variables :
230 * Result :
231 * Remark : the standard behaviour is to return an error code for non-
232 * existant request codes
233 * Status :
234 *
235 * Author :
236 *****************************************************************************/
237#ifdef USE_OS2SEMAPHORES
238BOOL HMDeviceMutexClass::DuplicateHandle(PHMHANDLEDATA pHMHandleData, HANDLE srcprocess,
239 PHMHANDLEDATA pHMSrcHandle,
240 HANDLE destprocess,
241 PHANDLE desthandle,
242 DWORD fdwAccess,
243 BOOL fInherit,
244 DWORD fdwOptions,
245 DWORD fdwOdinOptions)
246{
247 APIRET rc;
248 HMTX hmtx;
249
250 dprintf(("KERNEL32:HandleManager::DuplicateHandle %s(%08x,%08x,%08x,%08x,%08x) - NOT IMPLEMENTED!!!!!!!!\n",
251 lpHMDeviceName,
252 pHMHandleData,
253 srcprocess, pHMSrcHandle, destprocess, desthandle));
254
255 if(srcprocess != destprocess) {
256 DebugInt3();
257 SetLastError(ERROR_ACCESS_DENIED_W);
258 return FALSE;
259 }
260 hmtx = (HMTX)pHMSrcHandle->hHMHandle;
261 rc = DosOpenMutexSem(NULL, &hmtx);
262 if(rc) {
263 dprintf(("DosOpenMutexSem %x failed with rc %d", pHMSrcHandle->hHMHandle, rc));
264 pHMHandleData->hHMHandle = 0;
265 SetLastError(error2WinError(rc));
266 return FALSE;
267 }
268 pHMHandleData->dwAccess = fdwAccess;
269 pHMHandleData->hHMHandle = hmtx;
270 SetLastError(ERROR_SUCCESS_W);
271 return TRUE;
272}
273#endif
274
275/*****************************************************************************
276 * Name : HMReleaseMutex
277 * Purpose : router function for ReleaseMutex
278 * Parameters:
279 * Variables :
280 * Result :
281 * Remark :
282 * Status :
283 *
284 * Author : Patrick Haller [Tue, 1999/07/06 20:44]
285 *****************************************************************************/
286
287BOOL HMDeviceMutexClass::ReleaseMutex(PHMHANDLEDATA pHMHandleData)
288{
289#ifdef USE_OS2SEMAPHORES
290 APIRET rc;
291
292 dprintf(("KERNEL32: HandleManager::Mutex::ReleaseMutex(%08xh)\n",
293 pHMHandleData->hHMHandle));
294
295 rc = DosReleaseMutexSem(pHMHandleData->hHMHandle);
296 if(rc) {
297 dprintf(("DosReleaseMutexSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
298 SetLastError(error2WinError(rc));
299 return FALSE;
300 }
301 SetLastError(ERROR_SUCCESS_W);
302 return TRUE;
303#else
304 dprintf(("KERNEL32: HandleManager::Mutex::ReleaseMutex(%08xh)\n",
305 pHMHandleData->hHMHandle));
306
307 return (O32_ReleaseMutex(pHMHandleData->hHMHandle));
308#endif
309}
310
Note: See TracBrowser for help on using the repository browser.