diff options
Diffstat (limited to 'vm/Sync.cpp')
-rw-r--r-- | vm/Sync.cpp | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/vm/Sync.cpp b/vm/Sync.cpp index d2ef49fb5..c9a32d45f 100644 --- a/vm/Sync.cpp +++ b/vm/Sync.cpp @@ -550,9 +550,11 @@ static void absoluteTime(s8 msec, s4 nsec, struct timespec *ts) { s8 endSec; + // Clock used here must be consistent with clock specified in dvmInitCondForTimedWait #ifdef HAVE_POSIX_CLOCKS clock_gettime(CLOCK_MONOTONIC, ts); #else + // Platform (probably MacOS) doesn't support CLOCK_MONOTONIC { struct timeval tv; gettimeofday(&tv, NULL); @@ -575,6 +577,20 @@ static void absoluteTime(s8 msec, s4 nsec, struct timespec *ts) } } +void dvmInitCondForTimedWait(pthread_cond_t* cond) +{ + // Must be consistent with clock specified in absoluteTime +#ifdef HAVE_POSIX_CLOCKS + pthread_condattr_t condAttr; + pthread_condattr_init(&condAttr); + pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC); + pthread_cond_init(cond, &condAttr); +#else + // Platform (probably MacOS) doesn't support CLOCK_MONOTONIC or pthread_condattr_setclock + pthread_cond_init(cond, NULL); +#endif +} + int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, s8 msec, s4 nsec) { @@ -640,6 +656,7 @@ static void waitMonitor(Thread* self, Monitor* mon, s8 msec, s4 nsec, if (msec == 0 && nsec == 0) { timed = false; } else { + // Calculate absolute time before doing any other work so it is as accurate as possible. absoluteTime(msec, nsec, &ts); timed = true; } |