-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from yoshuawuyts/no-alloc-vec-merge
Implement "perfect" waking for `impl Merge for Vec` (2/2)
- Loading branch information
Showing
6 changed files
with
103 additions
and
56 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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::utils; | ||
use std::sync; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
use std::task; | ||
use std::task::Wake; | ||
use std::task::Waker; | ||
|
||
use super::Readiness; | ||
|
||
/// An efficient waker which delegates wake events. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct InlineWaker { | ||
pub(crate) id: usize, | ||
pub(crate) readiness: Arc<Mutex<Readiness>>, | ||
} | ||
|
||
impl InlineWaker { | ||
/// Create a new instance of `InlineWaker`. | ||
pub(crate) fn new(id: usize, readiness: Arc<Mutex<Readiness>>) -> Self { | ||
Self { id, readiness } | ||
} | ||
} | ||
|
||
impl Wake for InlineWaker { | ||
fn wake(self: std::sync::Arc<Self>) { | ||
let mut readiness = self.readiness.lock().unwrap(); | ||
if !readiness.set_ready(self.id) { | ||
readiness | ||
.parent_waker() | ||
.as_mut() | ||
.expect("`parent_waker` not available from `Readiness`. Did you forget to call `Readiness::set_waker`?") | ||
.wake_by_ref() | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod inline_waker; | ||
mod readiness; | ||
mod waker_list; | ||
|
||
pub(crate) use inline_waker::InlineWaker; | ||
pub(crate) use readiness::Readiness; | ||
pub(crate) use waker_list::WakerList; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
use std::task::Waker; | ||
|
||
use super::{InlineWaker, Readiness}; | ||
use crate::utils; | ||
|
||
/// A collection of wakers which delegate to an in-line waker. | ||
pub(crate) struct WakerList { | ||
wakers: Vec<Waker>, | ||
readiness: Arc<Mutex<Readiness>>, | ||
} | ||
|
||
impl WakerList { | ||
/// Create a new instance of `WakerList`. | ||
pub(crate) fn new(len: usize) -> Self { | ||
let readiness = Arc::new(Mutex::new(Readiness::new(len))); | ||
Self { | ||
wakers: (0..len) | ||
.map(|i| Arc::new(InlineWaker::new(i, readiness.clone())).into()) | ||
.collect(), | ||
readiness, | ||
} | ||
} | ||
|
||
pub(crate) fn get(&self, index: usize) -> Option<&Waker> { | ||
self.wakers.get(index) | ||
} | ||
|
||
/// Access the `Readiness`. | ||
pub(crate) fn readiness(&self) -> &Mutex<Readiness> { | ||
self.readiness.as_ref() | ||
} | ||
} |