From 5d049e3cff73ccc7b6fc0e7259db0f3238592997 Mon Sep 17 00:00:00 2001 From: Valerij Fredriksen Date: Fri, 22 Sep 2017 20:13:39 +0200 Subject: Revert "Freva/node admin shutdown" --- .../src/main/java/com/yahoo/concurrent/Lock.java | 24 +++++++++ .../src/main/java/com/yahoo/concurrent/Locks.java | 63 ++++++++++++++++++++++ .../main/java/com/yahoo/concurrent/lock/Lock.java | 24 --------- .../java/com/yahoo/concurrent/lock/Locking.java | 25 --------- .../main/java/com/yahoo/concurrent/lock/Locks.java | 63 ---------------------- .../com/yahoo/concurrent/lock/package-info.java | 5 -- 6 files changed, 87 insertions(+), 117 deletions(-) create mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/Lock.java create mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/Locks.java delete mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/lock/Lock.java delete mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/lock/Locking.java delete mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/lock/Locks.java delete mode 100644 vespajlib/src/main/java/com/yahoo/concurrent/lock/package-info.java (limited to 'vespajlib') 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..3b1919a987d --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Lock.java @@ -0,0 +1,24 @@ +// 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 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..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 + * + * try (Lock lock = locks.lock(id)) { + * exclusive use of the object with key id + * } + * + * + * @author bratseth + */ +public class Locks { + + private final Map 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 must 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); + } + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/lock/Lock.java b/vespajlib/src/main/java/com/yahoo/concurrent/lock/Lock.java deleted file mode 100644 index 1273a298592..00000000000 --- a/vespajlib/src/main/java/com/yahoo/concurrent/lock/Lock.java +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.concurrent.lock; - -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/lock/Locking.java b/vespajlib/src/main/java/com/yahoo/concurrent/lock/Locking.java deleted file mode 100644 index 4d9f45e959e..00000000000 --- a/vespajlib/src/main/java/com/yahoo/concurrent/lock/Locking.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.yahoo.concurrent.lock; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; - -/** - * @author valerijf - */ -public class Locking { - private final Map, ReentrantLock> locks = new ConcurrentHashMap<>(); - - /** - * Locks class. This will block until the lock is acquired. - * Users of this must close any lock acquired. - * - * @param key the key to lock - * @return the acquired lock - */ - public Lock lock(Class key) { - ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true)); - lock.lock(); - return new Lock(lock); - } -} diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/lock/Locks.java b/vespajlib/src/main/java/com/yahoo/concurrent/lock/Locks.java deleted file mode 100644 index 0308b26c903..00000000000 --- a/vespajlib/src/main/java/com/yahoo/concurrent/lock/Locks.java +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.concurrent.lock; - -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 - * - * try (Lock lock = locks.lock(id)) { - * exclusive use of the object with key id - * } - * - * - * @author bratseth - */ -public class Locks { - - private final Map 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 must 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); - } - } - -} diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/lock/package-info.java b/vespajlib/src/main/java/com/yahoo/concurrent/lock/package-info.java deleted file mode 100644 index 326124e600f..00000000000 --- a/vespajlib/src/main/java/com/yahoo/concurrent/lock/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -@ExportPackage -package com.yahoo.concurrent.lock; - -import com.yahoo.osgi.annotation.ExportPackage; -- cgit v1.2.3