2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis
10 #include <sys/times.h>
30 THDB
*owning_thread
; /* we are locked by this thread */
37 /***********************************************************************
38 * CreateMutexA (KERNEL32.52)
40 HANDLE32 WINAPI
CreateMutex32A(SECURITY_ATTRIBUTES
*sa
,BOOL32 on
,LPCSTR name
)
44 K32OBJ
*obj
= K32OBJ_FindName( name
);
47 if (obj
->type
== K32OBJ_MUTEX
) {
48 SetLastError( ERROR_ALREADY_EXISTS
);
49 return PROCESS_AllocHandle( obj
,0 );
51 SetLastError( ERROR_DUP_NAME
);
54 mut
= (K32MUTEX
*)HeapAlloc(GetProcessHeap(),0,sizeof(K32MUTEX
));
55 mut
->header
.type
= K32OBJ_MUTEX
;
56 mut
->header
.refcount
= 1;
57 mut
->owning_thread
= NULL
;
59 K32OBJ_AddName(&(mut
->header
),name
);
60 handle
= PROCESS_AllocHandle(&(mut
->header
),0);
61 if (handle
!= INVALID_HANDLE_VALUE32
) {
63 mut
->owning_thread
= (THDB
*)GetCurrentThreadId();
66 K32OBJ_DecCount(&(mut
->header
)); /* also frees name */
67 HeapFree(GetProcessHeap(),0,mut
);
71 /***********************************************************************
72 * CreateMutexW (KERNEL32.53)
74 HANDLE32 WINAPI
CreateMutex32W(SECURITY_ATTRIBUTES
*sa
, BOOL32 on
, LPCWSTR a
)
76 LPSTR name
= a
?HEAP_strdupWtoA(GetProcessHeap(),0,a
):NULL
;
79 ret
= CreateMutex32A(sa
,on
,name
);
80 if (name
) HeapFree(GetProcessHeap(),0,name
);
84 /***********************************************************************
85 * CreateSemaphoreA (KERNEL32.60)
87 HANDLE32 WINAPI
CreateSemaphore32A( LPSECURITY_ATTRIBUTES sa
,
88 LONG initial
,LONG max
,LPCSTR name
)
92 K32OBJ
*obj
= K32OBJ_FindName( name
);
95 if (obj
->type
== K32OBJ_SEMAPHORE
) {
96 SetLastError( ERROR_ALREADY_EXISTS
);
97 return PROCESS_AllocHandle( obj
,0 );
99 SetLastError( ERROR_DUP_NAME
);
102 sem
= (K32SEMAPHORE
*)HeapAlloc(GetProcessHeap(),0,sizeof(K32SEMAPHORE
));
103 sem
->header
.type
= K32OBJ_SEMAPHORE
;
104 sem
->header
.refcount
= 1;
107 sem
->count
= initial
;
108 sem
->initial
= initial
;
110 K32OBJ_AddName(&(sem
->header
),name
);
111 handle
= PROCESS_AllocHandle(&(sem
->header
),0);
112 if (handle
!= INVALID_HANDLE_VALUE32
)
114 K32OBJ_DecCount(&(sem
->header
)); /* also frees name */
115 HeapFree(GetProcessHeap(),0,sem
);
119 /***********************************************************************
120 * CreateSemaphoreW (KERNEL32.61)
122 HANDLE32 WINAPI
CreateSemaphore32W(SECURITY_ATTRIBUTES
*sa
,LONG initial
,LONG max
,LPCWSTR a
)
124 LPSTR name
=a
?HEAP_strdupWtoA(GetProcessHeap(),0,a
):NULL
;
127 ret
= CreateSemaphore32A(sa
,initial
,max
,name
);
128 if (a
) HeapFree(GetProcessHeap(),0,name
);
133 /***********************************************************************
134 * OpenSemaphoreA (KERNEL32.403)
136 HANDLE32 WINAPI
OpenSemaphore32A(DWORD desired
,BOOL32 inherit
,LPCSTR name
)
138 K32OBJ
*obj
= K32OBJ_FindName( name
);
141 if (obj
->type
== K32OBJ_SEMAPHORE
)
142 return PROCESS_AllocHandle( obj
,0 );
143 SetLastError( ERROR_DUP_NAME
);
149 /***********************************************************************
150 * OpenSemaphoreA (KERNEL32.404)
152 HANDLE32 WINAPI
OpenSemaphore32W(DWORD desired
,BOOL32 inherit
,LPCWSTR name
)
154 LPSTR nameA
= name
?HEAP_strdupWtoA(GetProcessHeap(),0,name
):NULL
;
155 HANDLE32 ret
= OpenSemaphore32A(desired
,inherit
,nameA
);
157 if (name
) HeapFree(GetProcessHeap(),0,nameA
);
161 /***********************************************************************
162 * ReleaseSemaphore (KERNEL32.403)
164 BOOL32 WINAPI
ReleaseSemaphore(HANDLE32 hSemaphore
,LONG lReleaseCount
,LPLONG lpPreviousCount
)
168 sem
= (K32SEMAPHORE
*)PROCESS_GetObjPtr(hSemaphore
,K32OBJ_SEMAPHORE
);
171 if (lpPreviousCount
) *lpPreviousCount
= sem
->count
;
172 sem
->count
+= lReleaseCount
;
173 if (sem
->count
>sem
->maxcount
) {
174 fprintf(stderr
,"ReleaseSemaphore(%d,%ld,.), released more then possible??\n",hSemaphore
,lReleaseCount
);
175 sem
->count
= sem
->maxcount
;
178 /* FIXME: wake up all threads blocked on that semaphore */
180 K32OBJ_DecCount(&(sem
->header
));
184 /***********************************************************************
185 * OpenMutexA (KERNEL32.399)
187 HANDLE32 WINAPI
OpenMutex32A(DWORD desiredaccess
, BOOL32 inherithandle
, LPCSTR name
)
189 K32OBJ
*obj
= K32OBJ_FindName( name
);
192 if (obj
->type
== K32OBJ_MUTEX
)
193 return PROCESS_AllocHandle( obj
,0 );
194 SetLastError( ERROR_DUP_NAME
);
200 /***********************************************************************
201 * OpenMutexW (KERNEL32.400)
203 HANDLE32 WINAPI
OpenMutex32W(DWORD desiredaccess
, BOOL32 inherithandle
,
206 LPSTR nameA
=name
?HEAP_strdupWtoA(GetProcessHeap(),0,name
):NULL
;
207 HANDLE32 ret
= OpenMutex32A(desiredaccess
,inherithandle
,nameA
);
209 if (name
) HeapFree(GetProcessHeap(),0,nameA
);
213 /***********************************************************************
214 * ReleaseMutex (KERNEL32.435)
216 BOOL32 WINAPI
ReleaseMutex (HANDLE32 h
)
218 K32MUTEX
*mut
= (K32MUTEX
*)PROCESS_GetObjPtr(h
,K32OBJ_MUTEX
);
222 if (mut
->owning_thread
!= (THDB
*)GetCurrentThreadId()) {
224 K32OBJ_DecCount(&(mut
->header
));
227 mut
->owning_thread
= NULL
;
229 /* FIXME: wake up all threads blocked on this mutex */
231 K32OBJ_DecCount(&(mut
->header
));
235 /***********************************************************************
236 * CreateEventA (KERNEL32.43)
238 HANDLE32 WINAPI
CreateEvent32A(SECURITY_ATTRIBUTES
*sa
,BOOL32 au
,BOOL32 on
,
243 K32OBJ
*obj
= K32OBJ_FindName( name
);
246 if (obj
->type
== K32OBJ_EVENT
) {
247 SetLastError( ERROR_ALREADY_EXISTS
);
248 return PROCESS_AllocHandle( obj
,0 );
250 SetLastError( ERROR_DUP_NAME
);
253 evt
= (K32EVENT
*)HeapAlloc(GetProcessHeap(),0,sizeof(K32EVENT
));
254 evt
->header
.type
= K32OBJ_EVENT
;
255 evt
->header
.refcount
= 1;
257 K32OBJ_AddName(&(evt
->header
),name
);
258 handle
= PROCESS_AllocHandle(&(evt
->header
),0);
259 if (handle
!= INVALID_HANDLE_VALUE32
)
261 K32OBJ_DecCount(&(evt
->header
)); /* also frees name */
262 HeapFree(GetProcessHeap(),0,evt
);
266 /***********************************************************************
267 * CreateEventW (KERNEL32.43)
269 HANDLE32 WINAPI
CreateEvent32W(SECURITY_ATTRIBUTES
*sa
, BOOL32 au
,
270 BOOL32 on
,LPCWSTR name
)
272 LPSTR nameA
=name
?HEAP_strdupWtoA(GetProcessHeap(),0,name
):NULL
;
273 HANDLE32 ret
= CreateEvent32A(sa
,au
,on
,nameA
);
275 if (name
) HeapFree(GetProcessHeap(),0,nameA
);
279 /***********************************************************************
280 * OpenEventA (KERNEL32.394)
282 HANDLE32 WINAPI
OpenEvent32A(DWORD desiredaccess
,BOOL32 inherithandle
,LPCSTR name
)
284 K32OBJ
*obj
= K32OBJ_FindName( name
);
287 if (obj
->type
== K32OBJ_EVENT
)
288 return PROCESS_AllocHandle( obj
,0 );
289 SetLastError( ERROR_DUP_NAME
);
295 /***********************************************************************
296 * OpenEventW (KERNEL32.395)
298 HANDLE32 WINAPI
OpenEvent32W(DWORD desiredaccess
,BOOL32 inherithandle
,LPCWSTR name
)
300 LPSTR nameA
= name
?HEAP_strdupWtoA(GetProcessHeap(),0,name
):NULL
;
301 HANDLE32 ret
= OpenEvent32A(desiredaccess
,inherithandle
,nameA
);
303 if (name
) HeapFree(GetProcessHeap(),0,nameA
);
307 /***********************************************************************
308 * SetEvent (KERNEL32.487)
310 BOOL32 WINAPI
SetEvent (HANDLE32 h
)
312 fprintf(stderr
,"SetEvent(%d) stub\n",h
);
315 /***********************************************************************
316 * ResetEvent (KERNEL32.439)
318 BOOL32 WINAPI
ResetEvent (HANDLE32 h
)
320 fprintf(stderr
,"ResetEvent(%d) stub\n",h
);
324 /***********************************************************************
325 * WaitForSingleObject (KERNEL32.561)
327 DWORD WINAPI
WaitForSingleObject(HANDLE32 h
, DWORD timeout
)
329 fprintf(stderr
,"WaitForSingleObject(%d,%ld) stub\n",h
,timeout
);
333 /***********************************************************************
334 * WaitForSingleObjectEx (KERNEL32)
336 DWORD WINAPI
WaitForSingleObjectEx(HANDLE32 h
,DWORD timeout
,BOOL32 bAlertable
)
338 fprintf(stderr
,"WaitForSingleObjectEx(%d,%ld,%d) stub\n",h
,timeout
,bAlertable
);
343 /***********************************************************************
344 * WaitForMultipleObjects (USER32.399)
346 DWORD WINAPI
MsgWaitForMultipleObjects(
347 DWORD nCount
,HANDLE32
*pHandles
,BOOL32 fWaitAll
,DWORD dwMilliseconds
,
351 fprintf(stderr
,"MsgWaitForMultipleObjects(%ld,[",nCount
);
352 for (i
=0;i
<nCount
;i
++)
353 fprintf(stderr
,"%ld,",(DWORD
)pHandles
[i
]);
354 fprintf(stderr
,"],%d,%ld,0x%08lx)\n",fWaitAll
,dwMilliseconds
,dwWakeMask
);
357 /***********************************************************************
358 * DuplicateHandle (KERNEL32.78)
360 BOOL32 WINAPI
DuplicateHandle(HANDLE32 a
, HANDLE32 b
, HANDLE32 c
, HANDLE32
* d
, DWORD e
, BOOL32 f
, DWORD g
)
362 fprintf(stderr
,"DuplicateHandle(%d,%d,%d,%p,%ld,%d,%ld) stub\n",a
,b
,c
,d
,e
,f
,g
);
367 /**********************************************************************
368 * GetProcessAffinityMask
370 BOOL32 WINAPI
GetProcessAffinityMask(HANDLE32 hProcess
,
371 LPDWORD lpProcessAffinityMask
,
372 LPDWORD lpSystemAffinityMask
)
374 dprintf_task(stddeb
,"GetProcessAffinityMask(%x,%lx,%lx)\n",
375 hProcess
,(lpProcessAffinityMask
?*lpProcessAffinityMask
:0),
376 (lpSystemAffinityMask
?*lpSystemAffinityMask
:0));
377 /* It is definitely important for a process to know on what processor
379 if(lpProcessAffinityMask
)
380 *lpProcessAffinityMask
=1;
381 if(lpSystemAffinityMask
)
382 *lpSystemAffinityMask
=1;
386 /**********************************************************************
387 * SetThreadAffinityMask
388 * Works now like the Windows95 (no MP support) version
390 BOOL32 WINAPI
SetThreadAffinityMask(HANDLE32 hThread
, DWORD dwThreadAffinityMask
)
392 THDB
*thdb
= (THDB
*)PROCESS_GetObjPtr(hThread
,K32OBJ_THREAD
);
396 if (dwThreadAffinityMask
!=1) {
397 fprintf(stderr
,"SetThreadAffinityMask(%d,%ld), only 1 processor supported.\n",(int)hThread
,dwThreadAffinityMask
);
398 K32OBJ_DecCount((K32OBJ
*)thdb
);
401 K32OBJ_DecCount((K32OBJ
*)thdb
);
405 BOOL32 WINAPI
CreateProcess32A(
406 LPCSTR appname
,LPSTR cmdline
,LPSECURITY_ATTRIBUTES processattributes
,
407 LPSECURITY_ATTRIBUTES threadattributes
,BOOL32 inherithandles
,
408 DWORD creationflags
,LPVOID env
,LPCSTR curdir
,
409 LPSTARTUPINFO32A startupinfo
,LPPROCESS_INFORMATION processinfo
411 fprintf(stderr
,"CreateProcess(%s,%s,%p,%p,%d,%08lx,%p,%s,%p,%p)\n",
412 appname
,cmdline
,processattributes
,threadattributes
,
413 inherithandles
,creationflags
,env
,curdir
,startupinfo
,processinfo
415 /* make from lcc uses system as fallback if CreateProcess returns
416 FALSE, so return false */
420 BOOL32 WINAPI
ContinueDebugEvent(DWORD pid
,DWORD tid
,DWORD contstatus
) {
421 fprintf(stderr
,"ContinueDebugEvent(%ld,%ld,%ld), stub\n",pid
,tid
,contstatus
);
425 /*********************************************************************
426 * GetProcessTimes [KERNEL32.262]
428 * FIXME: implement this better ...
430 BOOL32 WINAPI
GetProcessTimes(
431 HANDLE32 hprocess
,LPFILETIME lpCreationTime
,LPFILETIME lpExitTime
,
432 LPFILETIME lpKernelTime
, LPFILETIME lpUserTime
437 DOSFS_UnixTimeToFileTime(tms
.tms_utime
,lpUserTime
,0);
438 DOSFS_UnixTimeToFileTime(tms
.tms_stime
,lpKernelTime
,0);