Lock storage io

This commit is contained in:
crschnick 2023-12-05 20:17:44 +00:00
parent b061ab7a34
commit 8a567380df
2 changed files with 25 additions and 4 deletions

View file

@ -20,6 +20,7 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -58,6 +59,8 @@ public abstract class DataStorage {
@Getter
private final List<StorageListener> listeners = new CopyOnWriteArrayList<>();
protected final ReentrantLock busyIo = new ReentrantLock();
public DataStorage() {
this.dir = AppPrefs.get().storageDirectory().getValue();
this.storeEntries = new ConcurrentHashMap<>();
@ -146,8 +149,16 @@ public abstract class DataStorage {
public abstract void load();
public void saveAsync() {
// TODO: Don't make this a daemon thread to guarantee proper saving
ThreadHelper.unstarted(this::save).start();
// If we are already loading or saving, don't queue up another operation.
// This could otherwise lead to thread starvation with virtual threads
// Technically the load and save operations also return instantly if locked, but let's not even create new threads here
if (busyIo.isLocked()) {
return;
}
ThreadHelper.runAsync(() -> {
save();
});
}
public abstract void save();

View file

@ -110,7 +110,11 @@ public class StandardStorage extends DataStorage {
}
}
public synchronized void load() {
public void load() {
if (!busyIo.tryLock()) {
return;
}
this.gitStorageHandler.beforeStorageLoad();
var storesDir = getStoresDir();
@ -294,14 +298,19 @@ public class StandardStorage extends DataStorage {
deleteLeftovers();
loaded = true;
busyIo.unlock();
this.gitStorageHandler.afterStorageLoad();
}
public synchronized void save() {
public void save() {
if (!loaded || disposed) {
return;
}
if (!busyIo.tryLock()) {
return;
}
this.gitStorageHandler.beforeStorageSave();
try {
@ -354,6 +363,7 @@ public class StandardStorage extends DataStorage {
deleteLeftovers();
gitStorageHandler.afterStorageSave();
busyIo.unlock();
}
@Override