Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/OpenStreetMap-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ OpenStreetMap::~OpenStreetMap()

freeTilesCache();

if (cacheSemaphore)
vSemaphoreDelete(cacheSemaphore);
if (cacheMutex)
vSemaphoreDelete(cacheMutex);

if (pngCore0)
{
Expand Down Expand Up @@ -312,15 +312,15 @@ bool OpenStreetMap::composeMap(LGFX_Sprite &mapSprite, const tileList &requiredT

bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double latitude, uint8_t zoom)
{
if (!cacheSemaphore)
if (!cacheMutex)
{
cacheSemaphore = xSemaphoreCreateBinary();
if (!cacheSemaphore)
cacheMutex = xSemaphoreCreateBinary();
if (!cacheMutex)
{
log_e("could not init cache mutex");
return false;
}
xSemaphoreGive(cacheSemaphore);
xSemaphoreGive(cacheMutex);
}

if (!tasksStarted && !startTileWorkerTasks())
Expand Down Expand Up @@ -525,13 +525,6 @@ bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t
return true;
}

void OpenStreetMap::decrementActiveJobs()
{
log_d("pending jobs: %d", pendingJobs.load());
if (--pendingJobs == 0)
log_v("jobs done");
}

void OpenStreetMap::tileFetcherTask(void *param)
{
OpenStreetMap *osm = static_cast<OpenStreetMap *>(param);
Expand All @@ -540,27 +533,27 @@ void OpenStreetMap::tileFetcherTask(void *param)
TileJob job;
unsigned long startMS;
{
ScopedMutex lock(osm->cacheSemaphore);
ScopedMutex lock(osm->cacheMutex);
BaseType_t received = xQueueReceive(osm->jobQueue, &job, portMAX_DELAY);
startMS = millis();

if (received != pdTRUE)
continue;

if (job.z == 255) // poison pill: absolved by dtor
if (job.z == 255)
break;

if (!job.tile)
continue;

{
ScopedMutex lock(job.tile->mutex); // protect tile fields
ScopedMutex lock(job.tile->mutex);
if (job.tile->valid &&
job.tile->x == job.x &&
job.tile->y == job.y &&
job.tile->z == job.z)
{
continue; // Already fetched
continue;
}

job.tile->x = job.x;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenStreetMap-esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class OpenStreetMap
bool fetchMap(LGFX_Sprite &sprite, double longitude, double latitude, uint8_t zoom);

private:
SemaphoreHandle_t cacheSemaphore = nullptr;
SemaphoreHandle_t cacheMutex = nullptr;
std::vector<CachedTile> tilesCache;
thread_local static uint16_t *currentTileBuffer;
thread_local static OpenStreetMap *currentInstance;
Expand All @@ -105,7 +105,7 @@ class OpenStreetMap
bool composeMap(LGFX_Sprite &mapSprite, const tileList &requiredTiles, uint8_t zoom);
static void tileFetcherTask(void *param);
TaskHandle_t ownerTask = nullptr;
void decrementActiveJobs();
inline void decrementActiveJobs() { --pendingJobs; }
bool startTileWorkerTasks();

QueueHandle_t jobQueue = nullptr;
Expand Down