ThreadingDetector .java

net.minecraft.util.ThreadingDetector (class, 88 lines): members: threadThatFailedToAcquire, makeThreadingException, checkAndUnlock, stackTraceLock, lock.

net.minecraft.utilclass88 linescommon2.7 KB

API Surface (3)

void checkAndLock()
void checkAndUnlock()
ReportedException makeThreadingException(final String name, final @Nullable Thread threadThatFailedToAcquire)

Fields & Constants (6)

LOGGER
name
lock
stackTraceLock
threadThatFailedToAcquire
fullException

String literals (5)

"Accessing "
" from multiple threads"
"Thread dumps"
": \n\tat "
"\n\tat "

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ThreadingDetector.java88 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.mojang.logging.LogUtils;
4
import java.util.Arrays;
5
import java.util.Objects;
6
import java.util.concurrent.Semaphore;
7
import java.util.concurrent.locks.Lock;
8
import java.util.concurrent.locks.ReentrantLock;
9
import java.util.stream.Collectors;
10
import java.util.stream.Stream;
11
import net.minecraft.CrashReport;
12
import net.minecraft.CrashReportCategory;
13
import net.minecraft.ReportedException;
14
import org.jspecify.annotations.Nullable;
15
import org.slf4j.Logger;
16
17
public class ThreadingDetector {
18
	private static final Logger LOGGER = LogUtils.getLogger();
19
	private final String name;
20
	private final Semaphore lock = new Semaphore(1);
21
	private final Lock stackTraceLock = new ReentrantLock();
22
	private volatile @Nullable Thread threadThatFailedToAcquire;
23
	private volatile @Nullable ReportedException fullException;
24
25
	public ThreadingDetector(final String name) {
26
		this.name = name;
27
	}
28
29
	public void checkAndLock() {
30
		boolean released = false;
31
32
		try {
33
			this.stackTraceLock.lock();
34
			if (!this.lock.tryAcquire()) {
35
				this.threadThatFailedToAcquire = Thread.currentThread();
36
				released = true;
37
				this.stackTraceLock.unlock();
38
39
				try {
40
					this.lock.acquire();
41
				} catch (InterruptedException ignored) {
42
					Thread.currentThread().interrupt();
43
				}
44
45
				throw this.fullException;
46
			}
47
		} finally {
48
			if (!released) {
49
				this.stackTraceLock.unlock();
50
			}
51
		}
52
	}
53
54
	public void checkAndUnlock() {
55
		try {
56
			this.stackTraceLock.lock();
57
			Thread threadThatFailedToAcquire = this.threadThatFailedToAcquire;
58
			if (threadThatFailedToAcquire != null) {
59
				ReportedException fullException = makeThreadingException(this.name, threadThatFailedToAcquire);
60
				this.fullException = fullException;
61
				this.lock.release();
62
				throw fullException;
63
			}
64
65
			this.lock.release();
66
		} finally {
67
			this.stackTraceLock.unlock();
68
		}
69
	}
70
71
	public static ReportedException makeThreadingException(final String name, final @Nullable Thread threadThatFailedToAcquire) {
72
		String threads = Stream.of(Thread.currentThread(), threadThatFailedToAcquire)
73
			.filter(Objects::nonNull)
74
			.map(ThreadingDetector::stackTrace)
75
			.collect(Collectors.joining("\n"));
76
		String error = "Accessing " + name + " from multiple threads";
77
		CrashReport report = new CrashReport(error, new IllegalStateException(error));
78
		CrashReportCategory category = report.addCategory("Thread dumps");
79
		category.setDetail("Thread dumps", threads);
80
		LOGGER.error("Thread dumps: \n{}", threads);
81
		return new ReportedException(report);
82
	}
83
84
	private static String stackTrace(final Thread thread) {
85
		return thread.getName() + ": \n\tat " + Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n\tat "));
86
	}
87
}
88

Decompiled Minecraft 26.3 source of ThreadingDetector (net/minecraft/util/ThreadingDetector.java) with Mojang official mappings, stored verbatim on the site.