-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes Waker::will_wake not working (#1199)
- Loading branch information
1 parent
531d875
commit d2d4684
Showing
3 changed files
with
74 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,82 @@ | ||
use super::Event; | ||
use std::collections::HashMap; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::Arc; | ||
use std::task::Wake; | ||
use winit::event_loop::EventLoopProxy; | ||
|
||
/// List of pending tasks. | ||
#[derive(Default)] | ||
pub struct TaskList { | ||
list: HashMap<u64, Pin<Box<dyn Future<Output = ()>>>>, | ||
el: EventLoopProxy<Event>, | ||
list: HashMap<u64, Task>, | ||
next: u64, | ||
} | ||
|
||
impl TaskList { | ||
pub fn insert(&mut self, id: Option<u64>, task: Pin<Box<dyn Future<Output = ()>>>) -> u64 { | ||
// Get ID. | ||
let id = match id { | ||
Some(v) => v, | ||
None => { | ||
let v = self.next; | ||
self.next = self.next.checked_add(1).unwrap(); | ||
v | ||
} | ||
}; | ||
pub fn new(el: EventLoopProxy<Event>) -> Self { | ||
Self { | ||
el, | ||
list: HashMap::default(), | ||
next: 0, | ||
} | ||
} | ||
|
||
assert!(self.list.insert(id, task).is_none()); | ||
pub fn create(&mut self, task: impl Future<Output = ()> + 'static) -> Task { | ||
let id = self.next; | ||
|
||
self.next = self.next.checked_add(1).unwrap(); // It should be impossible but just in case. | ||
|
||
Task { | ||
future: Box::pin(task), | ||
waker: Arc::new(Waker { | ||
el: self.el.clone(), | ||
task: id, | ||
}), | ||
} | ||
} | ||
|
||
pub fn insert(&mut self, task: Task) -> u64 { | ||
let id = task.waker.task; | ||
assert!(self.list.insert(id, task).is_none()); | ||
id | ||
} | ||
|
||
pub fn remove(&mut self, id: u64) -> Option<Pin<Box<dyn Future<Output = ()>>>> { | ||
pub fn remove(&mut self, id: u64) -> Option<Task> { | ||
self.list.remove(&id) | ||
} | ||
} | ||
|
||
/// Contains a [`Future`] and its waker for a pending task. | ||
/// | ||
/// We need to use a single waker per [`Future`] to make [`std::task::Waker::will_wake()`] works. | ||
pub struct Task { | ||
future: Pin<Box<dyn Future<Output = ()>>>, | ||
waker: Arc<Waker>, | ||
} | ||
|
||
impl Task { | ||
pub fn future_mut(&mut self) -> Pin<&mut dyn Future<Output = ()>> { | ||
self.future.as_mut() | ||
} | ||
|
||
pub fn waker(&self) -> &Arc<impl Wake + Send + Sync + 'static> { | ||
&self.waker | ||
} | ||
} | ||
|
||
/// Implementation of [`Wake`]. | ||
struct Waker { | ||
el: EventLoopProxy<Event>, | ||
task: u64, | ||
} | ||
|
||
impl Wake for Waker { | ||
fn wake(self: Arc<Self>) { | ||
self.wake_by_ref(); | ||
} | ||
|
||
fn wake_by_ref(self: &Arc<Self>) { | ||
drop(self.el.send_event(Event::TaskReady(self.task))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.