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