Skip to content

Commit

Permalink
Fixes #5083 - Convert synchronized usages to AutoLock.
Browse files Browse the repository at this point in the history
Updates after review.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Jul 29, 2020
1 parent 8d69fc4 commit 089e51f
Show file tree
Hide file tree
Showing 86 changed files with 1,608 additions and 1,158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void introspect(Object o, Object metaInfo)

Class<?> clazz = o.getClass();

try (AutoLock ignored = _lock.lock())
try (AutoLock l = _lock.lock())
{
// Lock to ensure that only 1 thread can be introspecting, and that
// thread must have fully finished generating the products of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean associate(HttpExchange exchange)
{
boolean result = false;
boolean abort = true;
try (AutoLock ignored = _lock.lock())
try (AutoLock l = _lock.lock())
{
if (_exchange == null)
{
Expand All @@ -87,7 +87,7 @@ public boolean associate(HttpExchange exchange)
public boolean disassociate(HttpExchange exchange)
{
boolean result = false;
try (AutoLock ignored = _lock.lock())
try (AutoLock l = _lock.lock())
{
HttpExchange existing = _exchange;
_exchange = null;
Expand All @@ -105,7 +105,10 @@ public boolean disassociate(HttpExchange exchange)

public HttpExchange getHttpExchange()
{
return _lock.runLocked(() -> _exchange);
try (AutoLock l = _lock.lock())
{
return _exchange;
}
}

protected abstract HttpSender getHttpSender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected SendFailure send(HttpChannel channel, HttpExchange exchange)
// the request is associated to the channel and sent.
// Use a counter to support multiplexed requests.
boolean send;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
send = idleTimeoutGuard >= 0;
if (send)
Expand All @@ -113,7 +113,7 @@ protected SendFailure send(HttpChannel channel, HttpExchange exchange)
result = new SendFailure(new HttpRequestException("Could not associate request to connection", request), false);
}

try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
--idleTimeoutGuard;
idleTimeoutStamp = System.nanoTime();
Expand Down Expand Up @@ -252,7 +252,7 @@ private void applyRequestAuthentication(Request request)

public boolean onIdleTimeout(long idleTimeout)
{
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
if (idleTimeoutGuard == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public HttpRequest getRequest()

public Throwable getRequestFailure()
{
return lock.runLocked(() -> requestFailure);
try (AutoLock l = lock.lock())
{
return requestFailure;
}
}

public List<Response.ResponseListener> getResponseListeners()
Expand All @@ -85,7 +88,10 @@ public HttpResponse getResponse()

public Throwable getResponseFailure()
{
return lock.runLocked(() -> responseFailure);
try (AutoLock l = lock.lock())
{
return responseFailure;
}
}

/**
Expand All @@ -99,7 +105,7 @@ boolean associate(HttpChannel channel)
{
boolean result = false;
boolean abort = false;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
// Only associate if the exchange state is initial,
// as the exchange could be already failed.
Expand All @@ -123,7 +129,7 @@ boolean associate(HttpChannel channel)
void disassociate(HttpChannel channel)
{
boolean abort = false;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
if (_channel != channel || requestState != State.TERMINATED || responseState != State.TERMINATED)
abort = true;
Expand All @@ -136,12 +142,18 @@ void disassociate(HttpChannel channel)

private HttpChannel getHttpChannel()
{
return lock.runLocked(() -> _channel);
try (AutoLock l = lock.lock())
{
return _channel;
}
}

public boolean requestComplete(Throwable failure)
{
return lock.runLocked(() -> completeRequest(failure));
try (AutoLock l = lock.lock())
{
return completeRequest(failure);
}
}

private boolean completeRequest(Throwable failure)
Expand All @@ -157,7 +169,10 @@ private boolean completeRequest(Throwable failure)

public boolean responseComplete(Throwable failure)
{
return lock.runLocked(() -> completeResponse(failure));
try (AutoLock l = lock.lock())
{
return completeResponse(failure);
}
}

private boolean completeResponse(Throwable failure)
Expand All @@ -174,7 +189,7 @@ private boolean completeResponse(Throwable failure)
public Result terminateRequest()
{
Result result = null;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
if (requestState == State.COMPLETED)
requestState = State.TERMINATED;
Expand All @@ -191,7 +206,7 @@ public Result terminateRequest()
public Result terminateResponse()
{
Result result = null;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
if (responseState == State.COMPLETED)
responseState = State.TERMINATED;
Expand All @@ -211,7 +226,7 @@ public boolean abort(Throwable failure)
// This will avoid that this exchange can be associated to a channel.
boolean abortRequest;
boolean abortResponse;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
abortRequest = completeRequest(failure);
abortResponse = completeResponse(failure);
Expand Down Expand Up @@ -270,7 +285,7 @@ private void notifyFailureComplete(Throwable failure)

public void resetResponse()
{
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
responseState = State.PENDING;
responseFailure = null;
Expand All @@ -287,7 +302,7 @@ public void proceed(Throwable failure)
@Override
public String toString()
{
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
return String.format("%s@%x req=%s/%s@%h res=%s/%s@%h",
HttpExchange.class.getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void demand(long n)
throw new IllegalArgumentException("Invalid demand " + n);

boolean resume = false;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
demand = MathUtils.cappedAdd(demand, n);
if (stalled)
Expand Down Expand Up @@ -128,12 +128,15 @@ protected long demand()

private long demand(LongUnaryOperator operator)
{
return lock.runLocked(() -> demand = operator.applyAsLong(demand));
try (AutoLock l = lock.lock())
{
return demand = operator.applyAsLong(demand);
}
}

protected boolean hasDemandOrStall()
{
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
stalled = demand <= 0;
return !stalled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ private static int ceilDiv(int a, int b)
@Override
public int getMaxMultiplex()
{
return lock.runLocked(() -> maxMultiplex);
try (AutoLock l = lock.lock())
{
return maxMultiplex;
}
}

@Override
public void setMaxMultiplex(int maxMultiplex)
{
lock.runLocked(() -> this.maxMultiplex = maxMultiplex);
try (AutoLock l = lock.lock())
{
this.maxMultiplex = maxMultiplex;
}
}

@Override
Expand All @@ -99,7 +105,7 @@ public boolean accept(Connection connection)
LOG.debug("Accepted {} {}", accepted, connection);
if (accepted)
{
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
Holder holder = new Holder(connection);
activeConnections.put(connection, holder);
Expand All @@ -113,22 +119,28 @@ public boolean accept(Connection connection)
@Override
public boolean isActive(Connection connection)
{
return lock.runLocked(() -> activeConnections.containsKey(connection));
try (AutoLock l = lock.lock())
{
return activeConnections.containsKey(connection);
}
}

@Override
protected void onCreated(Connection connection)
{
// Use "cold" connections as last.
lock.runLocked(() -> idleConnections.offer(new Holder(connection)));
try (AutoLock l = lock.lock())
{
// Use "cold" connections as last.
idleConnections.offer(new Holder(connection));
}
idle(connection, false);
}

@Override
protected Connection activate()
{
Holder result = null;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
for (Holder holder : activeConnections.values())
{
Expand Down Expand Up @@ -159,7 +171,7 @@ public boolean release(Connection connection)
boolean closed = isClosed();
boolean idle = false;
Holder holder;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
holder = activeConnections.get(connection);
if (holder != null)
Expand Down Expand Up @@ -195,7 +207,7 @@ protected boolean remove(Connection connection, boolean force)
{
boolean activeRemoved = true;
boolean idleRemoved = false;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
Holder holder = activeConnections.remove(connection);
if (holder == null)
Expand Down Expand Up @@ -226,7 +238,7 @@ public void close()
{
super.close();
List<Connection> connections;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
connections = idleConnections.stream().map(holder -> holder.connection).collect(Collectors.toList());
connections.addAll(activeConnections.keySet());
Expand All @@ -239,7 +251,7 @@ public void dump(Appendable out, String indent) throws IOException
{
DumpableCollection active;
DumpableCollection idle;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
active = new DumpableCollection("active", new ArrayList<>(activeConnections.values()));
idle = new DumpableCollection("idle", new ArrayList<>(idleConnections));
Expand All @@ -251,7 +263,7 @@ public void dump(Appendable out, String indent) throws IOException
public boolean sweep()
{
List<Connection> toSweep = new ArrayList<>();
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
activeConnections.values().stream()
.map(holder -> holder.connection)
Expand Down Expand Up @@ -279,7 +291,7 @@ public String toString()
{
int activeSize;
int idleSize;
try (AutoLock ignored = lock.lock())
try (AutoLock l = lock.lock())
{
activeSize = activeConnections.size();
idleSize = idleConnections.size();
Expand Down
Loading

0 comments on commit 089e51f

Please sign in to comment.