From 35dffc151684a928d2bc24fc8b89d5e617e54b05 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 4 Nov 2025 11:45:54 -0500 Subject: [PATCH] serialqueue: Rework check_wake_receive() to receive_append_wake() Rename the function and perform list appending and locking in that function. Signed-off-by: Kevin O'Connor --- klippy/chelper/serialqueue.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/klippy/chelper/serialqueue.c b/klippy/chelper/serialqueue.c index f90f3c8ad..2762d406f 100644 --- a/klippy/chelper/serialqueue.c +++ b/klippy/chelper/serialqueue.c @@ -143,14 +143,20 @@ debug_queue_add(struct list_head *root, struct queue_message *qm) message_free(old); } -// Wake up the receiver thread if it is waiting +// Add messages and wake up the receiver thread if it is waiting static void -check_wake_receive(struct receiver *receiver) +receive_append_wake(struct receiver *receiver, struct list_head *msgs) { + int dokick = 0; + pthread_mutex_lock(&receiver->lock); + list_join_tail(msgs, &receiver->queue); if (receiver->waiting) { receiver->waiting = 0; - pthread_cond_signal(&receiver->cond); + dokick = 1; } + pthread_mutex_unlock(&receiver->lock); + if (dokick) + pthread_cond_signal(&receiver->cond); } // Write to the internal pipe to wake the background thread if in poll @@ -297,12 +303,8 @@ handle_message(struct serialqueue *sq, double eventtime, int len) list_add_tail(&qm->node, &received); } - if (!list_empty(&received)) { - pthread_mutex_lock(&sq->receiver.lock); - list_join_tail(&received, &sq->receiver.queue); - check_wake_receive(&sq->receiver); - pthread_mutex_unlock(&sq->receiver.lock); - } + if (!list_empty(&received)) + receive_append_wake(&sq->receiver, &received); // Check fast readers struct fastreader *fr; @@ -693,9 +695,10 @@ background_thread(void *data) set_thread_name(sq->name); pollreactor_run(sq->pr); - pthread_mutex_lock(&sq->receiver.lock); - check_wake_receive(&sq->receiver); - pthread_mutex_unlock(&sq->receiver.lock); + // Wake any waiting receivers + struct list_head dummy; + list_init(&dummy); + receive_append_wake(&sq->receiver, &dummy); return NULL; }