Fixes #1836 - Review Locker.

Made Locker a simpler wrapper around ReentrantLock.
Deprecated lockIfNotHeld() and replaced its usages.
This commit is contained in:
Simone Bordet 2017-09-21 17:20:51 +02:00
parent d4cd8c13e9
commit 7768a781be
3 changed files with 65 additions and 60 deletions

View File

@ -289,7 +289,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
protected void interruptAcceptors() protected void interruptAcceptors()
{ {
try (Locker.Lock lock = _locker.lockIfNotHeld()) try (Locker.Lock lock = _locker.lock())
{ {
for (Thread thread : _acceptors) for (Thread thread : _acceptors)
{ {
@ -388,7 +388,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void addConnectionFactory(ConnectionFactory factory) public void addConnectionFactory(ConnectionFactory factory)
{ {
try (Locker.Lock lock = _locker.lockIfNotHeld()) try (Locker.Lock lock = _locker.lock())
{ {
Set<ConnectionFactory> to_remove = new HashSet<>(); Set<ConnectionFactory> to_remove = new HashSet<>();
for (String key:factory.getProtocols()) for (String key:factory.getProtocols())

View File

@ -137,7 +137,7 @@ public class Session implements SessionHandler.SessionIf
// True if: // True if:
// 1. the session is still valid // 1. the session is still valid
// BUT if passivated out to disk, do we really want this timer to keep going off? // BUT if passivated out to disk, do we really want this timer to keep going off?
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return isValid() && isResident(); return isValid() && isResident();
} }
@ -198,7 +198,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
public long getRequests() public long getRequests()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _requests; return _requests;
} }
@ -217,7 +217,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
protected void cookieSet() protected void cookieSet()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
_sessionData.setCookieSet(_sessionData.getAccessed()); _sessionData.setCookieSet(_sessionData.getAccessed());
} }
@ -225,7 +225,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected boolean access(long time) protected boolean access(long time)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
if (!isValid()) if (!isValid())
return false; return false;
@ -249,7 +249,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected void complete() protected void complete()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
_requests--; _requests--;
} }
@ -265,7 +265,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
protected boolean isExpiredAt(long time) protected boolean isExpiredAt(long time)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _sessionData.isExpiredAt(time); return _sessionData.isExpiredAt(time);
} }
@ -281,7 +281,7 @@ public class Session implements SessionHandler.SessionIf
protected boolean isIdleLongerThan (int sec) protected boolean isIdleLongerThan (int sec)
{ {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return ((_sessionData.getAccessed() + (sec*1000)) <= now); return ((_sessionData.getAccessed() + (sec*1000)) <= now);
} }
@ -384,7 +384,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public boolean isValid() public boolean isValid()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _state==State.VALID; return _state==State.VALID;
} }
@ -394,7 +394,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
public long getCookieSetTime() public long getCookieSetTime()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _sessionData.getCookieSet(); return _sessionData.getCookieSet();
} }
@ -405,7 +405,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public long getCreationTime() throws IllegalStateException public long getCreationTime() throws IllegalStateException
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForRead(); checkValidForRead();
return _sessionData.getCreated(); return _sessionData.getCreated();
@ -420,7 +420,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public String getId() public String getId()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _sessionData.getId(); return _sessionData.getId();
} }
@ -450,7 +450,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public long getLastAccessedTime() public long getLastAccessedTime()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _sessionData.getLastAccessed(); return _sessionData.getLastAccessed();
} }
@ -473,7 +473,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public void setMaxInactiveInterval(int secs) public void setMaxInactiveInterval(int secs)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
_sessionData.setMaxInactiveMs((long)secs*1000L); _sessionData.setMaxInactiveMs((long)secs*1000L);
_sessionData.calcAndSetExpiry(); _sessionData.calcAndSetExpiry();
@ -496,7 +496,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
public void updateInactivityTimer () public void updateInactivityTimer ()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
if (LOG.isDebugEnabled())LOG.debug("updateInactivityTimer"); if (LOG.isDebugEnabled())LOG.debug("updateInactivityTimer");
@ -556,7 +556,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
public void stopInactivityTimer () public void stopInactivityTimer ()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
if (_sessionInactivityTimer != null) if (_sessionInactivityTimer != null)
{ {
@ -573,7 +573,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public int getMaxInactiveInterval() public int getMaxInactiveInterval()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return (int)(_sessionData.getMaxInactiveMs()/1000); return (int)(_sessionData.getMaxInactiveMs()/1000);
} }
@ -653,7 +653,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public Object getAttribute(String name) public Object getAttribute(String name)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForRead(); checkValidForRead();
return _sessionData.getAttribute(name); return _sessionData.getAttribute(name);
@ -667,7 +667,7 @@ public class Session implements SessionHandler.SessionIf
@Deprecated @Deprecated
public Object getValue(String name) public Object getValue(String name)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _sessionData.getAttribute(name); return _sessionData.getAttribute(name);
} }
@ -679,7 +679,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public Enumeration<String> getAttributeNames() public Enumeration<String> getAttributeNames()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForRead(); checkValidForRead();
final Iterator<String> itor = _sessionData.getKeys().iterator(); final Iterator<String> itor = _sessionData.getKeys().iterator();
@ -730,7 +730,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public String[] getValueNames() throws IllegalStateException public String[] getValueNames() throws IllegalStateException
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForRead(); checkValidForRead();
Iterator<String> itor = _sessionData.getKeys().iterator(); Iterator<String> itor = _sessionData.getKeys().iterator();
@ -751,7 +751,7 @@ public class Session implements SessionHandler.SessionIf
public void setAttribute(String name, Object value) public void setAttribute(String name, Object value)
{ {
Object old=null; Object old=null;
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
//if session is not valid, don't accept the set //if session is not valid, don't accept the set
checkValidForWrite(); checkValidForWrite();
@ -812,7 +812,7 @@ public class Session implements SessionHandler.SessionIf
String id = null; String id = null;
String extendedId = null; String extendedId = null;
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForWrite(); //don't renew id on a session that is not valid checkValidForWrite(); //don't renew id on a session that is not valid
id = _sessionData.getId(); //grab the values as they are now id = _sessionData.getId(); //grab the values as they are now
@ -820,7 +820,7 @@ public class Session implements SessionHandler.SessionIf
} }
String newId = _handler._sessionIdManager.renewSessionId(id, extendedId, request); String newId = _handler._sessionIdManager.renewSessionId(id, extendedId, request);
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForWrite(); checkValidForWrite();
_sessionData.setId(newId); _sessionData.setId(newId);
@ -877,7 +877,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
public Lock lockIfNotHeld () public Lock lockIfNotHeld ()
{ {
return _lock.lockIfNotHeld(); return _lock.lock();
} }
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
@ -888,7 +888,7 @@ public class Session implements SessionHandler.SessionIf
{ {
boolean result = false; boolean result = false;
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
switch (_state) switch (_state)
{ {
@ -934,7 +934,7 @@ public class Session implements SessionHandler.SessionIf
*/ */
protected void finishInvalidate() throws IllegalStateException protected void finishInvalidate() throws IllegalStateException
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
try try
{ {
@ -970,7 +970,7 @@ public class Session implements SessionHandler.SessionIf
@Override @Override
public boolean isNew() throws IllegalStateException public boolean isNew() throws IllegalStateException
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
checkValidForRead(); checkValidForRead();
return _newSession; return _newSession;
@ -982,7 +982,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
public void setIdChanged(boolean changed) public void setIdChanged(boolean changed)
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
_idChanged=changed; _idChanged=changed;
} }
@ -992,7 +992,7 @@ public class Session implements SessionHandler.SessionIf
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
public boolean isIdChanged () public boolean isIdChanged ()
{ {
try (Lock lock = _lock.lockIfNotHeld()) try (Lock lock = _lock.lock())
{ {
return _idChanged; return _idChanged;
} }

View File

@ -22,10 +22,10 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
/** /**
* Convenience Lock Wrapper. * <p>Convenience auto closeable {@link java.util.concurrent.locks.ReentrantLock} wrapper.</p>
* *
* <pre> * <pre>
* try(Locker.Lock lock = locker.lock()) * try (Locker.Lock lock = locker.lock())
* { * {
* // something * // something
* } * }
@ -33,44 +33,49 @@ import java.util.concurrent.locks.ReentrantLock;
*/ */
public class Locker public class Locker
{ {
private static final Lock LOCKED = new Lock();
private final ReentrantLock _lock = new ReentrantLock(); private final ReentrantLock _lock = new ReentrantLock();
private final Lock _unlock = new UnLock(); private final Lock _unlock = new Lock();
public Locker()
{
}
/**
* <p>Acquires the lock.</p>
*
* @return the lock to unlock
*/
public Lock lock() public Lock lock()
{ {
if (_lock.isHeldByCurrentThread())
throw new IllegalStateException("Locker is not reentrant");
_lock.lock(); _lock.lock();
return _unlock; return _unlock;
} }
public Lock lockIfNotHeld () /**
* @deprecated use {@link #lock()} instead
*/
@Deprecated
public Lock lockIfNotHeld()
{ {
if (_lock.isHeldByCurrentThread()) return lock();
return LOCKED;
_lock.lock();
return _unlock;
} }
/**
* @return whether this lock has been acquired
*/
public boolean isLocked() public boolean isLocked()
{ {
return _lock.isLocked(); return _lock.isLocked();
} }
public static class Lock implements AutoCloseable /**
* @return a {@link Condition} associated with this lock
*/
public Condition newCondition()
{ {
@Override return _lock.newCondition();
public void close()
{
}
} }
public class UnLock extends Lock /**
* <p>The unlocker object that unlocks when it is closed.</p>
*/
public class Lock implements AutoCloseable
{ {
@Override @Override
public void close() public void close()
@ -79,8 +84,8 @@ public class Locker
} }
} }
public Condition newCondition() @Deprecated
public class UnLock extends Lock
{ {
return _lock.newCondition();
} }
} }