Skip to content

Commit 4038938

Browse files
committed
impl feedback
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent df15c04 commit 4038938

File tree

7 files changed

+35
-55
lines changed

7 files changed

+35
-55
lines changed

Diff for: src/fs/read_dir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::fs::DirEntry;
55
use crate::future::Future;
66
use crate::io;
77
use crate::stream::Stream;
8-
use crate::task::{blocking, Context, Poll};
8+
use crate::task::{blocking, Context, Poll, JoinHandle};
99

1010
/// Returns a stream of entries in a directory.
1111
///
@@ -71,7 +71,7 @@ pub struct ReadDir(State);
7171
#[derive(Debug)]
7272
enum State {
7373
Idle(Option<std::fs::ReadDir>),
74-
Busy(blocking::JoinHandle<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
74+
Busy(JoinHandle<(std::fs::ReadDir, Option<io::Result<std::fs::DirEntry>>)>),
7575
}
7676

7777
impl ReadDir {

Diff for: src/io/stderr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cfg_if::cfg_if;
55

66
use crate::future::Future;
77
use crate::io::{self, Write};
8-
use crate::task::{blocking, Context, Poll};
8+
use crate::task::{blocking, Context, Poll, JoinHandle};
99

1010
/// Constructs a new handle to the standard error of the current process.
1111
///
@@ -56,7 +56,7 @@ enum State {
5656
/// The stderr is blocked on an asynchronous operation.
5757
///
5858
/// Awaiting this operation will result in the new state of the stderr.
59-
Busy(blocking::JoinHandle<State>),
59+
Busy(JoinHandle<State>),
6060
}
6161

6262
/// Inner representation of the asynchronous stderr.

Diff for: src/io/stdin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cfg_if::cfg_if;
55

66
use crate::future::{self, Future};
77
use crate::io::{self, Read};
8-
use crate::task::{blocking, Context, Poll};
8+
use crate::task::{blocking, Context, JoinHandle, Poll};
99

1010
/// Constructs a new handle to the standard input of the current process.
1111
///
@@ -57,7 +57,7 @@ enum State {
5757
/// The stdin is blocked on an asynchronous operation.
5858
///
5959
/// Awaiting this operation will result in the new state of the stdin.
60-
Busy(blocking::JoinHandle<State>),
60+
Busy(JoinHandle<State>),
6161
}
6262

6363
/// Inner representation of the asynchronous stdin.

Diff for: src/io/stdout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cfg_if::cfg_if;
55

66
use crate::future::Future;
77
use crate::io::{self, Write};
8-
use crate::task::{blocking, Context, Poll};
8+
use crate::task::{blocking, Context, JoinHandle, Poll};
99

1010
/// Constructs a new handle to the standard output of the current process.
1111
///
@@ -56,7 +56,7 @@ enum State {
5656
/// The stdout is blocked on an asynchronous operation.
5757
///
5858
/// Awaiting this operation will result in the new state of the stdout.
59-
Busy(blocking::JoinHandle<State>),
59+
Busy(JoinHandle<State>),
6060
}
6161

6262
/// Inner representation of the asynchronous stdout.

Diff for: src/net/addr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use cfg_if::cfg_if;
77

88
use crate::future::Future;
99
use crate::io;
10-
use crate::task::blocking;
11-
use crate::task::{Context, Poll};
10+
use crate::task::{blocking, Context, JoinHandle, Poll};
1211

1312
cfg_if! {
1413
if #[cfg(feature = "docs")] {
@@ -48,7 +47,7 @@ pub trait ToSocketAddrs {
4847
#[allow(missing_debug_implementations)]
4948
pub enum ToSocketAddrsFuture<'a, I> {
5049
Phantom(PhantomData<&'a ()>),
51-
Join(blocking::JoinHandle<io::Result<I>>),
50+
Join(JoinHandle<io::Result<I>>),
5251
Ready(Option<io::Result<I>>),
5352
}
5453

Diff for: src/task/blocking.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! A thread pool for running blocking functions asynchronously.
22
3-
use std::fmt;
4-
use std::pin::Pin;
53
use std::sync::atomic::{AtomicU64, Ordering};
64
use std::thread;
75
use std::time::Duration;
@@ -10,16 +8,16 @@ use crossbeam_channel::{bounded, Receiver, Sender};
108
use lazy_static::lazy_static;
119

1210
use crate::future::Future;
13-
use crate::task::{Context, Poll};
11+
use crate::task::task::{JoinHandle, Tag};
1412
use crate::utils::abort_on_panic;
1513

1614
const MAX_THREADS: u64 = 10_000;
1715

1816
static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
1917

2018
struct Pool {
21-
sender: Sender<async_task::Task<()>>,
22-
receiver: Receiver<async_task::Task<()>>,
19+
sender: Sender<async_task::Task<Tag>>,
20+
receiver: Receiver<async_task::Task<Tag>>,
2321
}
2422

2523
lazy_static! {
@@ -85,7 +83,7 @@ fn maybe_create_another_blocking_thread() {
8583
// Enqueues work, attempting to send to the threadpool in a
8684
// nonblocking way and spinning up another worker thread if
8785
// there is not a thread ready to accept the work.
88-
fn schedule(t: async_task::Task<()>) {
86+
fn schedule(t: async_task::Task<Tag>) {
8987
if let Err(err) = POOL.sender.try_send(t) {
9088
// We were not able to send to the channel without
9189
// blocking. Try to spin up another thread and then
@@ -98,35 +96,31 @@ fn schedule(t: async_task::Task<()>) {
9896
/// Spawns a blocking task.
9997
///
10098
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks.
101-
pub fn spawn<F, R>(future: F) -> JoinHandle<R>
99+
// Once this function stabilizes we should merge `blocking::spawn` into this so
100+
// all code in our crate uses `task::blocking` too.
101+
#[cfg(any(feature = "unstable", feature = "docs"))]
102+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
103+
#[inline]
104+
pub fn blocking<F, R>(future: F) -> task::JoinHandle<R>
102105
where
103-
F: Future<Output = R> + Send + 'static,
106+
F: crate::future::Future<Output = R> + Send + 'static,
104107
R: Send + 'static,
105108
{
106-
let (task, handle) = async_task::spawn(future, schedule, ());
107-
task.schedule();
108-
JoinHandle(handle)
109+
blocking::spawn(future)
109110
}
110111

111-
/// A handle to a blocking task.
112-
pub struct JoinHandle<R>(async_task::JoinHandle<R, ()>);
113-
114-
impl<R> Unpin for JoinHandle<R> {}
115-
116-
impl<R> Future for JoinHandle<R> {
117-
type Output = R;
118-
119-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
120-
Pin::new(&mut self.0).poll(cx).map(|out| out.unwrap())
121-
}
122-
}
123-
124-
impl<R> fmt::Debug for JoinHandle<R> {
125-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126-
f.debug_struct("JoinHandle")
127-
.field("handle", &self.0)
128-
.finish()
129-
}
112+
/// Spawns a blocking task.
113+
///
114+
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks.
115+
pub(crate) fn spawn<F, R>(future: F) -> JoinHandle<R>
116+
where
117+
F: Future<Output = R> + Send + 'static,
118+
R: Send + 'static,
119+
{
120+
let tag = Tag::new(None);
121+
let (task, handle) = async_task::spawn(future, schedule, tag);
122+
task.schedule();
123+
JoinHandle::new(handle)
130124
}
131125

132126
/// Generates a random number in `0..n`.

Diff for: src/task/mod.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub use std::task::{Context, Poll, Waker};
3131
pub use async_macros::ready;
3232

3333
pub use block_on::block_on;
34+
pub use blocking::blocking;
3435
pub use builder::Builder;
3536
pub use pool::spawn;
3637
pub use sleep::sleep;
@@ -48,17 +49,3 @@ mod task_local;
4849
mod worker;
4950

5051
pub(crate) mod blocking;
51-
52-
/// Spawns a blocking task.
53-
///
54-
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks.
55-
#[cfg(any(feature = "unstable", feature = "docs"))]
56-
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
57-
#[inline]
58-
pub fn blocking<F, R>(future: F) -> blocking::JoinHandle<R>
59-
where
60-
F: crate::future::Future<Output = R> + Send + 'static,
61-
R: Send + 'static,
62-
{
63-
blocking::spawn(future)
64-
}

0 commit comments

Comments
 (0)