Skip to content

Commit 9f138a2

Browse files
committed
Changed cpp macros to c++ possibly-null classes, thanks willmmiles
1 parent a302812 commit 9f138a2

5 files changed

Lines changed: 61 additions & 52 deletions

File tree

src/AsyncEventSource.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
192192

193193
AsyncEventSourceClient::~AsyncEventSourceClient() {
194194
// Protect message queue access (size checks and modifications) which is not thread-safe.
195-
LOCK(_lockmq);
195+
lock_guard_type lock(_lockmq);
196196
_messageQueue.clear();
197197
close();
198198
}
199199

200200
bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
201201
// Protect message queue access (size checks and modifications) which is not thread-safe.
202-
LOCK(_lockmq);
202+
lock_guard_type lock(_lockmq);
203203

204204
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
205205
async_ws_log_w("Event message queue overflow: discard message");
@@ -228,7 +228,7 @@ bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
228228

229229
bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) {
230230
// Protect message queue access (size checks and modifications) which is not thread-safe.
231-
LOCK(_lockmq);
231+
lock_guard_type lock(_lockmq);
232232

233233
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
234234
async_ws_log_w("Event message queue overflow: discard message");
@@ -256,7 +256,7 @@ bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) {
256256

257257
void AsyncEventSourceClient::_onAck(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))) {
258258
// Protect message queue access (size checks and modifications) which is not thread-safe.
259-
LOCK(_lockmq);
259+
lock_guard_type lock(_lockmq);
260260

261261
// adjust in-flight len
262262
if (len < _inflight) {
@@ -282,7 +282,7 @@ void AsyncEventSourceClient::_onAck(size_t len __attribute__((unused)), uint32_t
282282

283283
void AsyncEventSourceClient::_onPoll() {
284284
// Protect message queue access (size checks and modifications) which is not thread-safe.
285-
LOCK(_lockmq);
285+
lock_guard_type lock(_lockmq);
286286
if (_messageQueue.size()) {
287287
_runQueue();
288288
}
@@ -363,7 +363,7 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient *client) {
363363
_connectcb(client);
364364
}
365365

366-
LOCK(_client_queue_lock);
366+
lock_guard_type lock(_client_queue_lock);
367367
_clients.emplace_back(client);
368368

369369
_adjust_inflight_window();
@@ -373,7 +373,7 @@ void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient *client) {
373373
if (_disconnectcb) {
374374
_disconnectcb(client);
375375
}
376-
LOCK(_client_queue_lock);
376+
lock_guard_type lock(_client_queue_lock);
377377
for (auto i = _clients.begin(); i != _clients.end(); ++i) {
378378
if (i->get() == client) {
379379
_clients.erase(i);
@@ -387,7 +387,7 @@ void AsyncEventSource::close() {
387387
// While the whole loop is not done, the linked list is locked and so the
388388
// iterator should remain valid even when AsyncEventSource::_handleDisconnect()
389389
// is called very early
390-
LOCK(_client_queue_lock);
390+
lock_guard_type lock(_client_queue_lock);
391391
for (const auto &c : _clients) {
392392
if (c->connected()) {
393393
/**
@@ -404,7 +404,7 @@ void AsyncEventSource::close() {
404404
size_t AsyncEventSource::avgPacketsWaiting() const {
405405
size_t aql = 0;
406406
uint32_t nConnectedClients = 0;
407-
LOCK(_client_queue_lock);
407+
lock_guard_type lock(_client_queue_lock);
408408
for (const auto &c : _clients) {
409409
if (c->connected()) {
410410
aql += c->packetsWaiting();
@@ -416,7 +416,7 @@ size_t AsyncEventSource::avgPacketsWaiting() const {
416416

417417
AsyncEventSource::SendStatus AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect) {
418418
AsyncEvent_SharedData_t shared_msg = std::make_shared<String>(generateEventMessage(message, event, id, reconnect));
419-
LOCK(_client_queue_lock);
419+
lock_guard_type lock(_client_queue_lock);
420420
size_t hits = 0;
421421
size_t miss = 0;
422422
for (const auto &c : _clients) {
@@ -432,7 +432,7 @@ AsyncEventSource::SendStatus AsyncEventSource::send(const char *message, const c
432432
}
433433

434434
size_t AsyncEventSource::count() const {
435-
LOCK(_client_queue_lock);
435+
lock_guard_type lock(_client_queue_lock);
436436
size_t n_clients{0};
437437
for (const auto &i : _clients) {
438438
if (i->connected()) {

src/AsyncEventSource.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class AsyncEventSourceClient {
136136
size_t _inflight{0}; // num of unacknowledged bytes that has been written to socket buffer
137137
size_t _max_inflight{SSE_MAX_INFLIGH}; // max num of unacknowledged bytes that could be written to socket buffer
138138
std::list<AsyncEventSourceMessage> _messageQueue;
139-
MAKE_LOCK(_lockmq);
139+
mutable mutex_type _lockmq;
140140
bool _queueMessage(const char *message, size_t len);
141141
bool _queueMessage(AsyncEvent_SharedData_t &&msg);
142142
void _runQueue();
@@ -203,7 +203,7 @@ class AsyncEventSourceClient {
203203
return _lastId;
204204
}
205205
size_t packetsWaiting() const {
206-
LOCK(_lockmq);
206+
lock_guard_type lock(_lockmq);
207207
return _messageQueue.size();
208208
};
209209

@@ -243,7 +243,7 @@ class AsyncEventSource : public AsyncWebHandler {
243243
std::list<std::unique_ptr<AsyncEventSourceClient>> _clients;
244244
// Same as for individual messages, protect mutations of _clients list
245245
// since simultaneous access from different tasks is possible
246-
MAKE_LOCK(_client_queue_lock);
246+
mutable mutex_type _client_queue_lock;
247247
ArEventHandlerFunction _connectcb = nullptr;
248248
ArEventHandlerFunction _disconnectcb = nullptr;
249249

src/AsyncWebSocket.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ AsyncWebSocketClient::AsyncWebSocketClient(AsyncClient *client, AsyncWebSocket *
294294

295295
AsyncWebSocketClient::~AsyncWebSocketClient() {
296296
{
297-
LOCK(_lock);
297+
lock_guard_type lock(_lock);
298298
_messageQueue.clear();
299299
_controlQueue.clear();
300300
}
@@ -310,7 +310,7 @@ void AsyncWebSocketClient::_clearQueue() {
310310
void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
311311
_lastMessageTime = millis();
312312

313-
UNIQUE_LOCK(_lock);
313+
unique_lock_type lock(_lock);
314314

315315
async_ws_log_v("[%s][%" PRIu32 "] START ACK(%u, %" PRIu32 ") Q:%u", _server->url(), _clientId, len, time, _messageQueue.size());
316316

@@ -328,7 +328,7 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
328328
Due to _client->close() shall call the callback function _onDisconnect()
329329
The calling flow _onDisconnect() --> _handleDisconnect() --> ~AsyncWebSocketClient()
330330
*/
331-
UNLOCK();
331+
lock.unlock();
332332
_client->close();
333333
}
334334
return;
@@ -358,11 +358,11 @@ void AsyncWebSocketClient::_onPoll() {
358358
return;
359359
}
360360

361-
UNIQUE_LOCK(_lock);
361+
unique_lock_type lock(_lock);
362362
if (_client && _client->canSend() && (!_controlQueue.empty() || !_messageQueue.empty())) {
363363
_runQueue();
364364
} else if (_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && (_controlQueue.empty() && _messageQueue.empty())) {
365-
UNLOCK();
365+
lock.unlock();
366366
ping((uint8_t *)AWSC_PING_PAYLOAD, AWSC_PING_PAYLOAD_LEN);
367367
}
368368
}
@@ -426,17 +426,17 @@ void AsyncWebSocketClient::_runQueue() {
426426
}
427427

428428
bool AsyncWebSocketClient::queueIsFull() const {
429-
LOCK(_lock);
429+
lock_guard_type lock(_lock);
430430
return (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) || (_status != WS_CONNECTED);
431431
}
432432

433433
size_t AsyncWebSocketClient::queueLen() const {
434-
LOCK(_lock);
434+
lock_guard_type lock(_lock);
435435
return _messageQueue.size();
436436
}
437437

438438
bool AsyncWebSocketClient::canSend() const {
439-
LOCK(_lock);
439+
lock_guard_type lock(_lock);
440440
return _messageQueue.size() < WS_MAX_QUEUED_MESSAGES;
441441
}
442442

@@ -445,7 +445,7 @@ bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, si
445445
return false;
446446
}
447447

448-
LOCK(_lock);
448+
lock_guard_type lock(_lock);
449449

450450
_controlQueue.emplace_back(opcode, data, len, mask);
451451
async_ws_log_v("[%s][%" PRIu32 "] QUEUE CTRL (%u) << %" PRIu8, _server->url(), _clientId, _controlQueue.size(), opcode);
@@ -462,7 +462,7 @@ bool AsyncWebSocketClient::_queueMessage(AsyncWebSocketSharedBuffer buffer, uint
462462
return false;
463463
}
464464

465-
UNIQUE_LOCK(_lock);
465+
unique_lock_type lock(_lock);
466466

467467
if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) {
468468
if (closeWhenFull) {
@@ -474,7 +474,7 @@ bool AsyncWebSocketClient::_queueMessage(AsyncWebSocketSharedBuffer buffer, uint
474474
Due to _client->close() shall call the callback function _onDisconnect()
475475
The calling flow _onDisconnect() --> _handleDisconnect() --> ~AsyncWebSocketClient()
476476
*/
477-
UNLOCK();
477+
lock.unlock();
478478
_client->close();
479479
}
480480

@@ -969,7 +969,7 @@ void AsyncWebSocket::_handleEvent(AsyncWebSocketClient *client, AwsEventType typ
969969
}
970970

971971
AsyncWebSocketClient *AsyncWebSocket::_newClient(AsyncWebServerRequest *request) {
972-
LOCK(_lock);
972+
lock_guard_type lock(_lock);
973973
_clients.emplace_back(request, this);
974974
// we've just detached AsyncTCP client from AsyncWebServerRequest
975975
_handleEvent(&_clients.back(), WS_EVT_CONNECT, request, NULL, 0);
@@ -979,7 +979,7 @@ AsyncWebSocketClient *AsyncWebSocket::_newClient(AsyncWebServerRequest *request)
979979
}
980980

981981
void AsyncWebSocket::_handleDisconnect(AsyncWebSocketClient *client) {
982-
LOCK(_lock);
982+
lock_guard_type lock(_lock);
983983
const auto client_id = client->id();
984984
const auto iter = std::find_if(std::begin(_clients), std::end(_clients), [client_id](const AsyncWebSocketClient &c) {
985985
return c.id() == client_id;
@@ -990,14 +990,14 @@ void AsyncWebSocket::_handleDisconnect(AsyncWebSocketClient *client) {
990990
}
991991

992992
bool AsyncWebSocket::availableForWriteAll() {
993-
LOCK(_lock);
993+
lock_guard_type lock(_lock);
994994
return std::none_of(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
995995
return c.queueIsFull();
996996
});
997997
}
998998

999999
bool AsyncWebSocket::availableForWrite(uint32_t id) {
1000-
LOCK(_lock);
1000+
lock_guard_type lock(_lock);
10011001
const auto iter = std::find_if(std::begin(_clients), std::end(_clients), [id](const AsyncWebSocketClient &c) {
10021002
return c.id() == id;
10031003
});
@@ -1008,14 +1008,14 @@ bool AsyncWebSocket::availableForWrite(uint32_t id) {
10081008
}
10091009

10101010
size_t AsyncWebSocket::count() const {
1011-
LOCK(_lock);
1011+
lock_guard_type lock(_lock);
10121012
return std::count_if(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
10131013
return c.status() == WS_CONNECTED;
10141014
});
10151015
}
10161016

10171017
AsyncWebSocketClient *AsyncWebSocket::client(uint32_t id) {
1018-
LOCK(_lock);
1018+
lock_guard_type lock(_lock);
10191019
const auto iter = std::find_if(_clients.begin(), _clients.end(), [id](const AsyncWebSocketClient &c) {
10201020
return c.id() == id && c.status() == WS_CONNECTED;
10211021
});
@@ -1033,7 +1033,7 @@ void AsyncWebSocket::close(uint32_t id, uint16_t code, const char *message) {
10331033
}
10341034

10351035
void AsyncWebSocket::closeAll(uint16_t code, const char *message) {
1036-
LOCK(_lock);
1036+
lock_guard_type lock(_lock);
10371037
for (auto &c : _clients) {
10381038
if (c.status() == WS_CONNECTED) {
10391039
c.close(code, message);
@@ -1042,7 +1042,7 @@ void AsyncWebSocket::closeAll(uint16_t code, const char *message) {
10421042
}
10431043

10441044
void AsyncWebSocket::cleanupClients(uint16_t maxClients) {
1045-
LOCK(_lock);
1045+
lock_guard_type lock(_lock);
10461046
const size_t c = count();
10471047
if (c > maxClients) {
10481048
async_ws_log_v("[%s] CLEANUP %" PRIu32 " (%u/%" PRIu16 ")", _url.c_str(), _clients.front().id(), c, maxClients);
@@ -1063,7 +1063,7 @@ bool AsyncWebSocket::ping(uint32_t id, const uint8_t *data, size_t len) {
10631063
}
10641064

10651065
AsyncWebSocket::SendStatus AsyncWebSocket::pingAll(const uint8_t *data, size_t len) {
1066-
LOCK(_lock);
1066+
lock_guard_type lock(_lock);
10671067
size_t hit = 0;
10681068
size_t miss = 0;
10691069
for (auto &c : _clients) {
@@ -1172,7 +1172,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::textAll(AsyncWebSocketMessageBuffer *
11721172
}
11731173

11741174
AsyncWebSocket::SendStatus AsyncWebSocket::textAll(AsyncWebSocketSharedBuffer buffer) {
1175-
LOCK(_lock);
1175+
lock_guard_type lock(_lock);
11761176
size_t hit = 0;
11771177
size_t miss = 0;
11781178
for (auto &c : _clients) {
@@ -1262,7 +1262,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::binaryAll(AsyncWebSocketMessageBuffer
12621262
return status;
12631263
}
12641264
AsyncWebSocket::SendStatus AsyncWebSocket::binaryAll(AsyncWebSocketSharedBuffer buffer) {
1265-
LOCK(_lock);
1265+
lock_guard_type lock(_lock);
12661266
size_t hit = 0;
12671267
size_t miss = 0;
12681268
for (auto &c : _clients) {

src/AsyncWebSocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class AsyncWebSocketClient {
230230
uint8_t _pstate;
231231
uint32_t _lastMessageTime;
232232
uint32_t _keepAlivePeriod;
233-
MAKE_LOCK(_lock);
233+
mutable mutex_type _lock;
234234
std::deque<AsyncWebSocketControl> _controlQueue;
235235
std::deque<AsyncWebSocketMessage> _messageQueue;
236236
bool closeWhenFull = true;
@@ -378,7 +378,7 @@ class AsyncWebSocket : public AsyncWebHandler {
378378
AwsEventHandler _eventHandler;
379379
AwsHandshakeHandler _handshakeHandler;
380380
bool _enabled;
381-
MAKE_LOCK(_lock);
381+
mutable mutex_type _lock;
382382

383383
public:
384384
typedef enum {

src/ESPAsyncWebServer.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,30 @@
5656

5757
#if defined(ESP32) || defined(HOST)
5858
#include <mutex>
59-
#define MAKE_LOCK(var) mutable std::recursive_mutex var
60-
#define LOCK(var) std::lock_guard<std::recursive_mutex> lock(var)
61-
#define UNIQUE_LOCK(var) std::unique_lock<std::recursive_mutex> lock(var)
62-
#define UNLOCK() lock.unlock()
59+
typedef std::recursive_mutex mutex_type;
60+
typedef std::lock_guard<mutex_type> lock_guard_type;
61+
typedef std::unique_lock<mutex_type> unique_lock_type;
6362
#else
64-
#define MAKE_LOCK(var)
65-
#define LOCK(var) \
66-
do { \
67-
} while (0)
68-
#define UNIQUE_LOCK(var) \
69-
do { \
70-
} while (0)
71-
#define UNLOCK() \
72-
do { \
73-
} while (0)
63+
// Do-nothing locks that will evaporate under optimization
64+
class null_mutex {
65+
public:
66+
void lock() {}
67+
void unlock() {}
68+
bool try_lock() {
69+
return true;
70+
};
71+
};
72+
typedef null_mutex mutex_type;
73+
74+
class lock_guard_type {
75+
public:
76+
lock_guard_type(mutex_type &) {};
77+
};
78+
class unique_lock_type {
79+
public:
80+
unique_lock_type(mutex_type &) {};
81+
void unlock() {};
82+
};
7483
#endif
7584

7685
#include "AsyncWebServerVersion.h"

0 commit comments

Comments
 (0)