Improved encapsulation of thread synchronisation as follows

- replaced use of Object instance mutex with a private final Lock object
- replaced Object signaling with specific condition signalling
This commit is contained in:
Abraham Kiggundu
2014-12-23 23:55:56 +03:00
parent 276dcb1038
commit b074978472
19 changed files with 1001 additions and 478 deletions

View File

@@ -8,6 +8,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.briarproject.util.OsUtils;
@@ -38,6 +40,9 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl {
private final Map<String, Object> options;
private boolean initialised = false; // Locking: this
private final Lock synchLock = new ReentrantLock();
WindowsShutdownManagerImpl() {
// Use the Unicode versions of Win32 API calls
@@ -48,9 +53,15 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl {
}
@Override
public synchronized int addShutdownHook(Runnable r) {
if(!initialised) initialise();
return super.addShutdownHook(r);
public int addShutdownHook(Runnable r) {
synchLock.lock();
try {
if(!initialised) initialise();
return super.addShutdownHook(r);
}
finally{
synchLock.unlock();
}
}
@Override
@@ -69,20 +80,26 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl {
}
// Package access for testing
synchronized void runShutdownHooks() {
boolean interrupted = false;
// Start each hook in its own thread
for(Thread hook : hooks.values()) hook.start();
// Wait for all the hooks to finish
for(Thread hook : hooks.values()) {
try {
hook.join();
} catch(InterruptedException e) {
LOG.warning("Interrupted while running shutdown hooks");
interrupted = true;
void runShutdownHooks() {
synchLock.lock();
try {
boolean interrupted = false;
// Start each hook in its own thread
for(Thread hook : hooks.values()) hook.start();
// Wait for all the hooks to finish
for(Thread hook : hooks.values()) {
try {
hook.join();
} catch(InterruptedException e) {
LOG.warning("Interrupted while running shutdown hooks");
interrupted = true;
}
}
if(interrupted) Thread.currentThread().interrupt();
}
finally{
synchLock.unlock();
}
if(interrupted) Thread.currentThread().interrupt();
}
private class EventLoop extends Thread {