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