Skip to content

Commit 9ab7b1a

Browse files
authored
Merge pull request #251 from async-rs/blocking-unstable
add an unstable `task::blocking` function
2 parents 460b8af + c27623c commit 9ab7b1a

File tree

7 files changed

+34
-41
lines changed

7 files changed

+34
-41
lines changed

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, JoinHandle, Poll};
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 {

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, JoinHandle, Poll};
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.

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.

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.

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

src/task/blocking.rs

+8-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,15 @@ 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+
pub(crate) fn spawn<F, R>(future: F) -> JoinHandle<R>
102100
where
103101
F: Future<Output = R> + Send + 'static,
104102
R: Send + 'static,
105103
{
106-
let (task, handle) = async_task::spawn(future, schedule, ());
104+
let tag = Tag::new(None);
105+
let (task, handle) = async_task::spawn(future, schedule, tag);
107106
task.schedule();
108-
JoinHandle(handle)
109-
}
110-
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-
}
107+
JoinHandle::new(handle)
130108
}
131109

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

src/task/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,19 @@ mod task_local;
4848
mod worker;
4949

5050
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+
// Once this function stabilizes we should merge `blocking::spawn` into this so
56+
// all code in our crate uses `task::blocking` too.
57+
#[cfg(any(feature = "unstable", feature = "docs"))]
58+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
59+
#[inline]
60+
pub fn blocking<F, R>(future: F) -> task::JoinHandle<R>
61+
where
62+
F: crate::future::Future<Output = R> + Send + 'static,
63+
R: Send + 'static,
64+
{
65+
blocking::spawn(future)
66+
}

0 commit comments

Comments
 (0)