From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Fri, 3 Feb 2023 23:01:51 +0100 Subject: [PATCH] Thread-aware lock utility License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html) Gale - https://galemc.org diff --git a/src/main/java/org/galemc/gale/concurrent/ThreadAwareNonReentrantLock.java b/src/main/java/org/galemc/gale/concurrent/ThreadAwareNonReentrantLock.java new file mode 100644 index 0000000000000000000000000000000000000000..8c14d90d1a907adf994070cbe5d62f1fbfd8d9c8 --- /dev/null +++ b/src/main/java/org/galemc/gale/concurrent/ThreadAwareNonReentrantLock.java @@ -0,0 +1,113 @@ +// Gale - thread-aware lock utility + +package org.galemc.gale.concurrent; + +import net.minecraft.server.MinecraftServer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; + +/** + * A wrapper for a non-reentrant {@link Lock}, that is aware when the thread that already holds this lock + * is trying to acquire it again (for example by calling {@link #tryLock}), and throws an exception in that case. + *
+ * This is useful for debugging purposes when a {@link Lock} is not supposed to be reachable + * from any code that is executed while the lock is being held. + */ +public class ThreadAwareNonReentrantLock implements CheckableLock { + + private final CheckableLock innerLock; + + /** + * The {@link Thread} that currently holds this lock, or null if no thread currently holds this lock. + */ + private volatile @Nullable Thread currentHoldingThread; + + public ThreadAwareNonReentrantLock(CheckableLock innerLock) { + this.innerLock = innerLock; + } + + @Override + public void lock() { + var currentThread = Thread.currentThread(); + if (this.currentHoldingThread == currentThread) { + IllegalStateException exception = new IllegalStateException("Called lock() on a " + this.getClass().getSimpleName() + " from the thread (" + currentThread + ") that was already holding it"); + MinecraftServer.LOGGER.error(exception.getMessage() + ", at:"); + exception.printStackTrace(); + throw exception; + } + this.innerLock.lock(); + this.currentHoldingThread = currentThread; + } + + @Override + public void lockInterruptibly() throws InterruptedException { + var currentThread = Thread.currentThread(); + if (this.currentHoldingThread == currentThread) { + IllegalStateException exception = new IllegalStateException("Called lockInterruptibly() on a " + this.getClass().getSimpleName() + " from the thread (" + currentThread + ") that was already holding it"); + MinecraftServer.LOGGER.error(exception.getMessage() + ", at:"); + exception.printStackTrace(); + throw exception; + } + this.innerLock.lockInterruptibly(); + this.currentHoldingThread = currentThread; + } + + @Override + public boolean tryLock() { + var currentThread = Thread.currentThread(); + if (this.currentHoldingThread == currentThread) { + IllegalStateException exception = new IllegalStateException("Called tryLock() on a " + this.getClass().getSimpleName() + " from the thread (" + currentThread + ") that was already holding it"); + MinecraftServer.LOGGER.error(exception.getMessage() + ", at:"); + exception.printStackTrace(); + throw exception; + } + if (this.innerLock.tryLock()) { + this.currentHoldingThread = currentThread; + return true; + } + return false; + } + + @Override + public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException { + var currentThread = Thread.currentThread(); + if (this.currentHoldingThread == currentThread) { + IllegalStateException exception = new IllegalStateException("Called tryLock() on a " + this.getClass().getSimpleName() + " from the thread (" + currentThread + ") that was already holding it"); + MinecraftServer.LOGGER.error(exception.getMessage() + ", at:"); + exception.printStackTrace(); + throw exception; + } + if (this.innerLock.tryLock(time, unit)) { + this.currentHoldingThread = currentThread; + return true; + } + return false; + } + + @Override + public void unlock() { + this.innerLock.unlock(); + this.currentHoldingThread = null; + } + + @NotNull + @Override + public Condition newCondition() { + return this.innerLock.newCondition(); + } + + @Override + public boolean isLocked() { + return this.innerLock.isLocked(); + } + + @Override + public boolean isHeldByCurrentThread() { + return this.innerLock.isHeldByCurrentThread(); + } + +}