@ -33,7 +33,7 @@
// Timeout node with safe cancellation
struct timeout_node {
char name [ 16 ] ;
const char * name ;
void * arg ;
timeout_callback_t callback ;
uint64_t expiration_ms ; // absolute expiration time in milliseconds
@ -450,7 +450,7 @@ void process_immediate_queue(struct UASYNC* ua) {
if ( ! ua - > immediate_queue_head ) ua - > immediate_queue_tail = NULL ;
if ( node & & node - > callback ) {
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " timer→immediate %s " , node - > name [ 0 ] ? node - > name : " " ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " timer→immediate %s " , node - > name ) ;
node - > callback ( node - > arg ) ;
}
@ -485,7 +485,7 @@ static void process_timeouts(struct UASYNC* ua) {
struct timeout_node * node = ( struct timeout_node * ) entry . data ;
if ( node & & node - > callback ) {
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " timer→%s expired " , node - > name [ 0 ] ? node - > name : " " ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " timer→%s expired " , node - > name ) ;
node - > callback ( node - > arg ) ;
}
@ -545,10 +545,9 @@ void* uasync_set_timeout(struct UASYNC* ua, int timeout_tb, void* arg, timeout_c
ua - > timer_alloc_count + + ;
if ( name ) {
strncpy ( node - > name , name , sizeof ( node - > name ) - 1 ) ;
node - > name [ sizeof ( node - > name ) - 1 ] = ' \0 ' ;
node - > name = name ;
} else {
node - > name [ 0 ] = ' \0 ' ;
node - > name = " " ;
}
node - > arg = arg ;
node - > callback = callback ;
@ -591,7 +590,7 @@ void* uasync_call_soon(struct UASYNC* ua, void* user_arg, timeout_callback_t cal
}
ua - > timer_alloc_count + + ;
node - > name [ 0 ] = ' \0 ' ;
node - > name = " " ;
node - > arg = user_arg ;
node - > callback = callback ;
node - > ua = ua ;
@ -1002,24 +1001,18 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
( unsigned long long ) ( now_us_poll_entry - ua - > last_poll_exit_us ) ) ;
}
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " poll(%d sockets, %zu timers, timeout=%d.%dms) " ,
ua - > sockets - > count , ua - > timeout_heap - > size , timeout_tb > = 0 ? timeout_tb / 10000 : - 1 ,
timeout_tb > = 0 ? ( timeout_tb % 10000 ) / 10 : 0 ) ;
// Handle negative or zero timeout
if ( timeout_tb < 0 ) timeout_tb = - 1 ; // Infinite wait
else if ( timeout_tb = = 0 ) timeout_tb = 0 ; // No wait
// Get next timeout
if ( timeout_tb < 0 ) timeout_tb = - 1 ;
else if ( timeout_tb = = 0 ) timeout_tb = 0 ;
struct timeval next_timeout ;
get_next_timeout ( ua , & next_timeout ) ;
// Convert requested timeout to timeval
struct timeval req_timeout = { 0 } ;
if ( timeout_tb > = 0 ) {
req_timeout . tv_sec = timeout_tb / 10000 ;
req_timeout . tv_usec = ( timeout_tb % 10000 ) * 100 ;
}
struct timeval poll_timeout ;
if ( timeout_tb < 0 ) {
poll_timeout = next_timeout ;
@ -1033,28 +1026,27 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
}
if ( poll_timeout . tv_sec = = 0 & & poll_timeout . tv_usec = = 0 & & timeout_tb > 0 ) poll_timeout = req_timeout ;
int timeout_ms ;
if ( timeout_tb < 0 & & ( next_timeout . tv_sec > 0 | | next_timeout . tv_usec > 0 ) ) {
timeout_ms = ( int ) timeval_to_ms ( & poll_timeout ) ;
} else if ( timeout_tb < 0 ) {
timeout_ms = - 1 ; // Infinite
timeout_ms = - 1 ;
} else {
timeout_ms = ( int ) timeval_to_ms ( & poll_timeout ) ;
}
// Count active sockets
int socket_count = ua - > sockets - > count ;
if ( socket_count = = 0 & & timeout_ms = = - 1 ) {
// No sockets and infinite wait - but we have timers? Wait for timer
if ( ua - > timeout_heap - > size > 0 ) {
timeout_ms = ( int ) timeval_to_ms ( & poll_timeout ) ;
} else {
// Nothing to do - return immediately
ua - > last_poll_exit_us = get_time_us ( ) ;
return ;
}
}
DEBUG_DEBUG ( DEBUG_CATEGORY_SYS , " poll(%d sockets, %zu timers, timeout=%dms) " ,
socket_count , ua - > timeout_heap - > size , timeout_ms ) ;
# if HAS_EPOLL
// Use epoll on Linux if available