-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
NativeMutex.java
42 lines (33 loc) · 916 Bytes
/
NativeMutex.java
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
package me.nallar.tickthreading.util.concurrent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import me.nallar.unsafe.UnsafeAccess;
import sun.misc.Unsafe;
public final class NativeMutex implements Lock {
private static final Unsafe $ = UnsafeAccess.$;
@Override
public void lockInterruptibly() throws InterruptedException {
$.monitorEnter(this);
}
@Override
public void lock() {
$.monitorEnter(this);
}
@Override
public void unlock() {
$.monitorExit(this);
}
@Override
public boolean tryLock() {
return $.tryMonitorEnter(this);
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}