Skip to content

Commit

Permalink
Cleaned up chunk locking code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Oct 10, 2023
1 parent fea36dd commit 3d1343f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ public class ChunkLocker {

public IThreeDContainer<ChunkLock> chunkLocks = new EmptyContainer<>();

public int regionSize;

public ChunkLocker(int regionSize) {
this.regionSize = regionSize;
}

public void release(Lock lock) {
HashSet<Lock> locks = new HashSet<>();
lock.releaseLock();
lock.myLock.set(false);
for(ChunkLock chunkLock : lock.chunkLocks) {
locks.addAll(chunkLock.waitingLocks);
}
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/dev/hilligans/ourcraft/Server/Concurrent/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class Lock {
public AtomicInteger notifications = new AtomicInteger();


public Lock(ChunkPos... positions) {
public Lock(ChunkLocker chunkLocker, ChunkPos... positions) {
this.chunkPositions = positions;
Arrays.sort(this.chunkPositions, Comparator.comparingLong((ChunkPos o) -> o.chunkX).thenComparingLong(o -> o.chunkY).thenComparingLong(o -> o.chunkZ));
chunkLocks = new ChunkLock[chunkPositions.length];
this.chunkLocker = chunkLocker;
}

public void notifyOfUnlock() {
Expand All @@ -40,19 +41,17 @@ public void notifyOfUnlock() {
}
}

public void releaseLock() {
myLock.set(false);
}

public boolean hasAllLocks() {
return hasAllLocks;
}


public void acquire(long... positions) {}

public void acquire() {}

public void release() {}
public void acquire() {
chunkLocker.acquire(this);
}

public void release() {
chunkLocker.release(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public ChunkTask(IChunk chunk) {

@Override
public void tick() {
chunkLocker.acquire(new Lock(new ChunkPos(chunk.getX(), chunk.getY(), chunk.getZ())));
//we make the lock with all the chunk positions we want
Lock lock = new Lock(chunkLocker, new ChunkPos(chunk.getX(), chunk.getY(), chunk.getZ()));
//we can then acquire it when we need it
lock.acquire();
//and release it when we no longer need it
lock.release();
}
}

0 comments on commit 3d1343f

Please sign in to comment.