@@ -61,50 +61,34 @@ static volatile uint32_t g_cb_log_count;
6161static void wolfip_bsd_poll_task (void * arg )
6262{
6363 struct wolfIP * ipstack = (struct wolfIP * )arg ;
64- TickType_t next_heartbeat = xTaskGetTickCount () + pdMS_TO_TICKS (5000 );
6564
6665 for (;;) {
6766 uint32_t next_ms ;
6867 TickType_t delay_ticks ;
69- TickType_t now_ticks ;
7068 uint64_t now_ms = (uint64_t )xTaskGetTickCount () * (uint64_t )portTICK_PERIOD_MS ;
7169
70+ /* Run one wolfIP poll cycle under the global lock so socket operations
71+ * and timer processing see a consistent core state. */
7272 xSemaphoreTake (g_lock , portMAX_DELAY );
7373 next_ms = (uint32_t )wolfIP_poll (ipstack , now_ms );
7474 xSemaphoreGive (g_lock );
75+
76+ /* Bound sleep time to keep progress predictable and to avoid either
77+ * spinning too fast or sleeping too long when no timers are pending. */
7578 if (next_ms < WOLFIP_FREERTOS_POLL_MIN_MS ) {
7679 next_ms = WOLFIP_FREERTOS_POLL_MIN_MS ;
7780 }
7881 if (next_ms > WOLFIP_FREERTOS_POLL_MAX_MS ) {
7982 next_ms = WOLFIP_FREERTOS_POLL_MAX_MS ;
8083 }
8184
85+ /* Convert milliseconds to RTOS ticks and always sleep at least one tick
86+ * so the poll task yields CPU time to application tasks. */
8287 delay_ticks = pdMS_TO_TICKS (next_ms );
8388 if (delay_ticks == 0 ) {
8489 delay_ticks = 1 ;
8590 }
86- while (delay_ticks > 0 ) {
87- TickType_t chunk = delay_ticks ;
88- TickType_t max_chunk = pdMS_TO_TICKS (500 );
89- if (max_chunk == 0 ) {
90- max_chunk = 1 ;
91- }
92- if (chunk > max_chunk ) {
93- chunk = max_chunk ;
94- }
95- vTaskDelay (chunk );
96- delay_ticks -= chunk ;
97-
98- now_ticks = xTaskGetTickCount ();
99- if ((int32_t )(now_ticks - next_heartbeat ) >= 0 ) {
100- uint64_t virt_ms = (uint64_t )now_ticks * (uint64_t )portTICK_PERIOD_MS ;
101- printf ("[wolfip_poll] virt_ms=%lu virt_s=%lu next_ms=%lu\n" ,
102- (unsigned long )virt_ms ,
103- (unsigned long )(virt_ms / 1000u ),
104- (unsigned long )next_ms );
105- next_heartbeat = now_ticks + pdMS_TO_TICKS (5000 );
106- }
107- }
91+ vTaskDelay (delay_ticks );
10892 }
10993}
11094
0 commit comments