Implement CryptImport/ExportPublicKeyInfo.
[wine] / dlls / kernel / sync.c
1 /*
2  * Kernel synchronization objects
3  *
4  * Copyright 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 #include <sys/poll.h>
37 #endif
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <stdarg.h>
42 #include <stdio.h>
43
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
46
47 #include "ntstatus.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winerror.h"
51 #include "winnls.h"
52 #include "winternl.h"
53 #include "winioctl.h"
54
55 #include "wine/server.h"
56 #include "wine/unicode.h"
57 #include "wine/winbase16.h"
58 #include "kernel_private.h"
59
60 #include "wine/debug.h"
61
62 WINE_DEFAULT_DEBUG_CHANNEL(sync);
63
64 /* check if current version is NT or Win95 */
65 inline static int is_version_nt(void)
66 {
67     return !(GetVersion() & 0x80000000);
68 }
69
70
71 /***********************************************************************
72  *              Sleep  (KERNEL32.@)
73  */
74 VOID WINAPI Sleep( DWORD timeout )
75 {
76     SleepEx( timeout, FALSE );
77 }
78
79 /******************************************************************************
80  *              SleepEx   (KERNEL32.@)
81  */
82 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
83 {
84     NTSTATUS status;
85
86     if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
87     else
88     {
89         LARGE_INTEGER time;
90
91         time.QuadPart = timeout * (ULONGLONG)10000;
92         time.QuadPart = -time.QuadPart;
93         status = NtDelayExecution( alertable, &time );
94     }
95     if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
96     return status;
97 }
98
99
100 /***********************************************************************
101  *              SwitchToThread (KERNEL32.@)
102  */
103 BOOL WINAPI SwitchToThread(void)
104 {
105     return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
106 }
107
108
109 /***********************************************************************
110  *           WaitForSingleObject   (KERNEL32.@)
111  */
112 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
113 {
114     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
115 }
116
117
118 /***********************************************************************
119  *           WaitForSingleObjectEx   (KERNEL32.@)
120  */
121 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
122                                     BOOL alertable )
123 {
124     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
125 }
126
127
128 /***********************************************************************
129  *           WaitForMultipleObjects   (KERNEL32.@)
130  */
131 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
132                                      BOOL wait_all, DWORD timeout )
133 {
134     return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
135 }
136
137
138 /***********************************************************************
139  *           WaitForMultipleObjectsEx   (KERNEL32.@)
140  */
141 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
142                                        BOOL wait_all, DWORD timeout,
143                                        BOOL alertable )
144 {
145     NTSTATUS status;
146     HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
147     unsigned int i;
148
149     if (count >= MAXIMUM_WAIT_OBJECTS)
150     {
151         SetLastError(ERROR_INVALID_PARAMETER);
152         return WAIT_FAILED;
153     }
154     for (i = 0; i < count; i++)
155     {
156         if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
157             (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
158             (handles[i] == (HANDLE)STD_ERROR_HANDLE))
159             hloc[i] = GetStdHandle( (DWORD)handles[i] );
160         else
161             hloc[i] = handles[i];
162
163         /* yes, even screen buffer console handles are waitable, and are
164          * handled as a handle to the console itself !!
165          */
166         if (is_console_handle(hloc[i]))
167         {
168             if (!VerifyConsoleIoHandle(hloc[i]))
169             {
170                 return FALSE;
171             }
172             hloc[i] = GetConsoleInputWaitHandle();
173         }
174     }
175
176     if (timeout == INFINITE)
177     {
178         status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
179     }
180     else
181     {
182         LARGE_INTEGER time;
183
184         time.QuadPart = timeout * (ULONGLONG)10000;
185         time.QuadPart = -time.QuadPart;
186         status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
187     }
188
189     if (HIWORD(status))  /* is it an error code? */
190     {
191         SetLastError( RtlNtStatusToDosError(status) );
192         status = WAIT_FAILED;
193     }
194     return status;
195 }
196
197
198 /***********************************************************************
199  *           WaitForSingleObject   (KERNEL.460)
200  */
201 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
202 {
203     DWORD retval, mutex_count;
204
205     ReleaseThunkLock( &mutex_count );
206     retval = WaitForSingleObject( handle, timeout );
207     RestoreThunkLock( mutex_count );
208     return retval;
209 }
210
211 /***********************************************************************
212  *           WaitForMultipleObjects   (KERNEL.461)
213  */
214 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
215                                        BOOL wait_all, DWORD timeout )
216 {
217     DWORD retval, mutex_count;
218
219     ReleaseThunkLock( &mutex_count );
220     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
221     RestoreThunkLock( mutex_count );
222     return retval;
223 }
224
225 /***********************************************************************
226  *           WaitForMultipleObjectsEx   (KERNEL.495)
227  */
228 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
229                                          BOOL wait_all, DWORD timeout, BOOL alertable )
230 {
231     DWORD retval, mutex_count;
232
233     ReleaseThunkLock( &mutex_count );
234     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
235     RestoreThunkLock( mutex_count );
236     return retval;
237 }
238
239 /***********************************************************************
240  *           RegisterWaitForSingleObject   (KERNEL32.@)
241  */
242 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
243                 WAITORTIMERCALLBACK Callback, PVOID Context,
244                 ULONG dwMilliseconds, ULONG dwFlags)
245 {
246     FIXME("%p %p %p %p %ld %ld\n",
247           phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
248     return FALSE;
249 }
250
251 /***********************************************************************
252  *           RegisterWaitForSingleObjectEx   (KERNEL32.@)
253  */
254 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject, 
255                 WAITORTIMERCALLBACK Callback, PVOID Context,
256                 ULONG dwMilliseconds, ULONG dwFlags ) 
257 {
258     FIXME("%p %p %p %ld %ld\n",
259           hObject,Callback,Context,dwMilliseconds,dwFlags);
260     return 0;
261 }
262
263 /***********************************************************************
264  *           UnregisterWait   (KERNEL32.@)
265  */
266 BOOL WINAPI UnregisterWait( HANDLE WaitHandle ) 
267 {
268     FIXME("%p\n",WaitHandle);
269     return FALSE;
270 }
271
272 /***********************************************************************
273  *           UnregisterWaitEx   (KERNEL32.@)
274  */
275 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent ) 
276 {
277     FIXME("%p %p\n",WaitHandle, CompletionEvent);
278     return FALSE;
279 }
280
281 /***********************************************************************
282  *           SignalObjectAndWait  (KERNEL32.@)
283  *
284  * Allows to atomically signal any of the synchro objects (semaphore,
285  * mutex, event) and wait on another.
286  */
287 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
288                                   DWORD dwMilliseconds, BOOL bAlertable )
289 {
290     NTSTATUS status;
291     LARGE_INTEGER timeout, *ptimeout = NULL;
292
293     TRACE("%p %p %ld %d\n", hObjectToSignal,
294           hObjectToWaitOn, dwMilliseconds, bAlertable);
295
296     if (dwMilliseconds != INFINITE)
297     {
298         timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
299         timeout.QuadPart = -timeout.QuadPart;
300         ptimeout = &timeout;
301     }
302
303     status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
304                                              bAlertable, ptimeout );
305     if (HIWORD(status))
306     {
307         SetLastError( RtlNtStatusToDosError(status) );
308         status = WAIT_FAILED;
309     }
310     return status;
311 }
312
313 /***********************************************************************
314  *           InitializeCriticalSection   (KERNEL32.@)
315  *
316  * Initialise a critical section before use.
317  *
318  * PARAMS
319  *  crit [O] Critical section to initialise.
320  *
321  * RETURNS
322  *  Nothing. If the function fails an exception is raised.
323  */
324 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
325 {
326     NTSTATUS ret = RtlInitializeCriticalSection( crit );
327     if (ret) RtlRaiseStatus( ret );
328 }
329
330 /***********************************************************************
331  *           InitializeCriticalSectionAndSpinCount   (KERNEL32.@)
332  *
333  * Initialise a critical section with a spin count.
334  *
335  * PARAMS
336  *  crit      [O] Critical section to initialise.
337  *  spincount [I] Number of times to spin upon contention.
338  *
339  * RETURNS
340  *  Success: TRUE.
341  *  Failure: Nothing. If the function fails an exception is raised.
342  *
343  * NOTES
344  *  spincount is ignored on uni-processor systems.
345  */
346 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
347 {
348     NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
349     if (ret) RtlRaiseStatus( ret );
350     return !ret;
351 }
352
353 /***********************************************************************
354  *           MakeCriticalSectionGlobal   (KERNEL32.@)
355  */
356 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
357 {
358     /* let's assume that only one thread at a time will try to do this */
359     HANDLE sem = crit->LockSemaphore;
360     if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
361     crit->LockSemaphore = ConvertToGlobalHandle( sem );
362     if (crit->DebugInfo)
363     {
364         RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
365         crit->DebugInfo = NULL;
366     }
367 }
368
369
370 /***********************************************************************
371  *           ReinitializeCriticalSection   (KERNEL32.@)
372  *
373  * Initialise an already used critical section.
374  *
375  * PARAMS
376  *  crit [O] Critical section to initialise.
377  *
378  * RETURNS
379  *  Nothing.
380  */
381 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
382 {
383     if ( !crit->LockSemaphore )
384         RtlInitializeCriticalSection( crit );
385 }
386
387
388 /***********************************************************************
389  *           UninitializeCriticalSection   (KERNEL32.@)
390  *
391  * UnInitialise a critical section after use.
392  *
393  * PARAMS
394  *  crit [O] Critical section to uninitialise (destroy).
395  *
396  * RETURNS
397  *  Nothing.
398  */
399 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
400 {
401     RtlDeleteCriticalSection( crit );
402 }
403
404
405 /***********************************************************************
406  *           CreateEventA    (KERNEL32.@)
407  */
408 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
409                             BOOL initial_state, LPCSTR name )
410 {
411     WCHAR buffer[MAX_PATH];
412
413     if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
414
415     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
416     {
417         SetLastError( ERROR_FILENAME_EXCED_RANGE );
418         return 0;
419     }
420     return CreateEventW( sa, manual_reset, initial_state, buffer );
421 }
422
423
424 /***********************************************************************
425  *           CreateEventW    (KERNEL32.@)
426  */
427 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
428                             BOOL initial_state, LPCWSTR name )
429 {
430     HANDLE ret;
431     UNICODE_STRING nameW;
432     OBJECT_ATTRIBUTES attr;
433     NTSTATUS status;
434
435     /* one buggy program needs this
436      * ("Van Dale Groot woordenboek der Nederlandse taal")
437      */
438     if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
439     {
440         ERR("Bad security attributes pointer %p\n",sa);
441         SetLastError( ERROR_INVALID_PARAMETER);
442         return 0;
443     }
444
445     attr.Length                   = sizeof(attr);
446     attr.RootDirectory            = 0;
447     attr.ObjectName               = NULL;
448     attr.Attributes               = OBJ_CASE_INSENSITIVE |
449                                     (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
450     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
451     attr.SecurityQualityOfService = NULL;
452     if (name)
453     {
454         RtlInitUnicodeString( &nameW, name );
455         attr.ObjectName = &nameW;
456     }
457
458     status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
459     SetLastError( RtlNtStatusToDosError(status) );
460     return ret;
461 }
462
463
464 /***********************************************************************
465  *           CreateW32Event    (KERNEL.457)
466  */
467 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
468 {
469     return CreateEventW( NULL, manual_reset, initial_state, NULL );
470 }
471
472
473 /***********************************************************************
474  *           OpenEventA    (KERNEL32.@)
475  */
476 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
477 {
478     WCHAR buffer[MAX_PATH];
479
480     if (!name) return OpenEventW( access, inherit, NULL );
481
482     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
483     {
484         SetLastError( ERROR_FILENAME_EXCED_RANGE );
485         return 0;
486     }
487     return OpenEventW( access, inherit, buffer );
488 }
489
490
491 /***********************************************************************
492  *           OpenEventW    (KERNEL32.@)
493  */
494 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
495 {
496     HANDLE ret;
497     UNICODE_STRING nameW;
498     OBJECT_ATTRIBUTES attr;
499     NTSTATUS status;
500
501     if (!is_version_nt()) access = EVENT_ALL_ACCESS;
502
503     attr.Length                   = sizeof(attr);
504     attr.RootDirectory            = 0;
505     attr.ObjectName               = NULL;
506     attr.Attributes               = OBJ_CASE_INSENSITIVE |
507                                     (inherit) ? OBJ_INHERIT : 0;
508     attr.SecurityDescriptor       = NULL;
509     attr.SecurityQualityOfService = NULL;
510     if (name)
511     {
512         RtlInitUnicodeString( &nameW, name );
513         attr.ObjectName = &nameW;
514     }
515
516     status = NtOpenEvent( &ret, access, &attr );
517     if (status != STATUS_SUCCESS)
518     {
519         SetLastError( RtlNtStatusToDosError(status) );
520         return 0;
521     }
522     return ret;
523 }
524
525 /***********************************************************************
526  *           PulseEvent    (KERNEL32.@)
527  */
528 BOOL WINAPI PulseEvent( HANDLE handle )
529 {
530     NTSTATUS status;
531
532     if ((status = NtPulseEvent( handle, NULL )))
533         SetLastError( RtlNtStatusToDosError(status) );
534     return !status;
535 }
536
537
538 /***********************************************************************
539  *           SetW32Event (KERNEL.458)
540  *           SetEvent    (KERNEL32.@)
541  */
542 BOOL WINAPI SetEvent( HANDLE handle )
543 {
544     NTSTATUS status;
545
546     if ((status = NtSetEvent( handle, NULL )))
547         SetLastError( RtlNtStatusToDosError(status) );
548     return !status;
549 }
550
551
552 /***********************************************************************
553  *           ResetW32Event (KERNEL.459)
554  *           ResetEvent    (KERNEL32.@)
555  */
556 BOOL WINAPI ResetEvent( HANDLE handle )
557 {
558     NTSTATUS status;
559
560     if ((status = NtResetEvent( handle, NULL )))
561         SetLastError( RtlNtStatusToDosError(status) );
562     return !status;
563 }
564
565
566 /***********************************************************************
567  * NOTE: The Win95 VWin32_Event routines given below are really low-level
568  *       routines implemented directly by VWin32. The user-mode libraries
569  *       implement Win32 synchronisation routines on top of these low-level
570  *       primitives. We do it the other way around here :-)
571  */
572
573 /***********************************************************************
574  *       VWin32_EventCreate     (KERNEL.442)
575  */
576 HANDLE WINAPI VWin32_EventCreate(VOID)
577 {
578     HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
579     return ConvertToGlobalHandle( hEvent );
580 }
581
582 /***********************************************************************
583  *       VWin32_EventDestroy    (KERNEL.443)
584  */
585 VOID WINAPI VWin32_EventDestroy(HANDLE event)
586 {
587     CloseHandle( event );
588 }
589
590 /***********************************************************************
591  *       VWin32_EventWait       (KERNEL.450)
592  */
593 VOID WINAPI VWin32_EventWait(HANDLE event)
594 {
595     DWORD mutex_count;
596
597     ReleaseThunkLock( &mutex_count );
598     WaitForSingleObject( event, INFINITE );
599     RestoreThunkLock( mutex_count );
600 }
601
602 /***********************************************************************
603  *       VWin32_EventSet        (KERNEL.451)
604  *       KERNEL_479             (KERNEL.479)
605  */
606 VOID WINAPI VWin32_EventSet(HANDLE event)
607 {
608     SetEvent( event );
609 }
610
611
612
613 /***********************************************************************
614  *           CreateMutexA   (KERNEL32.@)
615  */
616 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
617 {
618     WCHAR buffer[MAX_PATH];
619
620     if (!name) return CreateMutexW( sa, owner, NULL );
621
622     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
623     {
624         SetLastError( ERROR_FILENAME_EXCED_RANGE );
625         return 0;
626     }
627     return CreateMutexW( sa, owner, buffer );
628 }
629
630
631 /***********************************************************************
632  *           CreateMutexW   (KERNEL32.@)
633  */
634 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
635 {
636     HANDLE ret;
637     UNICODE_STRING nameW;
638     OBJECT_ATTRIBUTES attr;
639     NTSTATUS status;
640
641     attr.Length                   = sizeof(attr);
642     attr.RootDirectory            = 0;
643     attr.ObjectName               = NULL;
644     attr.Attributes               = OBJ_CASE_INSENSITIVE |
645                                     (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
646     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
647     attr.SecurityQualityOfService = NULL;
648     if (name)
649     {
650         RtlInitUnicodeString( &nameW, name );
651         attr.ObjectName = &nameW;
652     }
653
654     status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
655     SetLastError( RtlNtStatusToDosError(status) );
656     return ret;
657 }
658
659
660 /***********************************************************************
661  *           OpenMutexA   (KERNEL32.@)
662  */
663 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
664 {
665     WCHAR buffer[MAX_PATH];
666
667     if (!name) return OpenMutexW( access, inherit, NULL );
668
669     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
670     {
671         SetLastError( ERROR_FILENAME_EXCED_RANGE );
672         return 0;
673     }
674     return OpenMutexW( access, inherit, buffer );
675 }
676
677
678 /***********************************************************************
679  *           OpenMutexW   (KERNEL32.@)
680  */
681 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
682 {
683     HANDLE ret;
684     UNICODE_STRING nameW;
685     OBJECT_ATTRIBUTES attr;
686     NTSTATUS status;
687
688     if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
689
690     attr.Length                   = sizeof(attr);
691     attr.RootDirectory            = 0;
692     attr.ObjectName               = NULL;
693     attr.Attributes               = OBJ_CASE_INSENSITIVE | 
694                                     (inherit) ? OBJ_INHERIT : 0;
695     attr.SecurityDescriptor       = NULL;
696     attr.SecurityQualityOfService = NULL;
697     if (name)
698     {
699         RtlInitUnicodeString( &nameW, name );
700         attr.ObjectName = &nameW;
701     }
702
703     status = NtOpenMutant( &ret, access, &attr );
704     if (status != STATUS_SUCCESS)
705     {
706         SetLastError( RtlNtStatusToDosError(status) );
707         return 0;
708     }
709     return ret;
710 }
711
712
713 /***********************************************************************
714  *           ReleaseMutex   (KERNEL32.@)
715  */
716 BOOL WINAPI ReleaseMutex( HANDLE handle )
717 {
718     NTSTATUS    status;
719
720     status = NtReleaseMutant(handle, NULL);
721     if (status != STATUS_SUCCESS)
722     {
723         SetLastError( RtlNtStatusToDosError(status) );
724         return FALSE;
725     }
726     return TRUE;
727 }
728
729
730 /*
731  * Semaphores
732  */
733
734
735 /***********************************************************************
736  *           CreateSemaphoreA   (KERNEL32.@)
737  */
738 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
739 {
740     WCHAR buffer[MAX_PATH];
741
742     if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
743
744     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
745     {
746         SetLastError( ERROR_FILENAME_EXCED_RANGE );
747         return 0;
748     }
749     return CreateSemaphoreW( sa, initial, max, buffer );
750 }
751
752
753 /***********************************************************************
754  *           CreateSemaphoreW   (KERNEL32.@)
755  */
756 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
757                                 LONG max, LPCWSTR name )
758 {
759     HANDLE ret;
760     UNICODE_STRING nameW;
761     OBJECT_ATTRIBUTES attr;
762     NTSTATUS status;
763
764     attr.Length                   = sizeof(attr);
765     attr.RootDirectory            = 0;
766     attr.ObjectName               = NULL;
767     attr.Attributes               = OBJ_CASE_INSENSITIVE | 
768                                     (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
769     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
770     attr.SecurityQualityOfService = NULL;
771     if (name)
772     {
773         RtlInitUnicodeString( &nameW, name );
774         attr.ObjectName = &nameW;
775     }
776
777     status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
778     SetLastError( RtlNtStatusToDosError(status) );
779     return ret;
780 }
781
782
783 /***********************************************************************
784  *           OpenSemaphoreA   (KERNEL32.@)
785  */
786 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
787 {
788     WCHAR buffer[MAX_PATH];
789
790     if (!name) return OpenSemaphoreW( access, inherit, NULL );
791
792     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
793     {
794         SetLastError( ERROR_FILENAME_EXCED_RANGE );
795         return 0;
796     }
797     return OpenSemaphoreW( access, inherit, buffer );
798 }
799
800
801 /***********************************************************************
802  *           OpenSemaphoreW   (KERNEL32.@)
803  */
804 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
805 {
806     HANDLE ret;
807     UNICODE_STRING nameW;
808     OBJECT_ATTRIBUTES attr;
809     NTSTATUS status;
810
811     if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
812
813     attr.Length                   = sizeof(attr);
814     attr.RootDirectory            = 0;
815     attr.ObjectName               = NULL;
816     attr.Attributes               = OBJ_CASE_INSENSITIVE |
817                                     (inherit) ? OBJ_INHERIT : 0;
818     attr.SecurityDescriptor       = NULL;
819     attr.SecurityQualityOfService = NULL;
820     if (name)
821     {
822         RtlInitUnicodeString( &nameW, name );
823         attr.ObjectName = &nameW;
824     }
825
826     status = NtOpenSemaphore( &ret, access, &attr );
827     if (status != STATUS_SUCCESS)
828     {
829         SetLastError( RtlNtStatusToDosError(status) );
830         return 0;
831     }
832     return ret;
833 }
834
835
836 /***********************************************************************
837  *           ReleaseSemaphore   (KERNEL32.@)
838  */
839 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
840 {
841     NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
842     if (status) SetLastError( RtlNtStatusToDosError(status) );
843     return !status;
844 }
845
846
847 /*
848  * Timers
849  */
850
851
852 /***********************************************************************
853  *           CreateWaitableTimerA    (KERNEL32.@)
854  */
855 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
856 {
857     WCHAR buffer[MAX_PATH];
858
859     if (!name) return CreateWaitableTimerW( sa, manual, NULL );
860
861     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
862     {
863         SetLastError( ERROR_FILENAME_EXCED_RANGE );
864         return 0;
865     }
866     return CreateWaitableTimerW( sa, manual, buffer );
867 }
868
869
870 /***********************************************************************
871  *           CreateWaitableTimerW    (KERNEL32.@)
872  */
873 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
874 {
875     HANDLE handle;
876     NTSTATUS status;
877     UNICODE_STRING nameW;
878     OBJECT_ATTRIBUTES attr;
879
880     attr.Length                   = sizeof(attr);
881     attr.RootDirectory            = 0;
882     attr.ObjectName               = NULL;
883     attr.Attributes               = OBJ_CASE_INSENSITIVE |
884                                     (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
885     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
886     attr.SecurityQualityOfService = NULL;
887     if (name)
888     {
889         RtlInitUnicodeString( &nameW, name );
890         attr.ObjectName = &nameW;
891     }
892
893     status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
894                            manual ? NotificationTimer : SynchronizationTimer);
895     SetLastError( RtlNtStatusToDosError(status) );
896     return handle;
897 }
898
899
900 /***********************************************************************
901  *           OpenWaitableTimerA    (KERNEL32.@)
902  */
903 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
904 {
905     WCHAR buffer[MAX_PATH];
906
907     if (!name) return OpenWaitableTimerW( access, inherit, NULL );
908
909     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
910     {
911         SetLastError( ERROR_FILENAME_EXCED_RANGE );
912         return 0;
913     }
914     return OpenWaitableTimerW( access, inherit, buffer );
915 }
916
917
918 /***********************************************************************
919  *           OpenWaitableTimerW    (KERNEL32.@)
920  */
921 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
922 {
923     HANDLE handle;
924     UNICODE_STRING nameW;
925     OBJECT_ATTRIBUTES attr;
926     NTSTATUS status;
927
928     if (!is_version_nt()) access = TIMER_ALL_ACCESS;
929
930     attr.Length                   = sizeof(attr);
931     attr.RootDirectory            = 0;
932     attr.ObjectName               = NULL;
933     attr.Attributes               = OBJ_CASE_INSENSITIVE |
934                                     (inherit) ? OBJ_INHERIT : 0;
935     attr.SecurityDescriptor       = NULL;
936     attr.SecurityQualityOfService = NULL;
937     if (name)
938     {
939         RtlInitUnicodeString( &nameW, name );
940         attr.ObjectName = &nameW;
941     }
942
943     status = NtOpenTimer(&handle, access, &attr);
944     if (status != STATUS_SUCCESS)
945     {
946         SetLastError( RtlNtStatusToDosError(status) );
947         return 0;
948     }
949     return handle;
950 }
951
952
953 /***********************************************************************
954  *           SetWaitableTimer    (KERNEL32.@)
955  */
956 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
957                               PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
958 {
959     NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
960                                  arg, resume, period, NULL);
961
962     if (status != STATUS_SUCCESS)
963     {
964         SetLastError( RtlNtStatusToDosError(status) );
965         if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
966     }
967     return TRUE;
968 }
969
970
971 /***********************************************************************
972  *           CancelWaitableTimer    (KERNEL32.@)
973  */
974 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
975 {
976     NTSTATUS status;
977
978     status = NtCancelTimer(handle, NULL);
979     if (status != STATUS_SUCCESS)
980     {
981         SetLastError( RtlNtStatusToDosError(status) );
982         return FALSE;
983     }
984     return TRUE;
985 }
986
987
988 /***********************************************************************
989  *           CreateTimerQueue  (KERNEL32.@)
990  */
991 HANDLE WINAPI CreateTimerQueue(void)
992 {
993     FIXME("stub\n");
994     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
995     return NULL;
996 }
997
998
999 /***********************************************************************
1000  *           DeleteTimerQueueEx  (KERNEL32.@)
1001  */
1002 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1003 {
1004     FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1005     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1006     return 0;
1007 }
1008
1009 /***********************************************************************
1010  *           CreateTimerQueueTimer  (KERNEL32.@)
1011  *
1012  * Creates a timer-queue timer. This timer expires at the specified due
1013  * time (in ms), then after every specified period (in ms). When the timer
1014  * expires, the callback function is called.
1015  *
1016  * RETURNS
1017  *   nonzero on success or zero on faillure
1018  *
1019  * BUGS
1020  *   Unimplemented
1021  */
1022 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1023                                    WAITORTIMERCALLBACK Callback, PVOID Parameter,
1024                                    DWORD DueTime, DWORD Period, ULONG Flags )
1025 {
1026     FIXME("stub\n");
1027     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1028     return TRUE;
1029 }
1030
1031 /***********************************************************************
1032  *           DeleteTimerQueueTimer  (KERNEL32.@)
1033  *
1034  * Cancels a timer-queue timer.
1035  *
1036  * RETURNS
1037  *   nonzero on success or zero on faillure
1038  *
1039  * BUGS
1040  *   Unimplemented
1041  */
1042 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1043                                    HANDLE CompletionEvent )
1044 {
1045     FIXME("stub\n");
1046     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1047     return TRUE;
1048 }
1049
1050
1051 /*
1052  * Pipes
1053  */
1054
1055
1056 /***********************************************************************
1057  *           CreateNamedPipeA   (KERNEL32.@)
1058  */
1059 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1060                                 DWORD dwPipeMode, DWORD nMaxInstances,
1061                                 DWORD nOutBufferSize, DWORD nInBufferSize,
1062                                 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1063 {
1064     WCHAR buffer[MAX_PATH];
1065
1066     if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1067                                         nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1068
1069     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1070     {
1071         SetLastError( ERROR_FILENAME_EXCED_RANGE );
1072         return INVALID_HANDLE_VALUE;
1073     }
1074     return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1075                              nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1076 }
1077
1078
1079 /***********************************************************************
1080  *           CreateNamedPipeW   (KERNEL32.@)
1081  */
1082 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1083                                 DWORD dwPipeMode, DWORD nMaxInstances,
1084                                 DWORD nOutBufferSize, DWORD nInBufferSize,
1085                                 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1086 {
1087     HANDLE handle;
1088     UNICODE_STRING nt_name;
1089     OBJECT_ATTRIBUTES attr;
1090     DWORD options;
1091     BOOLEAN pipe_type, read_mode, non_block;
1092     NTSTATUS status;
1093     IO_STATUS_BLOCK iosb;
1094     LARGE_INTEGER timeout;
1095
1096     TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1097           debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1098           nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1099
1100     if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1101     {
1102         SetLastError( ERROR_PATH_NOT_FOUND );
1103         return INVALID_HANDLE_VALUE;
1104     }
1105     if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1106     {
1107         SetLastError( ERROR_FILENAME_EXCED_RANGE );
1108         RtlFreeUnicodeString( &nt_name );
1109         return INVALID_HANDLE_VALUE;
1110     }
1111
1112     attr.Length                   = sizeof(attr);
1113     attr.RootDirectory            = 0;
1114     attr.ObjectName               = &nt_name;
1115     attr.Attributes               = OBJ_CASE_INSENSITIVE |
1116                                     (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1117     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
1118     attr.SecurityQualityOfService = NULL;
1119
1120     options = 0;
1121     if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1122     if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1123     if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1124         options |= FILE_PIPE_FULL_DUPLEX;
1125     else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1126     else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1127     pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1128     read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1129     non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1130     if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1131
1132     timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1133
1134     SetLastError(0);
1135         
1136     status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1137                                    0, FILE_OVERWRITE_IF, options, pipe_type,
1138                                    read_mode, non_block, nMaxInstances,
1139                                    nInBufferSize, nOutBufferSize, &timeout);
1140
1141     RtlFreeUnicodeString( &nt_name );
1142     if (status)
1143     {
1144         handle = INVALID_HANDLE_VALUE;
1145         SetLastError( RtlNtStatusToDosError(status) );
1146     }
1147     return handle;
1148 }
1149
1150
1151 /***********************************************************************
1152  *           PeekNamedPipe   (KERNEL32.@)
1153  */
1154 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1155                            LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1156 {
1157 #ifdef FIONREAD
1158     int avail=0, fd, ret, flags;
1159
1160     TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1161
1162     ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1163     if (ret)
1164     {
1165         SetLastError( RtlNtStatusToDosError(ret) );
1166         return FALSE;
1167     }
1168     if (flags & FD_FLAG_RECV_SHUTDOWN)
1169     {
1170         wine_server_release_fd( hPipe, fd );
1171         SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1172         return FALSE;
1173     }
1174
1175     if (ioctl(fd,FIONREAD, &avail ) != 0)
1176     {
1177         TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1178         wine_server_release_fd( hPipe, fd );
1179         return FALSE;
1180     }
1181     if (!avail)  /* check for closed pipe */
1182     {
1183         struct pollfd pollfd;
1184         pollfd.fd = fd;
1185         pollfd.events = POLLIN;
1186         pollfd.revents = 0;
1187         switch (poll( &pollfd, 1, 0 ))
1188         {
1189         case 0:
1190             break;
1191         case 1:  /* got something */
1192             if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1193             TRACE("POLLHUP | POLLERR\n");
1194             /* fall through */
1195         case -1:
1196             wine_server_release_fd( hPipe, fd );
1197             SetLastError(ERROR_BROKEN_PIPE);
1198             return FALSE;
1199         }
1200     }
1201     TRACE(" 0x%08x bytes available\n", avail );
1202     ret = TRUE;
1203     if (lpcbAvail)
1204         *lpcbAvail = avail;
1205     if (lpcbRead)
1206         *lpcbRead = 0;
1207     if (avail && lpvBuffer && cbBuffer)
1208     {
1209         int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1210         readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1211         if (readbytes < 0)
1212         {
1213             WARN("failed to peek socket (%d)\n", errno);
1214             ret = FALSE;
1215         }
1216         else if (lpcbRead)
1217             *lpcbRead = readbytes;
1218     }
1219     wine_server_release_fd( hPipe, fd );
1220     return ret;
1221 #endif /* defined(FIONREAD) */
1222
1223     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1224     FIXME("function not implemented\n");
1225     return FALSE;
1226 }
1227
1228 /***********************************************************************
1229  *           PIPE_CompletionWait   (Internal)
1230  */
1231 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1232 {
1233     LPOVERLAPPED        ovlp = (LPOVERLAPPED)user;
1234
1235     TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1236
1237     if (ovlp)
1238     {
1239         ovlp->Internal = status;
1240         SetEvent(ovlp->hEvent);
1241     }
1242     TRACE("done\n");
1243 }
1244
1245 /***********************************************************************
1246  *           WaitNamedPipeA   (KERNEL32.@)
1247  */
1248 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1249 {
1250     WCHAR buffer[MAX_PATH];
1251
1252     if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1253
1254     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1255     {
1256         SetLastError( ERROR_FILENAME_EXCED_RANGE );
1257         return 0;
1258     }
1259     return WaitNamedPipeW( buffer, nTimeOut );
1260 }
1261
1262
1263 /***********************************************************************
1264  *           WaitNamedPipeW   (KERNEL32.@)
1265  */
1266 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1267 {
1268     BOOL ret;
1269     OVERLAPPED ov;
1270     UNICODE_STRING nt_name;
1271     static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1272
1273     TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1274
1275     if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1276         return FALSE;
1277
1278     if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1279     {
1280         RtlFreeUnicodeString( &nt_name );
1281         return FALSE;
1282     }
1283     if (nt_name.Length < sizeof(leadin) ||
1284         strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1285     {
1286         RtlFreeUnicodeString( &nt_name );
1287         return FALSE;
1288     }
1289
1290     memset(&ov,0,sizeof(ov));
1291     ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1292     if (!ov.hEvent)
1293         return FALSE;
1294
1295     SERVER_START_REQ( wait_named_pipe )
1296     {
1297         req->timeout = nTimeOut;
1298         req->overlapped = &ov;
1299         req->func = PIPE_CompletionWait;
1300         wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1301         ret = !wine_server_call_err( req );
1302     }
1303     SERVER_END_REQ;
1304
1305     RtlFreeUnicodeString( &nt_name );
1306
1307     if(ret)
1308     {
1309         if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1310         {
1311             SetLastError(RtlNtStatusToDosError(ov.Internal));
1312             ret = (ov.Internal==STATUS_SUCCESS);
1313         }
1314     }
1315     CloseHandle(ov.hEvent);
1316     return ret;
1317 }
1318
1319
1320 /***********************************************************************
1321  *           ConnectNamedPipe   (KERNEL32.@)
1322  *
1323  *  Connects to a named pipe
1324  *
1325  *  Parameters
1326  *  hPipe: A handle to a named pipe returned by CreateNamedPipe
1327  *  overlapped: Optional OVERLAPPED struct
1328  *
1329  *  Return values
1330  *  TRUE: Success
1331  *  FALSE: Failure, GetLastError can be called for further details
1332  */
1333 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1334 {
1335     NTSTATUS status;
1336     IO_STATUS_BLOCK status_block;
1337
1338     TRACE("(%p,%p)\n", hPipe, overlapped);
1339
1340     if(overlapped)
1341         overlapped->Internal = STATUS_PENDING;
1342
1343     status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1344                              overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1345                              FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1346
1347     if (status == STATUS_SUCCESS) return TRUE;
1348     SetLastError( RtlNtStatusToDosError(status) );
1349     return FALSE;
1350 }
1351
1352 /***********************************************************************
1353  *           DisconnectNamedPipe   (KERNEL32.@)
1354  *
1355  *  Disconnects from a named pipe
1356  *
1357  *  Parameters
1358  *  hPipe: A handle to a named pipe returned by CreateNamedPipe
1359  *
1360  *  Return values
1361  *  TRUE: Success
1362  *  FALSE: Failure, GetLastError can be called for further details
1363  */
1364 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1365 {
1366     NTSTATUS status;
1367     IO_STATUS_BLOCK io_block;
1368
1369     TRACE("(%p)\n",hPipe);
1370
1371     status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1372                              NULL, 0, NULL, 0);
1373     if (status == STATUS_SUCCESS) return TRUE;
1374     SetLastError( RtlNtStatusToDosError(status) );
1375     return FALSE;
1376 }
1377
1378 /***********************************************************************
1379  *           TransactNamedPipe   (KERNEL32.@)
1380  *
1381  * BUGS
1382  *  should be done as a single operation in the wineserver or kernel
1383  */
1384 BOOL WINAPI TransactNamedPipe(
1385     HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1386     DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1387 {
1388     BOOL r;
1389     DWORD count;
1390
1391     TRACE("%p %p %ld %p %ld %p %p\n",
1392           handle, lpInput, dwInputSize, lpOutput,
1393           dwOutputSize, lpBytesRead, lpOverlapped);
1394
1395     if (lpOverlapped)
1396     {
1397         FIXME("Doesn't support overlapped operation as yet\n");
1398         return FALSE;
1399     }
1400
1401     r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1402     if (r)
1403         r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1404
1405     return r;
1406 }
1407
1408 /***********************************************************************
1409  *           GetNamedPipeInfo   (KERNEL32.@)
1410  */
1411 BOOL WINAPI GetNamedPipeInfo(
1412     HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1413     LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1414 {
1415     BOOL ret;
1416
1417     TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1418           lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1419
1420     SERVER_START_REQ( get_named_pipe_info )
1421     {
1422         req->handle = hNamedPipe;
1423         ret = !wine_server_call_err( req );
1424         if (lpFlags)
1425         {
1426             *lpFlags = 0;
1427             if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1428                 *lpFlags |= PIPE_TYPE_MESSAGE;
1429             if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1430                 *lpFlags |= PIPE_READMODE_MESSAGE;
1431             if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1432                 *lpFlags |= PIPE_NOWAIT;
1433         }
1434         if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1435         if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1436         if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1437     }
1438     SERVER_END_REQ;
1439
1440     return ret;
1441 }
1442
1443 /***********************************************************************
1444  *           GetNamedPipeHandleStateA  (KERNEL32.@)
1445  */
1446 BOOL WINAPI GetNamedPipeHandleStateA(
1447     HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1448     LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1449     LPSTR lpUsername, DWORD nUsernameMaxSize)
1450 {
1451     FIXME("%p %p %p %p %p %p %ld\n",
1452           hNamedPipe, lpState, lpCurInstances,
1453           lpMaxCollectionCount, lpCollectDataTimeout,
1454           lpUsername, nUsernameMaxSize);
1455
1456     return FALSE;
1457 }
1458
1459 /***********************************************************************
1460  *           GetNamedPipeHandleStateW  (KERNEL32.@)
1461  */
1462 BOOL WINAPI GetNamedPipeHandleStateW(
1463     HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1464     LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1465     LPWSTR lpUsername, DWORD nUsernameMaxSize)
1466 {
1467     FIXME("%p %p %p %p %p %p %ld\n",
1468           hNamedPipe, lpState, lpCurInstances,
1469           lpMaxCollectionCount, lpCollectDataTimeout,
1470           lpUsername, nUsernameMaxSize);
1471
1472     return FALSE;
1473 }
1474
1475 /***********************************************************************
1476  *           SetNamedPipeHandleState  (KERNEL32.@)
1477  */
1478 BOOL WINAPI SetNamedPipeHandleState(
1479     HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1480     LPDWORD lpCollectDataTimeout)
1481 {
1482     /* should be a fixme, but this function is called a lot by the RPC
1483      * runtime, and it slows down InstallShield a fair bit. */
1484     WARN("stub: %p %p/%ld %p %p\n",
1485           hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1486     return FALSE;
1487 }
1488
1489 /***********************************************************************
1490  *           CallNamedPipeA  (KERNEL32.@)
1491  */
1492 BOOL WINAPI CallNamedPipeA(
1493     LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1494     LPVOID lpOutput, DWORD dwOutputSize,
1495     LPDWORD lpBytesRead, DWORD nTimeout)
1496 {
1497     DWORD len;
1498     LPWSTR str = NULL;
1499     BOOL ret;
1500
1501     TRACE("%s %p %ld %p %ld %p %ld\n",
1502            debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1503            lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1504
1505     if( lpNamedPipeName )
1506     {
1507         len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1508         str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1509         MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1510     }
1511     ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1512                           dwOutputSize, lpBytesRead, nTimeout );
1513     if( lpNamedPipeName )
1514         HeapFree( GetProcessHeap(), 0, str );
1515
1516     return ret;
1517 }
1518
1519 /***********************************************************************
1520  *           CallNamedPipeW  (KERNEL32.@)
1521  */
1522 BOOL WINAPI CallNamedPipeW(
1523     LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1524     LPVOID lpOutput, DWORD lpOutputSize,
1525     LPDWORD lpBytesRead, DWORD nTimeout)
1526 {
1527     FIXME("%s %p %ld %p %ld %p %ld\n",
1528            debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1529            lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1530     return FALSE;
1531 }
1532
1533 /******************************************************************
1534  *              CreatePipe (KERNEL32.@)
1535  *
1536  */
1537 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1538                         LPSECURITY_ATTRIBUTES sa, DWORD size )
1539 {
1540     static unsigned     index /* = 0 */;
1541     WCHAR               name[64];
1542     HANDLE              hr, hw;
1543     unsigned            in_index = index;
1544     UNICODE_STRING      nt_name;
1545     OBJECT_ATTRIBUTES   attr;
1546     NTSTATUS            status;
1547     IO_STATUS_BLOCK     iosb;
1548     LARGE_INTEGER       timeout;
1549
1550     *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1551
1552     attr.Length                   = sizeof(attr);
1553     attr.RootDirectory            = 0;
1554     attr.ObjectName               = &nt_name;
1555     attr.Attributes               = OBJ_CASE_INSENSITIVE |
1556         (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1557     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
1558     attr.SecurityQualityOfService = NULL;
1559
1560     timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1561     /* generate a unique pipe name (system wide) */
1562     do
1563     {
1564         static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1565          '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1566          'u','.','%','0','8','u','\0' };
1567
1568         snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1569                   GetCurrentProcessId(), ++index);
1570         RtlInitUnicodeString(&nt_name, name);
1571         status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1572                                        0, FILE_OVERWRITE_IF,
1573                                        FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1574                                        FALSE, FALSE, FALSE, 
1575                                        1, size, size, &timeout);
1576         if (status)
1577         {
1578             SetLastError( RtlNtStatusToDosError(status) );
1579             hr = INVALID_HANDLE_VALUE;
1580         }
1581     } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1582     /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1583     if (hr == INVALID_HANDLE_VALUE) return FALSE;
1584
1585     status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1586                         FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1587
1588     if (status) 
1589     {
1590         SetLastError( RtlNtStatusToDosError(status) );
1591         NtClose(hr);
1592         return FALSE;
1593     }
1594
1595     *hReadPipe = hr;
1596     *hWritePipe = hw;
1597     return TRUE;
1598 }
1599
1600
1601 /******************************************************************************
1602  * CreateMailslotA [KERNEL32.@]
1603  */
1604 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1605                                DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1606 {
1607     DWORD len;
1608     HANDLE handle;
1609     LPWSTR name = NULL;
1610
1611     TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1612           nMaxMessageSize, lReadTimeout, sa);
1613
1614     if( lpName )
1615     {
1616         len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1617         name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1618         MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1619     }
1620
1621     handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1622
1623     HeapFree( GetProcessHeap(), 0, name );
1624
1625     return handle;
1626 }
1627
1628
1629 /******************************************************************************
1630  * CreateMailslotW [KERNEL32.@]
1631  *
1632  * Create a mailslot with specified name.
1633  *
1634  * PARAMS
1635  *    lpName          [I] Pointer to string for mailslot name
1636  *    nMaxMessageSize [I] Maximum message size
1637  *    lReadTimeout    [I] Milliseconds before read time-out
1638  *    sa              [I] Pointer to security structure
1639  *
1640  * RETURNS
1641  *    Success: Handle to mailslot
1642  *    Failure: INVALID_HANDLE_VALUE
1643  */
1644 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1645                                DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1646 {
1647     HANDLE handle = INVALID_HANDLE_VALUE;
1648     OBJECT_ATTRIBUTES attr;
1649     UNICODE_STRING nameW;
1650     LARGE_INTEGER timeout;
1651     IO_STATUS_BLOCK iosb;
1652     NTSTATUS status;
1653
1654     TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1655           nMaxMessageSize, lReadTimeout, sa);
1656
1657     if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1658     {
1659         SetLastError( ERROR_PATH_NOT_FOUND );
1660         return INVALID_HANDLE_VALUE;
1661     }
1662
1663     if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1664     {
1665         SetLastError( ERROR_FILENAME_EXCED_RANGE );
1666         RtlFreeUnicodeString( &nameW );
1667         return INVALID_HANDLE_VALUE;
1668     }
1669
1670     attr.Length = sizeof(attr);
1671     attr.RootDirectory = 0;
1672     attr.Attributes = OBJ_CASE_INSENSITIVE;
1673     attr.ObjectName = &nameW;
1674     attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1675     attr.SecurityQualityOfService = NULL;
1676
1677     timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1678
1679     status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1680                                    &iosb, 0, 0, nMaxMessageSize, &timeout );
1681     if (status)
1682     {
1683         SetLastError( RtlNtStatusToDosError(status) );
1684         handle = INVALID_HANDLE_VALUE;
1685     }
1686
1687     RtlFreeUnicodeString( &nameW );
1688     return handle;
1689 }
1690
1691
1692 /******************************************************************************
1693  * GetMailslotInfo [KERNEL32.@]
1694  *
1695  * Retrieve information about a mailslot.
1696  *
1697  * PARAMS
1698  *    hMailslot        [I] Mailslot handle
1699  *    lpMaxMessageSize [O] Address of maximum message size
1700  *    lpNextSize       [O] Address of size of next message
1701  *    lpMessageCount   [O] Address of number of messages
1702  *    lpReadTimeout    [O] Address of read time-out
1703  *
1704  * RETURNS
1705  *    Success: TRUE
1706  *    Failure: FALSE
1707  */
1708 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1709                                LPDWORD lpNextSize, LPDWORD lpMessageCount,
1710                                LPDWORD lpReadTimeout )
1711 {
1712     FILE_MAILSLOT_QUERY_INFORMATION info;
1713     IO_STATUS_BLOCK iosb;
1714     NTSTATUS status;
1715
1716     TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1717           lpNextSize, lpMessageCount, lpReadTimeout);
1718
1719     status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1720                                      FileMailslotQueryInformation );
1721
1722     if( status != STATUS_SUCCESS )
1723     {
1724         SetLastError( RtlNtStatusToDosError(status) );
1725         return FALSE;
1726     }
1727
1728     if( lpMaxMessageSize )
1729         *lpMaxMessageSize = info.MaximumMessageSize;
1730     if( lpNextSize )
1731         *lpNextSize = info.NextMessageSize;
1732     if( lpMessageCount )
1733         *lpMessageCount = info.MessagesAvailable;
1734     if( lpReadTimeout )
1735         *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1736
1737     return TRUE;
1738 }
1739
1740
1741 /******************************************************************************
1742  * SetMailslotInfo [KERNEL32.@]
1743  *
1744  * Set the read timeout of a mailslot.
1745  *
1746  * PARAMS
1747  *  hMailslot     [I] Mailslot handle
1748  *  dwReadTimeout [I] Timeout in milliseconds.
1749  *
1750  * RETURNS
1751  *    Success: TRUE
1752  *    Failure: FALSE
1753  */
1754 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1755 {
1756     FILE_MAILSLOT_SET_INFORMATION info;
1757     IO_STATUS_BLOCK iosb;
1758     NTSTATUS status;
1759
1760     TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1761
1762     info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1763     status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1764                                    FileMailslotSetInformation );
1765     if( status != STATUS_SUCCESS )
1766     {
1767         SetLastError( RtlNtStatusToDosError(status) );
1768         return FALSE;
1769     }
1770     return TRUE;
1771 }
1772
1773
1774 /******************************************************************************
1775  *              CreateIoCompletionPort (KERNEL32.@)
1776  */
1777 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1778                                      ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1779 {
1780     FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1781           hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1782     return NULL;
1783 }
1784
1785
1786 /******************************************************************************
1787  *              GetQueuedCompletionStatus (KERNEL32.@)
1788  */
1789 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1790                                        PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1791                                        DWORD dwMilliseconds )
1792 {
1793     FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1794           CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1795     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1796     return FALSE;
1797 }
1798
1799 /******************************************************************************
1800  *              CreateJobObjectW (KERNEL32.@)
1801  */
1802 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1803 {
1804     FIXME("%p %s\n", attr, debugstr_w(name) );
1805     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1806     return 0;
1807 }
1808
1809 /******************************************************************************
1810  *              CreateJobObjectA (KERNEL32.@)
1811  */
1812 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1813 {
1814     LPWSTR str = NULL;
1815     UINT len;
1816     HANDLE r;
1817
1818     TRACE("%p %s\n", attr, debugstr_a(name) );
1819
1820     if( name )
1821     {
1822         len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1823         str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1824         if( !str )
1825         {
1826             SetLastError( ERROR_OUTOFMEMORY );
1827             return 0;
1828         }
1829         len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1830     }
1831
1832     r = CreateJobObjectW( attr, str );
1833
1834     HeapFree( GetProcessHeap(), 0, str );
1835
1836     return r;
1837 }
1838
1839 /******************************************************************************
1840  *              AssignProcessToJobObject (KERNEL32.@)
1841  */
1842 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1843 {
1844     FIXME("%p %p\n", hJob, hProcess);
1845     return TRUE;
1846 }
1847
1848 #ifdef __i386__
1849
1850 /***********************************************************************
1851  *              InterlockedCompareExchange (KERNEL32.@)
1852  */
1853 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1854 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1855                   "movl 12(%esp),%eax\n\t"
1856                   "movl 8(%esp),%ecx\n\t"
1857                   "movl 4(%esp),%edx\n\t"
1858                   "lock; cmpxchgl %ecx,(%edx)\n\t"
1859                   "ret $12")
1860
1861 /***********************************************************************
1862  *              InterlockedExchange (KERNEL32.@)
1863  */
1864 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1865 __ASM_GLOBAL_FUNC(InterlockedExchange,
1866                   "movl 8(%esp),%eax\n\t"
1867                   "movl 4(%esp),%edx\n\t"
1868                   "lock; xchgl %eax,(%edx)\n\t"
1869                   "ret $8")
1870
1871 /***********************************************************************
1872  *              InterlockedExchangeAdd (KERNEL32.@)
1873  */
1874 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1875 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1876                   "movl 8(%esp),%eax\n\t"
1877                   "movl 4(%esp),%edx\n\t"
1878                   "lock; xaddl %eax,(%edx)\n\t"
1879                   "ret $8")
1880
1881 /***********************************************************************
1882  *              InterlockedIncrement (KERNEL32.@)
1883  */
1884 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1885 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1886                   "movl 4(%esp),%edx\n\t"
1887                   "movl $1,%eax\n\t"
1888                   "lock; xaddl %eax,(%edx)\n\t"
1889                   "incl %eax\n\t"
1890                   "ret $4")
1891
1892 /***********************************************************************
1893  *              InterlockedDecrement (KERNEL32.@)
1894  */
1895 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1896                   "movl 4(%esp),%edx\n\t"
1897                   "movl $-1,%eax\n\t"
1898                   "lock; xaddl %eax,(%edx)\n\t"
1899                   "decl %eax\n\t"
1900                   "ret $4")
1901
1902 #else  /* __i386__ */
1903
1904 /***********************************************************************
1905  *              InterlockedCompareExchange (KERNEL32.@)
1906  *
1907  * Atomically swap one value with another.
1908  *
1909  * PARAMS
1910  *  dest    [I/O] The value to replace
1911  *  xchq    [I]   The value to be swapped
1912  *  compare [I]   The value to compare to dest
1913  *
1914  * RETURNS
1915  *  The resulting value of dest.
1916  *
1917  * NOTES
1918  *  dest is updated only if it is equal to compare, otherwise no swap is done.
1919  */
1920 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1921 {
1922     return interlocked_cmpxchg( (int *)dest, xchg, compare );
1923 }
1924
1925 /***********************************************************************
1926  *              InterlockedExchange (KERNEL32.@)
1927  *
1928  * Atomically swap one value with another.
1929  *
1930  * PARAMS
1931  *  dest [I/O] The value to replace
1932  *  val  [I]   The value to be swapped
1933  *
1934  * RETURNS
1935  *  The resulting value of dest.
1936  */
1937 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1938 {
1939     return interlocked_xchg( (int *)dest, val );
1940 }
1941
1942 /***********************************************************************
1943  *              InterlockedExchangeAdd (KERNEL32.@)
1944  *
1945  * Atomically add one value to another.
1946  *
1947  * PARAMS
1948  *  dest [I/O] The value to add to
1949  *  incr [I]   The value to be added
1950  *
1951  * RETURNS
1952  *  The resulting value of dest.
1953  */
1954 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1955 {
1956     return interlocked_xchg_add( (int *)dest, incr );
1957 }
1958
1959 /***********************************************************************
1960  *              InterlockedIncrement (KERNEL32.@)
1961  *
1962  * Atomically increment a value.
1963  *
1964  * PARAMS
1965  *  dest [I/O] The value to increment
1966  *
1967  * RETURNS
1968  *  The resulting value of dest.
1969  */
1970 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
1971 {
1972     return interlocked_xchg_add( (int *)dest, 1 ) + 1;
1973 }
1974
1975 /***********************************************************************
1976  *              InterlockedDecrement (KERNEL32.@)
1977  *
1978  * Atomically decrement a value.
1979  *
1980  * PARAMS
1981  *  dest [I/O] The value to decrement
1982  *
1983  * RETURNS
1984  *  The resulting value of dest.
1985  */
1986 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
1987 {
1988     return interlocked_xchg_add( (int *)dest, -1 ) - 1;
1989 }
1990
1991 #endif  /* __i386__ */