1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-03-14 19:17:44 +01:00

Tile search can be done lock-free

This commit is contained in:
Martin Tůma 2023-05-21 09:14:19 +02:00
parent a92d6efec6
commit 822a0c2866
4 changed files with 31 additions and 22 deletions

View File

@ -13,14 +13,14 @@ bool MapData::polyCb(VectorTile *tile, void *context)
{ {
PolyCTX *ctx = (PolyCTX*)context; PolyCTX *ctx = (PolyCTX*)context;
tile->polys(ctx->rect, ctx->zoom, ctx->polygons, ctx->lines, tile->polys(ctx->rect, ctx->zoom, ctx->polygons, ctx->lines,
ctx->polyCache); ctx->polyCache, ctx->lock);
return true; return true;
} }
bool MapData::pointCb(VectorTile *tile, void *context) bool MapData::pointCb(VectorTile *tile, void *context)
{ {
PointCTX *ctx = (PointCTX*)context; PointCTX *ctx = (PointCTX*)context;
tile->points(ctx->rect, ctx->zoom, ctx->points, ctx->pointCache); tile->points(ctx->rect, ctx->zoom, ctx->points, ctx->pointCache, ctx->lock);
return true; return true;
} }
@ -45,7 +45,7 @@ MapData::~MapData()
void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons, void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons,
QList<Poly> *lines) QList<Poly> *lines)
{ {
PolyCTX ctx(rect, zoom(bits), polygons, lines, &_polyCache); PolyCTX ctx(rect, zoom(bits), polygons, lines, &_polyCache, &_lock);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();
@ -53,14 +53,12 @@ void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons,
max[0] = rect.right(); max[0] = rect.right();
max[1] = rect.top(); max[1] = rect.top();
_lock.lock();
_tileTree.Search(min, max, polyCb, &ctx); _tileTree.Search(min, max, polyCb, &ctx);
_lock.unlock();
} }
void MapData::points(const RectC &rect, int bits, QList<Point> *points) void MapData::points(const RectC &rect, int bits, QList<Point> *points)
{ {
PointCTX ctx(rect, zoom(bits), points, &_pointCache); PointCTX ctx(rect, zoom(bits), points, &_pointCache, &_lock);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();
@ -68,9 +66,7 @@ void MapData::points(const RectC &rect, int bits, QList<Point> *points)
max[0] = rect.right(); max[0] = rect.right();
max[1] = rect.top(); max[1] = rect.top();
_lock.lock();
_tileTree.Search(min, max, pointCb, &ctx); _tileTree.Search(min, max, pointCb, &ctx);
_lock.unlock();
} }
void MapData::load() void MapData::load()

View File

@ -105,27 +105,30 @@ private:
{ {
PolyCTX(const RectC &rect, const Zoom &zoom, PolyCTX(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
PolyCache *polyCache) PolyCache *polyCache, QMutex *lock)
: rect(rect), zoom(zoom), polygons(polygons), lines(lines), : rect(rect), zoom(zoom), polygons(polygons), lines(lines),
polyCache(polyCache) {} polyCache(polyCache), lock(lock) {}
const RectC &rect; const RectC &rect;
const Zoom &zoom; const Zoom &zoom;
QList<MapData::Poly> *polygons; QList<MapData::Poly> *polygons;
QList<MapData::Poly> *lines; QList<MapData::Poly> *lines;
PolyCache *polyCache; PolyCache *polyCache;
QMutex *lock;
}; };
struct PointCTX struct PointCTX
{ {
PointCTX(const RectC &rect, const Zoom &zoom, PointCTX(const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, PointCache *pointCache) QList<MapData::Point> *points, PointCache *pointCache, QMutex *lock)
: rect(rect), zoom(zoom), points(points), pointCache(pointCache) {} : rect(rect), zoom(zoom), points(points), pointCache(pointCache),
lock(lock) {}
const RectC &rect; const RectC &rect;
const Zoom &zoom; const Zoom &zoom;
QList<MapData::Point> *points; QList<MapData::Point> *points;
PointCache *pointCache; PointCache *pointCache;
QMutex *lock;
}; };
const Zoom &zoom(int bits) const; const Zoom &zoom(int bits) const;

View File

@ -102,15 +102,17 @@ void VectorTile::clear()
void VectorTile::polys(const RectC &rect, const Zoom &zoom, void VectorTile::polys(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
MapData::PolyCache *polyCache) MapData::PolyCache *polyCache, QMutex *lock)
{ {
SubFile::Handle *rgnHdl = 0, *lblHdl = 0, *netHdl = 0, *nodHdl = 0, SubFile::Handle *rgnHdl = 0, *lblHdl = 0, *netHdl = 0, *nodHdl = 0,
*nodHdl2 = 0; *nodHdl2 = 0;
if (_loaded < 0) lock->lock();
return;
//polyCache->lock.lock(); if (_loaded < 0) {
lock->unlock();
return;
}
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(_rgn);
@ -119,7 +121,7 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
nodHdl = new SubFile::Handle(_nod); nodHdl = new SubFile::Handle(_nod);
if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) { if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) {
//polyCache->lock.unlock(); lock->unlock();
delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl;
return; return;
} }
@ -170,19 +172,24 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
} }
} }
//polyCache->lock.unlock(); lock->unlock();
delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete nodHdl2; delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete nodHdl2;
} }
void VectorTile::points(const RectC &rect, const Zoom &zoom, void VectorTile::points(const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, QCache<const SubDiv *, QList<MapData::Point> *points, QCache<const SubDiv *,
QList<MapData::Point> > *pointCache) QList<MapData::Point> > *pointCache, QMutex *lock)
{ {
SubFile::Handle *rgnHdl = 0, *lblHdl = 0; SubFile::Handle *rgnHdl = 0, *lblHdl = 0;
if (_loaded < 0) lock->lock();
if (_loaded < 0) {
lock->unlock();
return; return;
}
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(_rgn);
lblHdl = new SubFile::Handle(_lbl); lblHdl = new SubFile::Handle(_lbl);
@ -190,6 +197,7 @@ void VectorTile::points(const RectC &rect, const Zoom &zoom,
SubFile::Handle netHdl(_net); SubFile::Handle netHdl(_net);
if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) { if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) {
lock->unlock();
delete rgnHdl; delete lblHdl; delete rgnHdl; delete lblHdl;
return; return;
} }
@ -223,6 +231,8 @@ void VectorTile::points(const RectC &rect, const Zoom &zoom,
copyPoints(rect, pl, points); copyPoints(rect, pl, points);
} }
lock->unlock();
delete rgnHdl; delete lblHdl; delete rgnHdl; delete lblHdl;
} }

View File

@ -29,10 +29,10 @@ public:
void polys(const RectC &rect, const Zoom &zoom, void polys(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
MapData::PolyCache *polyCache); MapData::PolyCache *polyCache, QMutex *lock);
void points(const RectC &rect, const Zoom &zoom, void points(const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, QCache<const SubDiv*, QList<MapData::Point> *points, QCache<const SubDiv*,
QList<MapData::Point> > *pointCache); QList<MapData::Point> > *pointCache, QMutex *lock);
static bool isTileFile(SubFile::Type type) static bool isTileFile(SubFile::Type type)
{ {