This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch issues/FELIX-6163
in repository https://gitbox.apache.org/repos/asf/felix-dev.git

commit 2e1868296881641cacbe7bed47fda5111f61445f
Author: Karl Pauls <[email protected]>
AuthorDate: Tue Dec 15 23:49:47 2020 +0100

    FELIX-6163: do not use System.currentTimeMillis() to check timeouts
---
 .../src/main/java/org/apache/felix/framework/util/ThreadGate.java   | 6 ++++--
 framework/src/main/java/org/osgi/util/tracker/ServiceTracker.java   | 5 +++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git 
a/framework/src/main/java/org/apache/felix/framework/util/ThreadGate.java 
b/framework/src/main/java/org/apache/felix/framework/util/ThreadGate.java
index f9900a3..ff52080 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/ThreadGate.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/ThreadGate.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.framework.util;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * This class implements a simple one-shot gate for threads. The gate
  * starts closed and will block any threads that try to wait on it. Once
@@ -72,14 +74,14 @@ public class ThreadGate
     **/
     public synchronized boolean await(long timeout) throws InterruptedException
     {
-        long start = System.currentTimeMillis();
+        long start = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
         long remaining = timeout;
         while (!m_open)
         {
             wait(remaining);
             if (timeout > 0)
             {
-                remaining = timeout - (System.currentTimeMillis() - start);
+                remaining = timeout - 
(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - start);
                 if (remaining <= 0)
                 {
                     break;
diff --git a/framework/src/main/java/org/osgi/util/tracker/ServiceTracker.java 
b/framework/src/main/java/org/osgi/util/tracker/ServiceTracker.java
index 42c176c..45b1f32 100644
--- a/framework/src/main/java/org/osgi/util/tracker/ServiceTracker.java
+++ b/framework/src/main/java/org/osgi/util/tracker/ServiceTracker.java
@@ -20,6 +20,7 @@ import java.lang.reflect.Array;
 import java.util.Collections;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.concurrent.TimeUnit;
 
 import org.osgi.annotation.versioning.ConsumerType;
 import org.osgi.framework.AllServiceListener;
@@ -493,7 +494,7 @@ public class ServiceTracker<S, T> implements 
ServiceTrackerCustomizer<S, T> {
                        return object;
                }
 
-               final long endTime = (timeout == 0) ? 0 : 
(System.currentTimeMillis() + timeout);
+               final long endTime = (timeout == 0) ? 0 : 
(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) + timeout);
                do {
                        final Tracked t = tracked();
                        if (t == null) { /* if ServiceTracker is not open */
@@ -506,7 +507,7 @@ public class ServiceTracker<S, T> implements 
ServiceTrackerCustomizer<S, T> {
                        }
                        object = getService();
                        if (endTime > 0) { // if we have a timeout
-                               timeout = endTime - System.currentTimeMillis();
+                               timeout = endTime - 
TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
                                if (timeout <= 0) { // that has expired
                                        break;
                                }

Reply via email to