From db287749961c090237b4e1deb76731ebb9469dd2 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Sun, 19 Mar 2023 17:08:55 -0700 Subject: [PATCH] Peer: WIP/Question: replace ReentrantLock Replace `pingIntervalsLock.lock()` with `synchronizedLock(pingIntervals)`. I'm not sure this is equivalent or if we are losing any functionality by doing this. Note that before the Duration[] was nullable, so there had to be something to lock on. Now that the Deque is final, we can use it. --- core/src/main/java/org/bitcoinj/core/Peer.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/Peer.java b/core/src/main/java/org/bitcoinj/core/Peer.java index 159675801..0fd76fb2e 100644 --- a/core/src/main/java/org/bitcoinj/core/Peer.java +++ b/core/src/main/java/org/bitcoinj/core/Peer.java @@ -169,10 +169,9 @@ public class Peer extends PeerSocketHandler { @GuardedBy("getAddrFutures") private final LinkedList> getAddrFutures; // Outstanding pings against this peer and how long the last one took to complete. - private final ReentrantLock pingIntervalsLock = new ReentrantLock(); - @GuardedBy("pingIntervalsLock") private final Deque pingIntervals = new ArrayDeque<>(PING_MOVING_AVERAGE_WINDOW); - private volatile Duration lastPing = null; // should only be written while holding pingIntervalsLock - private volatile Duration averagePing = null; // should only be written while holding pingIntervalsLock + private final Deque pingIntervals = new ArrayDeque<>(PING_MOVING_AVERAGE_WINDOW); + private volatile Duration lastPing = null; // should only be written while holding lock on pingIntervals + private volatile Duration averagePing = null; // should only be written while holding lock on pingIntervals private final CopyOnWriteArrayList pendingPings; // Disconnect from a peer that is not responding to Pings @@ -1513,8 +1512,7 @@ public class Peer extends PeerSocketHandler { /** Adds a ping time sample to the averaging window. */ private void addPingInterval(Duration sample) { - pingIntervalsLock.lock(); - try { + synchronized (pingIntervals) { if (pingIntervals.size() >= PING_MOVING_AVERAGE_WINDOW) { // Remove oldest sample from front of queue pingIntervals.remove(); @@ -1527,8 +1525,6 @@ public class Peer extends PeerSocketHandler { .reduce(Duration::plus) .map(d -> d.dividedBy(pingIntervals.size())) .orElse(null); - } finally { - pingIntervalsLock.unlock(); } }