This commit is contained in:
Jesse Boyd 2016-12-06 17:04:03 +11:00
parent 1c948cf0ed
commit 043da6668d
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F

View File

@ -32,8 +32,11 @@ import java.util.concurrent.atomic.AtomicInteger;
public abstract class FaweChangeSet implements ChangeSet { public abstract class FaweChangeSet implements ChangeSet {
private final World world; private final World world;
private final boolean mainThread; private final boolean mainThread;
private AtomicInteger waitingCombined = new AtomicInteger(0);
private AtomicInteger waitingAsync = new AtomicInteger(0);
private Object lockCombined = new Object();
private Object lockAsync = new Object();
public static FaweChangeSet getDefaultChangeSet(World world, UUID uuid) { public static FaweChangeSet getDefaultChangeSet(World world, UUID uuid) {
if (Settings.HISTORY.USE_DISK) { if (Settings.HISTORY.USE_DISK) {
@ -53,13 +56,13 @@ public abstract class FaweChangeSet implements ChangeSet {
} }
public boolean flushAsync() { public boolean flushAsync() {
waiting.incrementAndGet(); waitingAsync.incrementAndGet();
TaskManager.IMP.async(new Runnable() { TaskManager.IMP.async(new Runnable() {
@Override @Override
public void run() { public void run() {
waiting.decrementAndGet(); waitingAsync.decrementAndGet();
synchronized (lock) { synchronized (lockAsync) {
lock.notifyAll(); lockAsync.notifyAll();
} }
flush(); flush();
} }
@ -69,9 +72,16 @@ public abstract class FaweChangeSet implements ChangeSet {
public boolean flush() { public boolean flush() {
try { try {
while (waiting.get() > 0) { if (!Fawe.get().isMainThread()) {
synchronized (lock) { while (waitingAsync.get() > 0) {
lock.wait(1000); synchronized (lockAsync) {
lockAsync.wait(1000);
}
}
}
while (waitingCombined.get() > 0) {
synchronized (lockCombined) {
lockCombined.wait(1000);
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -191,14 +201,11 @@ public abstract class FaweChangeSet implements ChangeSet {
} }
} }
private AtomicInteger waiting = new AtomicInteger(0);
private Object lock = new Object();
public void addChangeTask(FaweQueue queue) { public void addChangeTask(FaweQueue queue) {
queue.setChangeTask(new RunnableVal2<FaweChunk, FaweChunk>() { queue.setChangeTask(new RunnableVal2<FaweChunk, FaweChunk>() {
@Override @Override
public void run(final FaweChunk previous, final FaweChunk next) { public void run(final FaweChunk previous, final FaweChunk next) {
waiting.incrementAndGet(); waitingCombined.incrementAndGet();
Runnable run = new Runnable() { Runnable run = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -242,7 +249,7 @@ public abstract class FaweChangeSet implements ChangeSet {
default: default:
char combinedIdPrevious = previousLayer != null ? previousLayer[index] : 0; char combinedIdPrevious = previousLayer != null ? previousLayer[index] : 0;
if (combinedIdCurrent != combinedIdPrevious) { if (combinedIdCurrent != combinedIdPrevious) {
synchronized (lock) { synchronized (lockCombined) {
add(xx, yy, zz, combinedIdPrevious, combinedIdCurrent); add(xx, yy, zz, combinedIdPrevious, combinedIdCurrent);
} }
} }
@ -257,14 +264,14 @@ public abstract class FaweChangeSet implements ChangeSet {
// Tiles created // Tiles created
Map<Short, CompoundTag> tiles = next.getTiles(); Map<Short, CompoundTag> tiles = next.getTiles();
for (Map.Entry<Short, CompoundTag> entry : tiles.entrySet()) { for (Map.Entry<Short, CompoundTag> entry : tiles.entrySet()) {
synchronized (lock) { synchronized (lockCombined) {
addTileCreate(entry.getValue()); addTileCreate(entry.getValue());
} }
} }
// Tiles removed // Tiles removed
tiles = previous.getTiles(); tiles = previous.getTiles();
for (Map.Entry<Short, CompoundTag> entry : tiles.entrySet()) { for (Map.Entry<Short, CompoundTag> entry : tiles.entrySet()) {
synchronized (lock) { synchronized (lockCombined) {
addTileRemove(entry.getValue()); addTileRemove(entry.getValue());
} }
} }
@ -274,14 +281,14 @@ public abstract class FaweChangeSet implements ChangeSet {
// Entities created // Entities created
Set<CompoundTag> entities = next.getEntities(); Set<CompoundTag> entities = next.getEntities();
for (CompoundTag entityTag : entities) { for (CompoundTag entityTag : entities) {
synchronized (lock) { synchronized (lockCombined) {
addEntityCreate(entityTag); addEntityCreate(entityTag);
} }
} }
// Entities removed // Entities removed
entities = previous.getEntities(); entities = previous.getEntities();
for (CompoundTag entityTag : entities) { for (CompoundTag entityTag : entities) {
synchronized (lock) { synchronized (lockCombined) {
addEntityRemove(entityTag); addEntityRemove(entityTag);
} }
} }
@ -289,9 +296,12 @@ public abstract class FaweChangeSet implements ChangeSet {
} catch (Throwable e) { } catch (Throwable e) {
MainUtil.handleError(e); MainUtil.handleError(e);
} finally { } finally {
if (waiting.decrementAndGet() <= 0) { if (waitingCombined.decrementAndGet() <= 0) {
synchronized (lock) { synchronized (waitingAsync) {
lock.notifyAll(); waitingAsync.notifyAll();
}
synchronized (waitingCombined) {
waitingCombined.notifyAll();
} }
} }
} }