Skip to content

Allow parking threads and wake correctly#89

Open
xarantolus wants to merge 2 commits into
mainfrom
feature/waiting-primitives
Open

Allow parking threads and wake correctly#89
xarantolus wants to merge 2 commits into
mainfrom
feature/waiting-primitives

Conversation

@xarantolus
Copy link
Copy Markdown
Contributor

This prevents use from getting lost wakeups.

Code example:

static FLAG:   AtomicBool   = false;   // condition we're waiting for
static WAITER: AtomicUsize  = 0;       // uid to wake, 0 = nobody

if FLAG.swap(false) { return; }
WAITER.store(my_uid());
// What if kick happens here? -> we lose the wakeup
sleep_forever();

Now with park_pending, we check the flag and don't lose the wakeup.

In addition, this also exposes the ParkedWaiter to the user, e.g. to implement efficient blocking queues.

This prevents use from getting lost wakeups.

Code example:

```
static FLAG:   AtomicBool   = false;   // condition we're waiting for
static WAITER: AtomicUsize  = 0;       // uid to wake, 0 = nobody

if FLAG.swap(false) { return; }
WAITER.store(my_uid());
// What if kick happens here? -> we lose the wakeup
sleep_forever();
```

Now with park_pending, we check the flag and don't lose the wakeup.

In addition, this also exposes the ParkedWaiter to the user, e.g. to implement efficient blocking queues.
@xarantolus
Copy link
Copy Markdown
Contributor Author

Tested and didn't seem to break anything

@xarantolus xarantolus marked this pull request as ready for review May 18, 2026 13:47
Copilot AI review requested due to automatic review settings May 18, 2026 13:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a “pending wake” mechanism to the scheduler and a park_pending syscall so a wake/kick that happens before a thread actually sleeps can be latched and consumed by the next park, preventing lost wakeups. It also extends and re-exports ParkedWaiter with a userspace-oriented waiting helper to build efficient blocking patterns.

Changes:

  • Added park_pending(timeout_ticks) to the userspace scheduler API and implemented syscall handler num = 7.
  • Updated scheduler kick() behavior to latch a wake when the target thread isn’t sleeping yet, plus added per-thread pending_wake state and a take_pending_wake_by_uid() helper.
  • Added ParkedWaiter::wait_while(...) (using park_pending) and introduced basic contract tests for ParkedWaiter.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/uapi/sched.rs Adds the park_pending syscall wrapper for userspace callers.
src/syscalls/sched.rs Implements syscall handler park_pending (num 7) to consume latched wakes or sleep.
src/sync/waiter.rs Adds ParkedWaiter::wait_while helper and unit tests.
src/sched/thread.rs Introduces pending_wake flag on Thread with set/take accessors.
src/sched.rs Latches wakeups in kick() when the thread isn’t sleeping; adds take_pending_wake_by_uid.
src/lib.rs Re-exports ParkedWaiter from the crate root.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sched.rs
Comment on lines 321 to +325
if let Some(thread) = self.threads.get_mut(uid) {
thread.resume();
if res.is_err() {
// Not sleeping yet: latch the wake for the next park.
thread.set_pending_wake();
Comment thread src/sync/waiter.rs
Comment on lines +91 to +95
pub fn wait_while<T>(
&self,
uid: usize,
timeout_ticks: u64,
mut probe: impl FnMut() -> Option<T>,
Comment thread src/syscalls/sched.rs
Comment on lines +103 to +108
if sched
.sleep_until(None, now.saturating_add(duration), now)
.is_err()
{
bug!("no current thread set.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants