Skip to content

Commit a65d158

Browse files
committed
Prevent concurrency in _allocate_closed_slot
1 parent 8210523 commit a65d158

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/AsyncTCP.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -994,23 +994,25 @@ int8_t AsyncClient::_close() {
994994
}
995995

996996
bool AsyncClient::_allocate_closed_slot() {
997-
if (_closed_slot != INVALID_CLOSED_SLOT) {
998-
return true;
999-
}
997+
bool allocated = false;
1000998
if (xSemaphoreTake(_slots_lock, portMAX_DELAY) == pdTRUE) {
1001999
uint32_t closed_slot_min_index = 0;
1002-
for (int i = 0; i < _number_of_closed_slots; ++i) {
1003-
if ((_closed_slot == INVALID_CLOSED_SLOT || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) {
1004-
closed_slot_min_index = _closed_slots[i];
1005-
_closed_slot = i;
1000+
allocated = _closed_slot != INVALID_CLOSED_SLOT;
1001+
if (!allocated) {
1002+
for (int i = 0; i < _number_of_closed_slots; ++i) {
1003+
if ((_closed_slot == INVALID_CLOSED_SLOT || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) {
1004+
closed_slot_min_index = _closed_slots[i];
1005+
_closed_slot = i;
1006+
}
1007+
}
1008+
allocated = _closed_slot != INVALID_CLOSED_SLOT;
1009+
if (allocated) {
1010+
_closed_slots[_closed_slot] = 0;
10061011
}
1007-
}
1008-
if (_closed_slot != INVALID_CLOSED_SLOT) {
1009-
_closed_slots[_closed_slot] = 0;
10101012
}
10111013
xSemaphoreGive(_slots_lock);
10121014
}
1013-
return (_closed_slot != INVALID_CLOSED_SLOT);
1015+
return allocated;
10141016
}
10151017

10161018
void AsyncClient::_free_closed_slot() {

0 commit comments

Comments
 (0)