Skip to content

Commit

Permalink
Add mutex for queue processing. (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Your Name <[email protected]>
  • Loading branch information
Fabian-Schmidt and Your Name authored Mar 31, 2024
1 parent f2a65ff commit 9c2034d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/AsyncEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ void AsyncEventSourceClient::send(const char *message, const char *event, uint32
}

void AsyncEventSourceClient::_runQueue(){
#if defined(ESP32)
if(!this->_messageQueue_mutex.try_lock()) {
return;
}
#else
if(this->_messageQueue_processing){
return;
}
this->_messageQueue_processing = true;
#endif // ESP32

while(!_messageQueue.isEmpty() && _messageQueue.front()->finished()){
_messageQueue.remove(_messageQueue.front());
}
Expand All @@ -245,6 +256,12 @@ void AsyncEventSourceClient::_runQueue(){
if(!(*i)->sent())
(*i)->send(_client);
}

#if defined(ESP32)
this->_messageQueue_mutex.unlock();
#else
this->_messageQueue_processing = false;
#endif // ESP32
}


Expand Down
9 changes: 9 additions & 0 deletions src/AsyncEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <ESPAsyncTCP.h>
#endif

#if defined(ESP32)
#include <mutex>
#endif // ESP32

#ifndef SSE_MAX_QUEUED_MESSAGES
#define SSE_MAX_QUEUED_MESSAGES 32
#endif
Expand Down Expand Up @@ -75,6 +79,11 @@ class AsyncEventSourceClient {
AsyncClient *_client;
AsyncEventSource *_server;
uint32_t _lastId;
#if defined(ESP32)
std::mutex _messageQueue_mutex;
#else
bool _messageQueue_processing;
#endif // ESP32
LinkedList<AsyncEventSourceMessage *> _messageQueue;
void _queueMessage(AsyncEventSourceMessage *dataMessage);
void _runQueue();
Expand Down

0 comments on commit 9c2034d

Please sign in to comment.