Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex for queue processing. #24

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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