2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
32 #ifdef HAVE_SYS_POLL_H
44 #include "wine/server.h"
45 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(win32);
52 /* check if current version is NT or Win95 */
53 inline static int is_version_nt(void)
55 return !(GetVersion() & 0x80000000);
59 /***********************************************************************
60 * InitializeCriticalSection (KERNEL32.@)
62 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
64 NTSTATUS ret = RtlInitializeCriticalSection( crit );
65 if (ret) RtlRaiseStatus( ret );
68 /***********************************************************************
69 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
71 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
73 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
74 if (ret) RtlRaiseStatus( ret );
78 /***********************************************************************
79 * SetCriticalSectionSpinCount (KERNEL32.@)
80 * This function is available on NT4SP3 or later, but not Win98
83 DWORD WINAPI SetCriticalSectionSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
85 ULONG_PTR oldspincount = crit->SpinCount;
86 if(spincount) FIXME("critsection=%p: spincount=%ld not supported\n", crit, spincount);
87 crit->SpinCount = spincount;
91 /***********************************************************************
92 * MakeCriticalSectionGlobal (KERNEL32.@)
94 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
96 /* let's assume that only one thread at a time will try to do this */
97 HANDLE sem = crit->LockSemaphore;
98 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
99 crit->LockSemaphore = ConvertToGlobalHandle( sem );
102 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
103 crit->DebugInfo = NULL;
108 /***********************************************************************
109 * ReinitializeCriticalSection (KERNEL32.@)
111 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
113 if ( !crit->LockSemaphore )
114 RtlInitializeCriticalSection( crit );
118 /***********************************************************************
119 * UninitializeCriticalSection (KERNEL32.@)
121 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
123 RtlDeleteCriticalSection( crit );
127 /***********************************************************************
128 * CreateEventA (KERNEL32.@)
130 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
131 BOOL initial_state, LPCSTR name )
133 WCHAR buffer[MAX_PATH];
135 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
137 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
139 SetLastError( ERROR_FILENAME_EXCED_RANGE );
142 return CreateEventW( sa, manual_reset, initial_state, buffer );
146 /***********************************************************************
147 * CreateEventW (KERNEL32.@)
149 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
150 BOOL initial_state, LPCWSTR name )
153 DWORD len = name ? strlenW(name) : 0;
156 SetLastError( ERROR_FILENAME_EXCED_RANGE );
159 /* one buggy program needs this
160 * ("Van Dale Groot woordenboek der Nederlandse taal")
162 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
164 ERR("Bad security attributes pointer %p\n",sa);
165 SetLastError( ERROR_INVALID_PARAMETER);
168 SERVER_START_REQ( create_event )
170 req->manual_reset = manual_reset;
171 req->initial_state = initial_state;
172 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
173 wine_server_add_data( req, name, len * sizeof(WCHAR) );
175 wine_server_call_err( req );
183 /***********************************************************************
184 * CreateW32Event (KERNEL.457)
186 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
188 return CreateEventA( NULL, manual_reset, initial_state, NULL );
192 /***********************************************************************
193 * OpenEventA (KERNEL32.@)
195 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
197 WCHAR buffer[MAX_PATH];
199 if (!name) return OpenEventW( access, inherit, NULL );
201 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
203 SetLastError( ERROR_FILENAME_EXCED_RANGE );
206 return OpenEventW( access, inherit, buffer );
210 /***********************************************************************
211 * OpenEventW (KERNEL32.@)
213 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
216 DWORD len = name ? strlenW(name) : 0;
219 SetLastError( ERROR_FILENAME_EXCED_RANGE );
222 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
224 SERVER_START_REQ( open_event )
226 req->access = access;
227 req->inherit = inherit;
228 wine_server_add_data( req, name, len * sizeof(WCHAR) );
229 wine_server_call_err( req );
237 /***********************************************************************
240 * Execute an event operation (set,reset,pulse).
242 static BOOL EVENT_Operation( HANDLE handle, enum event_op op )
245 SERVER_START_REQ( event_op )
247 req->handle = handle;
249 ret = !wine_server_call_err( req );
256 /***********************************************************************
257 * PulseEvent (KERNEL32.@)
259 BOOL WINAPI PulseEvent( HANDLE handle )
261 return EVENT_Operation( handle, PULSE_EVENT );
265 /***********************************************************************
266 * SetW32Event (KERNEL.458)
267 * SetEvent (KERNEL32.@)
269 BOOL WINAPI SetEvent( HANDLE handle )
271 return EVENT_Operation( handle, SET_EVENT );
275 /***********************************************************************
276 * ResetW32Event (KERNEL.459)
277 * ResetEvent (KERNEL32.@)
279 BOOL WINAPI ResetEvent( HANDLE handle )
281 return EVENT_Operation( handle, RESET_EVENT );
285 /***********************************************************************
286 * NOTE: The Win95 VWin32_Event routines given below are really low-level
287 * routines implemented directly by VWin32. The user-mode libraries
288 * implement Win32 synchronisation routines on top of these low-level
289 * primitives. We do it the other way around here :-)
292 /***********************************************************************
293 * VWin32_EventCreate (KERNEL.442)
295 HANDLE WINAPI VWin32_EventCreate(VOID)
297 HANDLE hEvent = CreateEventA( NULL, FALSE, 0, NULL );
298 return ConvertToGlobalHandle( hEvent );
301 /***********************************************************************
302 * VWin32_EventDestroy (KERNEL.443)
304 VOID WINAPI VWin32_EventDestroy(HANDLE event)
306 CloseHandle( event );
309 /***********************************************************************
310 * VWin32_EventWait (KERNEL.450)
312 VOID WINAPI VWin32_EventWait(HANDLE event)
316 ReleaseThunkLock( &mutex_count );
317 WaitForSingleObject( event, INFINITE );
318 RestoreThunkLock( mutex_count );
321 /***********************************************************************
322 * VWin32_EventSet (KERNEL.451)
323 * KERNEL_479 (KERNEL.479)
325 VOID WINAPI VWin32_EventSet(HANDLE event)
332 /***********************************************************************
333 * CreateMutexA (KERNEL32.@)
335 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
337 WCHAR buffer[MAX_PATH];
339 if (!name) return CreateMutexW( sa, owner, NULL );
341 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
343 SetLastError( ERROR_FILENAME_EXCED_RANGE );
346 return CreateMutexW( sa, owner, buffer );
350 /***********************************************************************
351 * CreateMutexW (KERNEL32.@)
353 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
356 DWORD len = name ? strlenW(name) : 0;
359 SetLastError( ERROR_FILENAME_EXCED_RANGE );
362 SERVER_START_REQ( create_mutex )
365 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
366 wine_server_add_data( req, name, len * sizeof(WCHAR) );
368 wine_server_call_err( req );
376 /***********************************************************************
377 * OpenMutexA (KERNEL32.@)
379 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
381 WCHAR buffer[MAX_PATH];
383 if (!name) return OpenMutexW( access, inherit, NULL );
385 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
387 SetLastError( ERROR_FILENAME_EXCED_RANGE );
390 return OpenMutexW( access, inherit, buffer );
394 /***********************************************************************
395 * OpenMutexW (KERNEL32.@)
397 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
400 DWORD len = name ? strlenW(name) : 0;
403 SetLastError( ERROR_FILENAME_EXCED_RANGE );
406 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
408 SERVER_START_REQ( open_mutex )
410 req->access = access;
411 req->inherit = inherit;
412 wine_server_add_data( req, name, len * sizeof(WCHAR) );
413 wine_server_call_err( req );
421 /***********************************************************************
422 * ReleaseMutex (KERNEL32.@)
424 BOOL WINAPI ReleaseMutex( HANDLE handle )
427 SERVER_START_REQ( release_mutex )
429 req->handle = handle;
430 ret = !wine_server_call_err( req );
442 /***********************************************************************
443 * CreateSemaphoreA (KERNEL32.@)
445 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
447 WCHAR buffer[MAX_PATH];
449 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
451 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
453 SetLastError( ERROR_FILENAME_EXCED_RANGE );
456 return CreateSemaphoreW( sa, initial, max, buffer );
460 /***********************************************************************
461 * CreateSemaphoreW (KERNEL32.@)
463 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
464 LONG max, LPCWSTR name )
467 DWORD len = name ? strlenW(name) : 0;
469 /* Check parameters */
471 if ((max <= 0) || (initial < 0) || (initial > max))
473 SetLastError( ERROR_INVALID_PARAMETER );
478 SetLastError( ERROR_FILENAME_EXCED_RANGE );
482 SERVER_START_REQ( create_semaphore )
484 req->initial = (unsigned int)initial;
485 req->max = (unsigned int)max;
486 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
487 wine_server_add_data( req, name, len * sizeof(WCHAR) );
489 wine_server_call_err( req );
497 /***********************************************************************
498 * OpenSemaphoreA (KERNEL32.@)
500 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
502 WCHAR buffer[MAX_PATH];
504 if (!name) return OpenSemaphoreW( access, inherit, NULL );
506 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
508 SetLastError( ERROR_FILENAME_EXCED_RANGE );
511 return OpenSemaphoreW( access, inherit, buffer );
515 /***********************************************************************
516 * OpenSemaphoreW (KERNEL32.@)
518 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
521 DWORD len = name ? strlenW(name) : 0;
524 SetLastError( ERROR_FILENAME_EXCED_RANGE );
527 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
529 SERVER_START_REQ( open_semaphore )
531 req->access = access;
532 req->inherit = inherit;
533 wine_server_add_data( req, name, len * sizeof(WCHAR) );
534 wine_server_call_err( req );
542 /***********************************************************************
543 * ReleaseSemaphore (KERNEL32.@)
545 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
547 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
548 if (status) SetLastError( RtlNtStatusToDosError(status) );
558 /***********************************************************************
559 * CreateWaitableTimerA (KERNEL32.@)
561 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
563 WCHAR buffer[MAX_PATH];
565 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
567 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
569 SetLastError( ERROR_FILENAME_EXCED_RANGE );
572 return CreateWaitableTimerW( sa, manual, buffer );
576 /***********************************************************************
577 * CreateWaitableTimerW (KERNEL32.@)
579 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
585 OBJECT_ATTRIBUTES oa;
587 if (name) RtlInitUnicodeString(&us, name);
588 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
590 InitializeObjectAttributes(&oa, name ? &us : NULL, attr,
591 NULL /* FIXME */, NULL /* FIXME */);
592 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &oa,
593 manual ? NotificationTimer : SynchronizationTimer);
595 if (status != STATUS_SUCCESS)
597 SetLastError( RtlNtStatusToDosError(status) );
604 /***********************************************************************
605 * OpenWaitableTimerA (KERNEL32.@)
607 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
609 WCHAR buffer[MAX_PATH];
611 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
613 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
615 SetLastError( ERROR_FILENAME_EXCED_RANGE );
618 return OpenWaitableTimerW( access, inherit, buffer );
622 /***********************************************************************
623 * OpenWaitableTimerW (KERNEL32.@)
625 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
631 OBJECT_ATTRIBUTES oa;
633 if (inherit) attr |= OBJ_INHERIT;
635 if (name) RtlInitUnicodeString(&us, name);
636 InitializeObjectAttributes(&oa, name ? &us : NULL, attr, NULL /* FIXME */, NULL /* FIXME */);
637 status = NtOpenTimer(&handle, access, &oa);
638 if (status != STATUS_SUCCESS)
640 SetLastError( RtlNtStatusToDosError(status) );
647 /***********************************************************************
648 * SetWaitableTimer (KERNEL32.@)
650 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
651 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
653 NTSTATUS status = NtSetTimer(handle, when, callback, arg, resume, period, NULL);
655 if (status != STATUS_SUCCESS)
657 SetLastError( RtlNtStatusToDosError(status) );
658 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
664 /***********************************************************************
665 * CancelWaitableTimer (KERNEL32.@)
667 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
671 status = NtCancelTimer(handle, NULL);
672 if (status != STATUS_SUCCESS)
674 SetLastError( RtlNtStatusToDosError(status) );
681 /***********************************************************************
682 * CreateTimerQueue (KERNEL32.@)
684 HANDLE WINAPI CreateTimerQueue(void)
687 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
692 /***********************************************************************
693 * DeleteTimerQueueEx (KERNEL32.@)
695 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
697 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
698 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
702 /***********************************************************************
703 * CreateTimerQueueTimer (KERNEL32.@)
705 * Creates a timer-queue timer. This timer expires at the specified due
706 * time (in ms), then after every specified period (in ms). When the timer
707 * expires, the callback function is called.
710 * nonzero on success or zero on faillure
715 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
716 WAITORTIMERCALLBACK Callback, PVOID Parameter,
717 DWORD DueTime, DWORD Period, ULONG Flags )
720 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
724 /***********************************************************************
725 * DeleteTimerQueueTimer (KERNEL32.@)
727 * Cancels a timer-queue timer.
730 * nonzero on success or zero on faillure
735 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
736 HANDLE CompletionEvent )
739 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
749 /***********************************************************************
750 * CreateNamedPipeA (KERNEL32.@)
752 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
753 DWORD dwPipeMode, DWORD nMaxInstances,
754 DWORD nOutBufferSize, DWORD nInBufferSize,
755 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
757 WCHAR buffer[MAX_PATH];
759 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
760 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
762 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
764 SetLastError( ERROR_FILENAME_EXCED_RANGE );
765 return INVALID_HANDLE_VALUE;
767 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
768 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
772 /***********************************************************************
773 * CreateNamedPipeW (KERNEL32.@)
775 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
776 DWORD dwPipeMode, DWORD nMaxInstances,
777 DWORD nOutBufferSize, DWORD nInBufferSize,
778 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
782 static const WCHAR leadin[] = {'\\','\\','.','\\','P','I','P','E','\\'};
784 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
785 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
786 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
790 SetLastError( ERROR_PATH_NOT_FOUND );
791 return INVALID_HANDLE_VALUE;
796 SetLastError( ERROR_FILENAME_EXCED_RANGE );
797 return INVALID_HANDLE_VALUE;
799 if (strncmpiW(name, leadin, sizeof(leadin)/sizeof(leadin[0])))
801 SetLastError( ERROR_INVALID_NAME );
802 return INVALID_HANDLE_VALUE;
804 SERVER_START_REQ( create_named_pipe )
806 req->openmode = dwOpenMode;
807 req->pipemode = dwPipeMode;
808 req->maxinstances = nMaxInstances;
809 req->outsize = nOutBufferSize;
810 req->insize = nInBufferSize;
811 req->timeout = nDefaultTimeOut;
812 wine_server_add_data( req, name, len * sizeof(WCHAR) );
814 if (!wine_server_call_err( req )) ret = reply->handle;
815 else ret = INVALID_HANDLE_VALUE;
822 /***********************************************************************
823 * PeekNamedPipe (KERNEL32.@)
825 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
826 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
831 fd = FILE_GetUnixHandle(hPipe, GENERIC_READ);
832 if (fd == -1) return FALSE;
834 if (ioctl(fd,FIONREAD, &avail ) != 0)
836 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
840 if (!avail) /* check for closed pipe */
842 struct pollfd pollfd;
844 pollfd.events = POLLIN;
846 switch (poll( &pollfd, 1, 0 ))
850 case 1: /* got something */
851 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
852 TRACE("POLLHUP | POLLERR\n");
856 SetLastError(ERROR_BROKEN_PIPE);
861 TRACE(" 0x%08x bytes available\n", avail );
862 if (!lpvBuffer && lpcbAvail)
867 #endif /* defined(FIONREAD) */
869 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
870 FIXME("function not implemented\n");
874 /***********************************************************************
875 * SYNC_CompletePipeOverlapped (Internal)
877 static void SYNC_CompletePipeOverlapped (LPOVERLAPPED overlapped, DWORD result)
879 TRACE("for %p result %08lx\n",overlapped,result);
882 overlapped->Internal = result;
883 SetEvent(overlapped->hEvent);
887 /***********************************************************************
888 * WaitNamedPipeA (KERNEL32.@)
890 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
892 WCHAR buffer[MAX_PATH];
894 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
896 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
898 SetLastError( ERROR_FILENAME_EXCED_RANGE );
901 return WaitNamedPipeW( buffer, nTimeOut );
905 /***********************************************************************
906 * WaitNamedPipeW (KERNEL32.@)
908 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
910 DWORD len = name ? strlenW(name) : 0;
916 SetLastError( ERROR_FILENAME_EXCED_RANGE );
920 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
922 memset(&ov,0,sizeof(ov));
923 ov.hEvent = CreateEventA( NULL, 0, 0, NULL );
927 SERVER_START_REQ( wait_named_pipe )
929 req->timeout = nTimeOut;
930 req->overlapped = &ov;
931 req->func = SYNC_CompletePipeOverlapped;
932 wine_server_add_data( req, name, len * sizeof(WCHAR) );
933 ret = !wine_server_call_err( req );
939 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
941 SetLastError(ov.Internal);
942 ret = (ov.Internal==STATUS_SUCCESS);
945 CloseHandle(ov.hEvent);
950 /***********************************************************************
951 * SYNC_ConnectNamedPipe (Internal)
953 static BOOL SYNC_ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
960 overlapped->Internal = STATUS_PENDING;
962 SERVER_START_REQ( connect_named_pipe )
965 req->overlapped = overlapped;
966 req->func = SYNC_CompletePipeOverlapped;
967 ret = !wine_server_call_err( req );
974 /***********************************************************************
975 * ConnectNamedPipe (KERNEL32.@)
977 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
982 TRACE("(%p,%p)\n",hPipe, overlapped);
986 if(SYNC_ConnectNamedPipe(hPipe,overlapped))
987 SetLastError( ERROR_IO_PENDING );
991 memset(&ov,0,sizeof(ov));
992 ov.hEvent = CreateEventA(NULL,0,0,NULL);
996 ret=SYNC_ConnectNamedPipe(hPipe, &ov);
999 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1001 SetLastError(ov.Internal);
1002 ret = (ov.Internal==STATUS_SUCCESS);
1006 CloseHandle(ov.hEvent);
1011 /***********************************************************************
1012 * DisconnectNamedPipe (KERNEL32.@)
1014 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1018 TRACE("(%p)\n",hPipe);
1020 SERVER_START_REQ( disconnect_named_pipe )
1022 req->handle = hPipe;
1023 ret = !wine_server_call_err( req );
1024 if (ret && reply->fd != -1) close( reply->fd );
1031 /***********************************************************************
1032 * TransactNamedPipe (KERNEL32.@)
1034 BOOL WINAPI TransactNamedPipe(
1035 HANDLE hPipe, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1036 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1038 FIXME("%p %p %ld %p %ld %p %p\n",
1039 hPipe, lpInput, dwInputSize, lpOutput,
1040 dwOutputSize, lpBytesRead, lpOverlapped);
1046 /***********************************************************************
1047 * GetNamedPipeInfo (KERNEL32.@)
1049 BOOL WINAPI GetNamedPipeInfo(
1050 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1051 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1055 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1056 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1058 SERVER_START_REQ( get_named_pipe_info )
1060 req->handle = hNamedPipe;
1061 ret = !wine_server_call_err( req );
1062 if(lpFlags) *lpFlags = reply->flags;
1063 if(lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1064 if(lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1065 if(lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1072 /***********************************************************************
1073 * GetNamedPipeHandleStateA (KERNEL32.@)
1075 BOOL WINAPI GetNamedPipeHandleStateA(
1076 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1077 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1078 LPSTR lpUsername, DWORD nUsernameMaxSize)
1080 FIXME("%p %p %p %p %p %p %ld\n",
1081 hNamedPipe, lpState, lpCurInstances,
1082 lpMaxCollectionCount, lpCollectDataTimeout,
1083 lpUsername, nUsernameMaxSize);
1088 /***********************************************************************
1089 * GetNamedPipeHandleStateW (KERNEL32.@)
1091 BOOL WINAPI GetNamedPipeHandleStateW(
1092 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1093 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1094 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1096 FIXME("%p %p %p %p %p %p %ld\n",
1097 hNamedPipe, lpState, lpCurInstances,
1098 lpMaxCollectionCount, lpCollectDataTimeout,
1099 lpUsername, nUsernameMaxSize);
1104 /***********************************************************************
1105 * SetNamedPipeHandleState (KERNEL32.@)
1107 BOOL WINAPI SetNamedPipeHandleState(
1108 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1109 LPDWORD lpCollectDataTimeout)
1111 FIXME("%p %p %p %p\n",
1112 hNamedPipe, lpMode, lpMaxCollectionCount, lpCollectDataTimeout);
1116 /***********************************************************************
1117 * CallNamedPipeA (KERNEL32.@)
1119 BOOL WINAPI CallNamedPipeA(
1120 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1121 LPVOID lpOutput, DWORD lpOutputSize,
1122 LPDWORD lpBytesRead, DWORD nTimeout)
1124 FIXME("%s %p %ld %p %ld %p %ld\n",
1125 debugstr_a(lpNamedPipeName), lpInput, lpInputSize,
1126 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1130 /***********************************************************************
1131 * CallNamedPipeW (KERNEL32.@)
1133 BOOL WINAPI CallNamedPipeW(
1134 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1135 LPVOID lpOutput, DWORD lpOutputSize,
1136 LPDWORD lpBytesRead, DWORD nTimeout)
1138 FIXME("%s %p %ld %p %ld %p %ld\n",
1139 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1140 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1144 /******************************************************************
1145 * CreatePipe (KERNEL32.@)
1148 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1149 LPSECURITY_ATTRIBUTES sa, DWORD size )
1151 static unsigned index = 0;
1154 unsigned in_index = index;
1156 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1157 /* generate a unique pipe name (system wide) */
1160 sprintf(name, "\\\\.\\pipe\\Win32.Pipes.%08lu.%08u", GetCurrentProcessId(), ++index);
1161 hr = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND,
1162 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1163 NMPWAIT_USE_DEFAULT_WAIT, sa);
1164 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1165 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1166 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1168 hw = CreateFileA(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1169 if (hw == INVALID_HANDLE_VALUE)
1181 /******************************************************************************
1182 * CreateMailslotA [KERNEL32.@]
1184 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1185 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1191 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1192 nMaxMessageSize, lReadTimeout, sa);
1196 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1197 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1198 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1201 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1204 HeapFree( GetProcessHeap(), 0, name );
1210 /******************************************************************************
1211 * CreateMailslotW [KERNEL32.@] Creates a mailslot with specified name
1214 * lpName [I] Pointer to string for mailslot name
1215 * nMaxMessageSize [I] Maximum message size
1216 * lReadTimeout [I] Milliseconds before read time-out
1217 * sa [I] Pointer to security structure
1220 * Success: Handle to mailslot
1221 * Failure: INVALID_HANDLE_VALUE
1223 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1224 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1226 FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_w(lpName),
1227 nMaxMessageSize, lReadTimeout, sa);
1232 /******************************************************************************
1233 * GetMailslotInfo [KERNEL32.@] Retrieves info about specified mailslot
1236 * hMailslot [I] Mailslot handle
1237 * lpMaxMessageSize [O] Address of maximum message size
1238 * lpNextSize [O] Address of size of next message
1239 * lpMessageCount [O] Address of number of messages
1240 * lpReadTimeout [O] Address of read time-out
1246 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1247 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1248 LPDWORD lpReadTimeout )
1250 FIXME("(%p): stub\n",hMailslot);
1251 if (lpMaxMessageSize) *lpMaxMessageSize = (DWORD)NULL;
1252 if (lpNextSize) *lpNextSize = (DWORD)NULL;
1253 if (lpMessageCount) *lpMessageCount = (DWORD)NULL;
1254 if (lpReadTimeout) *lpReadTimeout = (DWORD)NULL;
1259 /******************************************************************************
1260 * SetMailslotInfo [KERNEL32.@] Sets the read timeout of a specified mailslot
1266 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1268 FIXME("%p %ld: stub\n", hMailslot, dwReadTimeout);
1275 /***********************************************************************
1276 * InterlockedCompareExchange (KERNEL32.@)
1278 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1279 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1280 "movl 12(%esp),%eax\n\t"
1281 "movl 8(%esp),%ecx\n\t"
1282 "movl 4(%esp),%edx\n\t"
1283 "lock; cmpxchgl %ecx,(%edx)\n\t"
1286 /***********************************************************************
1287 * InterlockedExchange (KERNEL32.@)
1289 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1290 __ASM_GLOBAL_FUNC(InterlockedExchange,
1291 "movl 8(%esp),%eax\n\t"
1292 "movl 4(%esp),%edx\n\t"
1293 "lock; xchgl %eax,(%edx)\n\t"
1296 /***********************************************************************
1297 * InterlockedExchangeAdd (KERNEL32.@)
1299 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1300 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1301 "movl 8(%esp),%eax\n\t"
1302 "movl 4(%esp),%edx\n\t"
1303 "lock; xaddl %eax,(%edx)\n\t"
1306 /***********************************************************************
1307 * InterlockedIncrement (KERNEL32.@)
1309 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1310 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1311 "movl 4(%esp),%edx\n\t"
1313 "lock; xaddl %eax,(%edx)\n\t"
1317 /***********************************************************************
1318 * InterlockedDecrement (KERNEL32.@)
1320 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1321 "movl 4(%esp),%edx\n\t"
1323 "lock; xaddl %eax,(%edx)\n\t"
1327 #else /* __i386__ */
1329 /***********************************************************************
1330 * InterlockedCompareExchange (KERNEL32.@)
1332 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
1334 return interlocked_cmpxchg( dest, xchg, compare );
1337 /***********************************************************************
1338 * InterlockedExchange (KERNEL32.@)
1340 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
1342 return interlocked_xchg( dest, val );
1345 /***********************************************************************
1346 * InterlockedExchangeAdd (KERNEL32.@)
1348 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
1350 return interlocked_xchg_add( dest, incr );
1353 /***********************************************************************
1354 * InterlockedIncrement (KERNEL32.@)
1356 LONG WINAPI InterlockedIncrement( PLONG dest )
1358 return interlocked_xchg_add( dest, 1 ) + 1;
1361 /***********************************************************************
1362 * InterlockedDecrement (KERNEL32.@)
1364 LONG WINAPI InterlockedDecrement( PLONG dest )
1366 return interlocked_xchg_add( dest, -1 ) - 1;
1369 #endif /* __i386__ */