diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-06-28 13:37:02 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-06-28 13:37:02 +0200 |
commit | 3a2c9c9e1d9b97f3ca277408f4103ce1b7ec8860 (patch) | |
tree | 376f043f6e0958dad168084277526d096a28b01f /vespajlib | |
parent | 1f5b3720c323474ef5a30d3de124c990ddec6009 (diff) |
Add locking API
Diffstat (limited to 'vespajlib')
3 files changed, 91 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/Lock.java b/vespajlib/src/main/java/com/yahoo/concurrent/Lock.java new file mode 100644 index 00000000000..39ccab7034e --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Lock.java @@ -0,0 +1,23 @@ +package com.yahoo.vespa.hosted.controller.concurrent; + +import java.util.concurrent.locks.ReentrantLock; + +/** + * An acquired lock which is released on close + * + * @author bratseth + */ +public final class Lock implements AutoCloseable { + + private final ReentrantLock wrappedLock; + + Lock(ReentrantLock wrappedLock) { + this.wrappedLock = wrappedLock; + } + + /** Releases this lock */ + public void close() { + wrappedLock.unlock(); + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java new file mode 100644 index 00000000000..a08e61c5e8f --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java @@ -0,0 +1,54 @@ +package com.yahoo.vespa.hosted.controller.concurrent; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Holds a map of locks indexed on keys of a given type. + * This is suitable in cases where exclusive access should be granted to any one of a set of keyed objects and + * there is a finite collection of keyed objects. + * + * The returned locks are reentrant (i.e the owning thread may call lock multiple times) and auto-closable. + * + * Typical use is + * <code> + * try (Lock lock = locks.lock(id)) { + * exclusive use of the object with key id + * } + * </code> + * + * @author bratseth + */ +public class Locks<TYPE> { + + private final Map<TYPE, ReentrantLock> locks = new ConcurrentHashMap<>(); + + private final long timeoutMs; + + public Locks(int timeout, TimeUnit timeoutUnit) { + timeoutMs = timeoutUnit.toMillis(timeout); + } + + /** + * Locks key. This will block until the key is acquired. + * Users of this <b>must</b> close any lock acquired. + * + * @param key the key to lock + * @return the acquired lock + * @throws TimeoutException if the lock could not be acquired within the timeout + */ + public Lock lock(TYPE key) { + try { + ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true)); + boolean acquired = lock.tryLock(timeoutMs, TimeUnit.MILLISECONDS); + if ( ! acquired) + throw new TimeoutException("Timed out waiting for the lock to " + key); + return new Lock(lock); + } catch (InterruptedException e) { + throw new RuntimeException("Interrupted while waiting for lock of " + key); + } + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/TimeoutException.java b/vespajlib/src/main/java/com/yahoo/concurrent/TimeoutException.java new file mode 100644 index 00000000000..cd2cfacf902 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/TimeoutException.java @@ -0,0 +1,14 @@ +package com.yahoo.vespa.hosted.controller.concurrent; + +/** + * Throws on timeout + * + * @author bratseth + */ +public class TimeoutException extends RuntimeException { + + public TimeoutException(String message) { + super(message); + } + +} |