Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix waits on iOS #50388

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>

#if defined(TARGET_OSX)
// So we can use the declaration of pthread_cond_timedwait_relative_np
Expand Down Expand Up @@ -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);
Expand Down