Fix nanosleep returning negative rem

Corinna Vinschen corinna-cygwin@cygwin.com
Wed Jul 21 09:33:05 GMT 2021


On Jul 21 11:30, Corinna Vinschen wrote:
> I wrote a quick STC using the NT API calls and I can't reproduce the
> problem with this code either.  The output is either
> 
>   SignalState: 1 TimeRemaining: -5354077459183
> 
> or
> 
>   SignalState: 0 TimeRemaining: 653
> 
> I never get a small negative value in the latter case.  Can you
> reproduce your problem with this testcase or tweak it to reproduce it?

Now I actually attached the code :}


Corinna
-------------- next part --------------
#include <stdio.h>
#include <w32api/windows.h>
#include <w32api/winternl.h>
#include <w32api/ntdef.h>

typedef enum _TIMER_INFORMATION_CLASS {
  TimerBasicInformation = 0
} TIMER_INFORMATION_CLASS, *PTIMER_INFORMATION_CLASS;

typedef struct _TIMER_BASIC_INFORMATION {
  LARGE_INTEGER TimeRemaining;
  BOOLEAN SignalState;
} TIMER_BASIC_INFORMATION, *PTIMER_BASIC_INFORMATION;

NTSTATUS NTAPI NtCreateTimer(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES,
			     TIMER_TYPE);

NTSTATUS NTAPI NtCancelTimer(HANDLE, PBOOLEAN);

NTSTATUS NTAPI NtSetTimer(HANDLE, PLARGE_INTEGER, PVOID, PVOID,
			  BOOLEAN, LONG, PBOOLEAN);

NTSTATUS NTAPI NtQueryTimer (HANDLE, TIMER_INFORMATION_CLASS, PVOID,
			     ULONG, PULONG);

int
main ()
{
  HANDLE event;
  HANDLE timer;
  NTSTATUS status;
  LARGE_INTEGER timeout;
  TIMER_BASIC_INFORMATION tbi;

  event = CreateEvent (NULL, TRUE, FALSE, NULL);
  status = NtCreateTimer (&timer, TIMER_ALL_ACCESS, NULL, NotificationTimer);
  if (!NT_SUCCESS (status))
    {
      fprintf (stderr, "NtCreateTimer: 0x%08lx\n", status);
      return 1;
    }
  timeout.QuadPart = -10000000LL;	/* 1 sec */
  status = NtSetTimer (timer, &timeout, NULL, NULL, FALSE, 0, NULL);
  if (!NT_SUCCESS (status))
    {
      fprintf (stderr, "NtSetTimer: 0x%08lx\n", status);
      return 1;
    }
  WaitForSingleObject (event, 985L);
  NtQueryTimer (timer, TimerBasicInformation, &tbi, sizeof tbi, NULL);
  printf ("SignalState: %d TimeRemaining: %lld\n", tbi.SignalState,
						   tbi.TimeRemaining.QuadPart);
  NtCancelTimer (timer, NULL);
  NtClose (timer);
  return 0;
}


More information about the Cygwin-patches mailing list