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
add mapped_futures #2751
Open
StoicDeveloper
wants to merge
6
commits into
rust-lang:master
Choose a base branch
from
StoicDeveloper:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add mapped_futures #2751
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85da253
add mapped_futures
StoicDeveloper 373f965
add comments
StoicDeveloper 24082a0
futures_unordered consumes futures_keyed
StoicDeveloper 6421401
implement mapped_futures and futures_unordered as consumer of futures…
StoicDeveloper 6892653
clarify comment
StoicDeveloper 075f8fb
uncomment testing code
StoicDeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
File renamed without changes.
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,178 @@ | ||
use super::task::Task; | ||
use super::{FuturesKeyed, ReleasesTask}; | ||
use core::marker::PhantomData; | ||
use core::pin::Pin; | ||
use core::ptr; | ||
use core::sync::atomic::Ordering::Relaxed; | ||
|
||
/// Mutable iterator over all futures in the unordered set. | ||
#[derive(Debug)] | ||
pub(crate) struct IterPinMut<'a, K, Fut, S: ReleasesTask<K>> { | ||
pub(super) task: *const Task<K, Fut>, | ||
pub(crate) len: usize, | ||
pub(super) _marker: PhantomData<&'a mut FuturesKeyed<K, Fut, S>>, | ||
} | ||
|
||
/// Mutable iterator over all futures in the unordered set. | ||
#[derive(Debug)] | ||
pub(crate) struct IterMut<'a, K, Fut: Unpin, S: ReleasesTask<K>>( | ||
pub(super) IterPinMut<'a, K, Fut, S>, | ||
); | ||
|
||
/// Immutable iterator over all futures in the unordered set. | ||
#[derive(Debug)] | ||
pub(crate) struct IterPinRef<'a, K, Fut, S: ReleasesTask<K>> { | ||
pub(super) task: *const Task<K, Fut>, | ||
pub(crate) len: usize, | ||
pub(super) pending_next_all: *mut Task<K, Fut>, | ||
pub(super) _marker: PhantomData<&'a FuturesKeyed<K, Fut, S>>, | ||
} | ||
|
||
/// Immutable iterator over all the futures in the unordered set. | ||
#[derive(Debug)] | ||
pub(crate) struct Iter<'a, K, Fut: Unpin, S: ReleasesTask<K>>(pub(super) IterPinRef<'a, K, Fut, S>); | ||
|
||
/// Owned iterator over all futures in the unordered set. | ||
#[derive(Debug)] | ||
pub(crate) struct IntoIter<K, Fut: Unpin, S: ReleasesTask<K>> { | ||
pub(crate) len: usize, | ||
pub(super) inner: FuturesKeyed<K, Fut, S>, | ||
} | ||
|
||
impl<K, Fut: Unpin, S: ReleasesTask<K>> Iterator for IntoIter<K, Fut, S> { | ||
type Item = (K, Fut); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
// `head_all` can be accessed directly and we don't need to spin on | ||
// `Task::next_all` since we have exclusive access to the set. | ||
let task = self.inner.head_all.get_mut(); | ||
|
||
if (*task).is_null() { | ||
return None; | ||
} | ||
|
||
unsafe { | ||
// Moving out of the future is safe because it is `Unpin` | ||
let future = (*(**task).future.get()).take().unwrap(); | ||
let key = (**task).take_key(); | ||
|
||
// Mutable access to a previously shared `FuturesKeyed` implies | ||
// that the other threads already released the object before the | ||
// current thread acquired it, so relaxed ordering can be used and | ||
// valid `next_all` checks can be skipped. | ||
let next = (**task).next_all.load(Relaxed); | ||
*task = next; | ||
if !task.is_null() { | ||
*(**task).prev_all.get() = ptr::null_mut(); | ||
} | ||
self.len -= 1; | ||
Some((key, future)) | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.len, Some(self.len)) | ||
} | ||
} | ||
|
||
impl<K, Fut: Unpin, S: ReleasesTask<K>> ExactSizeIterator for IntoIter<K, Fut, S> {} | ||
|
||
impl<'a, K, Fut, S: ReleasesTask<K>> Iterator for IterPinMut<'a, K, Fut, S> { | ||
type Item = (&'a K, Pin<&'a mut Fut>); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.task.is_null() { | ||
return None; | ||
} | ||
|
||
unsafe { | ||
let future = (*(*self.task).future.get()).as_mut().unwrap(); | ||
let key = (*(*self.task).key.get()).as_ref().unwrap(); | ||
|
||
// Mutable access to a previously shared `FuturesKeyed` implies | ||
// that the other threads already released the object before the | ||
// current thread acquired it, so relaxed ordering can be used and | ||
// valid `next_all` checks can be skipped. | ||
let next = (*self.task).next_all.load(Relaxed); | ||
self.task = next; | ||
self.len -= 1; | ||
Some((key, Pin::new_unchecked(future))) | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.len, Some(self.len)) | ||
} | ||
} | ||
|
||
impl<K, Fut, S: ReleasesTask<K>> ExactSizeIterator for IterPinMut<'_, K, Fut, S> {} | ||
|
||
impl<'a, K, Fut: Unpin, S: ReleasesTask<K>> Iterator for IterMut<'a, K, Fut, S> { | ||
type Item = (&'a K, &'a mut Fut); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.0.next().map(|opt| (opt.0, Pin::get_mut(opt.1))) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
impl<K, Fut: Unpin, S: ReleasesTask<K>> ExactSizeIterator for IterMut<'_, K, Fut, S> {} | ||
|
||
impl<'a, K, Fut, S: ReleasesTask<K>> Iterator for IterPinRef<'a, K, Fut, S> { | ||
type Item = (&'a K, Pin<&'a Fut>); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.task.is_null() { | ||
return None; | ||
} | ||
|
||
unsafe { | ||
let future = (*(*self.task).future.get()).as_ref().unwrap(); | ||
let key = (*(*self.task).key.get()).as_ref().unwrap(); | ||
|
||
// Relaxed ordering can be used since acquire ordering when | ||
// `head_all` was initially read for this iterator implies acquire | ||
// ordering for all previously inserted nodes (and we don't need to | ||
// read `len_all` again for any other nodes). | ||
let next = (*self.task).spin_next_all(self.pending_next_all, Relaxed); | ||
self.task = next; | ||
self.len -= 1; | ||
Some((key, Pin::new_unchecked(future))) | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.len, Some(self.len)) | ||
} | ||
} | ||
|
||
impl<K, Fut, S: ReleasesTask<K>> ExactSizeIterator for IterPinRef<'_, K, Fut, S> {} | ||
|
||
impl<'a, K, Fut: Unpin, S: ReleasesTask<K>> Iterator for Iter<'a, K, Fut, S> { | ||
type Item = (&'a K, &'a Fut); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
// self.0.next().map(Pin::get_ref) | ||
self.0.next().map(|opt| (opt.0, Pin::get_ref(opt.1))) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
impl<K, Fut: Unpin, S: ReleasesTask<K>> ExactSizeIterator for Iter<'_, K, Fut, S> {} | ||
|
||
// SAFETY: we do nothing thread-local and there is no interior mutability, | ||
// so the usual structural `Send`/`Sync` apply. | ||
unsafe impl<K, Fut: Send, S: ReleasesTask<K>> Send for IterPinRef<'_, K, Fut, S> {} | ||
unsafe impl<K, Fut: Sync, S: ReleasesTask<K>> Sync for IterPinRef<'_, K, Fut, S> {} | ||
|
||
unsafe impl<K, Fut: Send, S: ReleasesTask<K>> Send for IterPinMut<'_, K, Fut, S> {} | ||
unsafe impl<K, Fut: Sync, S: ReleasesTask<K>> Sync for IterPinMut<'_, K, Fut, S> {} | ||
|
||
unsafe impl<K, Fut: Send + Unpin, S: ReleasesTask<K>> Send for IntoIter<K, Fut, S> {} | ||
unsafe impl<K, Fut: Sync + Unpin, S: ReleasesTask<K>> Sync for IntoIter<K, Fut, S> {} | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Keys are also returned from the iterator, so they must also be Send or Sync.