From d61f1318398d57d22ae8fb39e770959ff50846ed Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Mon, 29 Mar 2021 14:51:09 -0400 Subject: [PATCH] Fix waits on iOS I don't understand why CLOCK_MONOTONIC doesn't work even in newer simulators here, but this will at least fix the problem for now. --- .../Native/Unix/System.Native/pal_threading.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libraries/Native/Unix/System.Native/pal_threading.c b/src/libraries/Native/Unix/System.Native/pal_threading.c index 937edd1ce16e0..cf739c8454274 100644 --- a/src/libraries/Native/Unix/System.Native/pal_threading.c +++ b/src/libraries/Native/Unix/System.Native/pal_threading.c @@ -11,6 +11,7 @@ #include #include #include +#include #if defined(TARGET_OSX) // So we can use the declaration of pthread_cond_timedwait_relative_np @@ -169,13 +170,25 @@ int32_t SystemNative_LowLevelMonitor_TimedWait(LowLevelMonitor *monitor, int32_t #if HAVE_CLOCK_GETTIME_NSEC_NP timeoutTimeSpec.tv_sec = timeoutMilliseconds / 1000; timeoutTimeSpec.tv_nsec = (timeoutMilliseconds % 1000) * 1000 * 1000; + error = pthread_cond_timedwait_relative_np(&monitor->Condition, &monitor->Mutex, &timeoutTimeSpec); #else +#if HAVE_CLOCK_MONOTONIC error = clock_gettime(CLOCK_MONOTONIC, &timeoutTimeSpec); assert(error == 0); +#else + struct timeval tv; + + error = gettimeofday(&tv, NULL); + assert(error == 0); + + timeoutTimeSpec.tv_sec = tv.tv_sec; + timeoutTimeSpec.tv_nsec = tv.tv_usec * 1000; +#endif uint64_t nanoseconds = (uint64_t)timeoutMilliseconds * 1000 * 1000 + (uint64_t)timeoutTimeSpec.tv_nsec; timeoutTimeSpec.tv_sec += nanoseconds / (1000 * 1000 * 1000); timeoutTimeSpec.tv_nsec = nanoseconds % (1000 * 1000 * 1000); + error = pthread_cond_timedwait(&monitor->Condition, &monitor->Mutex, &timeoutTimeSpec); #endif assert(error == 0 || error == ETIMEDOUT);