From 3fc312a66ba44cb678a3c06c06eb1ad64f40c7f7 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 5 Dec 2011 00:59:58 +0000
Subject: [PATCH] locking tweaks

---
 .../transport/FIFOBandwidthRefiller.java      | 24 +++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java b/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
index 6e572921e4..a6da603ca6 100644
--- a/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
+++ b/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
@@ -3,6 +3,7 @@ package net.i2p.router.transport;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import net.i2p.I2PAppContext;
 import net.i2p.data.DataHelper;
@@ -303,6 +304,7 @@ public class FIFOBandwidthRefiller implements Runnable {
     private final int[] _counts = new int[PERIODS];
     /** the actual length of the period (nominally REPLENISH_FREQUENCY) */
     private final long[] _times = new long[PERIODS];
+    private final ReentrantReadWriteLock _updateLock = new ReentrantReadWriteLock(false);
 
     /**
      *  We sent a message.
@@ -320,7 +322,16 @@ public class FIFOBandwidthRefiller implements Runnable {
      *  @return Bps in recent period (a few seconds)
      *  @since 0.8.12
      */
-    synchronized int getCurrentParticipatingBandwidth() {
+    int getCurrentParticipatingBandwidth() {
+        _updateLock.readLock().lock();
+        try {
+            return locked_getCurrentParticipatingBandwidth();
+        } finally {
+            _updateLock.readLock().unlock();
+        }
+    }
+
+    private int locked_getCurrentParticipatingBandwidth() {
         int current = _currentParticipating.get();
         long totalTime = (_limiter.now() - _lastPartUpdateTime) + _lastTotalTime;
         if (totalTime <= 0)
@@ -337,7 +348,16 @@ public class FIFOBandwidthRefiller implements Runnable {
      *
      *  @since 0.8.12
      */
-    private synchronized void updateParticipating(long now) {
+    private void updateParticipating(long now) {
+        _updateLock.writeLock().lock();
+        try {
+            locked_updateParticipating(now);
+        } finally {
+            _updateLock.writeLock().unlock();
+        }
+    }
+
+    private void locked_updateParticipating(long now) {
         long elapsed = now - _lastPartUpdateTime;
         if (elapsed <= 0) {
             // glitch in the matrix
-- 
GitLab