summaryrefslogtreecommitdiffstats
path: root/vendor/futures-executor/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:29 +0000
commit631cd5845e8de329d0e227aaa707d7ea228b8f8f (patch)
treea1b87c8f8cad01cf18f7c5f57a08f102771ed303 /vendor/futures-executor/src
parentAdding debian version 1.69.0+dfsg1-1. (diff)
downloadrustc-631cd5845e8de329d0e227aaa707d7ea228b8f8f.tar.xz
rustc-631cd5845e8de329d0e227aaa707d7ea228b8f8f.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/futures-executor/src')
-rw-r--r--vendor/futures-executor/src/enter.rs2
-rw-r--r--vendor/futures-executor/src/local_pool.rs106
-rw-r--r--vendor/futures-executor/src/thread_pool.rs33
3 files changed, 74 insertions, 67 deletions
diff --git a/vendor/futures-executor/src/enter.rs b/vendor/futures-executor/src/enter.rs
index 5895a9efb..cb58c30bb 100644
--- a/vendor/futures-executor/src/enter.rs
+++ b/vendor/futures-executor/src/enter.rs
@@ -34,7 +34,7 @@ impl std::error::Error for EnterError {}
/// executor.
///
/// Executor implementations should call this function before beginning to
-/// execute a tasks, and drop the returned [`Enter`](Enter) value after
+/// execute a task, and drop the returned [`Enter`](Enter) value after
/// completing task execution:
///
/// ```
diff --git a/vendor/futures-executor/src/local_pool.rs b/vendor/futures-executor/src/local_pool.rs
index bee96d8db..8a9bc2fc9 100644
--- a/vendor/futures-executor/src/local_pool.rs
+++ b/vendor/futures-executor/src/local_pool.rs
@@ -63,7 +63,7 @@ thread_local! {
impl ArcWake for ThreadNotify {
fn wake_by_ref(arc_self: &Arc<Self>) {
// Make sure the wakeup is remembered until the next `park()`.
- let unparked = arc_self.unparked.swap(true, Ordering::Relaxed);
+ let unparked = arc_self.unparked.swap(true, Ordering::Release);
if !unparked {
// If the thread has not been unparked yet, it must be done
// now. If it was actually parked, it will run again,
@@ -90,33 +90,21 @@ fn run_executor<T, F: FnMut(&mut Context<'_>) -> Poll<T>>(mut f: F) -> T {
if let Poll::Ready(t) = f(&mut cx) {
return t;
}
- // Consume the wakeup that occurred while executing `f`, if any.
- let unparked = thread_notify.unparked.swap(false, Ordering::Acquire);
- if !unparked {
+
+ // Wait for a wakeup.
+ while !thread_notify.unparked.swap(false, Ordering::Acquire) {
// No wakeup occurred. It may occur now, right before parking,
// but in that case the token made available by `unpark()`
// is guaranteed to still be available and `park()` is a no-op.
thread::park();
- // When the thread is unparked, `unparked` will have been set
- // and needs to be unset before the next call to `f` to avoid
- // a redundant loop iteration.
- thread_notify.unparked.store(false, Ordering::Release);
}
}
})
}
-fn poll_executor<T, F: FnMut(&mut Context<'_>) -> T>(mut f: F) -> T {
- let _enter = enter().expect(
- "cannot execute `LocalPool` executor from within \
- another executor",
- );
-
- CURRENT_THREAD_NOTIFY.with(|thread_notify| {
- let waker = waker_ref(thread_notify);
- let mut cx = Context::from_waker(&waker);
- f(&mut cx)
- })
+/// Check for a wakeup, but don't consume it.
+fn woken() -> bool {
+ CURRENT_THREAD_NOTIFY.with(|thread_notify| thread_notify.unparked.load(Ordering::Acquire))
}
impl LocalPool {
@@ -212,20 +200,26 @@ impl LocalPool {
/// further use of one of the pool's run or poll methods.
/// Though only one task will be completed, progress may be made on multiple tasks.
pub fn try_run_one(&mut self) -> bool {
- poll_executor(|ctx| {
+ run_executor(|cx| {
loop {
- let ret = self.poll_pool_once(ctx);
-
- // return if we have executed a future
- if let Poll::Ready(Some(_)) = ret {
- return true;
+ self.drain_incoming();
+
+ match self.pool.poll_next_unpin(cx) {
+ // Success!
+ Poll::Ready(Some(())) => return Poll::Ready(true),
+ // The pool was empty.
+ Poll::Ready(None) => return Poll::Ready(false),
+ Poll::Pending => (),
}
- // if there are no new incoming futures
- // then there is no feature that can make progress
- // and we can return without having completed a single future
- if self.incoming.borrow().is_empty() {
- return false;
+ if !self.incoming.borrow().is_empty() {
+ // New tasks were spawned; try again.
+ continue;
+ } else if woken() {
+ // The pool yielded to us, but there's more progress to be made.
+ return Poll::Pending;
+ } else {
+ return Poll::Ready(false);
}
}
})
@@ -257,44 +251,52 @@ impl LocalPool {
/// of the pool's run or poll methods. While the function is running, all tasks
/// in the pool will try to make progress.
pub fn run_until_stalled(&mut self) {
- poll_executor(|ctx| {
- let _ = self.poll_pool(ctx);
+ run_executor(|cx| match self.poll_pool(cx) {
+ // The pool is empty.
+ Poll::Ready(()) => Poll::Ready(()),
+ Poll::Pending => {
+ if woken() {
+ Poll::Pending
+ } else {
+ // We're stalled for now.
+ Poll::Ready(())
+ }
+ }
});
}
- // Make maximal progress on the entire pool of spawned task, returning `Ready`
- // if the pool is empty and `Pending` if no further progress can be made.
+ /// Poll `self.pool`, re-filling it with any newly-spawned tasks.
+ /// Repeat until either the pool is empty, or it returns `Pending`.
+ ///
+ /// Returns `Ready` if the pool was empty, and `Pending` otherwise.
+ ///
+ /// NOTE: the pool may call `wake`, so `Pending` doesn't necessarily
+ /// mean that the pool can't make progress.
fn poll_pool(&mut self, cx: &mut Context<'_>) -> Poll<()> {
- // state for the FuturesUnordered, which will never be used
loop {
- let ret = self.poll_pool_once(cx);
+ self.drain_incoming();
- // we queued up some new tasks; add them and poll again
+ let pool_ret = self.pool.poll_next_unpin(cx);
+
+ // We queued up some new tasks; add them and poll again.
if !self.incoming.borrow().is_empty() {
continue;
}
- // no queued tasks; we may be done
- match ret {
- Poll::Pending => return Poll::Pending,
+ match pool_ret {
+ Poll::Ready(Some(())) => continue,
Poll::Ready(None) => return Poll::Ready(()),
- _ => {}
+ Poll::Pending => return Poll::Pending,
}
}
}
- // Try make minimal progress on the pool of spawned tasks
- fn poll_pool_once(&mut self, cx: &mut Context<'_>) -> Poll<Option<()>> {
- // empty the incoming queue of newly-spawned tasks
- {
- let mut incoming = self.incoming.borrow_mut();
- for task in incoming.drain(..) {
- self.pool.push(task)
- }
+ /// Empty the incoming queue of newly-spawned tasks.
+ fn drain_incoming(&mut self) {
+ let mut incoming = self.incoming.borrow_mut();
+ for task in incoming.drain(..) {
+ self.pool.push(task)
}
-
- // try to execute the next ready future
- self.pool.poll_next_unpin(cx)
}
}
diff --git a/vendor/futures-executor/src/thread_pool.rs b/vendor/futures-executor/src/thread_pool.rs
index 5e1f586eb..537100895 100644
--- a/vendor/futures-executor/src/thread_pool.rs
+++ b/vendor/futures-executor/src/thread_pool.rs
@@ -108,12 +108,15 @@ impl ThreadPool {
/// completion.
///
/// ```
+ /// # {
/// use futures::executor::ThreadPool;
///
/// let pool = ThreadPool::new().unwrap();
///
/// let future = async { /* ... */ };
/// pool.spawn_ok(future);
+ /// # }
+ /// # std::thread::sleep(std::time::Duration::from_millis(500)); // wait for background threads closed: https://github.com/rust-lang/miri/issues/1371
/// ```
///
/// > **Note**: This method is similar to `SpawnExt::spawn`, except that
@@ -346,9 +349,8 @@ impl fmt::Debug for Task {
impl ArcWake for WakeHandle {
fn wake_by_ref(arc_self: &Arc<Self>) {
- match arc_self.mutex.notify() {
- Ok(task) => arc_self.exec.state.send(Message::Run(task)),
- Err(()) => {}
+ if let Ok(task) = arc_self.mutex.notify() {
+ arc_self.exec.state.send(Message::Run(task))
}
}
}
@@ -360,16 +362,19 @@ mod tests {
#[test]
fn test_drop_after_start() {
- let (tx, rx) = mpsc::sync_channel(2);
- let _cpu_pool = ThreadPoolBuilder::new()
- .pool_size(2)
- .after_start(move |_| tx.send(1).unwrap())
- .create()
- .unwrap();
-
- // After ThreadPoolBuilder is deconstructed, the tx should be dropped
- // so that we can use rx as an iterator.
- let count = rx.into_iter().count();
- assert_eq!(count, 2);
+ {
+ let (tx, rx) = mpsc::sync_channel(2);
+ let _cpu_pool = ThreadPoolBuilder::new()
+ .pool_size(2)
+ .after_start(move |_| tx.send(1).unwrap())
+ .create()
+ .unwrap();
+
+ // After ThreadPoolBuilder is deconstructed, the tx should be dropped
+ // so that we can use rx as an iterator.
+ let count = rx.into_iter().count();
+ assert_eq!(count, 2);
+ }
+ std::thread::sleep(std::time::Duration::from_millis(500)); // wait for background threads closed: https://github.com/rust-lang/miri/issues/1371
}
}