From 92fac79ab83dad6578523d69bec3c15c9ecfc8de Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 18 Sep 2021 13:02:55 +0600 Subject: [PATCH 1/2] Fix pthreads building on Windows --- src/thread.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/thread.c b/src/thread.c index 916132fb2..b5151e573 100644 --- a/src/thread.c +++ b/src/thread.c @@ -2,8 +2,13 @@ #include #include #include +#ifndef _MSC_VER #include #include +#endif +#ifdef _WIN32 +#include +#endif #include #include <86box/86box.h> #include <86box/plat.h> @@ -103,6 +108,17 @@ thread_wait_event(event_t *handle, int timeout) event_pthread_t *event = (event_pthread_t *)handle; struct timespec abstime; +#ifdef _MSC_VER + /* Taken from https://stackoverflow.com/a/31335254 with some modifications. */ + FILETIME systime; + uint64_t systimeint = 0; + GetSystemTimeAsFileTime(&systime); + systimeint |= systime.dwLowDateTime; + systimeint |= (uint64_t)systime.dwHighDateTime << 32i64; + systimeint -= 116444736000000000i64; + abstime.tv_sec = systimeint / 10000000i64; + abstime.tv_nsec = systimeint % 10000000i64 * 100; +#else clock_gettime(CLOCK_REALTIME, &abstime); abstime.tv_nsec += (timeout % 1000) * 1000000; abstime.tv_sec += (timeout / 1000); @@ -110,6 +126,7 @@ thread_wait_event(event_t *handle, int timeout) abstime.tv_nsec -= 1000000000; abstime.tv_sec++; } +#endif pthread_mutex_lock(&event->mutex); if (timeout == -1) { @@ -138,7 +155,11 @@ thread_destroy_event(event_t *handle) void thread_sleep(int t) { +#ifdef _WIN32 + Sleep(t); +#else usleep(t * 1000); +#endif } From 80b85cd384cc05052f7e64547e64c67ac3263ac9 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 18 Sep 2021 13:16:39 +0600 Subject: [PATCH 2/2] Fix linking with pthreads on Windows --- src/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c0be6a5cb..08b441d3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,7 +48,11 @@ if(NOT WIN32 OR PTHREAD) target_sources(86Box PRIVATE thread.c) if(WIN32 AND VCPKG_TOOLCHAIN) find_package(pthreads REQUIRED) - target_link_libraries(86Box pthreads) + if (PThreads4W_FOUND) + target_link_libraries(86Box PThreads4W::PThreads4W) + else() + target_link_libraries(86Box pthreads) + endif() else() set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED)