Skip to content

Commit

Permalink
fix asynchrounous loading of Chunks
Browse files Browse the repository at this point in the history
Mutex is only necessary for modifying the QCache object.
When spanning Mutex also over parsing section, only one Chunk will be
loaded and all others are blocked.
  • Loading branch information
EtlamGit committed Aug 5, 2019
1 parent 5c30aea commit aad4804
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions chunkcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ QString ChunkCache::getPath() {

Chunk *ChunkCache::fetch(int x, int z) {
ChunkID id(x, z);
mutex.lock();
Chunk *chunk = cache[id];
mutex.unlock();
//mutex.lock();
Chunk *chunk = cache[id]; // const operation
//mutex.unlock();
if (chunk != NULL) {
if (chunk->loaded)
return chunk;
Expand All @@ -72,7 +72,7 @@ Chunk *ChunkCache::fetch(int x, int z) {
connect(chunk, SIGNAL(structureFound(QSharedPointer<GeneratedStructure>)),
this, SLOT (routeStructure(QSharedPointer<GeneratedStructure>)));
mutex.lock();
cache.insert(id, chunk);
cache.insert(id, chunk); // non-const operation !
mutex.unlock();
ChunkLoader *loader = new ChunkLoader(path, x, z, cache, &mutex);
connect(loader, SIGNAL(loaded(int, int)),
Expand Down
15 changes: 10 additions & 5 deletions chunkloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ChunkLoader::~ChunkLoader() {
}

void ChunkLoader::run() {
// get coordinates of Region file
int rx = x >> 5;
int rz = z >> 5;

Expand Down Expand Up @@ -42,13 +43,17 @@ void ChunkLoader::run() {
emit loaded(x, z);
return;
}
NBT nbt(raw);
// get existing Chunk entry from Cache
ChunkID id(x, z);
mutex->lock();
Chunk *chunk = cache[id];
if (chunk)
//mutex->lock();
Chunk *chunk = cache[id]; // const operation
//mutex->unlock();
// parse Chunk data
// Chunk will be flagged "loaded" in a thread save way
if (chunk) {
NBT nbt(raw);
chunk->load(nbt);
mutex->unlock();
}
f.unmap(raw);
f.close();

Expand Down

0 comments on commit aad4804

Please sign in to comment.