mirror of
https://github.com/wled/WLED.git
synced 2025-07-15 14:56:32 +00:00
Merge pull request #4089 from willmmiles/improved-locking
Improve jsonBufferLock management
This commit is contained in:
commit
9d860c265c
@ -213,21 +213,33 @@ bool requestJSONBufferLock(uint8_t module)
|
|||||||
DEBUG_PRINTLN(F("ERROR: JSON buffer not allocated!"));
|
DEBUG_PRINTLN(F("ERROR: JSON buffer not allocated!"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
unsigned long now = millis();
|
|
||||||
|
|
||||||
while (jsonBufferLock && millis()-now < 250) delay(1); // wait for fraction for buffer lock
|
|
||||||
|
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
// Use a recursive mutex type in case our task is the one holding the JSON buffer.
|
||||||
|
// This can happen during large JSON web transactions. In this case, we continue immediately
|
||||||
|
// and then will return out below if the lock is still held.
|
||||||
|
if (xSemaphoreTakeRecursive(jsonBufferLockMutex, 250) == pdFALSE) return false; // timed out waiting
|
||||||
|
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
// If we're in system context, delay() won't return control to the user context, so there's
|
||||||
|
// no point in waiting.
|
||||||
|
if (can_yield()) {
|
||||||
|
unsigned long now = millis();
|
||||||
|
while (jsonBufferLock && (millis()-now < 250)) delay(1); // wait for fraction for buffer lock
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#error Unsupported task framework - fix requestJSONBufferLock
|
||||||
|
#endif
|
||||||
|
// If the lock is still held - by us, or by another task
|
||||||
if (jsonBufferLock) {
|
if (jsonBufferLock) {
|
||||||
DEBUG_PRINT(F("ERROR: Locking JSON buffer failed! (still locked by "));
|
DEBUG_PRINTF_P(PSTR("ERROR: Locking JSON buffer (%d) failed! (still locked by %d)\n"), module, jsonBufferLock);
|
||||||
DEBUG_PRINT(jsonBufferLock);
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
DEBUG_PRINTLN(")");
|
xSemaphoreGiveRecursive(jsonBufferLockMutex);
|
||||||
return false; // waiting time-outed
|
#endif
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBufferLock = module ? module : 255;
|
jsonBufferLock = module ? module : 255;
|
||||||
DEBUG_PRINT(F("JSON buffer locked. ("));
|
DEBUG_PRINTF_P(PSTR("JSON buffer locked. (%d)\n"), jsonBufferLock);
|
||||||
DEBUG_PRINT(jsonBufferLock);
|
|
||||||
DEBUG_PRINTLN(")");
|
|
||||||
pDoc->clear();
|
pDoc->clear();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -235,10 +247,11 @@ bool requestJSONBufferLock(uint8_t module)
|
|||||||
|
|
||||||
void releaseJSONBufferLock()
|
void releaseJSONBufferLock()
|
||||||
{
|
{
|
||||||
DEBUG_PRINT(F("JSON buffer released. ("));
|
DEBUG_PRINTF_P(PSTR("JSON buffer released. (%d)\n"), jsonBufferLock);
|
||||||
DEBUG_PRINT(jsonBufferLock);
|
|
||||||
DEBUG_PRINTLN(")");
|
|
||||||
jsonBufferLock = 0;
|
jsonBufferLock = 0;
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
xSemaphoreGiveRecursive(jsonBufferLockMutex);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -810,6 +810,7 @@ WLED_GLOBAL int8_t spi_sclk _INIT(SPISCLKPIN);
|
|||||||
// global ArduinoJson buffer
|
// global ArduinoJson buffer
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
WLED_GLOBAL JsonDocument *pDoc _INIT(nullptr);
|
WLED_GLOBAL JsonDocument *pDoc _INIT(nullptr);
|
||||||
|
WLED_GLOBAL SemaphoreHandle_t jsonBufferLockMutex _INIT(xSemaphoreCreateRecursiveMutex());
|
||||||
#else
|
#else
|
||||||
WLED_GLOBAL StaticJsonDocument<JSON_BUFFER_SIZE> gDoc;
|
WLED_GLOBAL StaticJsonDocument<JSON_BUFFER_SIZE> gDoc;
|
||||||
WLED_GLOBAL JsonDocument *pDoc _INIT(&gDoc);
|
WLED_GLOBAL JsonDocument *pDoc _INIT(&gDoc);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user