Skip to content

Commit c447d4f

Browse files
WiFiClient/WiFiUDP list needs mutex protection
We track a list of all connections to allow the WiFi.stopAll() call (used for updates). This linked list needs to be protected from multiple cores updating it in parallel under FreeRTOS. Add a mutex around the ops.
1 parent 00a99e4 commit c447d4f

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

libraries/WiFi/src/WiFiClient.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ bool WiFiClient::getDefaultSync() {
5757

5858
template<>
5959
WiFiClient* SList<WiFiClient>::_s_first = 0;
60-
60+
#ifdef __FREERTOS
61+
template<>
62+
SemaphoreHandle_t SList<WiFiClient>::_s_first_lock = 0;
63+
template<>
64+
bool SList<WiFiClient>::_s_first_lock_created = false;
65+
#endif
6166

6267
WiFiClient::WiFiClient()
6368
: _client(0), _owned(0) {

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636

3737
template<>
3838
WiFiUDP* SList<WiFiUDP>::_s_first = 0;
39+
#ifdef __FREERTOS
40+
template<>
41+
SemaphoreHandle_t SList<WiFiUDP>::_s_first_lock = 0;
42+
template<>
43+
bool SList<WiFiUDP>::_s_first_lock_created = false;
44+
#endif
45+
3946

4047
/* Constructor */
4148
WiFiUDP::WiFiUDP() : _ctx(0), _multicast(false), _dirty(false) {

libraries/WiFi/src/include/slist.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef SLIST_H
22
#define SLIST_H
33

4+
#ifdef __FREERTOS
5+
#include "FreeRTOS.h"
6+
#include "semphr.h"
7+
#endif
8+
49
template<typename T>
510
class SList {
611
public:
@@ -9,27 +14,58 @@ class SList {
914
protected:
1015

1116
static void _add(T* self) {
17+
#ifdef __FREERTOS
18+
if (!_s_first_lock_created) {
19+
_s_first_lock_created = true;
20+
_s_first_lock = xSemaphoreCreateMutex();
21+
}
22+
xSemaphoreTake(_s_first_lock, portMAX_DELAY);
23+
#endif
1224
T* tmp = _s_first;
1325
_s_first = self;
1426
self->_next = tmp;
27+
#ifdef __FREERTOS
28+
xSemaphoreGive(_s_first_lock);
29+
#endif
1530
}
1631

1732
static void _remove(T* self) {
33+
#ifdef __FREERTOS
34+
if (!_s_first_lock_created) {
35+
_s_first_lock_created = true;
36+
_s_first_lock = xSemaphoreCreateMutex();
37+
}
38+
xSemaphoreTake(_s_first_lock, portMAX_DELAY);
39+
#endif
40+
1841
if (_s_first == self) {
1942
_s_first = self->_next;
2043
self->_next = 0;
44+
#ifdef __FREERTOS
45+
xSemaphoreGive(_s_first_lock);
46+
#endif
2147
return;
2248
}
2349

2450
for (T* prev = _s_first; prev->_next; prev = prev->_next) {
2551
if (prev->_next == self) {
2652
prev->_next = self->_next;
2753
self->_next = 0;
54+
#ifdef __FREERTOS
55+
xSemaphoreGive(_s_first_lock);
56+
#endif
2857
return;
2958
}
3059
}
60+
#ifdef __FREERTOS
61+
xSemaphoreGive(_s_first_lock);
62+
#endif
3163
}
3264

65+
#ifdef __FREERTOS
66+
static SemaphoreHandle_t _s_first_lock;
67+
static bool _s_first_lock_created;
68+
#endif
3369
static T* _s_first;
3470
T* _next;
3571
};

0 commit comments

Comments
 (0)