mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 21:17:53 +01:00
Simplify acquisition
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
acb8634b4b
commit
f48199397a
@ -84,8 +84,7 @@ public interface Acquirable<T> {
|
||||
* @see #sync(Consumer) for auto-closeable capability
|
||||
*/
|
||||
default @NotNull Acquired<T> lock() {
|
||||
final Optional<T> optional = local();
|
||||
return optional.map(Acquired::local).orElseGet(() -> Acquired.locked(this));
|
||||
return new Acquired<>(unwrap(), getHandler().getTickThread());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,32 +37,37 @@ final class AcquirableImpl<T> implements Acquirable<T> {
|
||||
return handler;
|
||||
}
|
||||
|
||||
static @Nullable ReentrantLock enter(@Nullable Thread currentThread, @Nullable TickThread elementThread) {
|
||||
static @Nullable ReentrantLock enter(@NotNull Thread currentThread, @Nullable TickThread elementThread) {
|
||||
if (elementThread == null) return null;
|
||||
if (currentThread == elementThread) return null;
|
||||
final ReentrantLock currentLock = currentThread instanceof TickThread ? ((TickThread) currentThread).getLock() : null;
|
||||
final ReentrantLock targetLock = elementThread.getLock();
|
||||
if (targetLock.isHeldByCurrentThread()) return null;
|
||||
|
||||
// Monitoring
|
||||
final long time = System.nanoTime();
|
||||
|
||||
ReentrantLock currentLock;
|
||||
{
|
||||
final ReentrantLock lock = currentThread instanceof TickThread ?
|
||||
((TickThread) currentThread).getLock() : null;
|
||||
currentLock = lock != null && lock.isHeldByCurrentThread() ? lock : null;
|
||||
// Enter the target thread
|
||||
// TODO reduce global lock scope
|
||||
if (currentLock != null) {
|
||||
while (!GLOBAL_LOCK.tryLock()) {
|
||||
currentLock.unlock();
|
||||
currentLock.lock();
|
||||
}
|
||||
} else {
|
||||
GLOBAL_LOCK.lock();
|
||||
}
|
||||
if (currentLock != null) currentLock.unlock();
|
||||
GLOBAL_LOCK.lock();
|
||||
if (currentLock != null) currentLock.lock();
|
||||
|
||||
final ReentrantLock lock = elementThread != null ? elementThread.getLock() : null;
|
||||
final boolean acquired = lock == null || lock.isHeldByCurrentThread();
|
||||
if (!acquired) lock.lock();
|
||||
targetLock.lock();
|
||||
|
||||
// Monitoring
|
||||
AcquirableImpl.WAIT_COUNTER_NANO.addAndGet(System.nanoTime() - time);
|
||||
|
||||
return !acquired ? lock : null;
|
||||
WAIT_COUNTER_NANO.addAndGet(System.nanoTime() - time);
|
||||
return targetLock;
|
||||
}
|
||||
|
||||
static void leave(@Nullable ReentrantLock lock) {
|
||||
if (lock != null) lock.unlock();
|
||||
GLOBAL_LOCK.unlock();
|
||||
if (lock != null) {
|
||||
lock.unlock();
|
||||
GLOBAL_LOCK.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,27 +8,12 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public final class Acquired<T> {
|
||||
private final T value;
|
||||
|
||||
private final boolean locked;
|
||||
private final ReentrantLock lock;
|
||||
|
||||
private boolean unlocked;
|
||||
|
||||
static <T> Acquired<T> local(@NotNull T value) {
|
||||
return new Acquired<>(value, false, null, null);
|
||||
}
|
||||
|
||||
static <T> Acquired<T> locked(@NotNull Acquirable<T> acquirable) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread tickThread = acquirable.getHandler().getTickThread();
|
||||
return new Acquired<>(acquirable.unwrap(), true, currentThread, tickThread);
|
||||
}
|
||||
|
||||
private Acquired(@NotNull T value,
|
||||
boolean locked, Thread currentThread, TickThread tickThread) {
|
||||
Acquired(T value, TickThread tickThread) {
|
||||
this.value = value;
|
||||
this.locked = locked;
|
||||
this.lock = locked ? AcquirableImpl.enter(currentThread, tickThread) : null;
|
||||
this.lock = AcquirableImpl.enter(Thread.currentThread(), tickThread);
|
||||
}
|
||||
|
||||
public @NotNull T get() {
|
||||
@ -39,7 +24,6 @@ public final class Acquired<T> {
|
||||
public void unlock() {
|
||||
checkLock();
|
||||
this.unlocked = true;
|
||||
if (!locked) return;
|
||||
AcquirableImpl.leave(lock);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user