2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
15 #include "wine/server.h"
16 #include "wine/unicode.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(win32);
27 /***********************************************************************
28 * CreateEventA (KERNEL32.@)
30 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
31 BOOL initial_state, LPCSTR name )
33 WCHAR buffer[MAX_PATH];
35 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
37 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
39 SetLastError( ERROR_FILENAME_EXCED_RANGE );
42 return CreateEventW( sa, manual_reset, initial_state, buffer );
46 /***********************************************************************
47 * CreateEventW (KERNEL32.@)
49 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
50 BOOL initial_state, LPCWSTR name )
53 DWORD len = name ? strlenW(name) : 0;
56 SetLastError( ERROR_FILENAME_EXCED_RANGE );
59 /* one buggy program needs this
60 * ("Van Dale Groot woordenboek der Nederlandse taal")
62 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
64 ERR("Bad security attributes pointer %p\n",sa);
65 SetLastError( ERROR_INVALID_PARAMETER);
68 SERVER_START_REQ( create_event )
70 req->manual_reset = manual_reset;
71 req->initial_state = initial_state;
72 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
73 wine_server_add_data( req, name, len * sizeof(WCHAR) );
75 wine_server_call_err( req );
83 /***********************************************************************
84 * CreateW32Event (KERNEL.457)
86 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
88 return CreateEventA( NULL, manual_reset, initial_state, NULL );
92 /***********************************************************************
93 * OpenEventA (KERNEL32.@)
95 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
97 WCHAR buffer[MAX_PATH];
99 if (!name) return OpenEventW( access, inherit, NULL );
101 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
103 SetLastError( ERROR_FILENAME_EXCED_RANGE );
106 return OpenEventW( access, inherit, buffer );
110 /***********************************************************************
111 * OpenEventW (KERNEL32.@)
113 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
116 DWORD len = name ? strlenW(name) : 0;
119 SetLastError( ERROR_FILENAME_EXCED_RANGE );
122 SERVER_START_REQ( open_event )
124 req->access = access;
125 req->inherit = inherit;
126 wine_server_add_data( req, name, len * sizeof(WCHAR) );
127 wine_server_call_err( req );
135 /***********************************************************************
138 * Execute an event operation (set,reset,pulse).
140 static BOOL EVENT_Operation( HANDLE handle, enum event_op op )
143 SERVER_START_REQ( event_op )
145 req->handle = handle;
147 ret = !wine_server_call_err( req );
154 /***********************************************************************
155 * PulseEvent (KERNEL32.@)
157 BOOL WINAPI PulseEvent( HANDLE handle )
159 return EVENT_Operation( handle, PULSE_EVENT );
163 /***********************************************************************
164 * SetW32Event (KERNEL.458)
165 * SetEvent (KERNEL32.@)
167 BOOL WINAPI SetEvent( HANDLE handle )
169 return EVENT_Operation( handle, SET_EVENT );
173 /***********************************************************************
174 * ResetW32Event (KERNEL.459)
175 * ResetEvent (KERNEL32.@)
177 BOOL WINAPI ResetEvent( HANDLE handle )
179 return EVENT_Operation( handle, RESET_EVENT );
183 /***********************************************************************
184 * NOTE: The Win95 VWin32_Event routines given below are really low-level
185 * routines implemented directly by VWin32. The user-mode libraries
186 * implement Win32 synchronisation routines on top of these low-level
187 * primitives. We do it the other way around here :-)
190 /***********************************************************************
191 * VWin32_EventCreate (KERNEL.442)
193 HANDLE WINAPI VWin32_EventCreate(VOID)
195 HANDLE hEvent = CreateEventA( NULL, FALSE, 0, NULL );
196 return ConvertToGlobalHandle( hEvent );
199 /***********************************************************************
200 * VWin32_EventDestroy (KERNEL.443)
202 VOID WINAPI VWin32_EventDestroy(HANDLE event)
204 CloseHandle( event );
207 /***********************************************************************
208 * VWin32_EventWait (KERNEL.450)
210 VOID WINAPI VWin32_EventWait(HANDLE event)
214 ReleaseThunkLock( &mutex_count );
215 WaitForSingleObject( event, INFINITE );
216 RestoreThunkLock( mutex_count );
219 /***********************************************************************
220 * VWin32_EventSet (KERNEL.451)
221 * KERNEL_479 (KERNEL.479)
223 VOID WINAPI VWin32_EventSet(HANDLE event)
230 /***********************************************************************
231 * CreateMutexA (KERNEL32.@)
233 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
235 WCHAR buffer[MAX_PATH];
237 if (!name) return CreateMutexW( sa, owner, NULL );
239 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
241 SetLastError( ERROR_FILENAME_EXCED_RANGE );
244 return CreateMutexW( sa, owner, buffer );
248 /***********************************************************************
249 * CreateMutexW (KERNEL32.@)
251 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
254 DWORD len = name ? strlenW(name) : 0;
257 SetLastError( ERROR_FILENAME_EXCED_RANGE );
260 SERVER_START_REQ( create_mutex )
263 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
264 wine_server_add_data( req, name, len * sizeof(WCHAR) );
266 wine_server_call_err( req );
274 /***********************************************************************
275 * OpenMutexA (KERNEL32.@)
277 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
279 WCHAR buffer[MAX_PATH];
281 if (!name) return OpenMutexW( access, inherit, NULL );
283 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
285 SetLastError( ERROR_FILENAME_EXCED_RANGE );
288 return OpenMutexW( access, inherit, buffer );
292 /***********************************************************************
293 * OpenMutexW (KERNEL32.@)
295 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
298 DWORD len = name ? strlenW(name) : 0;
301 SetLastError( ERROR_FILENAME_EXCED_RANGE );
304 SERVER_START_REQ( open_mutex )
306 req->access = access;
307 req->inherit = inherit;
308 wine_server_add_data( req, name, len * sizeof(WCHAR) );
309 wine_server_call_err( req );
317 /***********************************************************************
318 * ReleaseMutex (KERNEL32.@)
320 BOOL WINAPI ReleaseMutex( HANDLE handle )
323 SERVER_START_REQ( release_mutex )
325 req->handle = handle;
326 ret = !wine_server_call_err( req );
338 /***********************************************************************
339 * CreateSemaphoreA (KERNEL32.@)
341 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
343 WCHAR buffer[MAX_PATH];
345 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
347 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
349 SetLastError( ERROR_FILENAME_EXCED_RANGE );
352 return CreateSemaphoreW( sa, initial, max, buffer );
356 /***********************************************************************
357 * CreateSemaphoreW (KERNEL32.@)
359 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
360 LONG max, LPCWSTR name )
363 DWORD len = name ? strlenW(name) : 0;
365 /* Check parameters */
367 if ((max <= 0) || (initial < 0) || (initial > max))
369 SetLastError( ERROR_INVALID_PARAMETER );
374 SetLastError( ERROR_FILENAME_EXCED_RANGE );
378 SERVER_START_REQ( create_semaphore )
380 req->initial = (unsigned int)initial;
381 req->max = (unsigned int)max;
382 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
383 wine_server_add_data( req, name, len * sizeof(WCHAR) );
385 wine_server_call_err( req );
393 /***********************************************************************
394 * OpenSemaphoreA (KERNEL32.@)
396 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
398 WCHAR buffer[MAX_PATH];
400 if (!name) return OpenSemaphoreW( access, inherit, NULL );
402 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
404 SetLastError( ERROR_FILENAME_EXCED_RANGE );
407 return OpenSemaphoreW( access, inherit, buffer );
411 /***********************************************************************
412 * OpenSemaphoreW (KERNEL32.@)
414 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
417 DWORD len = name ? strlenW(name) : 0;
420 SetLastError( ERROR_FILENAME_EXCED_RANGE );
423 SERVER_START_REQ( open_semaphore )
425 req->access = access;
426 req->inherit = inherit;
427 wine_server_add_data( req, name, len * sizeof(WCHAR) );
428 wine_server_call_err( req );
436 /***********************************************************************
437 * ReleaseSemaphore (KERNEL32.@)
439 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
441 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
442 if (status) SetLastError( RtlNtStatusToDosError(status) );
452 /***********************************************************************
453 * CreateNamedPipeA (KERNEL32.@)
455 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
456 DWORD dwPipeMode, DWORD nMaxInstances,
457 DWORD nOutBufferSize, DWORD nInBufferSize,
458 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
460 WCHAR buffer[MAX_PATH];
462 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
463 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
465 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
467 SetLastError( ERROR_FILENAME_EXCED_RANGE );
470 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
471 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
475 /***********************************************************************
476 * CreateNamedPipeW (KERNEL32.@)
478 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
479 DWORD dwPipeMode, DWORD nMaxInstances,
480 DWORD nOutBufferSize, DWORD nInBufferSize,
481 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
484 DWORD len = name ? strlenW(name) : 0;
486 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
487 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
488 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
492 SetLastError( ERROR_FILENAME_EXCED_RANGE );
495 SERVER_START_REQ( create_named_pipe )
497 req->openmode = dwOpenMode;
498 req->pipemode = dwPipeMode;
499 req->maxinstances = nMaxInstances;
500 req->outsize = nOutBufferSize;
501 req->insize = nInBufferSize;
502 req->timeout = nDefaultTimeOut;
503 wine_server_add_data( req, name, len * sizeof(WCHAR) );
505 wine_server_call_err( req );
513 /***********************************************************************
514 * PeekNamedPipe (KERNEL32.@)
516 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
517 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
519 FIXME("(%08x, %p, %08lx, %p, %p, %p): stub\n",
520 hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
521 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
525 /***********************************************************************
526 * SYNC_CompletePipeOverlapped (Internal)
528 static void SYNC_CompletePipeOverlapped (LPOVERLAPPED overlapped, DWORD result)
530 TRACE("for %p result %08lx\n",overlapped,result);
533 overlapped->Internal = result;
534 SetEvent(overlapped->hEvent);
538 /***********************************************************************
539 * WaitNamedPipeA (KERNEL32.@)
541 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
543 WCHAR buffer[MAX_PATH];
545 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
547 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
549 SetLastError( ERROR_FILENAME_EXCED_RANGE );
552 return WaitNamedPipeW( buffer, nTimeOut );
556 /***********************************************************************
557 * WaitNamedPipeW (KERNEL32.@)
559 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
561 DWORD len = name ? strlenW(name) : 0;
567 SetLastError( ERROR_FILENAME_EXCED_RANGE );
571 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
573 memset(&ov,0,sizeof ov);
574 ov.hEvent = CreateEventA( NULL, 0, 0, NULL );
578 SERVER_START_REQ( wait_named_pipe )
580 req->timeout = nTimeOut;
581 req->overlapped = &ov;
582 req->func = SYNC_CompletePipeOverlapped;
583 wine_server_add_data( req, name, len * sizeof(WCHAR) );
584 ret = !wine_server_call_err( req );
590 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
592 SetLastError(ov.Internal);
593 ret = (ov.Internal==STATUS_SUCCESS);
596 CloseHandle(ov.hEvent);
601 /***********************************************************************
602 * SYNC_ConnectNamedPipe (Internal)
604 static BOOL SYNC_ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
611 overlapped->Internal = STATUS_PENDING;
613 SERVER_START_REQ( connect_named_pipe )
616 req->overlapped = overlapped;
617 req->func = SYNC_CompletePipeOverlapped;
618 ret = !wine_server_call_err( req );
625 /***********************************************************************
626 * ConnectNamedPipe (KERNEL32.@)
628 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
633 TRACE("(%d,%p)\n",hPipe, overlapped);
636 return SYNC_ConnectNamedPipe(hPipe,overlapped);
638 memset(&ov,0,sizeof ov);
639 ov.hEvent = CreateEventA(NULL,0,0,NULL);
643 ret=SYNC_ConnectNamedPipe(hPipe, &ov);
646 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
648 SetLastError(ov.Internal);
649 ret = (ov.Internal==STATUS_SUCCESS);
653 CloseHandle(ov.hEvent);
658 /***********************************************************************
659 * DisconnectNamedPipe (KERNEL32.@)
661 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
665 TRACE("(%d)\n",hPipe);
667 SERVER_START_REQ( disconnect_named_pipe )
670 ret = !wine_server_call_err( req );
677 /***********************************************************************
678 * TransactNamedPipe (KERNEL32.@)
680 BOOL WINAPI TransactNamedPipe(
681 HANDLE hPipe, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
682 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
684 FIXME("%d %p %ld %p %ld %p %p\n",
685 hPipe, lpInput, dwInputSize, lpOutput,
686 dwOutputSize, lpBytesRead, lpOverlapped);
692 /***********************************************************************
693 * GetNamedPipeInfo (KERNEL32.@)
695 BOOL WINAPI GetNamedPipeInfo(
696 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
697 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
701 TRACE("%d %p %p %p %p\n", hNamedPipe, lpFlags,
702 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
704 SERVER_START_REQ( get_named_pipe_info )
706 req->handle = hNamedPipe;
707 ret = !wine_server_call_err( req );
708 if(lpFlags) *lpFlags = reply->flags;
709 if(lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
710 if(lpInputBufferSize) *lpInputBufferSize = reply->outsize;
711 if(lpMaxInstances) *lpMaxInstances = reply->maxinstances;
718 /***********************************************************************
719 * GetNamedPipeHandleStateA (KERNEL32.@)
721 BOOL WINAPI GetNamedPipeHandleStateA(
722 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
723 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
724 LPSTR lpUsername, DWORD nUsernameMaxSize)
726 FIXME("%d %p %p %p %p %p %ld\n",
727 hNamedPipe, lpState, lpCurInstances,
728 lpMaxCollectionCount, lpCollectDataTimeout,
729 lpUsername, nUsernameMaxSize);
734 /***********************************************************************
735 * GetNamedPipeHandleStateW (KERNEL32.@)
737 BOOL WINAPI GetNamedPipeHandleStateW(
738 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
739 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
740 LPWSTR lpUsername, DWORD nUsernameMaxSize)
742 FIXME("%d %p %p %p %p %p %ld\n",
743 hNamedPipe, lpState, lpCurInstances,
744 lpMaxCollectionCount, lpCollectDataTimeout,
745 lpUsername, nUsernameMaxSize);
750 /***********************************************************************
751 * SetNamedPipeHandleState (KERNEL32.@)
753 BOOL WINAPI SetNamedPipeHandleState(
754 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
755 LPDWORD lpCollectDataTimeout)
757 FIXME("%d %p %p %p\n",
758 hNamedPipe, lpMode, lpMaxCollectionCount, lpCollectDataTimeout);
762 /***********************************************************************
763 * CallNamedPipeA (KERNEL32.@)
765 BOOL WINAPI CallNamedPipeA(
766 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
767 LPVOID lpOutput, DWORD lpOutputSize,
768 LPDWORD lpBytesRead, DWORD nTimeout)
770 FIXME("%s %p %ld %p %ld %p %ld\n",
771 debugstr_a(lpNamedPipeName), lpInput, lpInputSize,
772 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
776 /***********************************************************************
777 * CallNamedPipeW (KERNEL32.@)
779 BOOL WINAPI CallNamedPipeW(
780 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
781 LPVOID lpOutput, DWORD lpOutputSize,
782 LPDWORD lpBytesRead, DWORD nTimeout)
784 FIXME("%s %p %ld %p %ld %p %ld\n",
785 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
786 lpOutput, lpOutputSize, lpBytesRead, nTimeout);