diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2017-09-22 20:14:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-22 20:14:17 +0200 |
commit | a8d6f5c76bb7e9b18bcc16f6dd8f4e9b09d2ba4f (patch) | |
tree | 4738225b5fd79d5e780775a10f52a72273b0a192 /vespajlib/src/main/java/com/yahoo/concurrent/Locks.java | |
parent | 33720727a40c18ba3be652bdaba7b0809eb3d606 (diff) | |
parent | 5d049e3cff73ccc7b6fc0e7259db0f3238592997 (diff) |
Merge pull request #3507 from vespa-engine/revert-3464-freva/node-admin-shutdown
Revert "Freva/node admin shutdown"
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/concurrent/Locks.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/concurrent/Locks.java | 63 |
1 files changed, 63 insertions, 0 deletions
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..ab167392329 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java @@ -0,0 +1,63 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.concurrent; + +import com.google.common.util.concurrent.UncheckedTimeoutException; + +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; + + /** Create locks with a default timeout */ + public Locks(long timeout, TimeUnit timeoutUnit) { + timeoutMs = timeoutUnit.toMillis(timeout); + } + + /** Locks key. This will block until the key is acquired or the default timeout is reached. */ + public Lock lock(TYPE key) { + return lock(key, timeoutMs, TimeUnit.MILLISECONDS); + } + + /** + * Locks key. This will block until the key is acquired or the timeout is reached. + * Users of this <b>must</b> close any lock acquired. + * + * @param key the key to lock + * @return the acquired lock + * @throws UncheckedTimeoutException if the lock could not be acquired within the timeout + */ + public Lock lock(TYPE key, long timeout, TimeUnit timeoutUnit) { + try { + ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true)); + boolean acquired = lock.tryLock(timeout, timeoutUnit); + if ( ! acquired) + throw new UncheckedTimeoutException("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); + } + } + +} |