diff options
Diffstat (limited to 'vespajlib/src/main/java/com')
7 files changed, 85 insertions, 8 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/CopyOnWriteHashMap.java b/vespajlib/src/main/java/com/yahoo/concurrent/CopyOnWriteHashMap.java index 8b6392285c5..aff2b6af2f0 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/CopyOnWriteHashMap.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/CopyOnWriteHashMap.java @@ -7,13 +7,12 @@ import java.util.Map; import java.util.Set; /** - * <p>This is a thread hash map for small collections that are stable once built. Until it is stable there will be a + * This is a thread hash map for small collections that are stable once built. Until it is stable there will be a * race among all threads missing something in the map. They will then clone the map add the missing stuff and then put * it back as active again. Here are no locks, but the cost is that inserts will happen a lot more than necessary. The - * map reference is volatile, but on most multicpu machines that has no cost unless modified.</p> + * map reference is volatile, but on most multicpu machines that has no cost unless modified. * * @author baldersheim - * @since 5.2 */ public class CopyOnWriteHashMap<K, V> implements Map<K, V> { diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/DaemonThreadFactory.java b/vespajlib/src/main/java/com/yahoo/concurrent/DaemonThreadFactory.java index 92b35c4cc0b..4c15b6e2365 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/DaemonThreadFactory.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/DaemonThreadFactory.java @@ -8,7 +8,7 @@ import java.util.concurrent.ThreadFactory; * A simple thread factory that decorates <code>Executors.defaultThreadFactory()</code> * and sets all created threads to be daemon threads. * - * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a> + * @author Einar M R Rosenvinge */ public class DaemonThreadFactory implements ThreadFactory { private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/EventBarrier.java b/vespajlib/src/main/java/com/yahoo/concurrent/EventBarrier.java index 79402d928fc..8130c369b75 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/EventBarrier.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/EventBarrier.java @@ -16,8 +16,8 @@ import java.util.List; * algorithm would be to make a thread wait for events happening in * other threads to complete. * - * @author <a href="mailto:havardpe@yahoo-inc.com">Haavard Pettersen</a> - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Haavard Pettersen + * @author Simon Thoresen */ public class EventBarrier { 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..ca3707f84d6 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Lock.java @@ -0,0 +1,23 @@ +package com.yahoo.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..fcac7f31356 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java @@ -0,0 +1,56 @@ +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; + + 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 UncheckedTimeoutException 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 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); + } + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/path/Path.java b/vespajlib/src/main/java/com/yahoo/path/Path.java index 90c73f7f8c0..7389ca2af54 100644 --- a/vespajlib/src/main/java/com/yahoo/path/Path.java +++ b/vespajlib/src/main/java/com/yahoo/path/Path.java @@ -13,7 +13,6 @@ import java.util.List; * Represents a path represented by a list of elements. Immutable * * @author lulf - * @since 5.1 */ @Beta public final class Path { diff --git a/vespajlib/src/main/java/com/yahoo/transaction/Mutex.java b/vespajlib/src/main/java/com/yahoo/transaction/Mutex.java index 991e1473be8..f258aa2961e 100644 --- a/vespajlib/src/main/java/com/yahoo/transaction/Mutex.java +++ b/vespajlib/src/main/java/com/yahoo/transaction/Mutex.java @@ -8,6 +8,6 @@ package com.yahoo.transaction; */ public interface Mutex extends AutoCloseable { - public void close(); + void close(); } |