Author: ion Date: Wed Jun 28 21:08:35 2006 New Revision: 22676
URL: http://svn.reactos.org/svn/reactos?rev=22676&view=rev Log: - Rewrite all synch object wrappers in kernel32 to use a single unified method of implementation: - A->W converstion through static TEB buffer. - Failure if opening without a name. - Special warning code for objects that already exist (fixes some WINE test failures and probably makes a myriad of applications work). - Use BasepConvertObjectAttributes when creating an object to remove code duplication. - InitializeCrticalSectionAndSpinCount shouldn't raise an exception on failure. - Optimize WaitForMultipleObjects to cache 8 objects on the stack instead of only 3. - Reformat and comment all the files to ROS standards.
Modified: trunk/reactos/dll/win32/kernel32/synch/critical.c trunk/reactos/dll/win32/kernel32/synch/event.c trunk/reactos/dll/win32/kernel32/synch/mutex.c trunk/reactos/dll/win32/kernel32/synch/sem.c trunk/reactos/dll/win32/kernel32/synch/timer.c trunk/reactos/dll/win32/kernel32/synch/wait.c
Modified: trunk/reactos/dll/win32/kernel32/synch/critical.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/cr... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/critical.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/critical.c Wed Jun 28 21:08:35 2006 @@ -1,58 +1,58 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * FILE: lib/kernel32/sync/critical.c - * PURPOSE: Critical sections - * PROGRAMMER: Filip Navara - * Eric Kohl - * UPDATE HISTORY: - * Created 30/09/98 +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/critical.c + * PURPOSE: Wrappers for the RTL Critical Section Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
-/* INCLUDES ******************************************************************/ +/* INCLUDES *****************************************************************/
#include <k32.h>
#define NDEBUG -#include "../include/debug.h" - +#include "debug.h"
/* FUNCTIONS *****************************************************************/
/* * @implemented */ -VOID STDCALL -InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) +VOID +WINAPI +InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection) { - NTSTATUS Status; + NTSTATUS Status;
- Status = RtlInitializeCriticalSection((PRTL_CRITICAL_SECTION)lpCriticalSection); - if (!NT_SUCCESS(Status)) - { - RtlRaiseStatus(Status); - } + /* Initialize the critical section and raise an exception if we failed */ + Status = RtlInitializeCriticalSection( + (PRTL_CRITICAL_SECTION)lpCriticalSection); + if (!NT_SUCCESS(Status)) RtlRaiseStatus(Status); }
/* * @implemented */ BOOL -STDCALL -InitializeCriticalSectionAndSpinCount( - LPCRITICAL_SECTION lpCriticalSection, - DWORD dwSpinCount - ) +WINAPI +InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection, + IN DWORD dwSpinCount) { NTSTATUS Status;
- Status = RtlInitializeCriticalSectionAndSpinCount((PRTL_CRITICAL_SECTION)lpCriticalSection, dwSpinCount); - if (Status) - { - RtlRaiseStatus(Status); - } - return NT_SUCCESS(Status); + /* Initialize the critical section */ + Status = RtlInitializeCriticalSectionAndSpinCount( + (PRTL_CRITICAL_SECTION)lpCriticalSection, + dwSpinCount); + if (!NT_SUCCESS(Status)) + { + /* Set failure code */ + SetLastErrorByStatus(Status); + return FALSE; + } + + /* Success */ + return TRUE; }
/* EOF */
Modified: trunk/reactos/dll/win32/kernel32/synch/event.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/ev... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/event.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/event.c Wed Jun 28 21:08:35 2006 @@ -1,12 +1,9 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * FILE: lib/kernel32/synch/event.c - * PURPOSE: Local string functions - * PROGRAMMER: Ariadne ( ariadne@xs4all.nl) - * UPDATE HISTORY: - * Created 01/11/98 +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/event.c + * PURPOSE: Wrappers for the NT Event Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
/* INCLUDES *****************************************************************/ @@ -14,228 +11,233 @@ #include <k32.h>
#define NDEBUG -#include "../include/debug.h" - +#include "debug.h"
/* FUNCTIONS ****************************************************************/
+HANDLE +WINAPI +CreateEventA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, + IN BOOL bManualReset, + IN BOOL bInitialState, + IN LPCSTR lpName OPTIONAL) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + LPCWSTR UnicodeName = NULL; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Otherwise, save the buffer */ + UnicodeName = (LPCWSTR)UnicodeCache->Buffer; + } + + /* Call the Unicode API */ + return CreateEventW(lpEventAttributes, + bManualReset, + bInitialState, + UnicodeName); +} + +HANDLE +WINAPI +CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, + IN BOOL bManualReset, + IN BOOL bInitialState, + IN LPCWSTR lpName OPTIONAL) +{ + NTSTATUS Status; + OBJECT_ATTRIBUTES LocalAttributes; + POBJECT_ATTRIBUTES ObjectAttributes; + HANDLE Handle; + UNICODE_STRING ObjectName; + + /* Now check if we got a name */ + if (lpName) RtlInitUnicodeString(&ObjectName, lpName); + + /* Now convert the object attributes */ + ObjectAttributes = BasepConvertObjectAttributes(&LocalAttributes, + lpEventAttributes, + lpName ? &ObjectName : NULL); + + /* Create the event */ + Status = NtCreateEvent(&Handle, + EVENT_ALL_ACCESS, + ObjectAttributes, + bManualReset ? + NotificationEvent : SynchronizationEvent, + (BOOLEAN)bInitialState); + if (NT_SUCCESS(Status)) + { + /* Check if the object already existed */ + if (Status == STATUS_OBJECT_NAME_EXISTS) + { + /* Set distinguished Win32 error code */ + SetLastError(ERROR_ALREADY_EXISTS); + } + else + { + /* Otherwise, set success */ + SetLastError(ERROR_SUCCESS); + } + + /* Return the handle */ + return Handle; + } + else + { + /* Convert the NT Status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } +} + +HANDLE +WINAPI +OpenEventA(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpName) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + } + else + { + /* We need a name */ + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } + + /* Call the Unicode API */ + return OpenEventW(dwDesiredAccess, + bInheritHandle, + (LPCWSTR)UnicodeCache->Buffer); +} + +HANDLE +WINAPI +OpenEventW(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCWSTR lpName) +{ + OBJECT_ATTRIBUTES ObjectAttributes; + UNICODE_STRING ObjectName; + NTSTATUS Status; + HANDLE Handle; + + /* Make sure we got a name */ + if (!lpName) + { + /* Fail without one */ + SetLastErrorByStatus(STATUS_INVALID_PARAMETER); + return NULL; + } + + /* Initialize the object name and attributes */ + RtlInitUnicodeString(&ObjectName, lpName); + InitializeObjectAttributes(&ObjectAttributes, + &ObjectName, + bInheritHandle ? OBJ_INHERIT : 0, + hBaseDir, + NULL); + + /* Open the event */ + Status = NtOpenEvent(&Handle, dwDesiredAccess, &ObjectAttributes); + if (!NT_SUCCESS(Status)) + { + /* Convert the status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Return the handle */ + return Handle; +} + /* * @implemented */ -HANDLE STDCALL -CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, - BOOL bManualReset, - BOOL bInitialState, - LPCSTR lpName) -{ - UNICODE_STRING EventNameU; - ANSI_STRING EventName; - HANDLE EventHandle; - - if (lpName) - { - RtlInitAnsiString(&EventName, - (LPSTR)lpName); - RtlAnsiStringToUnicodeString(&EventNameU, - &EventName, - TRUE); - } - - EventHandle = CreateEventW(lpEventAttributes, - bManualReset, - bInitialState, - (lpName ? EventNameU.Buffer : NULL)); - - if (lpName) - { - RtlFreeUnicodeString(&EventNameU); - } - - return EventHandle; -} - +BOOL +WINAPI +PulseEvent(IN HANDLE hEvent) +{ + NTSTATUS Status; + + /* Pulse the event */ + Status = NtPulseEvent(hEvent, NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; +}
/* * @implemented */ -HANDLE STDCALL -CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, - BOOL bManualReset, - BOOL bInitialState, - LPCWSTR lpName) -{ - NTSTATUS Status; - HANDLE hEvent; - UNICODE_STRING UnicodeName; - OBJECT_ATTRIBUTES ObjectAttributes; - - if (lpName != NULL) - { - RtlInitUnicodeString(&UnicodeName, (LPWSTR)lpName); - } - - InitializeObjectAttributes(&ObjectAttributes, - (lpName ? &UnicodeName : NULL), - 0, - (lpName ? hBaseDir : NULL), - NULL); - - if (lpEventAttributes != NULL) - { - ObjectAttributes.SecurityDescriptor = lpEventAttributes->lpSecurityDescriptor; - if (lpEventAttributes->bInheritHandle) - { - ObjectAttributes.Attributes |= OBJ_INHERIT; - } - } - - Status = NtCreateEvent(&hEvent, - EVENT_ALL_ACCESS, - &ObjectAttributes, - (bManualReset ? NotificationEvent : SynchronizationEvent), - bInitialState); - DPRINT( "Called\n" ); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return hEvent; -} - +BOOL +WINAPI +ResetEvent(IN HANDLE hEvent) +{ + NTSTATUS Status; + + /* Clear the event */ + Status = NtResetEvent(hEvent, NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; +}
/* * @implemented */ -HANDLE STDCALL -OpenEventA(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCSTR lpName) -{ - UNICODE_STRING EventNameU; - ANSI_STRING EventName; - HANDLE EventHandle; - - if (lpName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitUnicodeString(&EventNameU, - NULL); - - RtlInitAnsiString(&EventName, - (LPSTR)lpName); - RtlAnsiStringToUnicodeString(&EventNameU, - &EventName, - TRUE); - - EventHandle = OpenEventW(dwDesiredAccess, - bInheritHandle, - EventNameU.Buffer); - - RtlFreeUnicodeString(&EventNameU); - - return EventHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenEventW(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCWSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING EventNameString; - NTSTATUS Status; - HANDLE hEvent = NULL; - - if (lpName == NULL) - { - SetLastError(ERROR_INVALID_PARAMETER); - return NULL; - } - - RtlInitUnicodeString(&EventNameString, (LPWSTR)lpName); - - InitializeObjectAttributes(&ObjectAttributes, - &EventNameString, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenEvent(&hEvent, - dwDesiredAccess, - &ObjectAttributes); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return hEvent; -} - - -/* - * @implemented - */ -BOOL STDCALL -PulseEvent(HANDLE hEvent) -{ - NTSTATUS Status; - - Status = NtPulseEvent(hEvent, NULL); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus (Status); - return FALSE; - } - - return TRUE; -} - - -/* - * @implemented - */ -BOOL STDCALL -ResetEvent(HANDLE hEvent) -{ - NTSTATUS Status; - - Status = NtClearEvent(hEvent); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - - return TRUE; -} - - -/* - * @implemented - */ -BOOL STDCALL -SetEvent(HANDLE hEvent) -{ - NTSTATUS Status; - - Status = NtSetEvent(hEvent, NULL); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - - return TRUE; +BOOL +WINAPI +SetEvent(IN HANDLE hEvent) +{ + NTSTATUS Status; + + /* Set the event */ + Status = NtSetEvent(hEvent, NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; }
/* EOF */
Modified: trunk/reactos/dll/win32/kernel32/synch/mutex.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/mu... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/mutex.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/mutex.c Wed Jun 28 21:08:35 2006 @@ -1,12 +1,9 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * FILE: lib/kernel32/synch/mutex.c - * PURPOSE: Mutex functions - * PROGRAMMER: Eric Kohl (ekohl@rz-online.de) - * UPDATE HISTORY: - * Created 01/20/2001 +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/mutex.c + * PURPOSE: Wrappers for the NT Mutant Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
/* INCLUDES *****************************************************************/ @@ -14,214 +11,204 @@ #include <k32.h>
#define NDEBUG -#include "../include/debug.h" - +#include "debug.h"
/* FUNCTIONS *****************************************************************/
/* * @implemented */ -HANDLE STDCALL -CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, - BOOL bInitialOwner, - LPCSTR lpName) -{ - UNICODE_STRING NameU; - ANSI_STRING Name; - HANDLE Handle; - - if (lpName != NULL) - { - RtlInitAnsiString(&Name, - (LPSTR)lpName); - - RtlAnsiStringToUnicodeString(&NameU, - &Name, - TRUE); - } - - Handle = CreateMutexW(lpMutexAttributes, - bInitialOwner, - (lpName ? NameU.Buffer : NULL)); - - if (lpName != NULL) - { - RtlFreeUnicodeString(&NameU); - } - - return Handle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, - BOOL bInitialOwner, - LPCWSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - NTSTATUS Status; - UNICODE_STRING UnicodeName; - HANDLE MutantHandle; - - if (lpName != NULL) - { - RtlInitUnicodeString(&UnicodeName, - (LPWSTR)lpName); - } - - InitializeObjectAttributes(&ObjectAttributes, - (lpName ? &UnicodeName : NULL), - 0, - (lpName ? hBaseDir : NULL), - NULL); - - if (lpMutexAttributes != NULL) - { - ObjectAttributes.SecurityDescriptor = lpMutexAttributes->lpSecurityDescriptor; - if (lpMutexAttributes->bInheritHandle) - { - ObjectAttributes.Attributes |= OBJ_INHERIT; - } - } - - Status = NtCreateMutant(&MutantHandle, - MUTEX_ALL_ACCESS, - &ObjectAttributes, - (BOOLEAN)bInitialOwner); - if (Status == STATUS_OBJECT_NAME_COLLISION) - { - Status = NtOpenMutant(&MutantHandle, - MUTEX_ALL_ACCESS, - &ObjectAttributes); - if (NT_SUCCESS(Status)) - { - SetLastError(ERROR_ALREADY_EXISTS); - } - } - else if (NT_SUCCESS(Status)) - { - SetLastError(ERROR_SUCCESS); - } - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return MutantHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenMutexA(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING NameU; - ANSI_STRING Name; - HANDLE Handle; - NTSTATUS Status; - - if (lpName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitAnsiString(&Name, - (LPSTR)lpName); - RtlAnsiStringToUnicodeString(&NameU, - &Name, - TRUE); - - InitializeObjectAttributes(&ObjectAttributes, - &NameU, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenMutant(&Handle, - (ACCESS_MASK)dwDesiredAccess, - &ObjectAttributes); - - RtlFreeUnicodeString(&NameU); - - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return Handle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenMutexW(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCWSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING Name; - HANDLE Handle; - NTSTATUS Status; - - if (lpName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitUnicodeString(&Name, - (LPWSTR)lpName); - - InitializeObjectAttributes(&ObjectAttributes, - &Name, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenMutant(&Handle, - (ACCESS_MASK)dwDesiredAccess, - &ObjectAttributes); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return Handle; -} - - -/* - * @implemented - */ -BOOL STDCALL -ReleaseMutex(HANDLE hMutex) -{ - NTSTATUS Status; - - Status = NtReleaseMutant(hMutex, - NULL); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - - return TRUE; +HANDLE +WINAPI +CreateMutexA(IN LPSECURITY_ATTRIBUTES lpMutexAttributes OPTIONAL, + IN BOOL bInitialOwner, + IN LPCSTR lpName OPTIONAL) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + LPCWSTR UnicodeName = NULL; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Otherwise, save the buffer */ + UnicodeName = (LPCWSTR)UnicodeCache->Buffer; + } + + /* Call the Unicode API */ + return CreateMutexW(lpMutexAttributes, + bInitialOwner, + UnicodeName); +} + +/* + * @implemented + */ +HANDLE +WINAPI +CreateMutexW(IN LPSECURITY_ATTRIBUTES lpMutexAttributes OPTIONAL, + IN BOOL bInitialOwner, + IN LPCWSTR lpName OPTIONAL) +{ + NTSTATUS Status; + OBJECT_ATTRIBUTES LocalAttributes; + POBJECT_ATTRIBUTES ObjectAttributes; + HANDLE Handle; + UNICODE_STRING ObjectName; + + /* Now check if we got a name */ + if (lpName) RtlInitUnicodeString(&ObjectName, lpName); + + /* Now convert the object attributes */ + ObjectAttributes = BasepConvertObjectAttributes(&LocalAttributes, + lpMutexAttributes, + lpName ? &ObjectName : NULL); + + /* Create the mutant */ + Status = NtCreateMutant(&Handle, + MUTANT_ALL_ACCESS, + ObjectAttributes, + (BOOLEAN)bInitialOwner); + if (NT_SUCCESS(Status)) + { + /* Check if the object already existed */ + if (Status == STATUS_OBJECT_NAME_EXISTS) + { + /* Set distinguished Win32 error code */ + SetLastError(ERROR_ALREADY_EXISTS); + } + else + { + /* Otherwise, set success */ + SetLastError(ERROR_SUCCESS); + } + + /* Return the handle */ + return Handle; + } + else + { + /* Convert the NT Status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenMutexA(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpName) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + } + else + { + /* We need a name */ + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } + + /* Call the Unicode API */ + return OpenMutexW(dwDesiredAccess, + bInheritHandle, + (LPCWSTR)UnicodeCache->Buffer); +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenMutexW(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCWSTR lpName) +{ + OBJECT_ATTRIBUTES ObjectAttributes; + UNICODE_STRING ObjectName; + NTSTATUS Status; + HANDLE Handle; + + /* Make sure we got a name */ + if (!lpName) + { + /* Fail without one */ + SetLastErrorByStatus(STATUS_INVALID_PARAMETER); + return NULL; + } + + /* Initialize the object name and attributes */ + RtlInitUnicodeString(&ObjectName, lpName); + InitializeObjectAttributes(&ObjectAttributes, + &ObjectName, + bInheritHandle ? OBJ_INHERIT : 0, + hBaseDir, + NULL); + + /* Open the mutant */ + Status = NtOpenMutant(&Handle, dwDesiredAccess, &ObjectAttributes); + if (!NT_SUCCESS(Status)) + { + /* Convert the status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Return the handle */ + return Handle; +} + +/* + * @implemented + */ +BOOL +WINAPI +ReleaseMutex(IN HANDLE hMutex) +{ + NTSTATUS Status; + + /* Release the mutant */ + Status = NtReleaseMutant(hMutex, NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; }
Modified: trunk/reactos/dll/win32/kernel32/synch/sem.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/se... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/sem.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/sem.c Wed Jun 28 21:08:35 2006 @@ -1,12 +1,9 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * FILE: lib/kernel32/synch/sem.c - * PURPOSE: Semaphore functions - * PROGRAMMER: Eric Kohl (ekohl@rz-online.de) - * UPDATE HISTORY: - * Created 01/20/2001 +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/sem.c + * PURPOSE: Wrappers for the NT Semaphore Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
/* INCLUDES *****************************************************************/ @@ -14,204 +11,210 @@ #include <k32.h>
#define NDEBUG -#include "../include/debug.h" - +#include "debug.h"
/* FUNCTIONS ****************************************************************/
/* * @implemented */ -HANDLE STDCALL -CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, - LONG lInitialCount, - LONG lMaximumCount, - LPCSTR lpName) -{ - UNICODE_STRING NameU; - ANSI_STRING Name; - HANDLE Handle; - - if (lpName != NULL) - { - RtlInitAnsiString(&Name, - (LPSTR)lpName); - RtlAnsiStringToUnicodeString(&NameU, - &Name, - TRUE); - } - - Handle = CreateSemaphoreW(lpSemaphoreAttributes, - lInitialCount, - lMaximumCount, - (lpName ? NameU.Buffer : NULL)); - - if (lpName != NULL) - { - RtlFreeUnicodeString (&NameU); - } - - return Handle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, - LONG lInitialCount, - LONG lMaximumCount, - LPCWSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - NTSTATUS Status; - UNICODE_STRING UnicodeName; - HANDLE SemaphoreHandle; - - if (lpName != NULL) - { - RtlInitUnicodeString(&UnicodeName, lpName); - } - - InitializeObjectAttributes(&ObjectAttributes, - (lpName ? &UnicodeName : NULL), - 0, - (lpName ? hBaseDir : NULL), - NULL); - - if (lpSemaphoreAttributes != NULL) - { - ObjectAttributes.SecurityDescriptor = lpSemaphoreAttributes->lpSecurityDescriptor; - if (lpSemaphoreAttributes->bInheritHandle) - { - ObjectAttributes.Attributes |= OBJ_INHERIT; - } - } - - Status = NtCreateSemaphore(&SemaphoreHandle, - SEMAPHORE_ALL_ACCESS, - &ObjectAttributes, - lInitialCount, - lMaximumCount); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - return SemaphoreHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenSemaphoreA(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING NameU; - ANSI_STRING Name; - HANDLE Handle; - NTSTATUS Status; - - if (lpName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitAnsiString(&Name, - (LPSTR)lpName); - RtlAnsiStringToUnicodeString(&NameU, - &Name, - TRUE); - - InitializeObjectAttributes(&ObjectAttributes, - &NameU, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenSemaphore(&Handle, - (ACCESS_MASK)dwDesiredAccess, - &ObjectAttributes); - - RtlFreeUnicodeString(&NameU); - - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return Handle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenSemaphoreW(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCWSTR lpName) -{ - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING Name; - HANDLE Handle; - NTSTATUS Status; - - if (lpName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitUnicodeString(&Name, - (LPWSTR)lpName); - - InitializeObjectAttributes(&ObjectAttributes, - &Name, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenSemaphore(&Handle, - (ACCESS_MASK)dwDesiredAccess, - &ObjectAttributes); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return Handle; -} - - -/* - * @implemented - */ -BOOL STDCALL -ReleaseSemaphore(HANDLE hSemaphore, - LONG lReleaseCount, - LPLONG lpPreviousCount) -{ - NTSTATUS Status; - - Status = NtReleaseSemaphore(hSemaphore, - lReleaseCount, - lpPreviousCount); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - - return TRUE; +HANDLE +WINAPI +CreateSemaphoreA(IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes OPTIONAL, + IN LONG lInitialCount, + IN LONG lMaximumCount, + IN LPCSTR lpName OPTIONAL) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + LPCWSTR UnicodeName = NULL; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Otherwise, save the buffer */ + UnicodeName = (LPCWSTR)UnicodeCache->Buffer; + } + + /* Call the Unicode API */ + return CreateSemaphoreW(lpSemaphoreAttributes, + lInitialCount, + lMaximumCount, + UnicodeName); +} + +/* + * @implemented + */ +HANDLE +WINAPI +CreateSemaphoreW(IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes OPTIONAL, + IN LONG lInitialCount, + IN LONG lMaximumCount, + IN LPCWSTR lpName OPTIONAL) +{ + NTSTATUS Status; + OBJECT_ATTRIBUTES LocalAttributes; + POBJECT_ATTRIBUTES ObjectAttributes; + HANDLE Handle; + UNICODE_STRING ObjectName; + + /* Now check if we got a name */ + if (lpName) RtlInitUnicodeString(&ObjectName, lpName); + + /* Now convert the object attributes */ + ObjectAttributes = BasepConvertObjectAttributes(&LocalAttributes, + lpSemaphoreAttributes, + lpName ? &ObjectName : NULL); + + /* Create the semaphore */ + Status = NtCreateSemaphore(&Handle, + SEMAPHORE_ALL_ACCESS, + ObjectAttributes, + lInitialCount, + lMaximumCount); + if (NT_SUCCESS(Status)) + { + /* Check if the object already existed */ + if (Status == STATUS_OBJECT_NAME_EXISTS) + { + /* Set distinguished Win32 error code */ + SetLastError(ERROR_ALREADY_EXISTS); + } + else + { + /* Otherwise, set success */ + SetLastError(ERROR_SUCCESS); + } + + /* Return the handle */ + return Handle; + } + else + { + /* Convert the NT Status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenSemaphoreA(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpName) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + + /* Check for a name */ + if (lpName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + } + else + { + /* We need a name */ + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } + + /* Call the Unicode API */ + return OpenSemaphoreW(dwDesiredAccess, + bInheritHandle, + (LPCWSTR)UnicodeCache->Buffer); +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenSemaphoreW(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCWSTR lpName) +{ + OBJECT_ATTRIBUTES ObjectAttributes; + UNICODE_STRING ObjectName; + NTSTATUS Status; + HANDLE Handle; + + /* Make sure we got a name */ + if (!lpName) + { + /* Fail without one */ + SetLastErrorByStatus(STATUS_INVALID_PARAMETER); + return NULL; + } + + /* Initialize the object name and attributes */ + RtlInitUnicodeString(&ObjectName, lpName); + InitializeObjectAttributes(&ObjectAttributes, + &ObjectName, + bInheritHandle ? OBJ_INHERIT : 0, + hBaseDir, + NULL); + + /* Open the semaphore */ + Status = NtOpenSemaphore(&Handle, dwDesiredAccess, &ObjectAttributes); + if (!NT_SUCCESS(Status)) + { + /* Convert the status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Return the handle */ + return Handle; +} + +/* + * @implemented + */ +BOOL +WINAPI +ReleaseSemaphore(IN HANDLE hSemaphore, + IN LONG lReleaseCount, + IN LPLONG lpPreviousCount) +{ + NTSTATUS Status; + + /* Release the semaphore */ + Status = NtReleaseSemaphore(hSemaphore, lReleaseCount, lpPreviousCount); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; }
/* EOF */
Modified: trunk/reactos/dll/win32/kernel32/synch/timer.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/ti... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/timer.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/timer.c Wed Jun 28 21:08:35 2006 @@ -1,224 +1,244 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS kernel - * FILE: lib/kernel32/synch/timer.c - * PURPOSE: Implementing timer - * PROGRAMMER: - */ - -/* INCLUDES ******************************************************************/ +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/timer.c + * PURPOSE: Wrappers for the NT Timer Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) + */ + +/* INCLUDES *****************************************************************/
#include <k32.h>
#define NDEBUG -#include "../include/debug.h" - - -/* FUNCTIONS *****************************************************************/ - -/* - * @implemented - */ -HANDLE STDCALL -CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes, - BOOL bManualReset, - LPCWSTR lpTimerName) -{ - NTSTATUS Status; - HANDLE TimerHandle; - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING UnicodeName; - - if (lpTimerName) - { - RtlInitUnicodeString(&UnicodeName, lpTimerName); - } - - InitializeObjectAttributes(&ObjectAttributes, - (lpTimerName ? &UnicodeName : NULL), - 0, - (lpTimerName ? hBaseDir : NULL), - NULL); - - if (lpTimerAttributes != NULL) - { - ObjectAttributes.SecurityDescriptor = lpTimerAttributes->lpSecurityDescriptor; - if(lpTimerAttributes->bInheritHandle) - { - ObjectAttributes.Attributes |= OBJ_INHERIT; - } - } - - Status = NtCreateTimer(&TimerHandle, - TIMER_ALL_ACCESS, - &ObjectAttributes, - (bManualReset ? NotificationTimer : SynchronizationTimer)); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return TimerHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes, - BOOL bManualReset, - LPCSTR lpTimerName) -{ - UNICODE_STRING TimerNameU; - ANSI_STRING TimerName; - HANDLE TimerHandle; - - if (lpTimerName != NULL) - { - RtlInitAnsiString (&TimerName, - (LPSTR)lpTimerName); - RtlAnsiStringToUnicodeString (&TimerNameU, - &TimerName, - TRUE); - } - - TimerHandle = CreateWaitableTimerW (lpTimerAttributes, - bManualReset, - (lpTimerName ? TimerNameU.Buffer : NULL)); - - if (lpTimerName != NULL) - { - RtlFreeUnicodeString (&TimerNameU); - } - - return TimerHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenWaitableTimerW(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCWSTR lpTimerName) -{ - NTSTATUS Status; - HANDLE TimerHandle; - OBJECT_ATTRIBUTES ObjectAttributes; - UNICODE_STRING UnicodeName; - - if (lpTimerName == NULL) - { - SetLastErrorByStatus(STATUS_INVALID_PARAMETER); - return NULL; - } - - RtlInitUnicodeString(&UnicodeName, - lpTimerName); - InitializeObjectAttributes(&ObjectAttributes, - &UnicodeName, - (bInheritHandle ? OBJ_INHERIT : 0), - hBaseDir, - NULL); - - Status = NtOpenTimer(&TimerHandle, - dwDesiredAccess, - &ObjectAttributes); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return NULL; - } - - return TimerHandle; -} - - -/* - * @implemented - */ -HANDLE STDCALL -OpenWaitableTimerA(DWORD dwDesiredAccess, - BOOL bInheritHandle, - LPCSTR lpTimerName) -{ - UNICODE_STRING TimerNameU; - ANSI_STRING TimerName; - HANDLE TimerHandle; - - if (lpTimerName == NULL) - { +#include "debug.h" + +/* FUNCTIONS ****************************************************************/ + +/* + * @implemented + */ +HANDLE +WINAPI +CreateWaitableTimerW(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL, + IN BOOL bManualReset, + IN LPCWSTR lpTimerName OPTIONAL) +{ + NTSTATUS Status; + OBJECT_ATTRIBUTES LocalAttributes; + POBJECT_ATTRIBUTES ObjectAttributes; + HANDLE Handle; + UNICODE_STRING ObjectName; + + /* Now check if we got a name */ + if (lpTimerName) RtlInitUnicodeString(&ObjectName, lpTimerName); + + /* Now convert the object attributes */ + ObjectAttributes = BasepConvertObjectAttributes(&LocalAttributes, + lpTimerAttributes, + lpTimerName ? &ObjectName : NULL); + + /* Create the timer */ + Status = NtCreateTimer(&Handle, + TIMER_ALL_ACCESS, + ObjectAttributes, + bManualReset ? + NotificationTimer : SynchronizationTimer); + if (NT_SUCCESS(Status)) + { + /* Check if the object already existed */ + if (Status == STATUS_OBJECT_NAME_EXISTS) + { + /* Set distinguished Win32 error code */ + SetLastError(ERROR_ALREADY_EXISTS); + } + else + { + /* Otherwise, set success */ + SetLastError(ERROR_SUCCESS); + } + + /* Return the handle */ + return Handle; + } + else + { + /* Convert the NT Status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } +} + +/* + * @implemented + */ +HANDLE +WINAPI +CreateWaitableTimerA(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL, + IN BOOL bManualReset, + IN LPCSTR lpTimerName OPTIONAL) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + LPCWSTR UnicodeName = NULL; + + /* Check for a name */ + if (lpTimerName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpTimerName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Otherwise, save the buffer */ + UnicodeName = (LPCWSTR)UnicodeCache->Buffer; + } + + /* Call the Unicode API */ + return CreateWaitableTimerW(lpTimerAttributes, + bManualReset, + UnicodeName); +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenWaitableTimerW(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCWSTR lpTimerName) +{ + OBJECT_ATTRIBUTES ObjectAttributes; + UNICODE_STRING ObjectName; + NTSTATUS Status; + HANDLE Handle; + + /* Make sure we got a name */ + if (!lpTimerName) + { + /* Fail without one */ SetLastErrorByStatus(STATUS_INVALID_PARAMETER); return NULL; - } - - RtlInitAnsiString (&TimerName, - (LPSTR)lpTimerName); - RtlAnsiStringToUnicodeString (&TimerNameU, - &TimerName, - TRUE); - - TimerHandle = OpenWaitableTimerW (dwDesiredAccess, - bInheritHandle, - TimerNameU.Buffer); - - RtlFreeUnicodeString (&TimerNameU); - - return TimerHandle; -} - - -/* - * @implemented - */ -BOOL STDCALL -SetWaitableTimer(HANDLE hTimer, - const LARGE_INTEGER *pDueTime, - LONG lPeriod, - PTIMERAPCROUTINE pfnCompletionRoutine, - LPVOID lpArgToCompletionRoutine, - BOOL fResume) -{ - NTSTATUS Status; - BOOLEAN pState; - - Status = NtSetTimer(hTimer, - (LARGE_INTEGER *)pDueTime, - (PTIMER_APC_ROUTINE)pfnCompletionRoutine, - lpArgToCompletionRoutine, - fResume, - lPeriod, - &pState); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - return TRUE; -} - - -/* - * @implemented - */ -BOOL STDCALL -CancelWaitableTimer(HANDLE hTimer) -{ - NTSTATUS Status; - BOOLEAN CurrentState; - - Status = NtCancelTimer(hTimer, - &CurrentState); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } - return TRUE; + } + + /* Initialize the object name and attributes */ + RtlInitUnicodeString(&ObjectName, lpTimerName); + InitializeObjectAttributes(&ObjectAttributes, + &ObjectName, + bInheritHandle ? OBJ_INHERIT : 0, + hBaseDir, + NULL); + + /* Open the timer */ + Status = NtOpenTimer(&Handle, dwDesiredAccess, &ObjectAttributes); + if (!NT_SUCCESS(Status)) + { + /* Convert the status and fail */ + SetLastErrorByStatus(Status); + return NULL; + } + + /* Return the handle */ + return Handle; +} + +/* + * @implemented + */ +HANDLE +WINAPI +OpenWaitableTimerA(IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpTimerName) +{ + NTSTATUS Status; + ANSI_STRING AnsiName; + PUNICODE_STRING UnicodeCache; + + /* Check for a name */ + if (lpTimerName) + { + /* Use TEB Cache */ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; + + /* Convert to unicode */ + RtlInitAnsiString(&AnsiName, lpTimerName); + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); + if (!NT_SUCCESS(Status)) + { + /* Conversion failed */ + SetLastErrorByStatus(Status); + return NULL; + } + } + else + { + /* We need a name */ + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } + + /* Call the Unicode API */ + return OpenWaitableTimerW(dwDesiredAccess, + bInheritHandle, + (LPCWSTR)UnicodeCache->Buffer); +} + +/* + * @implemented + */ +BOOL +WINAPI +SetWaitableTimer(IN HANDLE hTimer, + IN const LARGE_INTEGER *pDueTime, + IN LONG lPeriod, + IN PTIMERAPCROUTINE pfnCompletionRoutine OPTIONAL, + IN OPTIONAL LPVOID lpArgToCompletionRoutine, + IN BOOL fResume) +{ + NTSTATUS Status; + + /* Set the timer */ + Status = NtSetTimer(hTimer, + (PLARGE_INTEGER)pDueTime, + (PTIMER_APC_ROUTINE)pfnCompletionRoutine, + lpArgToCompletionRoutine, + fResume, + lPeriod, + NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; +} + +/* + * @implemented + */ +BOOL +WINAPI +CancelWaitableTimer(IN HANDLE hTimer) +{ + NTSTATUS Status; + + /* Cancel the timer */ + Status = NtCancelTimer(hTimer, NULL); + if (NT_SUCCESS(Status)) return TRUE; + + /* If we got here, then we failed */ + SetLastErrorByStatus(Status); + return FALSE; }
/* EOF */
Modified: trunk/reactos/dll/win32/kernel32/synch/wait.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/synch/wa... ============================================================================== --- trunk/reactos/dll/win32/kernel32/synch/wait.c (original) +++ trunk/reactos/dll/win32/kernel32/synch/wait.c Wed Jun 28 21:08:35 2006 @@ -1,12 +1,9 @@ -/* $Id$ - * - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * FILE: lib/kernel32/synch/wait.c - * PURPOSE: Wait functions - * PROGRAMMER: Ariadne ( ariadne@xs4all.nl) - * UPDATE HISTORY: - * Created 01/11/98 +/* + * PROJECT: ReactOS Win32 Base API + * LICENSE: GPL - See COPYING in the top level directory + * FILE: dll/win32/kernel32/synch/wait.c + * PURPOSE: Wrappers for the NT Wait Implementation + * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
/* INCLUDES *****************************************************************/ @@ -14,296 +11,310 @@ #include <k32.h>
#define NDEBUG -#include "../include/debug.h" - - -/* FUNCTIONS ****************************************************************/ - -DWORD STDCALL -GetConsoleInputWaitHandle (VOID); - -/* - * @implemented - */ -DWORD STDCALL -WaitForSingleObject(HANDLE hHandle, - DWORD dwMilliseconds) -{ - return WaitForSingleObjectEx(hHandle, - dwMilliseconds, - FALSE); -} - - -/* - * @implemented - */ -DWORD STDCALL -WaitForSingleObjectEx(HANDLE hHandle, - DWORD dwMilliseconds, - BOOL bAlertable) +#include "debug.h" + +/* FUNCTIONS *****************************************************************/ + +/* + * @implemented + */ +DWORD +WINAPI +WaitForSingleObject(IN HANDLE hHandle, + IN DWORD dwMilliseconds) +{ + /* Call the extended API */ + return WaitForSingleObjectEx(hHandle, dwMilliseconds, FALSE); +} + +/* + * @implemented + */ +DWORD +WINAPI +WaitForSingleObjectEx(IN HANDLE hHandle, + IN DWORD dwMilliseconds, + IN BOOL bAlertable) { PLARGE_INTEGER TimePtr; LARGE_INTEGER Time; NTSTATUS Status;
- /* Get real handle */ - switch ((ULONG)hHandle) - { - case STD_INPUT_HANDLE: - hHandle = NtCurrentPeb()->ProcessParameters->StandardInput; - break; - - case STD_OUTPUT_HANDLE: - hHandle = NtCurrentPeb()->ProcessParameters->StandardOutput; - break; - - case STD_ERROR_HANDLE: - hHandle = NtCurrentPeb()->ProcessParameters->StandardError; - break; - } - - /* Check for console handle */ - if (IsConsoleHandle(hHandle)) - { - if (!VerifyConsoleIoHandle(hHandle)) - { - SetLastError (ERROR_INVALID_HANDLE); - return WAIT_FAILED; - } - - hHandle = (HANDLE)GetConsoleInputWaitHandle(); - if (hHandle == NULL || hHandle == INVALID_HANDLE_VALUE) - { - SetLastError (ERROR_INVALID_HANDLE); - return WAIT_FAILED; - - } - } - - if (dwMilliseconds == INFINITE) - { - TimePtr = NULL; - } - else - { - Time.QuadPart = -10000 * (LONGLONG)dwMilliseconds; - TimePtr = &Time; - } - - do - { - Status = NtWaitForSingleObject(hHandle, - (BOOLEAN) bAlertable, - TimePtr); - - if (HIWORD(Status)) - { - SetLastErrorByStatus (Status); - return WAIT_FAILED; - } - - } while (Status == STATUS_ALERTED && bAlertable); - - return Status; -} - - -/* - * @implemented - */ -DWORD STDCALL -WaitForMultipleObjects(DWORD nCount, - CONST HANDLE *lpHandles, - BOOL bWaitAll, - DWORD dwMilliseconds) -{ - return WaitForMultipleObjectsEx(nCount, - lpHandles, - bWaitAll, - dwMilliseconds, - FALSE); -} - - -/* - * @implemented - */ -DWORD STDCALL -WaitForMultipleObjectsEx(DWORD nCount, - CONST HANDLE *lpHandles, - BOOL bWaitAll, - DWORD dwMilliseconds, - BOOL bAlertable) -{ - PLARGE_INTEGER TimePtr; - LARGE_INTEGER Time; - PHANDLE HandleBuffer; - HANDLE Handle[3]; - DWORD i; - NTSTATUS Status; - - DPRINT("nCount %lu\n", nCount); - - if (nCount > 3) - { - HandleBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, nCount * sizeof(HANDLE)); - if (HandleBuffer == NULL) - { - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - return WAIT_FAILED; - } - } - else - { - HandleBuffer = Handle; - } - for (i = 0; i < nCount; i++) - { - switch ((DWORD)lpHandles[i]) - { - case STD_INPUT_HANDLE: - HandleBuffer[i] = NtCurrentPeb()->ProcessParameters->StandardInput; - break; - - case STD_OUTPUT_HANDLE: - HandleBuffer[i] = NtCurrentPeb()->ProcessParameters->StandardOutput; - break; - - case STD_ERROR_HANDLE: - HandleBuffer[i] = NtCurrentPeb()->ProcessParameters->StandardError; - break; - - default: - HandleBuffer[i] = lpHandles[i]; - break; - } - - /* Check for console handle */ - if (IsConsoleHandle(HandleBuffer[i])) - { - if (!VerifyConsoleIoHandle(HandleBuffer[i])) - { - if (HandleBuffer != Handle) - { - RtlFreeHeap(GetProcessHeap(),0,HandleBuffer); - } - SetLastError (ERROR_INVALID_HANDLE); - return WAIT_FAILED; - } - HandleBuffer[i] = (HANDLE)GetConsoleInputWaitHandle(); - if (HandleBuffer[i] == NULL || HandleBuffer[i] == INVALID_HANDLE_VALUE) - { - if (HandleBuffer != Handle) - { - RtlFreeHeap(GetProcessHeap(),0,HandleBuffer); - } - SetLastError (ERROR_INVALID_HANDLE); - return WAIT_FAILED; - } - } - } - - if (dwMilliseconds == INFINITE) - { - TimePtr = NULL; - } - else - { - Time.QuadPart = -10000 * (LONGLONG)dwMilliseconds; - TimePtr = &Time; - } - - do - { - Status = NtWaitForMultipleObjects (nCount, - HandleBuffer, - bWaitAll ? WaitAll : WaitAny, - (BOOLEAN)bAlertable, - TimePtr); - if (HIWORD(Status)) - { - SetLastErrorByStatus (Status); - Status = WAIT_FAILED; - } - - } while (Status == STATUS_ALERTED && bAlertable); - - if (HandleBuffer != Handle) - { - RtlFreeHeap(RtlGetProcessHeap(), 0, HandleBuffer); - } - - return Status; -} - - -/* - * @implemented - */ -DWORD STDCALL -SignalObjectAndWait(HANDLE hObjectToSignal, - HANDLE hObjectToWaitOn, - DWORD dwMilliseconds, - BOOL bAlertable) -{ - PLARGE_INTEGER TimePtr; - LARGE_INTEGER Time; - NTSTATUS Status; - - /* Get real handle */ - switch ((ULONG)hObjectToWaitOn) - { - case STD_INPUT_HANDLE: - hObjectToWaitOn = NtCurrentPeb()->ProcessParameters->StandardInput; - break; - - case STD_OUTPUT_HANDLE: - hObjectToWaitOn = NtCurrentPeb()->ProcessParameters->StandardOutput; - break; - - case STD_ERROR_HANDLE: - hObjectToWaitOn = NtCurrentPeb()->ProcessParameters->StandardError; - break; - } - - /* Check for console handle */ - if (IsConsoleHandle(hObjectToWaitOn)) - { - if (VerifyConsoleIoHandle(hObjectToWaitOn)) - { - DPRINT1("Console handles are not supported yet!\n"); - SetLastError(ERROR_INVALID_HANDLE); - return WAIT_FAILED; - } - } - - if (dwMilliseconds == INFINITE) - { - TimePtr = NULL; - } - else - { - Time.QuadPart = -10000 * (LONGLONG)dwMilliseconds; - TimePtr = &Time; - } - - do - { - Status = NtSignalAndWaitForSingleObject (hObjectToSignal, - hObjectToWaitOn, - (BOOLEAN)bAlertable, - TimePtr); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus (Status); - return WAIT_FAILED; - } - - } while (Status == STATUS_ALERTED && bAlertable); - - /* STATUS_SUCCESS maps to WAIT_OBJECT_0 */ - return Status; + /* Get real handle */ + switch ((ULONG)hHandle) + { + /* Input handle */ + case STD_INPUT_HANDLE: + + /* Read it from the PEB */ + hHandle = NtCurrentPeb()->ProcessParameters->StandardInput; + break; + + /* Output handle */ + case STD_OUTPUT_HANDLE: + + /* Read it from the PEB */ + hHandle = NtCurrentPeb()->ProcessParameters->StandardOutput; + break; + + /* Error handle */ + case STD_ERROR_HANDLE: + + /* Read it from the PEB */ + hHandle = NtCurrentPeb()->ProcessParameters->StandardError; + break; + } + + /* Check for console handle */ + if ((IsConsoleHandle(hHandle)) && (!VerifyConsoleIoHandle(hHandle))) + { + /* Get the real wait handle */ + hHandle = GetConsoleInputWaitHandle(); + } + + /* Check if this is an infinite wait */ + if (dwMilliseconds == INFINITE) + { + /* Under NT, this means no timer argument */ + TimePtr = NULL; + } + else + { + /* Otherwise, convert the time to NT Format */ + Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds); + TimePtr = &Time; + } + + /* Start wait loop */ + do + { + /* Do the wait */ + Status = NtWaitForSingleObject(hHandle, (BOOLEAN)bAlertable, TimePtr); + if (!NT_SUCCESS(Status)) + { + /* The wait failed */ + SetLastErrorByStatus (Status); + return WAIT_FAILED; + } + } while ((Status == STATUS_ALERTED) && (bAlertable)); + + /* Return wait status */ + return Status; +} + +/* + * @implemented + */ +DWORD +WINAPI +WaitForMultipleObjects(IN DWORD nCount, + IN CONST HANDLE *lpHandles, + IN BOOL bWaitAll, + IN DWORD dwMilliseconds) +{ + /* Call the extended API */ + return WaitForMultipleObjectsEx(nCount, + lpHandles, + bWaitAll, + dwMilliseconds, + FALSE); +} + +/* + * @implemented + */ +DWORD +WINAPI +WaitForMultipleObjectsEx(IN DWORD nCount, + IN CONST HANDLE *lpHandles, + IN BOOL bWaitAll, + IN DWORD dwMilliseconds, + IN BOOL bAlertable) +{ + PLARGE_INTEGER TimePtr; + LARGE_INTEGER Time; + PHANDLE HandleBuffer; + HANDLE Handle[8]; + DWORD i; + NTSTATUS Status; + + /* Check if we have more handles then we locally optimize */ + if (nCount > 8) + { + /* Allocate a buffer for them */ + HandleBuffer = RtlAllocateHeap(RtlGetProcessHeap(), + 0, + nCount * sizeof(HANDLE)); + if (!HandleBuffer) + { + /* No buffer, fail the wait */ + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return WAIT_FAILED; + } + } + else + { + /* Otherwise, use our local buffer */ + HandleBuffer = Handle; + } + + /* Copy the handles into our buffer and loop them all */ + RtlCopyMemory(HandleBuffer, (LPVOID)lpHandles, nCount * sizeof(HANDLE)); + for (i = 0; i < nCount; i++) + { + /* Check what kind of handle this is */ + switch ((ULONG)HandleBuffer[i]) + { + /* Input handle */ + case STD_INPUT_HANDLE: + HandleBuffer[i] = NtCurrentPeb()-> + ProcessParameters->StandardInput; + break; + + /* Output handle */ + case STD_OUTPUT_HANDLE: + HandleBuffer[i] = NtCurrentPeb()-> + ProcessParameters->StandardOutput; + break; + + /* Error handle */ + case STD_ERROR_HANDLE: + HandleBuffer[i] = NtCurrentPeb()-> + ProcessParameters->StandardError; + break; + } + + /* Check for console handle */ + if ((IsConsoleHandle(HandleBuffer[i])) && + (!VerifyConsoleIoHandle(HandleBuffer[i]))) + { + /* Get the real wait handle */ + HandleBuffer[i] = GetConsoleInputWaitHandle(); + } + } + + /* Check if this is an infinite wait */ + if (dwMilliseconds == INFINITE) + { + /* Under NT, this means no timer argument */ + TimePtr = NULL; + } + else + { + /* Otherwise, convert the time to NT Format */ + Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds); + TimePtr = &Time; + } + + /* Start wait loop */ + do + { + /* Do the wait */ + Status = NtWaitForMultipleObjects(nCount, + HandleBuffer, + bWaitAll ? WaitAll : WaitAny, + (BOOLEAN)bAlertable, + TimePtr); + if (!NT_SUCCESS(Status)) + { + /* Wait failed */ + SetLastErrorByStatus (Status); + return WAIT_FAILED; + } + } while ((Status == STATUS_ALERTED) && (bAlertable)); + + /* Check if we didn't use our local buffer */ + if (HandleBuffer != Handle) + { + /* Free the allocated one */ + RtlFreeHeap(RtlGetProcessHeap(), 0, HandleBuffer); + } + + /* Return wait status */ + return Status; +} + +/* + * @implemented + */ +DWORD +WINAPI +SignalObjectAndWait(IN HANDLE hObjectToSignal, + IN HANDLE hObjectToWaitOn, + IN DWORD dwMilliseconds, + IN BOOL bAlertable) +{ + PLARGE_INTEGER TimePtr; + LARGE_INTEGER Time; + NTSTATUS Status; + + /* Get real handle */ + switch ((ULONG)hObjectToWaitOn) + { + /* Input handle */ + case STD_INPUT_HANDLE: + + /* Read it from the PEB */ + hObjectToWaitOn = NtCurrentPeb()-> + ProcessParameters->StandardInput; + break; + + /* Output handle */ + case STD_OUTPUT_HANDLE: + + /* Read it from the PEB */ + hObjectToWaitOn = NtCurrentPeb()-> + ProcessParameters->StandardOutput; + break; + + /* Error handle */ + case STD_ERROR_HANDLE: + + /* Read it from the PEB */ + hObjectToWaitOn = NtCurrentPeb()-> + ProcessParameters->StandardError; + break; + } + + /* Check for console handle */ + if ((IsConsoleHandle(hObjectToWaitOn)) && + (!VerifyConsoleIoHandle(hObjectToWaitOn))) + { + /* Get the real wait handle */ + hObjectToWaitOn = GetConsoleInputWaitHandle(); + } + + /* Check if this is an infinite wait */ + if (dwMilliseconds == INFINITE) + { + /* Under NT, this means no timer argument */ + TimePtr = NULL; + } + else + { + /* Otherwise, convert the time to NT Format */ + Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds); + TimePtr = &Time; + } + + /* Start wait loop */ + do + { + /* Do the wait */ + Status = NtSignalAndWaitForSingleObject(hObjectToSignal, + hObjectToWaitOn, + (BOOLEAN)bAlertable, + TimePtr); + if (!NT_SUCCESS(Status)) + { + /* The wait failed */ + SetLastErrorByStatus (Status); + return WAIT_FAILED; + } + } while ((Status == STATUS_ALERTED) && (bAlertable)); + + /* Return wait status */ + return Status; }
/* EOF */