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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 11 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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! {

0 commit comments

Comments
 (0)