Fix pthreads on non-MinGW Windows builds
- Use C11 standard `timespec_get` instead of `clock_gettime` - Remove the unused `thread_sleep` function
This commit is contained in:
@@ -104,4 +104,11 @@ if(MSVC_TOOLSET_VERSION GREATER_EQUAL 142 AND ARCH STREQUAL "arm64")
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# HACK: MinGW does not have `timespec_get`
|
||||||
|
include(CheckSymbolExists)
|
||||||
|
check_symbol_exists(timespec_get time.h HAS_TIMESPEC_GET)
|
||||||
|
if(HAS_TIMESPEC_GET)
|
||||||
|
add_compile_definitions(HAS_TIMESPEC_GET)
|
||||||
|
endif()
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
|||||||
36
src/thread.c
36
src/thread.c
@@ -2,17 +2,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#ifndef _MSC_VER
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <86box/86box.h>
|
#include <86box/86box.h>
|
||||||
#include <86box/plat.h>
|
#include <86box/plat.h>
|
||||||
|
|
||||||
|
#ifndef HAS_TIMESPEC_GET
|
||||||
|
# define timespec_get(ts, _) clock_gettime(CLOCK_REALTIME, ts)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
typedef struct event_pthread_t
|
typedef struct event_pthread_t
|
||||||
{
|
{
|
||||||
@@ -108,25 +105,13 @@ thread_wait_event(event_t *handle, int timeout)
|
|||||||
event_pthread_t *event = (event_pthread_t *)handle;
|
event_pthread_t *event = (event_pthread_t *)handle;
|
||||||
struct timespec abstime;
|
struct timespec abstime;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
timespec_get(&abstime, TIME_UTC);
|
||||||
/* 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_nsec += (timeout % 1000) * 1000000;
|
||||||
abstime.tv_sec += (timeout / 1000);
|
abstime.tv_sec += (timeout / 1000);
|
||||||
if (abstime.tv_nsec > 1000000000) {
|
if (abstime.tv_nsec > 1000000000) {
|
||||||
abstime.tv_nsec -= 1000000000;
|
abstime.tv_nsec -= 1000000000;
|
||||||
abstime.tv_sec++;
|
abstime.tv_sec++;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
pthread_mutex_lock(&event->mutex);
|
pthread_mutex_lock(&event->mutex);
|
||||||
if (timeout == -1) {
|
if (timeout == -1) {
|
||||||
@@ -152,17 +137,6 @@ thread_destroy_event(event_t *handle)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
thread_sleep(int t)
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
Sleep(t);
|
|
||||||
#else
|
|
||||||
usleep(t * 1000);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mutex_t *
|
mutex_t *
|
||||||
thread_create_mutex(void)
|
thread_create_mutex(void)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user