-
Notifications
You must be signed in to change notification settings - Fork 421
Rewrite sync bg processor #3820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,10 @@ use std::time::Duration; | |
use core::future::Future as StdFuture; | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
use std::{ | ||
sync::atomic::{AtomicBool, Ordering}, | ||
thread, | ||
}; | ||
|
||
/// Used to signal to one of many waiters that the condition they're waiting on has happened. | ||
/// | ||
|
@@ -340,6 +344,47 @@ impl Sleeper { | |
} | ||
} | ||
|
||
pub struct Sleep { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already |
||
is_done: Arc<AtomicBool>, | ||
waker: Arc<Mutex<Option<Waker>>>, | ||
} | ||
|
||
impl Sleep { | ||
pub fn new(duration: Duration) -> Self { | ||
let is_done = Arc::new(AtomicBool::new(false)); | ||
let waker: Arc<Mutex<Option<Waker>>> = Arc::new(Mutex::new(None)); | ||
|
||
let is_done_clone = is_done.clone(); | ||
let waker_clone = waker.clone(); | ||
|
||
thread::spawn(move || { | ||
thread::sleep(duration); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chatgpt implementation - probably a lot wrong about it. |
||
is_done_clone.store(true, Ordering::SeqCst); | ||
|
||
if let Some(w) = waker_clone.lock().unwrap().take() { | ||
w.wake(); | ||
} | ||
}); | ||
|
||
Self { is_done, waker } | ||
} | ||
} | ||
|
||
impl core::future::Future for Sleep { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { | ||
if self.is_done.load(Ordering::SeqCst) { | ||
Poll::Ready(()) | ||
} else { | ||
let mut waker_lock = self.waker.lock().unwrap(); | ||
// Store latest waker in case the task is moved or re-polled | ||
*waker_lock = Some(cx.waker().clone()); | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this wasn't done in the first place? All the timer checks seem to be non-blocking.