Skip to content

Commit 3dd59d7

Browse files
author
Stjepan Glavina
authored
Refactor the task module (#421)
* Refactor the task module * Fix clippy warning * Simplify task-local entries * Reduce the amount of future wrapping * Cleanup * Simplify stealing
1 parent c1e8517 commit 3dd59d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+817
-761
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ unstable = ["broadcaster"]
2727
[dependencies]
2828
async-macros = "1.0.0"
2929
async-task = "1.0.0"
30+
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
3031
crossbeam-channel = "0.3.9"
3132
crossbeam-deque = "0.7.1"
3233
crossbeam-utils = "0.6.6"
3334
futures-core-preview = "=0.3.0-alpha.19"
3435
futures-io-preview = "=0.3.0-alpha.19"
3536
futures-timer = "1.0.2"
37+
kv-log-macro = "1.0.4"
3638
log = { version = "0.4.8", features = ["kv_unstable"] }
3739
memchr = "2.2.1"
3840
mio = "0.6.19"
3941
mio-uds = "0.6.7"
4042
num_cpus = "1.10.1"
4143
once_cell = "1.2.0"
44+
pin-project-lite = "0.1"
4245
pin-utils = "0.1.0-alpha.4"
4346
slab = "0.4.2"
44-
kv-log-macro = "1.0.4"
45-
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
46-
pin-project-lite = "0.1"
4747

4848
[dev-dependencies]
4949
femme = "1.2.0"

src/fs/canonicalize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::{Path, PathBuf};
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Returns the canonical form of a path.
66
///
@@ -32,5 +32,5 @@ use crate::task::blocking;
3232
/// ```
3333
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3434
let path = path.as_ref().to_owned();
35-
blocking::spawn(move || std::fs::canonicalize(&path).map(Into::into)).await
35+
spawn_blocking(move || std::fs::canonicalize(&path).map(Into::into)).await
3636
}

src/fs/copy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Copies the contents and permissions of a file to a new location.
66
///
@@ -41,5 +41,5 @@ use crate::task::blocking;
4141
pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
4242
let from = from.as_ref().to_owned();
4343
let to = to.as_ref().to_owned();
44-
blocking::spawn(move || std::fs::copy(&from, &to)).await
44+
spawn_blocking(move || std::fs::copy(&from, &to)).await
4545
}

src/fs/create_dir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Creates a new directory.
66
///
@@ -34,5 +34,5 @@ use crate::task::blocking;
3434
/// ```
3535
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
3636
let path = path.as_ref().to_owned();
37-
blocking::spawn(move || std::fs::create_dir(path)).await
37+
spawn_blocking(move || std::fs::create_dir(path)).await
3838
}

src/fs/create_dir_all.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Creates a new directory and all of its parents if they are missing.
66
///
@@ -29,5 +29,5 @@ use crate::task::blocking;
2929
/// ```
3030
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
3131
let path = path.as_ref().to_owned();
32-
blocking::spawn(move || std::fs::create_dir_all(path)).await
32+
spawn_blocking(move || std::fs::create_dir_all(path)).await
3333
}

src/fs/dir_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::future::Future;
22

33
use crate::io;
44
use crate::path::Path;
5-
use crate::task::blocking;
5+
use crate::task::spawn_blocking;
66

77
/// A builder for creating directories with configurable options.
88
///
@@ -107,7 +107,7 @@ impl DirBuilder {
107107
}
108108

109109
let path = path.as_ref().to_owned();
110-
async move { blocking::spawn(move || builder.create(path)).await }
110+
async move { spawn_blocking(move || builder.create(path)).await }
111111
}
112112
}
113113

src/fs/dir_entry.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use crate::fs::{FileType, Metadata};
66
use crate::io;
77
use crate::path::PathBuf;
8-
use crate::task::blocking;
8+
use crate::task::spawn_blocking;
99

1010
/// An entry in a directory.
1111
///
@@ -87,7 +87,7 @@ impl DirEntry {
8787
/// ```
8888
pub async fn metadata(&self) -> io::Result<Metadata> {
8989
let inner = self.0.clone();
90-
blocking::spawn(move || inner.metadata()).await
90+
spawn_blocking(move || inner.metadata()).await
9191
}
9292

9393
/// Reads the file type for this entry.
@@ -125,7 +125,7 @@ impl DirEntry {
125125
/// ```
126126
pub async fn file_type(&self) -> io::Result<FileType> {
127127
let inner = self.0.clone();
128-
blocking::spawn(move || inner.file_type()).await
128+
spawn_blocking(move || inner.file_type()).await
129129
}
130130

131131
/// Returns the bare name of this entry without the leading path.

src/fs/file.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::future;
1212
use crate::io::{self, Read, Seek, SeekFrom, Write};
1313
use crate::path::Path;
1414
use crate::prelude::*;
15-
use crate::task::{self, blocking, Context, Poll, Waker};
15+
use crate::task::{self, spawn_blocking, Context, Poll, Waker};
1616

1717
/// An open file on the filesystem.
1818
///
@@ -112,7 +112,7 @@ impl File {
112112
/// ```
113113
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
114114
let path = path.as_ref().to_owned();
115-
let file = blocking::spawn(move || std::fs::File::open(&path)).await?;
115+
let file = spawn_blocking(move || std::fs::File::open(&path)).await?;
116116
Ok(File::new(file, true))
117117
}
118118

@@ -147,7 +147,7 @@ impl File {
147147
/// ```
148148
pub async fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
149149
let path = path.as_ref().to_owned();
150-
let file = blocking::spawn(move || std::fs::File::create(&path)).await?;
150+
let file = spawn_blocking(move || std::fs::File::create(&path)).await?;
151151
Ok(File::new(file, true))
152152
}
153153

@@ -180,7 +180,7 @@ impl File {
180180
})
181181
.await?;
182182

183-
blocking::spawn(move || state.file.sync_all()).await
183+
spawn_blocking(move || state.file.sync_all()).await
184184
}
185185

186186
/// Synchronizes OS-internal buffered contents to disk.
@@ -216,7 +216,7 @@ impl File {
216216
})
217217
.await?;
218218

219-
blocking::spawn(move || state.file.sync_data()).await
219+
spawn_blocking(move || state.file.sync_data()).await
220220
}
221221

222222
/// Truncates or extends the file.
@@ -249,7 +249,7 @@ impl File {
249249
})
250250
.await?;
251251

252-
blocking::spawn(move || state.file.set_len(size)).await
252+
spawn_blocking(move || state.file.set_len(size)).await
253253
}
254254

255255
/// Reads the file's metadata.
@@ -268,7 +268,7 @@ impl File {
268268
/// ```
269269
pub async fn metadata(&self) -> io::Result<Metadata> {
270270
let file = self.file.clone();
271-
blocking::spawn(move || file.metadata()).await
271+
spawn_blocking(move || file.metadata()).await
272272
}
273273

274274
/// Changes the permissions on the file.
@@ -297,7 +297,7 @@ impl File {
297297
/// ```
298298
pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
299299
let file = self.file.clone();
300-
blocking::spawn(move || file.set_permissions(perm)).await
300+
spawn_blocking(move || file.set_permissions(perm)).await
301301
}
302302
}
303303

@@ -692,7 +692,7 @@ impl LockGuard<State> {
692692
self.register(cx);
693693

694694
// Start a read operation asynchronously.
695-
blocking::spawn(move || {
695+
spawn_blocking(move || {
696696
// Read some data from the file into the cache.
697697
let res = {
698698
let State { file, cache, .. } = &mut *self;
@@ -801,7 +801,7 @@ impl LockGuard<State> {
801801
self.register(cx);
802802

803803
// Start a write operation asynchronously.
804-
blocking::spawn(move || {
804+
spawn_blocking(move || {
805805
match (&*self.file).write_all(&self.cache) {
806806
Ok(_) => {
807807
// Switch to idle mode.
@@ -834,7 +834,7 @@ impl LockGuard<State> {
834834
self.register(cx);
835835

836836
// Start a flush operation asynchronously.
837-
blocking::spawn(move || {
837+
spawn_blocking(move || {
838838
match (&*self.file).flush() {
839839
Ok(()) => {
840840
// Mark the file as flushed.

src/fs/hard_link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Creates a hard link on the filesystem.
66
///
@@ -32,5 +32,5 @@ use crate::task::blocking;
3232
pub async fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
3333
let from = from.as_ref().to_owned();
3434
let to = to.as_ref().to_owned();
35-
blocking::spawn(move || std::fs::hard_link(&from, &to)).await
35+
spawn_blocking(move || std::fs::hard_link(&from, &to)).await
3636
}

src/fs/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Reads metadata for a path.
66
///
@@ -34,7 +34,7 @@ use crate::task::blocking;
3434
/// ```
3535
pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
3636
let path = path.as_ref().to_owned();
37-
blocking::spawn(move || std::fs::metadata(path)).await
37+
spawn_blocking(move || std::fs::metadata(path)).await
3838
}
3939

4040
cfg_not_docs! {

src/fs/open_options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::future::Future;
33
use crate::fs::File;
44
use crate::io;
55
use crate::path::Path;
6-
use crate::task::blocking;
6+
use crate::task::spawn_blocking;
77

88
/// A builder for opening files with configurable options.
99
///
@@ -285,7 +285,7 @@ impl OpenOptions {
285285
let path = path.as_ref().to_owned();
286286
let options = self.0.clone();
287287
async move {
288-
let file = blocking::spawn(move || options.open(path)).await?;
288+
let file = spawn_blocking(move || options.open(path)).await?;
289289
Ok(File::new(file, true))
290290
}
291291
}

src/fs/read.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Reads the entire contents of a file as raw bytes.
66
///
@@ -36,5 +36,5 @@ use crate::task::blocking;
3636
/// ```
3737
pub async fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
3838
let path = path.as_ref().to_owned();
39-
blocking::spawn(move || std::fs::read(path)).await
39+
spawn_blocking(move || std::fs::read(path)).await
4040
}

src/fs/read_dir.rs

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

1010
/// Returns a stream of entries in a directory.
1111
///
@@ -45,7 +45,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll};
4545
/// ```
4646
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
4747
let path = path.as_ref().to_owned();
48-
blocking::spawn(move || std::fs::read_dir(path))
48+
spawn_blocking(move || std::fs::read_dir(path))
4949
.await
5050
.map(ReadDir::new)
5151
}
@@ -91,7 +91,7 @@ impl Stream for ReadDir {
9191
let mut inner = opt.take().unwrap();
9292

9393
// Start the operation asynchronously.
94-
self.0 = State::Busy(blocking::spawn(move || {
94+
self.0 = State::Busy(spawn_blocking(move || {
9595
let next = inner.next();
9696
(inner, next)
9797
}));

src/fs/read_link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::{Path, PathBuf};
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Reads a symbolic link and returns the path it points to.
66
///
@@ -28,5 +28,5 @@ use crate::task::blocking;
2828
/// ```
2929
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3030
let path = path.as_ref().to_owned();
31-
blocking::spawn(move || std::fs::read_link(path).map(Into::into)).await
31+
spawn_blocking(move || std::fs::read_link(path).map(Into::into)).await
3232
}

src/fs/read_to_string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Reads the entire contents of a file as a string.
66
///
@@ -37,5 +37,5 @@ use crate::task::blocking;
3737
/// ```
3838
pub async fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
3939
let path = path.as_ref().to_owned();
40-
blocking::spawn(move || std::fs::read_to_string(path)).await
40+
spawn_blocking(move || std::fs::read_to_string(path)).await
4141
}

src/fs/remove_dir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Removes an empty directory.
66
///
@@ -29,5 +29,5 @@ use crate::task::blocking;
2929
/// ```
3030
pub async fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
3131
let path = path.as_ref().to_owned();
32-
blocking::spawn(move || std::fs::remove_dir(path)).await
32+
spawn_blocking(move || std::fs::remove_dir(path)).await
3333
}

src/fs/remove_dir_all.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Removes a directory and all of its contents.
66
///
@@ -29,5 +29,5 @@ use crate::task::blocking;
2929
/// ```
3030
pub async fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
3131
let path = path.as_ref().to_owned();
32-
blocking::spawn(move || std::fs::remove_dir_all(path)).await
32+
spawn_blocking(move || std::fs::remove_dir_all(path)).await
3333
}

src/fs/remove_file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io;
22
use crate::path::Path;
3-
use crate::task::blocking;
3+
use crate::task::spawn_blocking;
44

55
/// Removes a file.
66
///
@@ -29,5 +29,5 @@ use crate::task::blocking;
2929
/// ```
3030
pub async fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
3131
let path = path.as_ref().to_owned();
32-
blocking::spawn(move || std::fs::remove_file(path)).await
32+
spawn_blocking(move || std::fs::remove_file(path)).await
3333
}

0 commit comments

Comments
 (0)