Renamed a bunch of lock variables.

"synchLock" will become confusing when we have lots of objects with "sync" in the name.
This commit is contained in:
akwizgran
2015-12-03 16:39:53 +00:00
parent 4f59491c9f
commit 8529c976c2
18 changed files with 180 additions and 179 deletions

View File

@@ -19,9 +19,9 @@ class FortunaGenerator {
private static final int KEY_BYTES = 32;
private static final int BLOCK_BYTES = 16;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
private final MessageDigest digest = new DoubleDigest(new SHA256Digest());
private final BlockCipher cipher = new AESLightEngine();
private final byte[] key = new byte[KEY_BYTES];
@@ -34,21 +34,21 @@ class FortunaGenerator {
}
void reseed(byte[] seed) {
synchLock.lock();
lock.lock();
try {
digest.update(key);
digest.update(seed);
digest.digest(key, 0, KEY_BYTES);
incrementCounter();
} finally {
synchLock.unlock();
lock.unlock();
}
}
// Package access for testing
void incrementCounter() {
synchLock.lock();
lock.lock();
try {
counter[0]++;
for (int i = 0; counter[i] == 0; i++) {
@@ -57,23 +57,23 @@ class FortunaGenerator {
counter[i + 1]++;
}
} finally {
synchLock.unlock();
lock.unlock();
}
}
// Package access for testing
byte[] getCounter() {
synchLock.lock();
lock.lock();
try {
return counter;
} finally {
synchLock.unlock();
lock.unlock();
}
}
int nextBytes(byte[] dest, int off, int len) {
synchLock.lock();
lock.lock();
try {
// Don't write more than the maximum number of bytes in one request
if (len > MAX_BYTES_PER_REQUEST) len = MAX_BYTES_PER_REQUEST;
@@ -104,7 +104,7 @@ class FortunaGenerator {
// Return the number of bytes written
return len;
} finally {
synchLock.unlock();
lock.unlock();
}
}
}

View File

@@ -62,9 +62,9 @@ class ConnectorGroup extends Thread implements InvitationTask {
private final Collection<InvitationListener> listeners;
private final AtomicBoolean connected;
private final CountDownLatch localConfirmationLatch;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
private int localConfirmationCode = -1, remoteConfirmationCode = -1;
private boolean connectionFailed = false;
private boolean localCompared = false, remoteCompared = false;
@@ -103,7 +103,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
}
public InvitationState addListener(InvitationListener l) {
synchLock.lock();
lock.lock();
try {
listeners.add(l);
return new InvitationState(localInvitationCode,
@@ -112,7 +112,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
localCompared, remoteCompared, localMatched, remoteMatched,
remoteName);
} finally {
synchLock.unlock();
lock.unlock();
}
}
@@ -134,11 +134,11 @@ class ConnectorGroup extends Thread implements InvitationTask {
localProps = db.getLocalProperties();
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
synchLock.lock();
lock.lock();
try {
connectionFailed = true;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners) l.connectionFailed();
return;
@@ -170,11 +170,11 @@ class ConnectorGroup extends Thread implements InvitationTask {
}
// If none of the threads connected, inform the listeners
if (!connected.get()) {
synchLock.lock();
lock.lock();
try {
connectionFailed = true;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners) l.connectionFailed();
}
@@ -203,23 +203,23 @@ class ConnectorGroup extends Thread implements InvitationTask {
}
public void localConfirmationSucceeded() {
synchLock.lock();
lock.lock();
try {
localCompared = true;
localMatched = true;
} finally {
synchLock.unlock();
lock.unlock();
}
localConfirmationLatch.countDown();
}
public void localConfirmationFailed() {
synchLock.lock();
lock.lock();
try {
localCompared = true;
localMatched = false;
} finally {
synchLock.unlock();
lock.unlock();
}
localConfirmationLatch.countDown();
}
@@ -232,12 +232,12 @@ class ConnectorGroup extends Thread implements InvitationTask {
}
void keyAgreementSucceeded(int localCode, int remoteCode) {
synchLock.lock();
lock.lock();
try {
localConfirmationCode = localCode;
remoteConfirmationCode = remoteCode;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners)
l.keyAgreementSucceeded(localCode, remoteCode);
@@ -249,43 +249,43 @@ class ConnectorGroup extends Thread implements InvitationTask {
boolean waitForLocalConfirmationResult() throws InterruptedException {
localConfirmationLatch.await(CONFIRMATION_TIMEOUT, MILLISECONDS);
synchLock.lock();
lock.lock();
try {
return localMatched;
} finally {
synchLock.unlock();
lock.unlock();
}
}
void remoteConfirmationSucceeded() {
synchLock.lock();
lock.lock();
try {
remoteCompared = true;
remoteMatched = true;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners) l.remoteConfirmationSucceeded();
}
void remoteConfirmationFailed() {
synchLock.lock();
lock.lock();
try {
remoteCompared = true;
remoteMatched = false;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners) l.remoteConfirmationFailed();
}
void pseudonymExchangeSucceeded(Author remoteAuthor) {
String name = remoteAuthor.getName();
synchLock.lock();
lock.lock();
try {
remoteName = name;
} finally {
synchLock.unlock();
lock.unlock();
}
for (InvitationListener l : listeners)
l.pseudonymExchangeSucceeded(name);

View File

@@ -9,9 +9,9 @@ import org.briarproject.api.lifecycle.ShutdownManager;
class ShutdownManagerImpl implements ShutdownManager {
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
protected final Map<Integer, Thread> hooks;
private int nextHandle = 0;
@@ -20,7 +20,7 @@ class ShutdownManagerImpl implements ShutdownManager {
}
public int addShutdownHook(Runnable r) {
synchLock.lock();
lock.lock();
try {
int handle = nextHandle++;
Thread hook = createThread(r);
@@ -28,7 +28,7 @@ class ShutdownManagerImpl implements ShutdownManager {
Runtime.getRuntime().addShutdownHook(hook);
return handle;
} finally {
synchLock.unlock();
lock.unlock();
}
}
@@ -38,13 +38,13 @@ class ShutdownManagerImpl implements ShutdownManager {
}
public boolean removeShutdownHook(int handle) {
synchLock.lock();
lock.lock();
try {
Thread hook = hooks.remove(handle);
if (hook == null) return false;
else return Runtime.getRuntime().removeShutdownHook(hook);
} finally {
synchLock.unlock();
lock.unlock();
}
}

View File

@@ -27,9 +27,9 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
Logger.getLogger(ConnectionRegistryImpl.class.getName());
private final EventBus eventBus;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
private final Map<TransportId, Map<ContactId, Integer>> connections;
private final Map<ContactId, Integer> contactCounts;
@@ -43,7 +43,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
public void registerConnection(ContactId c, TransportId t) {
LOG.info("Connection registered");
boolean firstConnection = false;
synchLock.lock();
lock.lock();
try {
Map<ContactId, Integer> m = connections.get(t);
if (m == null) {
@@ -61,7 +61,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
contactCounts.put(c, count + 1);
}
} finally {
synchLock.unlock();
lock.unlock();
}
if (firstConnection) {
@@ -73,7 +73,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
public void unregisterConnection(ContactId c, TransportId t) {
LOG.info("Connection unregistered");
boolean lastConnection = false;
synchLock.lock();
lock.lock();
try {
Map<ContactId, Integer> m = connections.get(t);
if (m == null) throw new IllegalArgumentException();
@@ -93,7 +93,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
contactCounts.put(c, count - 1);
}
} finally {
synchLock.unlock();
lock.unlock();
}
if (lastConnection) {
@@ -104,7 +104,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
public Collection<ContactId> getConnectedContacts(
TransportId t) {
synchLock.lock();
lock.lock();
try {
Map<ContactId, Integer> m = connections.get(t);
if (m == null) return Collections.emptyList();
@@ -112,17 +112,17 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
if (LOG.isLoggable(INFO)) LOG.info(ids.size() + " contacts connected");
return Collections.unmodifiableList(ids);
} finally {
synchLock.unlock();
lock.unlock();
}
}
public boolean isConnected(ContactId c) {
synchLock.lock();
lock.lock();
try {
return contactCounts.containsKey(c);
} finally {
synchLock.unlock();
lock.unlock();
}
}

View File

@@ -50,9 +50,9 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
private final TagRecogniser tagRecogniser;
private final Clock clock;
private final Timer timer;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
private final Map<TransportId, Integer> maxLatencies;
private final Map<EndpointKey, TemporarySecret> oldSecrets;
private final Map<EndpointKey, TemporarySecret> currentSecrets;
@@ -75,7 +75,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
}
public boolean start() {
synchLock.lock();
lock.lock();
try {
eventBus.addListener(this);
// Load the temporary secrets and transport latencies from the DB
@@ -116,12 +116,12 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
MS_BETWEEN_CHECKS);
return true;
} finally {
synchLock.unlock();
lock.unlock();
}
}
// Assigns secrets to the appropriate maps and returns any dead secrets
// Locking: synchLock
// Locking: lock
private Collection<TemporarySecret> assignSecretsToMaps(long now,
Collection<TemporarySecret> secrets) {
Collection<TemporarySecret> dead = new ArrayList<TemporarySecret>();
@@ -154,7 +154,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
}
// Replaces the given secrets and returns any secrets created
// Locking: synchLock
// Locking: lock
private Collection<TemporarySecret> replaceDeadSecrets(long now,
Collection<TemporarySecret> dead) {
// If there are several dead secrets for an endpoint, use the newest
@@ -213,7 +213,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
}
public boolean stop() {
synchLock.lock();
lock.lock();
try {
eventBus.removeListener(this);
timer.cancel();
@@ -224,13 +224,13 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
newSecrets.clear();
return true;
} finally {
synchLock.unlock();
lock.unlock();
}
}
public StreamContext getStreamContext(ContactId c,
TransportId t) {
synchLock.lock();
lock.lock();
try {
TemporarySecret s = currentSecrets.get(new EndpointKey(c, t));
if (s == null) {
@@ -251,13 +251,13 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
byte[] secret = s.getSecret();
return new StreamContext(c, t, secret, streamNumber, s.getAlice());
} finally {
synchLock.unlock();
lock.unlock();
}
}
public void endpointAdded(Endpoint ep, int maxLatency,
byte[] initialSecret) {
synchLock.lock();
lock.lock();
try {
maxLatencies.put(ep.getTransportId(), maxLatency);
// Work out which rotation period we're in
@@ -291,13 +291,13 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
tagRecogniser.addSecret(s2);
tagRecogniser.addSecret(s3);
} finally {
synchLock.unlock();
lock.unlock();
}
}
@Override
public void run() {
synchLock.lock();
lock.lock();
try {
// Rebuild the maps because we may be running a whole period late
Collection<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
@@ -330,7 +330,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
for (TemporarySecret s : created) tagRecogniser.addSecret(s);
}
} finally {
synchLock.unlock();
lock.unlock();
}
}
@@ -347,14 +347,14 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
}
}
// Locking: synchLock
// Locking: lock
private void removeSecrets(ContactId c, Map<?, TemporarySecret> m) {
Iterator<TemporarySecret> it = m.values().iterator();
while (it.hasNext())
if (it.next().getContactId().equals(c)) it.remove();
}
// Locking: synchLock
// Locking: lock
private void removeSecrets(TransportId t, Map<?, TemporarySecret> m) {
Iterator<TemporarySecret> it = m.values().iterator();
while (it.hasNext())
@@ -403,13 +403,13 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
public void run() {
ContactId c = event.getContactId();
tagRecogniser.removeSecrets(c);
synchLock.lock();
lock.lock();
try {
removeSecrets(c, oldSecrets);
removeSecrets(c, currentSecrets);
removeSecrets(c, newSecrets);
} finally {
synchLock.unlock();
lock.unlock();
}
}
}
@@ -424,11 +424,11 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
@Override
public void run() {
synchLock.lock();
lock.lock();
try {
maxLatencies.put(event.getTransportId(), event.getMaxLatency());
} finally {
synchLock.unlock();
lock.unlock();
}
}
}
@@ -445,14 +445,14 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
public void run() {
TransportId t = event.getTransportId();
tagRecogniser.removeSecrets(t);
synchLock.lock();
lock.lock();
try {
maxLatencies.remove(t);
removeSecrets(t, oldSecrets);
removeSecrets(t, currentSecrets);
removeSecrets(t, newSecrets);
} finally {
synchLock.unlock();
lock.unlock();
}
}
}

View File

@@ -20,9 +20,9 @@ class TagRecogniserImpl implements TagRecogniser {
private final CryptoComponent crypto;
private final DatabaseComponent db;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// Locking: synchLock
// Locking: lock
private final Map<TransportId, TransportTagRecogniser> recognisers;
@Inject
@@ -35,11 +35,11 @@ class TagRecogniserImpl implements TagRecogniser {
public StreamContext recogniseTag(TransportId t, byte[] tag)
throws DbException {
TransportTagRecogniser r;
synchLock.lock();
lock.lock();
try {
r = recognisers.get(t);
} finally {
synchLock.unlock();
lock.unlock();
}
if (r == null) return null;
return r.recogniseTag(tag);
@@ -48,7 +48,7 @@ class TagRecogniserImpl implements TagRecogniser {
public void addSecret(TemporarySecret s) {
TransportId t = s.getTransportId();
TransportTagRecogniser r;
synchLock.lock();
lock.lock();
try {
r = recognisers.get(t);
if (r == null) {
@@ -56,49 +56,49 @@ class TagRecogniserImpl implements TagRecogniser {
recognisers.put(t, r);
}
} finally {
synchLock.unlock();
lock.unlock();
}
r.addSecret(s);
}
public void removeSecret(ContactId c, TransportId t, long period) {
TransportTagRecogniser r;
synchLock.lock();
lock.lock();
try {
r = recognisers.get(t);
} finally {
synchLock.unlock();
lock.unlock();
}
if (r != null) r.removeSecret(c, period);
}
public void removeSecrets(ContactId c) {
synchLock.lock();
lock.lock();
try {
for (TransportTagRecogniser r : recognisers.values())
r.removeSecrets(c);
} finally {
synchLock.unlock();
lock.unlock();
}
}
public void removeSecrets(TransportId t) {
synchLock.lock();
lock.lock();
try {
recognisers.remove(t);
} finally {
synchLock.unlock();
lock.unlock();
}
}
public void removeSecrets() {
synchLock.lock();
lock.lock();
try {
for (TransportTagRecogniser r : recognisers.values())
r.removeSecrets();
} finally {
synchLock.unlock();
lock.unlock();
}
}

View File

@@ -29,9 +29,9 @@ class TransportTagRecogniser {
private final CryptoComponent crypto;
private final DatabaseComponent db;
private final TransportId transportId;
private final Lock synchLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
// The following are locking: synchLock
// The following are locking: lock
private final Map<Bytes, TagContext> tagMap;
private final Map<RemovalKey, RemovalContext> removalMap;
@@ -45,7 +45,7 @@ class TransportTagRecogniser {
}
StreamContext recogniseTag(byte[] tag) throws DbException {
synchLock.lock();
lock.lock();
try {
TagContext t = tagMap.remove(new Bytes(tag));
if (t == null) return null; // The tag was not expected
@@ -69,12 +69,12 @@ class TransportTagRecogniser {
return new StreamContext(t.contactId, transportId, t.secret,
t.streamNumber, t.alice);
} finally {
synchLock.unlock();
lock.unlock();
}
}
void addSecret(TemporarySecret s) {
synchLock.lock();
lock.lock();
try {
ContactId contactId = s.getContactId();
boolean alice = s.getAlice();
@@ -97,23 +97,23 @@ class TransportTagRecogniser {
RemovalContext r = new RemovalContext(window, secret, alice);
removalMap.put(new RemovalKey(contactId, period), r);
} finally {
synchLock.unlock();
lock.unlock();
}
}
void removeSecret(ContactId contactId, long period) {
synchLock.lock();
lock.lock();
try {
RemovalKey k = new RemovalKey(contactId, period);
RemovalContext removed = removalMap.remove(k);
if (removed == null) throw new IllegalArgumentException();
removeSecret(removed);
} finally {
synchLock.unlock();
lock.unlock();
}
}
// Locking: synchLock
// Locking: lock
private void removeSecret(RemovalContext r) {
// Remove the expected tags
SecretKey key = crypto.deriveTagKey(r.secret, !r.alice);
@@ -126,7 +126,7 @@ class TransportTagRecogniser {
}
void removeSecrets(ContactId c) {
synchLock.lock();
lock.lock();
try {
Collection<RemovalKey> keysToRemove = new ArrayList<RemovalKey>();
for (RemovalKey k : removalMap.keySet())
@@ -134,18 +134,18 @@ class TransportTagRecogniser {
for (RemovalKey k : keysToRemove)
removeSecret(k.contactId, k.period);
} finally {
synchLock.unlock();
lock.unlock();
}
}
void removeSecrets() {
synchLock.lock();
lock.lock();
try {
for (RemovalContext r : removalMap.values()) removeSecret(r);
assert tagMap.isEmpty();
removalMap.clear();
} finally {
synchLock.unlock();
lock.unlock();
}
}