Skip to content

Commit c185f37

Browse files
committed
improved mutex error handling
- debug message in case of "unexpected" mutex errors - removed experimental delay() in requestJSONBufferLock
1 parent 1c2e6aa commit c185f37

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

wled00/json.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
100100
id = strip.getSegmentsNum()-1; // segments are added at the end of list
101101
newSeg = true;
102102
esp32SemGive(segmentMux);
103+
} else {
104+
USER_PRINTLN(F("deserializeSegment(): segment not added - failed to acquire segmentMux."));
105+
return false;
103106
}
104107
}
105108

@@ -358,6 +361,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
358361
strip.waitUntilIdle();
359362
}
360363
// WLEDMM protect against parallel drawing
364+
bool drawSuccess = false;
361365
if (esp32SemTake(busDrawMux, 250) == pdTRUE) { // WLEDMM first acquire draw mutex, start of critical section
362366
seg.startFrame();
363367

@@ -409,11 +413,13 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
409413
set = 0;
410414
}
411415
}
416+
drawSuccess = true;
412417
esp32SemGive(busDrawMux); // release lock
413418
} // end of critical section
414419

415420
seg.map1D2D = oldMap1D2D; // restore mapping
416-
strip.trigger(); // force segment update
421+
if (drawSuccess) strip.trigger(); // force segment update
422+
else USER_PRINTLN(F("deserializeSegment() image drawing failed, cannot not to acquire busDrawMux.")); // log failure messaage
417423
suspendStripService = oldLock; // restore previous lock status
418424
}
419425
// send UDP/WS if segment options changed (except selection; will also deselect current preset)

wled00/util.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ bool requestJSONBufferLock(uint8_t module)
239239

240240
if (jsonBufferLock || !haveLock) {
241241
if (haveLock) esp32SemGive(jsonBufferLockMutex); // we got the mutex, but jsonBufferLock says the opposite -> give up
242-
delay(10); // WLEDMM experimental: small extra wait, in case that esp32 cores temporarily disagree on the value of jsonBufferLock
243242
USER_PRINT(F("ERROR: Locking JSON buffer failed! (still locked by "));
244243
USER_PRINT(jsonBufferLock);
245244
USER_PRINTLN(")");

wled00/wled.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ void WLED::setup()
480480
busDrawMux = xSemaphoreCreateRecursiveMutex(); // WLEDMM prevent concurrent running of strip.show and strip.service
481481
segmentMux = xSemaphoreCreateRecursiveMutex(); // WLEDMM prevent segment changes while effects are running
482482
jsonBufferLockMutex = xSemaphoreCreateRecursiveMutex(); // WLEDMM prevent concurrent JSON buffer writing
483+
if ((busDrawMux == nullptr) || (segmentMux == nullptr) || (jsonBufferLockMutex == nullptr))
484+
USER_PRINTLN(F("setup error: xSemaphoreCreateRecursiveMutex failed.")); // should never happen.
483485
xSemaphoreGiveRecursive(busDrawMux); // init semaphores to initially allow drawing
484486
xSemaphoreGiveRecursive(segmentMux);
485487
xSemaphoreGiveRecursive(jsonBufferLockMutex);

0 commit comments

Comments
 (0)