Author: zothar
Date: 2006-07-23 15:50:39 +0000 (Sun, 23 Jul 2006)
New Revision: 9734

Modified:
   
trunk/freenet/src/freenet/support/math/BootstrappingDecayingRunningAverage.java
   trunk/freenet/src/freenet/support/math/SimpleBinaryRunningAverage.java
   trunk/freenet/src/freenet/support/math/SimpleRunningAverage.java
   trunk/freenet/src/freenet/support/math/TimeDecayingRunningAverage.java
Log:
Locking consistency in support/math/

Modified: 
trunk/freenet/src/freenet/support/math/BootstrappingDecayingRunningAverage.java
===================================================================
--- 
trunk/freenet/src/freenet/support/math/BootstrappingDecayingRunningAverage.java 
    2006-07-23 15:50:20 UTC (rev 9733)
+++ 
trunk/freenet/src/freenet/support/math/BootstrappingDecayingRunningAverage.java 
    2006-07-23 15:50:39 UTC (rev 9734)
@@ -35,7 +35,7 @@
     long zeros;
     long ones;

-    public String toString() {
+    public synchronized String toString() {
         return super.toString() + ": min="+min+", max="+max+", currentValue="+
                currentValue+", reports="+reports+", maxReports="+maxReports
                // FIXME
@@ -137,7 +137,7 @@
         return 4 + 4 + 4 + 8 + 8;
     }

-    public long countReports() {
+    public synchronized  long countReports() {
         return reports;
     }
 }

Modified: trunk/freenet/src/freenet/support/math/SimpleBinaryRunningAverage.java
===================================================================
--- trunk/freenet/src/freenet/support/math/SimpleBinaryRunningAverage.java      
2006-07-23 15:50:20 UTC (rev 9733)
+++ trunk/freenet/src/freenet/support/math/SimpleBinaryRunningAverage.java      
2006-07-23 15:50:39 UTC (rev 9734)
@@ -33,7 +33,7 @@
        final BitSet ba;
        boolean logDEBUG;

-       private final int baSize() {
+       private synchronized final int baSize() {
                return Math.min((int)totalReported, maximumSize);
        }

@@ -121,13 +121,13 @@
                return ((double)to) / (double)(tz + to);
        }

-       public String extraToString() {
+       public synchronized  String extraToString() {
                return Integer.toString(totalZeros) + " 0s, "+
                        totalOnes + " 1s, "+(totalZeros+totalOnes)+
                        " total";
        }

-       public String toString() {
+       public synchronized String toString() {
                return super.toString() + " ("+extraToString()+")"+
                        ", init="+defaultValue+", index="+index+", 
totalReported="+
                        totalReported;
@@ -178,7 +178,7 @@
         this.totalZeros = a.totalZeros;
     }

-       private void calculateTotalOnesZeros() {
+       private synchronized void calculateTotalOnesZeros() {
                StringBuffer sb = new StringBuffer();
                int tones = 0;
                int tzeros = 0;
@@ -229,7 +229,7 @@
                return valueIfReported(convert(d));
        }

-    public long countReports() {
+    public synchronized long countReports() {
         return totalReported;
     }
 }

Modified: trunk/freenet/src/freenet/support/math/SimpleRunningAverage.java
===================================================================
--- trunk/freenet/src/freenet/support/math/SimpleRunningAverage.java    
2006-07-23 15:50:20 UTC (rev 9733)
+++ trunk/freenet/src/freenet/support/math/SimpleRunningAverage.java    
2006-07-23 15:50:39 UTC (rev 9734)
@@ -61,29 +61,29 @@
         }
     }

-    public void report(double d) {
+    public synchronized void report(double d) {
         totalReports++;
                if (logDEBUG)
                        Logger.debug(this, "report(" + d + ") on " + this);
-               synchronized (this) {
-                       if (curLen < refs.length)
-                               curLen++;
-                       else
-                               total -= popValue();
-                       pushValue(d);
-                       total += d;
-               }
+               if (curLen < refs.length)
+                       curLen++;
+               else
+                       total -= popValue();
+               pushValue(d);
+               total += d;
        }
-    protected void pushValue(double value){
+
+    protected synchronized void pushValue(double value){
                refs[nextSlotPtr] = value;
                nextSlotPtr++;
                if(nextSlotPtr >= refs.length) nextSlotPtr = 0;
     }
-       protected double popValue(){
+
+       protected synchronized double popValue(){
                return refs[nextSlotPtr];
        }

-    public String toString() {
+    public synchronized String toString() {
         return super.toString() + ": curLen="+curLen+", ptr="+nextSlotPtr+", 
total="+
                total+", average="+total/curLen;
     }
@@ -96,7 +96,7 @@
         throw new UnsupportedOperationException();
     }

-    public long countReports() {
+    public synchronized long countReports() {
         return totalReports;
     }


Modified: trunk/freenet/src/freenet/support/math/TimeDecayingRunningAverage.java
===================================================================
--- trunk/freenet/src/freenet/support/math/TimeDecayingRunningAverage.java      
2006-07-23 15:50:20 UTC (rev 9733)
+++ trunk/freenet/src/freenet/support/math/TimeDecayingRunningAverage.java      
2006-07-23 15:50:39 UTC (rev 9734)
@@ -38,11 +38,14 @@
     boolean logDEBUG;

     public String toString() {
-        return super.toString() + ": currentValue="+curValue+", 
halfLife="+halfLife+
-               ", lastReportTime="+(System.currentTimeMillis()-lastReportTime)+
-               "ms ago, createdTime="+(System.currentTimeMillis()-createdTime)+
-               "ms ago, totalReports="+totalReports+", started="+started+
-               ", defaultValue="+defaultValue+", min="+minReport+", 
max="+maxReport;
+               long now = System.currentTimeMillis();
+               synchronized(this) {
+               return super.toString() + ": currentValue="+curValue+", 
halfLife="+halfLife+
+                       ", lastReportTime="+(now - lastReportTime)+
+                       "ms ago, createdTime="+(now - createdTime)+
+                       "ms ago, totalReports="+totalReports+", 
started="+started+
+                       ", defaultValue="+defaultValue+", min="+minReport+", 
max="+maxReport;
+               }
     }

     public TimeDecayingRunningAverage(double defaultValue, long halfLife,
@@ -99,38 +102,40 @@
        return curValue;
     }

-    public synchronized void report(double d) {
-        if(d < minReport) d = minReport;
-        if(d > maxReport) d = maxReport;
-        totalReports++;
-        long now = System.currentTimeMillis(); 
-        if(!started) {
-               curValue = d;
-            started = true;
-            if(logDEBUG)
-                Logger.debug(this, "Reported "+d+" on "+this+" when just 
started");
-        } else if(lastReportTime != -1) { // might be just serialized in
-            long thisInterval =
-                 now - lastReportTime;
-            double thisHalfLife = halfLife;
-            long uptime = now - createdTime;
-            if((uptime / 4) < thisHalfLife) thisHalfLife = (uptime / 4);
-            if(thisHalfLife == 0) thisHalfLife = 1;
-            double changeFactor =
-               Math.pow(0.5, (thisInterval) / thisHalfLife);
-            double oldCurValue = curValue;
-            curValue = curValue * changeFactor /* close to 1.0 if short 
interval, close to 0.0 if long interval */ 
-               + (1.0 - changeFactor) * d;
-            if(logDEBUG)
-                Logger.debug(this, "Reported "+d+" on "+this+": 
thisInterval="+thisInterval+
-                               ", halfLife="+halfLife+", uptime="+uptime+", 
thisHalfLife="+thisHalfLife+
-                        ", changeFactor="+changeFactor+", 
oldCurValue="+oldCurValue+
-                                               ", 
currentValue="+currentValue()+
-                                               ", 
thisInterval="+thisInterval+", thisHalfLife="+thisHalfLife+
-                                               ", uptime="+uptime+", 
changeFactor="+changeFactor);
-        }
-        lastReportTime = now;
-    }
+       public void report(double d) {
+               long now = System.currentTimeMillis();
+               synchronized(this) {
+                       if(d < minReport) d = minReport;
+                       if(d > maxReport) d = maxReport;
+                       totalReports++;
+                       if(!started) {
+                               curValue = d;
+                               started = true;
+                               if(logDEBUG)
+                                       Logger.debug(this, "Reported "+d+" on 
"+this+" when just started");
+                       } else if(lastReportTime != -1) { // might be just 
serialized in
+                               long thisInterval =
+                                        now - lastReportTime;
+                               double thisHalfLife = halfLife;
+                               long uptime = now - createdTime;
+                               if((uptime / 4) < thisHalfLife) thisHalfLife = 
(uptime / 4);
+                               if(thisHalfLife == 0) thisHalfLife = 1;
+                               double changeFactor =
+                                       Math.pow(0.5, (thisInterval) / 
thisHalfLife);
+                               double oldCurValue = curValue;
+                               curValue = curValue * changeFactor /* close to 
1.0 if short interval, close to 0.0 if long interval */ 
+                                       + (1.0 - changeFactor) * d;
+                               if(logDEBUG)
+                                       Logger.debug(this, "Reported "+d+" on 
"+this+": thisInterval="+thisInterval+
+                                                       ", 
halfLife="+halfLife+", uptime="+uptime+", thisHalfLife="+thisHalfLife+
+                                                       ", 
changeFactor="+changeFactor+", oldCurValue="+oldCurValue+
+                                                       ", 
currentValue="+currentValue()+
+                                                       ", 
thisInterval="+thisInterval+", thisHalfLife="+thisHalfLife+
+                                                       ", uptime="+uptime+", 
changeFactor="+changeFactor);
+                       }
+                       lastReportTime = now;
+               }
+       }

     public void report(long d) {
         report((double)d);
@@ -140,24 +145,27 @@
         throw new UnsupportedOperationException();
     }

-    public synchronized void writeDataTo(DataOutputStream out) throws 
IOException {
-        out.writeInt(MAGIC);
-        out.writeInt(1);
-        out.writeDouble(curValue);
-        out.writeBoolean(started);
-        out.writeLong(totalReports);
-        out.writeLong(System.currentTimeMillis() - createdTime);
-    }
+       public void writeDataTo(DataOutputStream out) throws IOException {
+               long now = System.currentTimeMillis();
+               synchronized(this) {
+                       out.writeInt(MAGIC);
+                       out.writeInt(1);
+                       out.writeDouble(curValue);
+                       out.writeBoolean(started);
+                       out.writeLong(totalReports);
+                       out.writeLong(now - createdTime);
+               }
+       }

     public int getDataLength() {
         return 4 + 4 + 8 + 8 + 1 + 8 + 8;
     }

-    public long countReports() {
+    public synchronized long countReports() {
         return totalReports;
     }

-       public long lastReportTime() {
+       public synchronized long lastReportTime() {
                return lastReportTime;
        }
 }


Reply via email to