summaryrefslogtreecommitdiffstats
path: root/vm/Sync.cpp
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2014-04-11 12:18:50 +0100
committerPaul Duffin <paulduffin@google.com>2014-04-24 15:39:13 +0100
commitdb7d74f72f3686a538dff539b10c10c1d172014c (patch)
treef3e066ea9818a88b855c7c424909509e79f0cc7c /vm/Sync.cpp
parent8bf12deda0c7ee362e0479f88451e61e96f36bc8 (diff)
downloadandroid_dalvik-db7d74f72f3686a538dff539b10c10c1d172014c.tar.gz
android_dalvik-db7d74f72f3686a538dff539b10c10c1d172014c.tar.bz2
android_dalvik-db7d74f72f3686a538dff539b10c10c1d172014c.zip
Remove inconsistent usage of macros that broke Thread.sleep/wait on host
Code for managing Thread.sleep/wait used both !defined(__APPLE__) and HAVE_POSIX_CLOCKS interchangeably to mean the same thing. That worked when one or other was set but failed on host because neither were set and could also fail if they were both set. This changed to use HAVE_POSIX_CLOCKS consistently. The code that used this was spread across 4 separate files with quite a lot of duplication so that has been replaced with a new method dvmInitCondForTimedWait in Sync.cpp. A few extra comments were added to make some things that were a bit confusing clearer, hopefully. Change-Id: I7660c931f0cafcd0563afd1165de2aa615040602 Bug: https://code.google.com/p/android/issues/detail?id=68402
Diffstat (limited to 'vm/Sync.cpp')
-rw-r--r--vm/Sync.cpp17
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;
}