Skip to content

Commit 61a032b

Browse files
committed
Async: New lock type.
Extremely useful to guarantee safety :^)
1 parent 2e2b504 commit 61a032b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2025 Casterlabs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
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.
10+
See the License for the specific language governing permissions and limitations under the License.
11+
*/
12+
package co.casterlabs.commons.async;
13+
14+
import java.util.concurrent.locks.ReentrantLock;
15+
16+
import lombok.AllArgsConstructor;
17+
18+
@AllArgsConstructor
19+
public class LockableResource<T> {
20+
private final ReentrantLock lock = new ReentrantLock();
21+
22+
private volatile T resource;
23+
24+
public void set(T resource) {
25+
this.lock.lock();
26+
try {
27+
this.resource = resource;
28+
} finally {
29+
this.lock.unlock();
30+
}
31+
}
32+
33+
public T acquire() {
34+
this.lock.lock();
35+
return this.resource;
36+
}
37+
38+
public T acquireUnsafe() {
39+
return this.resource;
40+
}
41+
42+
public void release() {
43+
this.lock.unlock();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)