Allow parking threads and wake correctly#89
Open
xarantolus wants to merge 2 commits into
Open
Conversation
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.
Contributor
Author
|
Tested and didn't seem to break anything |
Contributor
There was a problem hiding this comment.
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 handlernum = 7. - Updated scheduler
kick()behavior to latch a wake when the target thread isn’t sleeping yet, plus added per-threadpending_wakestate and atake_pending_wake_by_uid()helper. - Added
ParkedWaiter::wait_while(...)(usingpark_pending) and introduced basic contract tests forParkedWaiter.
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 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 on lines
+91
to
+95
| pub fn wait_while<T>( | ||
| &self, | ||
| uid: usize, | ||
| timeout_ticks: u64, | ||
| mut probe: impl FnMut() -> Option<T>, |
Comment on lines
+103
to
+108
| if sched | ||
| .sleep_until(None, now.saturating_add(duration), now) | ||
| .is_err() | ||
| { | ||
| bug!("no current thread set."); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This prevents use from getting lost wakeups.
Code example:
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.