-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
NativeSpinLockMutex.java
48 lines (39 loc) · 1.22 KB
/
NativeSpinLockMutex.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
43
44
45
46
47
48
package nallar.tickthreading.util.concurrent;
import nallar.unsafe.UnsafeAccess;
import sun.misc.Unsafe;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public final class NativeSpinLockMutex implements Lock {
private static final Unsafe $ = UnsafeAccess.$;
@Override
public synchronized void lock() {
//noinspection StatementWithEmptyBody
while (!$.tryMonitorEnter(this)) {
// Spin lock.
// TODO: Could we instead work on something else here?
// Avoids overhead of OS scheduling without doing nothing
// might not be worth the effort to get it working correctly
}
}
@Override
public synchronized void unlock() {
$.monitorExit(this);
}
@Override
public synchronized boolean tryLock() {
return $.tryMonitorEnter(this);
}
@Override
public synchronized void lockInterruptibly() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}