summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--vm/Sync.cpp17
-rw-r--r--vm/Sync.h8
-rw-r--r--vm/Thread.cpp9
-rw-r--r--vm/alloc/HeapSource.cpp9
-rw-r--r--vm/compiler/Compiler.cpp12
5 files changed, 29 insertions, 26 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;
}
diff --git a/vm/Sync.h b/vm/Sync.h
index 2016c0381..7ac6e8bbc 100644
--- a/vm/Sync.h
+++ b/vm/Sync.h
@@ -142,6 +142,14 @@ Thread* dvmGetObjectLockHolder(Object* obj);
bool dvmHoldsLock(Thread* thread, Object* obj);
/*
+ * Initialize a pthread_cond_t for use with a timed wait.
+ *
+ * This must be used for initializing conditional variables that are used by dvmRelativeCondWait
+ * to ensure that consistent timers are used.
+ */
+void dvmInitCondForTimedWait(pthread_cond_t* cond);
+
+/*
* Relative timed wait on condition
*/
int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
diff --git a/vm/Thread.cpp b/vm/Thread.cpp
index f5e302084..ab4c58e7e 100644
--- a/vm/Thread.cpp
+++ b/vm/Thread.cpp
@@ -936,14 +936,7 @@ static bool prepareThread(Thread* thread)
memset(&thread->jniMonitorRefTable, 0, sizeof(thread->jniMonitorRefTable));
-#if defined(__APPLE__)
- pthread_cond_init(&thread->waitCond, NULL);
-#else
- pthread_condattr_t condAttr;
- pthread_condattr_init(&condAttr);
- pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC);
- pthread_cond_init(&thread->waitCond, &condAttr);
-#endif
+ dvmInitCondForTimedWait(&thread->waitCond);
dvmInitMutex(&thread->waitMutex);
/* Initialize safepoint callback mechanism */
diff --git a/vm/alloc/HeapSource.cpp b/vm/alloc/HeapSource.cpp
index 316288491..176a71834 100644
--- a/vm/alloc/HeapSource.cpp
+++ b/vm/alloc/HeapSource.cpp
@@ -532,14 +532,7 @@ static void *gcDaemonThread(void* arg)
static bool gcDaemonStartup()
{
dvmInitMutex(&gHs->gcThreadMutex);
-#if defined(__APPLE__)
- pthread_cond_init(&gHs->gcThreadCond, NULL);
-#else
- pthread_condattr_t condAttr;
- pthread_condattr_init(&condAttr);
- pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC);
- pthread_cond_init(&gHs->gcThreadCond, &condAttr);
-#endif // defined(__APPLE__)
+ dvmInitCondForTimedWait(&gHs->gcThreadCond);
gHs->gcThreadShutdown = false;
gHs->hasGcThread = dvmCreateInternalThread(&gHs->gcThread, "GC",
gcDaemonThread, NULL);
diff --git a/vm/compiler/Compiler.cpp b/vm/compiler/Compiler.cpp
index 9f0fcd876..781f0141e 100644
--- a/vm/compiler/Compiler.cpp
+++ b/vm/compiler/Compiler.cpp
@@ -753,16 +753,8 @@ bool dvmCompilerStartup(void)
dvmInitMutex(&gDvmJit.compilerICPatchLock);
dvmInitMutex(&gDvmJit.codeCacheProtectionLock);
dvmLockMutex(&gDvmJit.compilerLock);
-#if defined(__APPLE__)
- pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
- pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
-#else
- pthread_condattr_t condAttr;
- pthread_condattr_init(&condAttr);
- pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC);
- pthread_cond_init(&gDvmJit.compilerQueueActivity, &condAttr);
- pthread_cond_init(&gDvmJit.compilerQueueEmpty, &condAttr);
-#endif
+ dvmInitCondForTimedWait(&gDvmJit.compilerQueueActivity);
+ dvmInitCondForTimedWait(&gDvmJit.compilerQueueEmpty);
/* Reset the work queue */
gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;