~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/sync.c

Version: ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.