Explorar o código

fix: EventLock - use #nanoTime instead of #currentTimeMillis

Ranides Atterwim %!s(int64=10) %!d(string=hai) anos
pai
achega
b2390a6b3f
Modificáronse 1 ficheiros con 19 adicións e 13 borrados
  1. 19 13
      assira/src/main/java/net/ranides/assira/events/EventLock.java

+ 19 - 13
assira/src/main/java/net/ranides/assira/events/EventLock.java

@@ -52,6 +52,8 @@ import org.slf4j.Logger;
  */
 public abstract class EventLock<E extends Event> {
     
+    private static final long NS_PER_MS = 1_000_000;
+    
     private static final int LOG_BATCH = 100;
     
     /**
@@ -228,6 +230,10 @@ public abstract class EventLock<E extends Event> {
         router.addEventListener(event, lock);
         return lock;
     }
+    
+    protected void waitNS(long ns) throws InterruptedException {
+        this.wait(ns/NS_PER_MS, (int)(ns%NS_PER_MS));
+    }
 
     private static class UnsafeEventLock<EE extends Event> extends EventLock<EE> implements EventListener<EE> {
 
@@ -261,12 +267,12 @@ public abstract class EventLock<E extends Event> {
 
         @Override
         public EE waitForEvent(long timeout) throws InterruptedException {
-            long end = System.currentTimeMillis() + timeout;
-            long rem = timeout;
+            long rem = TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+            long end = System.nanoTime()+ rem;
             synchronized (this) {
                 while (hit==null && rem>0) {
-                    this.wait(rem);
-                    rem = end - System.currentTimeMillis();
+                    this.waitNS(rem);
+                    rem = end - System.nanoTime();
                 }
                 EE result = hit;
                 hit = null;
@@ -283,15 +289,15 @@ public abstract class EventLock<E extends Event> {
                 return event.cast(ret);
             }
         }
-
+        
         @Override
         public <T extends EE> T waitForEvent(Class<T> event, long timeout) throws InterruptedException {
-            long end = System.currentTimeMillis() + timeout;
-            long rem = timeout;
+            long rem = TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+            long end = System.nanoTime()+ rem;
             synchronized (this) {
                 while( !event.isInstance(hit) && rem>0) {
-                    this.wait(rem);
-                    rem = end - System.currentTimeMillis();
+                    this.waitNS(rem);
+                    rem = end - System.nanoTime();
                 }
                 EE ret = hit;
                 hit = null;
@@ -351,14 +357,14 @@ public abstract class EventLock<E extends Event> {
 
         @Override
         public <T extends EE> T waitForEvent(Class<T> event, long timeout) throws InterruptedException {
-            long end = System.currentTimeMillis() + timeout;
-            long rem = timeout;
+            long rem = TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+            long end = System.nanoTime()+ rem;
             while(rem > 0) {
-                EE recent = events.poll(rem, TimeUnit.MILLISECONDS);
+                EE recent = events.poll(rem, TimeUnit.NANOSECONDS);
                 if( event.isInstance(recent) ) {
                     return event.cast(recent);
                 }
-                rem = end - System.currentTimeMillis();
+                rem = end - System.nanoTime();
             }
             return null;
         }