-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCountingLock.java
More file actions
124 lines (110 loc) · 3.37 KB
/
CountingLock.java
File metadata and controls
124 lines (110 loc) · 3.37 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2009 Marc-Olaf Jaschke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.jkeylockmanager.manager.implementation.lockstripe;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import de.jkeylockmanager.manager.exception.KeyLockManagerInterruptedException;
import de.jkeylockmanager.manager.exception.KeyLockManagerTimeoutException;
/**
*
* Special lock implementation for internal use in this package only.
*
* {@link CountingLock} adds a counter for counting its uses.
*
* The counting functionality is not thread safe and so it is essential to use
* the following methods only in the scope of a shared lock:
*
* {@link #decrementUses()}, {@link #incrementUses()}, {@link #isUsed()}
*
*
* @see ReentrantLock
*
* @author Marc-Olaf Jaschke
*
*/
final class CountingLock {
private final ReentrantLock delegate = new ReentrantLock();
private final long lockTimeout;
private final TimeUnit lockTimeoutUnit;
private long uses = 0;
/**
* Creates a new instance of {@link CountingLock} with a usage counter set
* to zero.
*
* @param lockTimeout
* - the time to wait for a lock before an Exception is thrown -
* must be greater than 0
* @param lockTimeoutUnit
* - the unit for lockTimeout - must not be null
*/
CountingLock(final long lockTimeout, final TimeUnit lockTimeoutUnit) {
assert lockTimeout > 0 : "contract broken: lockTimeout > 0";
assert lockTimeoutUnit != null : "contract broken: lockTimeoutUnit != null";
this.lockTimeout = lockTimeout;
this.lockTimeoutUnit = lockTimeoutUnit;
}
/**
* Decrements the usage counter. See class commentary for thread safety!
*/
void decrementUses() {
uses--;
}
/**
* Delegates to {@link ReentrantLock#getQueueLength()}
*/
int getQueueLength() {
return delegate.getQueueLength();
}
/**
* Increments the usage counter. See class commentary for thread safety!
*/
void incrementUses() {
uses++;
}
/**
* See class commentary for thread safety!
*
* @return true, if the usage counter is zero
*/
boolean isUsed() {
return uses != 0;
}
/**
* Decorates {@link ReentrantLock#tryLock(long, TimeUnit)}.
*
* @throws KeyLockManagerInterruptedException
* if the current thread becomes interrupted while waiting for
* the lock
* @throws KeyLockManagerTimeoutException
* if the instance wide waiting time is exceeded
*/
void tryLock() {
try {
if (!delegate.tryLock(lockTimeout, lockTimeoutUnit)) {
throw new KeyLockManagerTimeoutException(lockTimeout, lockTimeoutUnit);
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new KeyLockManagerInterruptedException();
}
}
/**
* Delegates to {@link java.util.concurrent.locks.ReentrantLock#unlock()}
*/
void unlock() {
delegate.unlock();
}
}