/* COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS system libraries
 * PURPOSE:         Condition Variable functions
 * PROGRAMMER:      Thomas Weidenmueller <w3seek@reactos.com>
 */

/* File contains Vista Semantics */
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600

#include <k32.h>

#define NDEBUG
#include "../include/debug.h"

/* FIXME: Move these RTL declarations to the NDK */
NTSTATUS
NTAPI
RtlSleepConditionVariableCS(IN OUT PRTL_CONDITION_VARIABLE ConditionVariable,
                            IN OUT PRTL_CRITICAL_SECTION CriticalSection,
                            IN PLARGE_INTEGER TimeOut  OPTIONAL);

NTSTATUS
NTAPI
RtlSleepConditionVariableSRW(IN OUT PRTL_CONDITION_VARIABLE ConditionVariable,
                             IN OUT PRTL_SRWLOCK SRWLock,
                             IN PLARGE_INTEGER TimeOut  OPTIONAL,
                             IN ULONG Flags);

/* PUBLIC FUNCTIONS ***********************************************************/

/*
 * @implemented
 */
BOOL
WINAPI
SleepConditionVariableCS(IN OUT PCONDITION_VARIABLE ConditionVariable,
                         IN OUT PCRITICAL_SECTION CriticalSection,
                         IN DWORD dwMilliseconds)
{
    NTSTATUS Status;
    LARGE_INTEGER TimeOut;
    PLARGE_INTEGER TimeOutPtr = NULL;

    if (dwMilliseconds != INFINITE)
    {
        TimeOut.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
        TimeOutPtr = &TimeOut;
    }

    Status = RtlSleepConditionVariableCS((PRTL_CONDITION_VARIABLE)ConditionVariable,
                                         (PRTL_CRITICAL_SECTION)CriticalSection,
                                         TimeOutPtr);

    if (!NT_SUCCESS(Status))
    {
        SetLastErrorByStatus(Status);
        return FALSE;
    }

    return TRUE;
}


/*
 * @implemented
 */
BOOL
WINAPI
SleepConditionVariableSRW(IN OUT PCONDITION_VARIABLE ConditionVariable,
                          IN OUT PSRWLOCK SRWLock,
                          IN DWORD dwMilliseconds,
                          IN ULONG Flags)
{
    NTSTATUS Status;
    LARGE_INTEGER TimeOut;
    PLARGE_INTEGER TimeOutPtr = NULL;

    if (dwMilliseconds != INFINITE)
    {
        TimeOut.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
        TimeOutPtr = &TimeOut;
    }

    Status = RtlSleepConditionVariableSRW((PRTL_CONDITION_VARIABLE)ConditionVariable,
                                          (PRTL_SRWLOCK)SRWLock,
                                          TimeOutPtr,
                                          Flags);

    if (!NT_SUCCESS(Status))
    {
        SetLastErrorByStatus(Status);
        return FALSE;
    }

    return TRUE;
}
