Skip to content

Commit d66a3c6

Browse files
Remove tile decode mutexes for improved multicore performance (#74)
Mutex-based tile locking was originally used to prevent concurrent access, but job scheduling already guarantees exclusive ownership of each tile.
1 parent b27e338 commit d66a3c6

File tree

4 files changed

+7
-110
lines changed

4 files changed

+7
-110
lines changed

src/CachedTile.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ struct CachedTile
3434
uint8_t z;
3535
bool valid;
3636
bool busy;
37-
SemaphoreHandle_t mutex;
3837
uint16_t *buffer;
3938

4039
CachedTile()
@@ -43,18 +42,12 @@ struct CachedTile
4342
z(0),
4443
valid(false),
4544
busy(false),
46-
mutex(xSemaphoreCreateMutex()),
4745
buffer(nullptr)
4846
{
4947
}
5048

5149
~CachedTile()
5250
{
53-
if (mutex)
54-
{
55-
vSemaphoreDelete(mutex);
56-
mutex = nullptr;
57-
}
5851
free();
5952
}
6053

src/OpenStreetMap-esp32.cpp

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ OpenStreetMap::~OpenStreetMap()
4747

4848
freeTilesCache();
4949

50-
if (cacheMutex)
51-
{
52-
vSemaphoreDelete(cacheMutex);
53-
cacheMutex = nullptr;
54-
}
55-
5650
if (pngCore0)
5751
{
5852
pngCore0->~PNG();
@@ -148,7 +142,6 @@ CachedTile *OpenStreetMap::findUnusedTile(const tileList &requiredTiles, uint8_t
148142
{
149143
for (auto &tile : tilesCache)
150144
{
151-
ScopedMutex lock(tile.mutex);
152145
if (tile.busy)
153146
continue;
154147

@@ -253,10 +246,8 @@ void OpenStreetMap::updateCache(const tileList &requiredTiles, uint8_t zoom)
253246
delay(1);
254247

255248
for (const TileJob &job : jobs)
256-
{
257-
ScopedMutex _(job.tile->mutex);
258249
job.tile->busy = false;
259-
}
250+
260251
log_i("Cache updated in %lu ms", millis() - startMS);
261252
}
262253
}
@@ -317,17 +308,6 @@ bool OpenStreetMap::composeMap(LGFX_Sprite &mapSprite, const tileList &requiredT
317308

318309
bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double latitude, uint8_t zoom)
319310
{
320-
if (!cacheMutex)
321-
{
322-
cacheMutex = xSemaphoreCreateBinary();
323-
if (!cacheMutex)
324-
{
325-
log_e("could not init cache mutex");
326-
return false;
327-
}
328-
xSemaphoreGive(cacheMutex);
329-
}
330-
331311
if (!tasksStarted && !startTileWorkerTasks())
332312
{
333313
log_e("Failed to start tile worker(s)");
@@ -533,36 +513,14 @@ void OpenStreetMap::tileFetcherTask(void *param)
533513
while (true)
534514
{
535515
TileJob job;
536-
unsigned long startMS;
537-
{
538-
ScopedMutex lock(osm->cacheMutex);
539-
BaseType_t received = xQueueReceive(osm->jobQueue, &job, portMAX_DELAY);
540-
startMS = millis();
541-
542-
if (received != pdTRUE)
543-
continue;
544-
545-
if (job.z == 255)
546-
break;
516+
xQueueReceive(osm->jobQueue, &job, portMAX_DELAY);
517+
const unsigned long startMS = millis();
547518

548-
if (!job.tile)
549-
continue;
519+
if (job.z == 255)
520+
break;
550521

551-
{
552-
ScopedMutex lock(job.tile->mutex);
553-
if (job.tile->valid &&
554-
job.tile->x == job.x &&
555-
job.tile->y == job.y &&
556-
job.tile->z == job.z)
557-
{
558-
continue;
559-
}
560-
561-
job.tile->x = job.x;
562-
job.tile->y = job.y;
563-
job.tile->z = job.z;
564-
}
565-
}
522+
if (!job.tile)
523+
continue;
566524

567525
String result;
568526
if (!osm->fetchTile(*job.tile, job.x, job.y, job.z, result))

src/OpenStreetMap-esp32.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <PNGdec.h>
3535

3636
#include "CachedTile.hpp"
37-
#include "ScopedMutex.hpp"
3837
#include "TileJob.hpp"
3938
#include "MemoryBuffer.hpp"
4039
#include "HTTPClientRAII.hpp"
@@ -85,7 +84,6 @@ class OpenStreetMap
8584
bool fetchMap(LGFX_Sprite &sprite, double longitude, double latitude, uint8_t zoom);
8685

8786
private:
88-
SemaphoreHandle_t cacheMutex = nullptr;
8987
std::vector<CachedTile> tilesCache;
9088
static inline thread_local OpenStreetMap *currentInstance = nullptr;
9189
static inline thread_local uint16_t *currentTileBuffer = nullptr;

src/ScopedMutex.hpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)