#1110: Make CraftMapColorCache more thread safe

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2022-09-21 06:54:55 +10:00
parent c38953dda8
commit 61e6302177

View file

@ -11,6 +11,7 @@ import java.io.OutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.DeflaterOutputStream; import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
@ -24,7 +25,7 @@ public class CraftMapColorCache implements MapPalette.MapColorCache {
private byte[] cache; private byte[] cache;
private final Logger logger; private final Logger logger;
private boolean cached = false; private boolean cached = false;
private boolean running = false; private final AtomicBoolean running = new AtomicBoolean(false);
public CraftMapColorCache(Logger logger) { public CraftMapColorCache(Logger logger) {
this.logger = logger; this.logger = logger;
@ -57,7 +58,7 @@ public class CraftMapColorCache implements MapPalette.MapColorCache {
} }
public CompletableFuture<Void> initCache() { public CompletableFuture<Void> initCache() {
Preconditions.checkState(!cached && !running, "Cache is already build or is currently being build"); Preconditions.checkState(!cached && !running.getAndSet(true), "Cache is already build or is currently being build");
cache = new byte[256 * 256 * 256]; // Red, Green and Blue have each a range from 0 to 255 each mean we need space for 256 * 256 * 256 values cache = new byte[256 * 256 * 256]; // Red, Green and Blue have each a range from 0 to 255 each mean we need space for 256 * 256 * 256 values
if (CACHE_FILE.exists()) { if (CACHE_FILE.exists()) {
@ -108,21 +109,18 @@ public class CraftMapColorCache implements MapPalette.MapColorCache {
} }
private CompletableFuture<Void> buildAndSaveCache() { private CompletableFuture<Void> buildAndSaveCache() {
running = true;
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
buildCache(); buildCache();
if (!CACHE_FILE.exists()) { if (!CACHE_FILE.exists()) {
try { try {
if (!CACHE_FILE.createNewFile()) { if (!CACHE_FILE.createNewFile()) {
running = false;
cached = true; cached = true;
return; return;
} }
} catch (IOException e) { } catch (IOException e) {
logger.warning("Error while building map color cache"); logger.warning("Error while building map color cache");
e.printStackTrace(); e.printStackTrace();
running = false;
cached = true; cached = true;
return; return;
} }
@ -133,12 +131,10 @@ public class CraftMapColorCache implements MapPalette.MapColorCache {
} catch (IOException e) { } catch (IOException e) {
logger.warning("Error while building map color cache"); logger.warning("Error while building map color cache");
e.printStackTrace(); e.printStackTrace();
running = false;
cached = true; cached = true;
return; return;
} }
running = false;
cached = true; cached = true;
logger.info("Map color cache build successfully"); logger.info("Map color cache build successfully");
}, SystemUtils.backgroundExecutor()); }, SystemUtils.backgroundExecutor());
@ -150,7 +146,7 @@ public class CraftMapColorCache implements MapPalette.MapColorCache {
@Override @Override
public boolean isCached() { public boolean isCached() {
return cached || (!running && initCache().isDone()); return cached || (!running.get() && initCache().isDone());
} }
@Override @Override