From 7e3e08532f9ad2fb82f6b692c470a47c97a9b42f Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 17 Nov 2013 15:03:10 +0000
Subject: [PATCH] more static atomics

---
 .../java/src/org/klomp/snark/PeerConnectionOut.java       | 7 ++++---
 .../java/src/net/i2p/i2ptunnel/I2PTunnelRunner.java       | 5 ++---
 .../src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java    | 8 +++++---
 core/java/src/net/i2p/client/MessageState.java            | 7 ++++---
 core/java/src/net/i2p/util/LogManager.java                | 8 +++++---
 .../router/networkdb/kademlia/SearchMessageSelector.java  | 6 ++++--
 router/java/src/net/i2p/router/transport/UPnP.java        | 7 ++++---
 7 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java
index 9d6862a734..a01ea8e18a 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
 
 import net.i2p.I2PAppContext;
 import net.i2p.util.I2PAppThread;
@@ -44,8 +45,8 @@ class PeerConnectionOut implements Runnable
   // Contains Messages.
   private final List<Message> sendQueue = new ArrayList();
   
-  private static long __id = 0;
-  private long _id;
+  private static final AtomicLong __id = new AtomicLong();
+  private final long _id;
   
   long lastSent;
 
@@ -53,7 +54,7 @@ class PeerConnectionOut implements Runnable
   {
     this.peer = peer;
     this.dout = dout;
-    _id = ++__id;
+    _id = __id.incrementAndGet();
 
     lastSent = System.currentTimeMillis();
   }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelRunner.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelRunner.java
index fe512be233..e9a9a5b212 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelRunner.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelRunner.java
@@ -55,7 +55,7 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
     private long totalSent;
     private long totalReceived;
 
-    private volatile long __forwarderId;
+    private static final AtomicLong __forwarderId = new AtomicLong();
     
     public I2PTunnelRunner(Socket s, I2PSocket i2ps, Object slock, byte[] initialI2PData,
                            List<I2PSocket> sockList) {
@@ -98,7 +98,6 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
         if (_log.shouldLog(Log.INFO))
             _log.info("I2PTunnelRunner started");
         _runnerId = __runnerId.incrementAndGet();
-        __forwarderId = i2ps.hashCode();
         setName("I2PTunnelRunner " + _runnerId);
         start();
     }
@@ -312,7 +311,7 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
             _toI2P = toI2P;
             direction = (toI2P ? "toI2P" : "fromI2P");
             _cache = ByteCache.getInstance(32, NETWORK_BUFFER_SIZE);
-            setName("StreamForwarder " + _runnerId + "." + (++__forwarderId));
+            setName("StreamForwarder " + _runnerId + '.' + __forwarderId.incrementAndGet());
             start();
         }
 
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java
index 2c0fc2d776..b4363afe0f 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java
@@ -7,6 +7,7 @@
 package net.i2p.i2ptunnel.socks;
 
 import java.net.Socket;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import net.i2p.client.streaming.I2PSocket;
 import net.i2p.i2ptunnel.I2PTunnel;
@@ -30,7 +31,7 @@ import net.i2p.util.Log;
  */
 public class I2PSOCKSIRCTunnel extends I2PSOCKSTunnel {
 
-    private static int __clientId = 0;
+    private static final AtomicInteger __clientId = new AtomicInteger();
 
     /** @param pkf private key file name or null for transient key */
     public I2PSOCKSIRCTunnel(int localPort, Logging l, boolean ownDest, EventDispatcher notifyThis, I2PTunnel tunnel, String pkf) {
@@ -50,11 +51,12 @@ public class I2PSOCKSIRCTunnel extends I2PSOCKSTunnel {
             Socket clientSock = serv.getClientSocket();
             I2PSocket destSock = serv.getDestinationI2PSocket(this);
             StringBuffer expectedPong = new StringBuffer();
+            int id = __clientId.incrementAndGet();
             Thread in = new I2PAppThread(new IrcInboundFilter(clientSock, destSock, expectedPong, _log),
-                                         "SOCKS IRC Client " + (++__clientId) + " in", true);
+                                         "SOCKS IRC Client " + id + " in", true);
             in.start();
             Thread out = new I2PAppThread(new IrcOutboundFilter(clientSock, destSock, expectedPong, _log),
-                                          "SOCKS IRC Client " + __clientId + " out", true);
+                                          "SOCKS IRC Client " + id + " out", true);
             out.start();
         } catch (SOCKSException e) {
             if (_log.shouldLog(Log.WARN))
diff --git a/core/java/src/net/i2p/client/MessageState.java b/core/java/src/net/i2p/client/MessageState.java
index 32858dce23..326fec9898 100644
--- a/core/java/src/net/i2p/client/MessageState.java
+++ b/core/java/src/net/i2p/client/MessageState.java
@@ -3,6 +3,7 @@ package net.i2p.client;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
 
 import net.i2p.I2PAppContext;
 import net.i2p.data.Destination;
@@ -31,11 +32,11 @@ class MessageState {
     private boolean _cancelled;
     private final long _created;
 
-    private static long __stateId = 0;
-    private long _stateId;
+    private static final AtomicLong __stateId = new AtomicLong();
+    private final long _stateId;
     
     public MessageState(I2PAppContext ctx, long nonce, String prefix) {
-        _stateId = ++__stateId;
+        _stateId = __stateId.incrementAndGet();
         _context = ctx;
         _log = ctx.logManager().getLog(MessageState.class);
         _nonce = nonce;
diff --git a/core/java/src/net/i2p/util/LogManager.java b/core/java/src/net/i2p/util/LogManager.java
index a340c04c33..e919f236ee 100644
--- a/core/java/src/net/i2p/util/LogManager.java
+++ b/core/java/src/net/i2p/util/LogManager.java
@@ -24,6 +24,7 @@ import java.util.Set;
 import java.util.TimeZone;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
 import net.i2p.I2PAppContext;
@@ -738,11 +739,12 @@ public class LogManager {
         _consoleBuffer.clear();
     }
 
-    private static int __id = 0;
+    private static final AtomicInteger __id = new AtomicInteger();
+
     private class ShutdownHook extends Thread {
-        private int _id;
+        private final int _id;
         public ShutdownHook() {
-            _id = ++__id;
+            _id = __id.incrementAndGet();
         }
         @Override
         public void run() {
diff --git a/router/java/src/net/i2p/router/networkdb/kademlia/SearchMessageSelector.java b/router/java/src/net/i2p/router/networkdb/kademlia/SearchMessageSelector.java
index 2ff63b6dab..52e7a1602b 100644
--- a/router/java/src/net/i2p/router/networkdb/kademlia/SearchMessageSelector.java
+++ b/router/java/src/net/i2p/router/networkdb/kademlia/SearchMessageSelector.java
@@ -1,5 +1,7 @@
 package net.i2p.router.networkdb.kademlia;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import net.i2p.data.Hash;
 import net.i2p.data.RouterInfo;
 import net.i2p.data.i2np.DatabaseSearchReplyMessage;
@@ -17,7 +19,7 @@ import net.i2p.util.Log;
 class SearchMessageSelector implements MessageSelector {
     private final Log _log;
     private final RouterContext _context;
-    private static int __searchSelectorId = 0;
+    private static final AtomicInteger __searchSelectorId = new AtomicInteger();
     private final Hash _peer;
     private boolean _found;
     private final int _id;
@@ -31,7 +33,7 @@ class SearchMessageSelector implements MessageSelector {
         _found = false;
         _exp = expiration;
         _state = state;
-        _id = ++__searchSelectorId;
+        _id = __searchSelectorId.incrementAndGet();
         if (_log.shouldLog(Log.DEBUG))
             _log.debug("[" + _id + "] Created: " + toString());
     }
diff --git a/router/java/src/net/i2p/router/transport/UPnP.java b/router/java/src/net/i2p/router/transport/UPnP.java
index fd17053bec..980de066f8 100644
--- a/router/java/src/net/i2p/router/transport/UPnP.java
+++ b/router/java/src/net/i2p/router/transport/UPnP.java
@@ -14,6 +14,7 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import net.i2p.I2PAppContext;
 import net.i2p.data.DataHelper;
@@ -902,7 +903,7 @@ class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
 		return "?";
 	}
 
-	private static int __id = 0;
+	private static final AtomicInteger __id = new AtomicInteger();
 
 	/**
 	 *  postControlAction() can take many seconds, especially if it's failing,
@@ -925,7 +926,7 @@ class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
 		if (_log.shouldLog(Log.INFO))
 			_log.info("Starting thread to forward " + portsToForwardNow.size() + " ports");
 	        Thread t = new Thread(new RegisterPortsThread(portsToForwardNow));
-		t.setName("UPnP Port Opener " + (++__id));
+		t.setName("UPnP Port Opener " + __id.incrementAndGet());
 		t.setDaemon(true);
 		t.start();
 	}
@@ -967,7 +968,7 @@ class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
 		if (_log.shouldLog(Log.INFO))
 			_log.info("Starting thread to un-forward " + portsToForwardNow.size() + " ports");
 	        Thread t = new Thread(new UnregisterPortsThread(portsToForwardNow));
-		t.setName("UPnP Port Closer " + (++__id));
+		t.setName("UPnP Port Closer " + __id.incrementAndGet());
 		t.setDaemon(true);
 		t.start();
 	}
-- 
GitLab