@@ -78,9 +78,20 @@ typedef struct {
78
78
79
79
static xQueueHandle _async_queue;
80
80
static TaskHandle_t _async_service_task_handle = NULL ;
81
+
82
+
83
+ SemaphoreHandle_t _slots_lock;
81
84
const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP;
82
- static int _closed_index = 0 ;
83
85
static int _closed_slots[_number_of_closed_slots];
86
+ static int _closed_index = []() {
87
+ _slots_lock = xSemaphoreCreateBinary ();
88
+ xSemaphoreGive (_slots_lock);
89
+ for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
90
+ _closed_slots[i] = 1 ;
91
+ }
92
+ return 1 ;
93
+ }();
94
+
84
95
85
96
static inline bool _init_async_event_queue (){
86
97
if (!_async_queue){
@@ -554,22 +565,16 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
554
565
_pcb = pcb;
555
566
_closed_slot = -1 ;
556
567
if (_pcb){
557
- _closed_slot = 0 ;
558
- if (_closed_index == 0 ) {
559
- _closed_index = 1 ;
560
- for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
561
- _closed_slots[i] = 1 ;
562
- }
563
- } else {
564
- int closed_slot_min_index = _closed_slots[0 ];
565
- for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
566
- if (_closed_slots[i] <= closed_slot_min_index && _closed_slots[i] != 0 ) {
567
- closed_slot_min_index = _closed_slots[i];
568
- _closed_slot = i;
569
- }
568
+ xSemaphoreTake (_slots_lock, portMAX_DELAY);
569
+ int closed_slot_min_index = 0 ;
570
+ for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
571
+ if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0 ) {
572
+ closed_slot_min_index = _closed_slots[i];
573
+ _closed_slot = i;
570
574
}
571
575
}
572
576
_closed_slots[_closed_slot] = 0 ;
577
+ xSemaphoreGive (_slots_lock);
573
578
574
579
_rx_last_packet = millis ();
575
580
tcp_arg (_pcb, this );
0 commit comments