Skip to content

Commit

Permalink
Add semaphore
Browse files Browse the repository at this point in the history
Revert "Add semaphore"

This reverts commit 5656a5e.

ProcessQueue semaphore
  • Loading branch information
1technophile committed Nov 8, 2023
1 parent bd3219f commit dd1a651
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ unsigned long timer_sys_checks = 0;
# define ARDUINOJSON_ENABLE_STD_STRING 1

# include <queue>
bool processQueue = false;
# ifndef QueueSize
# define QueueSize 32
# define QueueSize 12
# endif
#ifndef QueueTImeOut
# define QueueTImeOut 10000
#endif

#endif

Expand Down Expand Up @@ -429,6 +433,18 @@ void enqueueJsonObject(const StaticJsonDocument<JSON_MSG_BUFFER>& jsonDoc) {
Log.error(F("Empty JSON, not enqueued" CR));
return;
}
unsigned long startMillis = millis();
unsigned long currentMillis = millis();

while (jsonQueue.size() >= QueueSize || processQueue) {
currentMillis = millis();
Log.warning(F("Queue full or processing, waiting" CR));
delay(100);
if (currentMillis - startMillis > QueueTImeOut) {
Log.error(F("Queue timeout" CR));
return;
}
}
Log.trace(F("Enqueue JSON" CR));
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
JsonBundle bundle;
Expand Down Expand Up @@ -485,12 +501,13 @@ void buildTopicFromId(JsonObject& Jsondata, const char* origin) {
// Empty the documents queue
void emptyQueue() {
while (true) {
JsonBundle bundle;

if (jsonQueue.empty()) {
processQueue = false;
break;
}
JsonBundle bundle;
Log.trace(F("Dequeue JSON" CR));
processQueue = true;
bundle = jsonQueue.front();
jsonQueue.pop();

Expand Down

0 comments on commit dd1a651

Please sign in to comment.