fix: use RAII render lock everywhere (#916)

Follow-up to
https://github.com/crosspoint-reader/crosspoint-reader/pull/774

---

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **NO**

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

* **Refactor**
* Modernized internal synchronization mechanisms across multiple
components to improve code reliability and maintainability. All
functionality remains unchanged.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Xuan-Son Nguyen
2026-02-16 13:53:00 +01:00
committed by cottongin
parent ed8a0feac1
commit ebcd3a8b94
7 changed files with 153 additions and 128 deletions

View File

@@ -244,9 +244,10 @@ void WifiSelectionActivity::checkConnectionStatus() {
// Save this as the last connected network - SD card operations need lock as // Save this as the last connected network - SD card operations need lock as
// we use SPI for both // we use SPI for both
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
WIFI_STORE.setLastConnectedSsid(selectedSSID); WIFI_STORE.setLastConnectedSsid(selectedSSID);
xSemaphoreGive(renderingMutex); }
// If we entered a new password, ask if user wants to save it // If we entered a new password, ask if user wants to save it
// Otherwise, immediately complete so parent can start web server // Otherwise, immediately complete so parent can start web server

View File

@@ -296,11 +296,12 @@ void EpubReaderActivity::loop() {
if (skipChapter) { if (skipChapter) {
// We don't want to delete the section mid-render, so grab the semaphore // We don't want to delete the section mid-render, so grab the semaphore
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
nextPageNumber = 0; nextPageNumber = 0;
currentSpineIndex = nextTriggered ? currentSpineIndex + 1 : currentSpineIndex - 1; currentSpineIndex = nextTriggered ? currentSpineIndex + 1 : currentSpineIndex - 1;
section.reset(); section.reset();
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
@@ -316,11 +317,12 @@ void EpubReaderActivity::loop() {
section->currentPage--; section->currentPage--;
} else { } else {
// We don't want to delete the section mid-render, so grab the semaphore // We don't want to delete the section mid-render, so grab the semaphore
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
nextPageNumber = UINT16_MAX; nextPageNumber = UINT16_MAX;
currentSpineIndex--; currentSpineIndex--;
section.reset(); section.reset();
xSemaphoreGive(renderingMutex); }
} }
requestUpdate(); requestUpdate();
} else { } else {
@@ -328,11 +330,12 @@ void EpubReaderActivity::loop() {
section->currentPage++; section->currentPage++;
} else { } else {
// We don't want to delete the section mid-render, so grab the semaphore // We don't want to delete the section mid-render, so grab the semaphore
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
nextPageNumber = 0; nextPageNumber = 0;
currentSpineIndex++; currentSpineIndex++;
section.reset(); section.reset();
xSemaphoreGive(renderingMutex); }
} }
requestUpdate(); requestUpdate();
} }
@@ -402,12 +405,13 @@ void EpubReaderActivity::jumpToPercent(int percent) {
} }
// Reset state so render() reloads and repositions on the target spine. // Reset state so render() reloads and repositions on the target spine.
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
currentSpineIndex = targetSpineIndex; currentSpineIndex = targetSpineIndex;
nextPageNumber = 0; nextPageNumber = 0;
pendingPercentJump = true; pendingPercentJump = true;
section.reset(); section.reset();
xSemaphoreGive(renderingMutex); }
} }
void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action) { void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action) {
@@ -706,7 +710,8 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
break; break;
} }
case EpubReaderMenuActivity::MenuAction::DELETE_CACHE: { case EpubReaderMenuActivity::MenuAction::DELETE_CACHE: {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
if (epub) { if (epub) {
// 2. BACKUP: Read current progress // 2. BACKUP: Read current progress
// We use the current variables that track our position // We use the current variables that track our position
@@ -726,7 +731,7 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
// 5. Remove from recent books so the home screen doesn't show a stale/placeholder cover // 5. Remove from recent books so the home screen doesn't show a stale/placeholder cover
RECENT_BOOKS.removeBook(epub->getPath()); RECENT_BOOKS.removeBook(epub->getPath());
} }
xSemaphoreGive(renderingMutex); }
// Defer go home to avoid race condition with display task // Defer go home to avoid race condition with display task
pendingGoHome = true; pendingGoHome = true;
break; break;
@@ -768,7 +773,8 @@ void EpubReaderActivity::applyOrientation(const uint8_t orientation) {
} }
// Preserve current reading position so we can restore after reflow. // Preserve current reading position so we can restore after reflow.
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
if (section) { if (section) {
cachedSpineIndex = currentSpineIndex; cachedSpineIndex = currentSpineIndex;
cachedChapterTotalPageCount = section->pageCount; cachedChapterTotalPageCount = section->pageCount;
@@ -784,7 +790,7 @@ void EpubReaderActivity::applyOrientation(const uint8_t orientation) {
// Reset section to force re-layout in the new orientation. // Reset section to force re-layout in the new orientation.
section.reset(); section.reset();
xSemaphoreGive(renderingMutex); }
} }
// TODO: Failure handling // TODO: Failure handling

View File

@@ -51,18 +51,20 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
LOG_DBG("KOSync", "WiFi connected, starting sync"); LOG_DBG("KOSync", "WiFi connected, starting sync");
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = SYNCING; state = SYNCING;
statusMessage = "Syncing time..."; statusMessage = "Syncing time...";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
// Sync time with NTP before making API requests // Sync time with NTP before making API requests
syncTimeWithNTP(); syncTimeWithNTP();
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
statusMessage = "Calculating document hash..."; statusMessage = "Calculating document hash...";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
performSync(); performSync();
@@ -76,19 +78,21 @@ void KOReaderSyncActivity::performSync() {
documentHash = KOReaderDocumentId::calculate(epubPath); documentHash = KOReaderDocumentId::calculate(epubPath);
} }
if (documentHash.empty()) { if (documentHash.empty()) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = SYNC_FAILED; state = SYNC_FAILED;
statusMessage = "Failed to calculate document hash"; statusMessage = "Failed to calculate document hash";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
LOG_DBG("KOSync", "Document hash: %s", documentHash.c_str()); LOG_DBG("KOSync", "Document hash: %s", documentHash.c_str());
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
statusMessage = "Fetching remote progress..."; statusMessage = "Fetching remote progress...";
xSemaphoreGive(renderingMutex); }
requestUpdateAndWait(); requestUpdateAndWait();
// Fetch remote progress // Fetch remote progress
@@ -96,19 +100,21 @@ void KOReaderSyncActivity::performSync() {
if (result == KOReaderSyncClient::NOT_FOUND) { if (result == KOReaderSyncClient::NOT_FOUND) {
// No remote progress - offer to upload // No remote progress - offer to upload
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = NO_REMOTE_PROGRESS; state = NO_REMOTE_PROGRESS;
hasRemoteProgress = false; hasRemoteProgress = false;
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
if (result != KOReaderSyncClient::OK) { if (result != KOReaderSyncClient::OK) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = SYNC_FAILED; state = SYNC_FAILED;
statusMessage = KOReaderSyncClient::errorString(result); statusMessage = KOReaderSyncClient::errorString(result);
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
@@ -122,18 +128,20 @@ void KOReaderSyncActivity::performSync() {
CrossPointPosition localPos = {currentSpineIndex, currentPage, totalPagesInSpine}; CrossPointPosition localPos = {currentSpineIndex, currentPage, totalPagesInSpine};
localProgress = ProgressMapper::toKOReader(epub, localPos); localProgress = ProgressMapper::toKOReader(epub, localPos);
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = SHOWING_RESULT; state = SHOWING_RESULT;
selectedOption = 0; // Default to "Apply" selectedOption = 0; // Default to "Apply"
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
} }
void KOReaderSyncActivity::performUpload() { void KOReaderSyncActivity::performUpload() {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = UPLOADING; state = UPLOADING;
statusMessage = "Uploading progress..."; statusMessage = "Uploading progress...";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
requestUpdateAndWait(); requestUpdateAndWait();
@@ -149,17 +157,19 @@ void KOReaderSyncActivity::performUpload() {
const auto result = KOReaderSyncClient::updateProgress(progress); const auto result = KOReaderSyncClient::updateProgress(progress);
if (result != KOReaderSyncClient::OK) { if (result != KOReaderSyncClient::OK) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = SYNC_FAILED; state = SYNC_FAILED;
statusMessage = KOReaderSyncClient::errorString(result); statusMessage = KOReaderSyncClient::errorString(result);
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = UPLOAD_COMPLETE; state = UPLOAD_COMPLETE;
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
} }
@@ -190,9 +200,10 @@ void KOReaderSyncActivity::onEnter() {
auto* self = static_cast<KOReaderSyncActivity*>(param); auto* self = static_cast<KOReaderSyncActivity*>(param);
// Sync time first // Sync time first
syncTimeWithNTP(); syncTimeWithNTP();
xSemaphoreTake(self->renderingMutex, portMAX_DELAY); {
RenderLock lock(*self);
self->statusMessage = "Calculating document hash..."; self->statusMessage = "Calculating document hash...";
xSemaphoreGive(self->renderingMutex); }
self->requestUpdate(); self->requestUpdate();
self->performSync(); self->performSync();
vTaskDelete(nullptr); vTaskDelete(nullptr);

View File

@@ -118,9 +118,10 @@ void ClearCacheActivity::loop() {
if (state == WARNING) { if (state == WARNING) {
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) { if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
LOG_DBG("CLEAR_CACHE", "User confirmed, starting cache clear"); LOG_DBG("CLEAR_CACHE", "User confirmed, starting cache clear");
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = CLEARING; state = CLEARING;
xSemaphoreGive(renderingMutex); }
requestUpdateAndWait(); requestUpdateAndWait();
clearCache(); clearCache();

View File

@@ -14,18 +14,20 @@ void KOReaderAuthActivity::onWifiSelectionComplete(const bool success) {
exitActivity(); exitActivity();
if (!success) { if (!success) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = FAILED; state = FAILED;
errorMessage = "WiFi connection failed"; errorMessage = "WiFi connection failed";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = AUTHENTICATING; state = AUTHENTICATING;
statusMessage = "Authenticating..."; statusMessage = "Authenticating...";
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
performAuthentication(); performAuthentication();
@@ -34,7 +36,8 @@ void KOReaderAuthActivity::onWifiSelectionComplete(const bool success) {
void KOReaderAuthActivity::performAuthentication() { void KOReaderAuthActivity::performAuthentication() {
const auto result = KOReaderSyncClient::authenticate(); const auto result = KOReaderSyncClient::authenticate();
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
if (result == KOReaderSyncClient::OK) { if (result == KOReaderSyncClient::OK) {
state = SUCCESS; state = SUCCESS;
statusMessage = "Successfully authenticated!"; statusMessage = "Successfully authenticated!";
@@ -42,7 +45,7 @@ void KOReaderAuthActivity::performAuthentication() {
state = FAILED; state = FAILED;
errorMessage = KOReaderSyncClient::errorString(result); errorMessage = KOReaderSyncClient::errorString(result);
} }
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
} }

View File

@@ -121,7 +121,6 @@ void KOReaderSettingsActivity::handleSelection() {
// Authenticate // Authenticate
if (!KOREADER_STORE.hasCredentials()) { if (!KOREADER_STORE.hasCredentials()) {
// Can't authenticate without credentials - just show message briefly // Can't authenticate without credentials - just show message briefly
xSemaphoreGive(renderingMutex);
return; return;
} }
exitActivity(); exitActivity();

View File

@@ -20,33 +20,37 @@ void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
LOG_DBG("OTA", "WiFi connected, checking for update"); LOG_DBG("OTA", "WiFi connected, checking for update");
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = CHECKING_FOR_UPDATE; state = CHECKING_FOR_UPDATE;
xSemaphoreGive(renderingMutex); }
requestUpdateAndWait(); requestUpdateAndWait();
const auto res = updater.checkForUpdate(); const auto res = updater.checkForUpdate();
if (res != OtaUpdater::OK) { if (res != OtaUpdater::OK) {
LOG_DBG("OTA", "Update check failed: %d", res); LOG_DBG("OTA", "Update check failed: %d", res);
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = FAILED; state = FAILED;
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
if (!updater.isUpdateNewer()) { if (!updater.isUpdateNewer()) {
LOG_DBG("OTA", "No new update available"); LOG_DBG("OTA", "No new update available");
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = NO_UPDATE; state = NO_UPDATE;
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
return; return;
} }
xSemaphoreTake(renderingMutex, portMAX_DELAY); {
RenderLock lock(*this);
state = WAITING_CONFIRMATION; state = WAITING_CONFIRMATION;
xSemaphoreGive(renderingMutex); }
requestUpdate(); requestUpdate();
} }