From 9fb3646511998310fc8c967c239803f38b2bdc5b Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 25 May 2025 18:35:12 -0400 Subject: Windows XP support Signed-off-by: Andrew Opalach --- src/util/file/file_stdio.c | 2 +- src/util/thread/thread.h | 4 +++ src/util/thread/thread_windows.c | 59 ++++++++++++++++++++++++++++++++++++++++ src/util/timer/timer_windows.c | 17 +++++++++--- src/winwrap.h | 1 + 5 files changed, 78 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c index ae07908..97dd0f0 100644 --- a/src/util/file/file_stdio.c +++ b/src/util/file/file_stdio.c @@ -17,7 +17,7 @@ AL_ASSERT_TYPE_SIZE(off_t, 8); static inline bool open_stdio_file(FILE **file, str *path, const char *mode) { char *c_str = al_str_to_c_str(path); -#ifdef NAUNET_ON_WINDOWS +#if defined NAUNET_ON_WINDOWS && !defined NAUNET_WIN32_COMPAT_MODE // Still sets errno, according to the Microsoft docs. errno_t ret = fopen_s(file, c_str, mode); #else diff --git a/src/util/thread/thread.h b/src/util/thread/thread.h index 6ea5722..142aa3c 100644 --- a/src/util/thread/thread.h +++ b/src/util/thread/thread.h @@ -47,7 +47,11 @@ struct nn_mutex { struct nn_cond { #ifdef NAUNET_ON_WINDOWS +#ifdef NAUNET_WIN32_COMPAT_MODE + HANDLE event; +#else CONDITION_VARIABLE cond; +#endif #else pthread_cond_t cond; #endif diff --git a/src/util/thread/thread_windows.c b/src/util/thread/thread_windows.c index 9ce85e2..8722d0a 100644 --- a/src/util/thread/thread_windows.c +++ b/src/util/thread/thread_windows.c @@ -1,3 +1,5 @@ +#include + #include "thread.h" NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval); @@ -42,6 +44,28 @@ void nn_thread_detach(struct nn_thread *thread) } */ +/* +void nn_mutex_init(struct nn_mutex *mutex) +{ + mutex->mutex = CreateMutex(NULL, FALSE, NULL); +} + +void nn_mutex_lock(struct nn_mutex *mutex) +{ + WaitForSingleObject(mutex->mutex, INFINITE); +} + +void nn_mutex_unlock(struct nn_mutex *mutex) +{ + ReleaseMutex(mutex->mutex); +} + +void nn_mutex_destroy(struct nn_mutex *mutex) +{ + CloseHandle(mutex->mutex); +} +*/ + void nn_mutex_init(struct nn_mutex *mutex) { InitializeCriticalSection(&mutex->mutex); @@ -62,6 +86,40 @@ void nn_mutex_destroy(struct nn_mutex *mutex) DeleteCriticalSection(&mutex->mutex); } +#ifdef NAUNET_WIN32_COMPAT_MODE +void nn_cond_init(struct nn_cond *cond) +{ + cond->condition = true; + cond->event = CreateEvent(NULL, TRUE, FALSE, NULL); +} + +void nn_cond_wait(struct nn_cond *cond, struct nn_mutex *mutex) +{ + al_assert(cond->condition); + cond->condition = false; + nn_mutex_unlock(mutex); + WaitForSingleObject(cond->event, INFINITE); + ResetEvent(cond->event); + nn_mutex_lock(mutex); +} + +bool nn_cond_is_waiting(struct nn_cond *cond) +{ + return !cond->condition; +} + +void nn_cond_signal(struct nn_cond *cond) +{ + al_assert(!cond->condition); + cond->condition = true; + SetEvent(cond->event); +} + +void nn_cond_destroy(struct nn_cond *cond) +{ + CloseHandle(cond->event); +} +#else void nn_cond_init(struct nn_cond *cond) { cond->condition = true; @@ -91,3 +149,4 @@ void nn_cond_destroy(struct nn_cond *cond) { (void)cond; } +#endif diff --git a/src/util/timer/timer_windows.c b/src/util/timer/timer_windows.c index f26e56a..4ff95ff 100644 --- a/src/util/timer/timer_windows.c +++ b/src/util/timer/timer_windows.c @@ -14,8 +14,17 @@ static const s64 UNIX_TIME_START = 0x019DB1DED53E8000; // January 1st, 1970 in " u64 nn_get_timestamp(void) { - FILETIME ft; - GetSystemTimePreciseAsFileTime(&ft); - return (((LARGE_INTEGER){ .LowPart = ft.dwLowDateTime, .HighPart = ft.dwHighDateTime }).QuadPart - - UNIX_TIME_START) / 10Lu; + FILETIME ft; +#ifdef NAUNET_WIN32_COMPAT_MODE + GetSystemTimeAsFileTime(&ft); +#else + // https://github.com/llvm/llvm-project/commit/3ec634e65a02d5f443cc982e9e00ec4e51143d07 +#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN10) + GetSystemTimePreciseAsFileTime(&ft); +#else +#error "GetSystemTimePreciseAsFileTime() requested but not supported." +#endif +#endif + return (((LARGE_INTEGER){ .LowPart = ft.dwLowDateTime, .HighPart = ft.dwHighDateTime }).QuadPart - UNIX_TIME_START) / 10u; } diff --git a/src/winwrap.h b/src/winwrap.h index 0dfa8c6..c97abda 100644 --- a/src/winwrap.h +++ b/src/winwrap.h @@ -2,6 +2,7 @@ #include +#define _WINSOCK_DEPRECATED_NO_WARNINGS // https://kirkshoop.github.io/2011/09/20/ntstatus.html #include #include -- cgit v1.2.3-101-g0448