RLPark 1.0.0
Reinforcement Learning Framework in Java

Clock.java

Go to the documentation of this file.
00001 package zephyr.plugin.core.api.synchronization;
00002 
00003 import java.io.Serializable;
00004 import java.util.concurrent.Semaphore;
00005 import zephyr.plugin.core.api.signals.Signal;
00006 
00007 public class Clock implements Serializable {
00008   private static final long serialVersionUID = -1155346148292134613L;
00009   static private boolean enableDataLock = false;
00010   public final transient Signal<Clock> onTick = new Signal<Clock>();
00011   public final transient Signal<Clock> onTerminate = new Signal<Clock>();
00012   private long timeStep = -1;
00013   private long lastUpdate = System.nanoTime();
00014   private long lastPeriod = 0;
00015   private boolean terminating = false;
00016   private boolean terminated = false;
00017   private final ClockInfo info;
00018   private final Semaphore dataLock = enableDataLock ? new Semaphore(0, true) : null;
00019 
00020   public Clock() {
00021     this("NoName");
00022   }
00023 
00024   public Clock(String label) {
00025     this(label, true, true);
00026   }
00027 
00028   public Clock(String label, boolean isSuspendable, boolean isTerminable) {
00029     info = new ClockInfo(label, isSuspendable, isTerminable);
00030   }
00031 
00032   public boolean tick() {
00033     releaseData();
00034     if (terminated)
00035       throw new RuntimeException("Clock is terminated");
00036     if (terminating) {
00037       terminated = true;
00038       onTerminate.fire(this);
00039       return false;
00040     }
00041     timeStep++;
00042     updateChrono();
00043     onTick.fire(this);
00044     return acquireData();
00045   }
00046 
00047   public void releaseData() {
00048     if (!enableDataLock)
00049       return;
00050     assert dataLock.availablePermits() == 0;
00051     dataLock.release();
00052   }
00053 
00054   public boolean acquireData() {
00055     if (!enableDataLock)
00056       return true;
00057     try {
00058       dataLock.acquire();
00059     } catch (InterruptedException e) {
00060       prepareTermination();
00061       return false;
00062     }
00063     assert dataLock.availablePermits() == 0;
00064     return true;
00065   }
00066 
00067   private void updateChrono() {
00068     long currentTime = System.nanoTime();
00069     lastPeriod = currentTime - lastUpdate;
00070     lastUpdate = currentTime;
00071   }
00072 
00073   public long timeStep() {
00074     return timeStep;
00075   }
00076 
00077   public long lastPeriodNano() {
00078     return lastPeriod;
00079   }
00080 
00081   public void prepareTermination() {
00082     terminating = true;
00083   }
00084 
00085   public void terminate() {
00086     if (terminated)
00087       return;
00088     prepareTermination();
00089     tick();
00090   }
00091 
00092   public boolean isTerminated() {
00093     return terminating;
00094   }
00095 
00096   public ClockInfo info() {
00097     return info;
00098   }
00099 
00100   static public void setEnableDataLock(boolean enabled) {
00101     enableDataLock = enabled;
00102   }
00103 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark