Revert "Revise synchronized blocks"

This reverts commit 497bbf9c2d.
This commit is contained in:
Moritz Halbritter
2023-08-03 17:26:31 +02:00
parent 02a7c22f40
commit 1a8b8ce26e
19 changed files with 151 additions and 381 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -158,7 +158,9 @@ public class FileSystemWatcher {
}
private void checkNotStarted() {
Assert.state(this.watchThread == null, "FileSystemWatcher already started");
synchronized (this.monitor) {
Assert.state(this.watchThread == null, "FileSystemWatcher already started");
}
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,8 +29,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -59,12 +57,7 @@ public class LiveReloadServer {
private final List<Connection> connections = new ArrayList<>();
/**
* Guards access to {@link #connections}.
*/
private final Lock connectionsLock = new ReentrantLock();
private final Lock lock = new ReentrantLock();
private final Object monitor = new Object();
private final int port;
@@ -115,8 +108,7 @@ public class LiveReloadServer {
* @throws IOException in case of I/O errors
*/
public int start() throws IOException {
this.lock.lock();
try {
synchronized (this.monitor) {
Assert.state(!isStarted(), "Server already started");
logger.debug(LogMessage.format("Starting live reload server on port %s", this.port));
this.serverSocket = new ServerSocket(this.port);
@@ -127,9 +119,6 @@ public class LiveReloadServer {
this.listenThread.start();
return localPort;
}
finally {
this.lock.unlock();
}
}
/**
@@ -137,13 +126,9 @@ public class LiveReloadServer {
* @return {@code true} if the server is running
*/
public boolean isStarted() {
this.lock.lock();
try {
synchronized (this.monitor) {
return this.listenThread != null;
}
finally {
this.lock.unlock();
}
}
/**
@@ -178,8 +163,7 @@ public class LiveReloadServer {
* @throws IOException in case of I/O errors
*/
public void stop() throws IOException {
this.lock.lock();
try {
synchronized (this.monitor) {
if (this.listenThread != null) {
closeAllConnections();
try {
@@ -200,31 +184,22 @@ public class LiveReloadServer {
this.serverSocket = null;
}
}
finally {
this.lock.unlock();
}
}
private void closeAllConnections() throws IOException {
this.connectionsLock.lock();
try {
synchronized (this.connections) {
for (Connection connection : this.connections) {
connection.close();
}
}
finally {
this.connectionsLock.unlock();
}
}
/**
* Trigger livereload of all connected clients.
*/
public void triggerReload() {
this.lock.lock();
try {
this.connectionsLock.lock();
try {
synchronized (this.monitor) {
synchronized (this.connections) {
for (Connection connection : this.connections) {
try {
connection.triggerReload();
@@ -234,33 +209,19 @@ public class LiveReloadServer {
}
}
}
finally {
this.connectionsLock.unlock();
}
}
finally {
this.lock.unlock();
}
}
private void addConnection(Connection connection) {
this.connectionsLock.lock();
try {
synchronized (this.connections) {
this.connections.add(connection);
}
finally {
this.connectionsLock.unlock();
}
}
private void removeConnection(Connection connection) {
this.connectionsLock.lock();
try {
synchronized (this.connections) {
this.connections.remove(connection);
}
finally {
this.connectionsLock.unlock();
}
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -29,7 +30,6 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
@@ -92,7 +92,7 @@ public class Restarter {
private final ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles();
private final Map<String, Object> attributes = new ConcurrentHashMap<>();
private final Map<String, Object> attributes = new HashMap<>();
private final BlockingDeque<LeakSafeThread> leakSafeThreads = new LinkedBlockingDeque<>();
@@ -440,11 +440,18 @@ public class Restarter {
}
public Object getOrAddAttribute(String name, final ObjectFactory<?> objectFactory) {
return this.attributes.computeIfAbsent(name, (ignore) -> objectFactory.getObject());
synchronized (this.attributes) {
if (!this.attributes.containsKey(name)) {
this.attributes.put(name, objectFactory.getObject());
}
return this.attributes.get(name);
}
}
public Object removeAttribute(String name) {
return this.attributes.remove(name);
synchronized (this.attributes) {
return this.attributes.remove(name);
}
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,8 +25,6 @@ import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -51,7 +49,7 @@ public class TunnelClient implements SmartInitializingSingleton {
private final TunnelClientListeners listeners = new TunnelClientListeners();
private final Lock lock = new ReentrantLock();
private final Object monitor = new Object();
private final int listenPort;
@@ -68,8 +66,7 @@ public class TunnelClient implements SmartInitializingSingleton {
@Override
public void afterSingletonsInstantiated() {
this.lock.lock();
try {
synchronized (this.monitor) {
if (this.serverThread == null) {
try {
start();
@@ -79,9 +76,6 @@ public class TunnelClient implements SmartInitializingSingleton {
}
}
}
finally {
this.lock.unlock();
}
}
/**
@@ -90,8 +84,7 @@ public class TunnelClient implements SmartInitializingSingleton {
* @throws IOException in case of I/O errors
*/
public int start() throws IOException {
this.lock.lock();
try {
synchronized (this.monitor) {
Assert.state(this.serverThread == null, "Server already started");
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
@@ -101,9 +94,6 @@ public class TunnelClient implements SmartInitializingSingleton {
this.serverThread.start();
return port;
}
finally {
this.lock.unlock();
}
}
/**
@@ -111,8 +101,7 @@ public class TunnelClient implements SmartInitializingSingleton {
* @throws IOException in case of I/O errors
*/
public void stop() throws IOException {
this.lock.lock();
try {
synchronized (this.monitor) {
if (this.serverThread != null) {
this.serverThread.close();
try {
@@ -124,19 +113,12 @@ public class TunnelClient implements SmartInitializingSingleton {
this.serverThread = null;
}
}
finally {
this.lock.unlock();
}
}
protected final ServerThread getServerThread() {
this.lock.lock();
try {
synchronized (this.monitor) {
return this.serverThread;
}
finally {
this.lock.unlock();
}
}
public void addListener(TunnelClientListener listener) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,6 @@ import java.io.IOException;
import java.nio.channels.WritableByteChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.util.Assert;
@@ -38,7 +36,7 @@ public class HttpTunnelPayloadForwarder {
private final Map<Long, HttpTunnelPayload> queue = new HashMap<>();
private final Lock lock = new ReentrantLock();
private final Object monitor = new Object();
private final WritableByteChannel targetChannel;
@@ -54,8 +52,7 @@ public class HttpTunnelPayloadForwarder {
}
public void forward(HttpTunnelPayload payload) throws IOException {
this.lock.lock();
try {
synchronized (this.monitor) {
long seq = payload.getSequence();
if (this.lastRequestSeq != seq - 1) {
Assert.state(this.queue.size() < MAXIMUM_QUEUE_SIZE, "Too many messages queued");
@@ -70,9 +67,6 @@ public class HttpTunnelPayloadForwarder {
forward(queuedItem);
}
}
finally {
this.lock.unlock();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,9 +25,6 @@ import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -125,12 +122,7 @@ public class HttpTunnelServer {
private long disconnectTimeout = DEFAULT_DISCONNECT_TIMEOUT;
/**
* Guards access to {@link #serverThread}.
*/
private final Lock serverThreadLock = new ReentrantLock();
private ServerThread serverThread;
private volatile ServerThread serverThread;
/**
* Creates a new {@link HttpTunnelServer} instance.
@@ -172,8 +164,7 @@ public class HttpTunnelServer {
* @throws IOException in case of I/O errors
*/
protected ServerThread getServerThread() throws IOException {
this.serverThreadLock.lock();
try {
synchronized (this) {
if (this.serverThread == null) {
ByteChannel channel = this.serverConnection.open(this.longPollTimeout);
this.serverThread = new ServerThread(channel);
@@ -181,22 +172,15 @@ public class HttpTunnelServer {
}
return this.serverThread;
}
finally {
this.serverThreadLock.unlock();
}
}
/**
* Called when the server thread exits.
*/
void clearServerThread() {
this.serverThreadLock.lock();
try {
synchronized (this) {
this.serverThread = null;
}
finally {
this.serverThreadLock.unlock();
}
}
/**
@@ -226,13 +210,6 @@ public class HttpTunnelServer {
private final Deque<HttpConnection> httpConnections;
/**
* Guards access to {@link #httpConnections}.
*/
private final Lock httpConnectionsLock = new ReentrantLock();
private final Condition httpConnectionsCondition = this.httpConnectionsLock.newCondition();
private final HttpTunnelPayloadForwarder payloadForwarder;
private boolean closed;
@@ -270,8 +247,7 @@ public class HttpTunnelServer {
while (this.targetServer.isOpen()) {
closeStaleHttpConnections();
ByteBuffer data = HttpTunnelPayload.getPayloadData(this.targetServer);
this.httpConnectionsLock.lock();
try {
synchronized (this.httpConnections) {
if (data != null) {
HttpTunnelPayload payload = new HttpTunnelPayload(this.responseSeq.incrementAndGet(), data);
payload.logIncoming();
@@ -279,20 +255,15 @@ public class HttpTunnelServer {
connection.respond(payload);
}
}
finally {
this.httpConnectionsLock.unlock();
}
}
}
private HttpConnection getOrWaitForHttpConnection() {
this.httpConnectionsLock.lock();
try {
synchronized (this.httpConnections) {
HttpConnection httpConnection = this.httpConnections.pollFirst();
while (httpConnection == null) {
try {
this.httpConnectionsCondition.await(HttpTunnelServer.this.longPollTimeout,
TimeUnit.MILLISECONDS);
this.httpConnections.wait(HttpTunnelServer.this.longPollTimeout);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
@@ -302,14 +273,10 @@ public class HttpTunnelServer {
}
return httpConnection;
}
finally {
this.httpConnectionsLock.unlock();
}
}
private void closeStaleHttpConnections() throws IOException {
this.httpConnectionsLock.lock();
try {
synchronized (this.httpConnections) {
checkNotDisconnected();
Iterator<HttpConnection> iterator = this.httpConnections.iterator();
while (iterator.hasNext()) {
@@ -320,9 +287,6 @@ public class HttpTunnelServer {
}
}
}
finally {
this.httpConnectionsLock.unlock();
}
}
private void checkNotDisconnected() {
@@ -334,8 +298,7 @@ public class HttpTunnelServer {
}
private void closeHttpConnections() {
this.httpConnectionsLock.lock();
try {
synchronized (this.httpConnections) {
while (!this.httpConnections.isEmpty()) {
try {
this.httpConnections.removeFirst().respond(HttpStatus.GONE);
@@ -345,9 +308,6 @@ public class HttpTunnelServer {
}
}
}
finally {
this.httpConnectionsLock.unlock();
}
}
private void closeTargetServer() {
@@ -368,17 +328,13 @@ public class HttpTunnelServer {
if (this.closed) {
httpConnection.respond(HttpStatus.GONE);
}
this.httpConnectionsLock.lock();
try {
synchronized (this.httpConnections) {
while (this.httpConnections.size() > 1) {
this.httpConnections.removeFirst().respond(HttpStatus.TOO_MANY_REQUESTS);
}
this.lastHttpRequestTime = System.currentTimeMillis();
this.httpConnections.addLast(httpConnection);
this.httpConnectionsCondition.signal();
}
finally {
this.httpConnectionsLock.unlock();
this.httpConnections.notify();
}
forwardToTargetServer(httpConnection);
}
@@ -412,10 +368,6 @@ public class HttpTunnelServer {
private volatile boolean complete = false;
private final Lock lock = new ReentrantLock();
private final Condition lockCondition = this.lock.newCondition();
public HttpConnection(ServerHttpRequest request, ServerHttpResponse response) {
this.createTime = System.currentTimeMillis();
this.request = request;
@@ -474,12 +426,8 @@ public class HttpTunnelServer {
if (this.async == null) {
while (!this.complete) {
try {
this.lock.lock();
try {
this.lockCondition.await(1, TimeUnit.SECONDS);
}
finally {
this.lock.unlock();
synchronized (this) {
wait(1000);
}
}
catch (InterruptedException ex) {
@@ -528,13 +476,9 @@ public class HttpTunnelServer {
this.async.complete();
}
else {
this.lock.lock();
try {
synchronized (this) {
this.complete = true;
this.lockCondition.signalAll();
}
finally {
this.lock.unlock();
notifyAll();
}
}
}