Home > Mobile >  Can I test whether an auto-reset event is signaled with this strategy?
Can I test whether an auto-reset event is signaled with this strategy?

Time:01-16

I'd like to emit debug output that contains the state of an auto-reset event. This is to aid concurrency debugging.

It's possible to test the current state of an auto-reset event by waiting on it with a timeout of zero. Unfortunately, this resets the event. So I thought, I'd first test it and the just set it again if it was set already.

Would this strategy work in the sense that it can't possibly alter the behavior of the rest of the application?

CodePudding user response:

Would this strategy work in the sense that it can't possibly alter the behavior of the rest of the application?

of course this can affect behavior of the rest of the application. because you not only query state, but change state of event with this.

however special api exist (undocumented) for query information of event - it type and state (of course state after api return can change at any type)

typedef struct EVENT_BASIC_INFORMATION
{
    EVENT_TYPE EventType;
    LONG EventState;
} *PEVENT_BASIC_INFORMATION;

enum EVENT_INFORMATION_CLASS
{
    EventBasicInformation
};

EXTERN_C
NTSYSAPI
NTSTATUS
NTAPI
NtQueryEvent (
               _In_ HANDLE EventHandle,
               _In_ EVENT_INFORMATION_CLASS EventInformationClass,
               _Out_writes_bytes_(Length) PVOID EventInformation,
               _In_ ULONG Length,
               _Out_opt_ PULONG ReturnLength
               );

example of usage

if (HANDLE hEvent = CreateEventW(0, 0, 0, 0))
{
    EVENT_BASIC_INFORMATION ebi;
    NtQueryEvent(hEvent, EventBasicInformation, &ebi, sizeof(ebi), 0);
    SetEvent(hEvent);
    NtQueryEvent(hEvent, EventBasicInformation, &ebi, sizeof(ebi), 0);
    NtClose(hEvent);
}

note, that auto-reset event - this is SynchronizationEvent and manual-reset event - this is NotificationEvent

  •  Tags:  
  • Related