Commit 9c40d9da authored by Ate Douma's avatar Ate Douma

CMS-10970 Support closing a LockResource by a different thread

parent c07a2585
......@@ -4,3 +4,4 @@
/.project
/.settings
/target
log4j.log
\ No newline at end of file
......@@ -19,15 +19,15 @@ package org.onehippo.cms7.services.lock;
public class Lock {
String lockKey;
private final String lockKey;
String lockOwner;
private final String lockOwner;
String lockThread;
private final String lockThread;
long lockTime;
private final long lockTime;
String status;
private final String status;
public Lock(final String lockKey, final String lockOwner, final String lockThread, final long lockTime,
final String status) {
......
......@@ -82,6 +82,11 @@ import org.onehippo.cms7.services.SingletonService;
@SingletonService
public interface LockManager {
/**
* Maximum number of chars for a {@link #lock(String)} key.
*/
int LOCK_KEY_MAX_LENGTH = 256;
/**
* <p>
* Tries to create a {@link Lock} for {@code key}. The {@code key} is not allowed to exceed 256 chars. If there
......@@ -90,14 +95,15 @@ public interface LockManager {
* </p>
* <p>
* Invoking this method multiple times with the same {@code key} and the same {@link Thread} results in the hold count
* being incremented. To unlock the lock, {@link #unlock(String)} must be invoked an equal amount of times as
* {@link #lock(String)} was invoked and the unlock must be invoked with the same {@link Thread} as the one that
* obtained the {@link Lock}.
* being incremented. To unlock the lock, {@link #unlock(String)} or {@link LockResource#close()} must be invoked
* an equal amount of times as {@link #lock(String)} was invoked and the unlock must be invoked with the
* same {@link Thread} as the one that obtained the {@link Lock}. Note that the {@link LockResource#close()} may
* be invoked by a different {@link Thread}!
* </p>
* <p>
* A lock is released when a successful {@link #unlock(String)} is invoked as many times as
* {@link #lock(String)}. Alternatively, when the {@link LockManager} implementation detects that the Thread
* that held the lock is not live any more, the {@link LockManager} implementation can also release the lock.
* A lock is released when a successful {@link #unlock(String)} or {@link LockResource#close()} is invoked as
* many times as {@link #lock(String)}. Alternatively, when the {@link LockManager} implementation detects that
* the Thread that held the lock is not live any more, the {@link LockManager} implementation can also release the lock.
* </p>
* <p>
* In a clustered setup, a lock will be released (in the database) when it has not been refreshed for more than
......
......@@ -17,6 +17,21 @@ package org.onehippo.cms7.services.lock;
public interface LockResource extends AutoCloseable {
/**
* @return true if this {@link LockResource} has been closed
*/
boolean isClosed();
/**
* Close the {@link LockResource} and unlocks (removes) the lock.
* <p>Note: unlike {@link LockManager#unlock(String)} this may be invoked by another thread, allowing
* delegation of unlocking the lock to another thread</p>
* <p>Warning: while the LockResource may be closed by another thread, the lock itself remains tied to the thread
* creating it!<br/>
* Therefore the thread creating the lock must <em>NOT</em> be terminated before the other thread completes the
* process requiring the lock, as the lock then <em>may</em> expire prematurely!</p>
*/
@Override
void close();
......@@ -25,6 +40,12 @@ public interface LockResource extends AutoCloseable {
*/
Lock getLock();
/**
* @return true if this {@link #getLock()} was created together with this {@link LockResource} instance;
* false if this {@link #getLock()} already was created earlier by the same thread creating this {@link LockResource}.
*/
boolean isNewLock();
/**
* @return the {@link Thread} that holds this {@link LockResource} or {@code null} in case the {@link Thread}
* that created this lock has already stopped and been GC-ed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment