blob: a08e61c5e8f599e36b13499e7632f42b513ce240 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}
}
}
|