Skip to content

Commit

Permalink
winapi: Add Event functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 authored and mborgerson committed Jul 5, 2023
1 parent 2827db5 commit 8a4665f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
77 changes: 77 additions & 0 deletions lib/winapi/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,80 @@ BOOL ReleaseMutex (HANDLE hMutex)
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

HANDLE CreateEventA (LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName)
{
NTSTATUS status;
HANDLE handle;
ANSI_STRING obj_name;
OBJECT_ATTRIBUTES obj_attributes;
POBJECT_ATTRIBUTES obj_attributes_ptr;
EVENT_TYPE event;

if (lpName) {
RtlInitAnsiString(&obj_name, lpName);
InitializeObjectAttributes(&obj_attributes, &obj_name, OBJ_OPENIF, ObWin32NamedObjectsDirectory(), NULL);
obj_attributes_ptr = &obj_attributes;
} else {
obj_attributes_ptr = NULL;
}

if (bManualReset) {
event = NotificationEvent;
} else {
event = SynchronizationEvent;
}

status = NtCreateEvent(&handle, obj_attributes_ptr, event, bInitialState);
if (!NT_SUCCESS(status)) {
SetLastError(RtlNtStatusToDosError(status));
return NULL;
}

if (status == STATUS_OBJECT_NAME_EXISTS) {
SetLastError(ERROR_ALREADY_EXISTS);
} else {
SetLastError(0);
}

return handle;
}

BOOL SetEvent (HANDLE hEvent)
{
NTSTATUS status;

status = NtSetEvent(hEvent, NULL);
if (NT_SUCCESS(status)) {
return TRUE;
}

SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

BOOL ResetEvent (HANDLE hEvent)
{
NTSTATUS status;

status = NtClearEvent(hEvent);
if (NT_SUCCESS(status)) {
return TRUE;
}

SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

BOOL PulseEvent (HANDLE hEvent)
{
NTSTATUS status;

status = NtPulseEvent(hEvent, NULL);
if (NT_SUCCESS(status)) {
return TRUE;
}

SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
6 changes: 6 additions & 0 deletions lib/winapi/synchapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ BOOL ReleaseSemaphore (HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousC
HANDLE CreateMutexA (LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName);
BOOL ReleaseMutex (HANDLE hMutex);

HANDLE CreateEventA (LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName);
BOOL SetEvent (HANDLE hEvent);
BOOL ResetEvent (HANDLE hEvent);
BOOL PulseEvent (HANDLE hEvent);

#ifndef UNICODE
#define CreateMutex CreateMutexA
#define CreateEvent CreateEventA
#else
#error nxdk does not support the Unicode API
#endif
Expand Down

0 comments on commit 8a4665f

Please sign in to comment.