Fixes lock
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -16,7 +17,6 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
|
||||
import co.aikar.timings.Timing;
|
||||
import co.aikar.timings.Timings;
|
||||
import io.akarin.api.internal.utils.ReentrantSpinningLock;
|
||||
import io.akarin.server.core.AkarinGlobalConfig;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
||||
@@ -94,7 +94,7 @@ public abstract class Akari {
|
||||
return serverVersion + " (MC: " + MinecraftServer.getServer().getVersion() + ")";
|
||||
}
|
||||
|
||||
public static final ReentrantSpinningLock eventLock = new ReentrantSpinningLock();
|
||||
public static final ReentrantLock eventLock = new ReentrantLock();
|
||||
|
||||
/*
|
||||
* Timings
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package io.akarin.api.internal.utils;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ReentrantSpinningLock {
|
||||
private final AtomicBoolean attemptLock = new AtomicBoolean(false);
|
||||
private final AtomicInteger reentrantLocks = new AtomicInteger(0);
|
||||
private volatile long heldThreadId = 0;
|
||||
|
||||
public void lock() {
|
||||
long currentThreadId = Thread.currentThread().getId();
|
||||
if (heldThreadId == currentThreadId) {
|
||||
reentrantLocks.getAndIncrement(); // Reentrant
|
||||
} else {
|
||||
while (heldThreadId != 0) ; // The current thread is spinning here
|
||||
}
|
||||
tryLock(currentThreadId);
|
||||
}
|
||||
|
||||
private void tryLock(long currentThreadId) {
|
||||
attemptLock.getAndSet(true); // In case acquire one lock concurrently
|
||||
heldThreadId = currentThreadId;
|
||||
attemptLock.set(false);
|
||||
}
|
||||
|
||||
public void unlock() {
|
||||
if (reentrantLocks.get() == 0 || reentrantLocks.getAndDecrement() == 1) heldThreadId = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user