Skip to content

Commit

Permalink
possibly fixed threading problems with chunk cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkite committed Mar 19, 2013
1 parent 08a1ae1 commit 02fdbaf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion chunkcache.cpp
Expand Up @@ -52,7 +52,9 @@ ChunkCache::~ChunkCache()
void ChunkCache::clear()
{
QThreadPool::globalInstance()->waitForDone();
mutex.lock();
cache.clear();
mutex.unlock();
}

void ChunkCache::setPath(QString path)
Expand All @@ -67,7 +69,9 @@ QString ChunkCache::getPath()
Chunk *ChunkCache::fetch(int x, int z)
{
ChunkID id(x,z);
mutex.lock();
Chunk *chunk=cache[id];
mutex.unlock();
if (chunk!=NULL)
{
if (chunk->loaded)
Expand All @@ -76,8 +80,10 @@ Chunk *ChunkCache::fetch(int x, int z)
}
// launch background process to load this chunk
chunk=new Chunk();
mutex.lock();
cache.insert(id,chunk);
ChunkLoader *loader=new ChunkLoader(path,x,z,cache);
mutex.unlock();
ChunkLoader *loader=new ChunkLoader(path,x,z,cache,mutex);
connect(loader,SIGNAL(loaded(int,int)),
this,SLOT(gotChunk(int,int)));
QThreadPool::globalInstance()->start(loader);
Expand Down
1 change: 1 addition & 0 deletions chunkcache.h
Expand Up @@ -64,6 +64,7 @@ private slots:
private:
QString path;
QCache<ChunkID,Chunk> cache;
QMutex mutex;
};

#endif
4 changes: 3 additions & 1 deletion chunkloader.cpp
Expand Up @@ -29,7 +29,7 @@
#include "chunkcache.h"
#include "chunk.h"

ChunkLoader::ChunkLoader(QString path,int x,int z,QCache<ChunkID,Chunk> &cache) : path(path),x(x),z(z),cache(cache)
ChunkLoader::ChunkLoader(QString path,int x,int z,QCache<ChunkID,Chunk> &cache,QMutex &mutex) : path(path),x(x),z(z),cache(cache),mutex(mutex)
{
}
ChunkLoader::~ChunkLoader()
Expand Down Expand Up @@ -64,9 +64,11 @@ void ChunkLoader::run()
uchar *raw=f.map(coffset*4096,numSectors*4096);
NBT nbt(raw);
ChunkID id(x,z);
mutex.lock();
Chunk *chunk=cache[id];
if (chunk)
chunk->load(nbt);
mutex.unlock();
f.unmap(raw);
f.close();

Expand Down
4 changes: 3 additions & 1 deletion chunkloader.h
Expand Up @@ -32,12 +32,13 @@
#include <QRunnable>
class Chunk;
class ChunkID;
class QMutex;

class ChunkLoader : public QObject, public QRunnable
{
Q_OBJECT
public:
ChunkLoader(QString path,int x,int z,QCache<ChunkID,Chunk> &cache);
ChunkLoader(QString path,int x,int z,QCache<ChunkID,Chunk> &cache,QMutex &mutex);
~ChunkLoader();
signals:
void loaded(int x,int z);
Expand All @@ -47,6 +48,7 @@ class ChunkLoader : public QObject, public QRunnable
QString path;
int x,z;
QCache<ChunkID,Chunk> &cache;
QMutex &mutex;
};

#endif
9 changes: 9 additions & 0 deletions mapview.cpp
Expand Up @@ -184,7 +184,16 @@ void MapView::paintEvent(QPaintEvent *)
void MapView::redraw()
{
if (!this->isEnabled())
{
// blank
uchar *bits=image.bits();
int imgstride=image.bytesPerLine();
int imgoffset=0;
for (int y=0;y<image.height();y++,imgoffset+=imgstride)
memset(bits+imgoffset,0xee,imgstride);
update();
return;
}

double chunksize=16*zoom;

Expand Down

0 comments on commit 02fdbaf

Please sign in to comment.