From 7c630ece5442978ed5947379576ef271bfd094d8 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 14 Mar 2026 22:20:10 +0000 Subject: [PATCH] sched: add wait begin/end functions that don't change thread state these functions can be used when waiting on multiple queues at once, to prevent the thread state from being changed unexpectedly while initialising a set of wait items. --- include/kernel/wait.h | 6 ++++++ sched/wait.c | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/kernel/wait.h b/include/kernel/wait.h index 07af1d5..2903702 100644 --- a/include/kernel/wait.h +++ b/include/kernel/wait.h @@ -32,6 +32,12 @@ struct waitqueue { extern void wait_item_init(struct wait_item *item, struct thread *thr); extern void thread_wait_begin(struct wait_item *waiter, struct waitqueue *q); extern void thread_wait_end(struct wait_item *waiter, struct waitqueue *q); +extern void thread_wait_begin_nosleep( + struct wait_item *waiter, + struct waitqueue *q); +extern void thread_wait_end_nosleep( + struct wait_item *waiter, + struct waitqueue *q); extern void wait_on_queue(struct waitqueue *q); extern void wakeup_queue(struct waitqueue *q); extern void wakeup_one(struct waitqueue *q); diff --git a/sched/wait.c b/sched/wait.c index 06f7ea9..0005bac 100644 --- a/sched/wait.c +++ b/sched/wait.c @@ -32,6 +32,26 @@ void thread_wait_end(struct wait_item *waiter, struct waitqueue *q) spin_unlock_irqrestore(&q->wq_lock, flags); } +void thread_wait_begin_nosleep(struct wait_item *waiter, struct waitqueue *q) +{ + unsigned long flags; + spin_lock_irqsave(&q->wq_lock, &flags); + + queue_push_back(&q->wq_waiters, &waiter->w_entry); + + spin_unlock_irqrestore(&q->wq_lock, flags); +} + +void thread_wait_end_nosleep(struct wait_item *waiter, struct waitqueue *q) +{ + unsigned long flags; + spin_lock_irqsave(&q->wq_lock, &flags); + + queue_delete(&q->wq_waiters, &waiter->w_entry); + + spin_unlock_irqrestore(&q->wq_lock, flags); +} + void wakeup_queue(struct waitqueue *q) { unsigned long flags;