Skip to content

Commit 07359ef

Browse files
authored
chore: replace num_cpus with available_parallelism (#2946)
Signed-off-by: tison <[email protected]>
1 parent 97ea37e commit 07359ef

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

futures-executor/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ Executors for asynchronous tasks based on the futures-rs library.
1313
[features]
1414
default = ["std"]
1515
std = ["futures-core/std", "futures-task/std", "futures-util/std"]
16-
thread-pool = ["std", "num_cpus"]
16+
thread-pool = ["std"]
1717

1818
[dependencies]
1919
futures-core = { path = "../futures-core", version = "=1.0.0-alpha.0", default-features = false }
2020
futures-task = { path = "../futures-task", version = "=0.4.0-alpha.0", default-features = false }
2121
futures-util = { path = "../futures-util", version = "=0.4.0-alpha.0", default-features = false }
22-
num_cpus = { version = "1.8.0", optional = true }
2322

2423
[dev-dependencies]
2524
futures = { path = "../futures", features = ["thread-pool"] }

futures-executor/src/thread_pool.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use futures_task::{waker_ref, ArcWake};
66
use futures_task::{FutureObj, Spawn, SpawnError};
77
use futures_util::future::FutureExt;
88
use std::boxed::Box;
9-
use std::cmp;
109
use std::fmt;
1110
use std::format;
1211
use std::io;
@@ -190,13 +189,8 @@ impl ThreadPoolBuilder {
190189
///
191190
/// See the other methods on this type for details on the defaults.
192191
pub fn new() -> Self {
193-
Self {
194-
pool_size: cmp::max(1, num_cpus::get()),
195-
stack_size: 0,
196-
name_prefix: None,
197-
after_start: None,
198-
before_stop: None,
199-
}
192+
let pool_size = thread::available_parallelism().map_or(1, |p| p.get());
193+
Self { pool_size, stack_size: 0, name_prefix: None, after_start: None, before_stop: None }
200194
}
201195

202196
/// Set size of a future ThreadPool
@@ -283,7 +277,7 @@ impl ThreadPoolBuilder {
283277
let before_stop = self.before_stop.clone();
284278
let mut thread_builder = thread::Builder::new();
285279
if let Some(ref name_prefix) = self.name_prefix {
286-
thread_builder = thread_builder.name(format!("{}{}", name_prefix, counter));
280+
thread_builder = thread_builder.name(format!("{name_prefix}{counter}"));
287281
}
288282
if self.stack_size > 0 {
289283
thread_builder = thread_builder.stack_size(self.stack_size);

futures-util/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ std = ["alloc", "futures-core/std", "futures-task/std", "slab/std"]
1616
alloc = ["futures-core/alloc", "futures-task/alloc", "slab"]
1717
async-await = []
1818
async-await-macro = ["async-await", "futures-macro"]
19-
compat = ["std", "futures_01"]
20-
io-compat = ["io", "compat", "tokio-io"]
19+
compat = ["std", "futures_01", "libc"]
20+
io-compat = ["io", "compat", "tokio-io", "libc"]
2121
sink = ["futures-sink"]
2222
io = ["std", "futures-io", "memchr"]
2323
channel = ["std", "futures-channel"]
@@ -44,6 +44,9 @@ tokio-io = { version = "0.1.9", optional = true }
4444
pin-project-lite = "0.2.6"
4545
spin = { version = "0.10.0", optional = true }
4646

47+
# INDIRECT DEPENDENCYS BUT ONLY FOR SPECIFIC MINIMAL VERSIONS
48+
libc = { version = "0.2.26", optional = true }
49+
4750
[dev-dependencies]
4851
futures = { path = "../futures", features = ["async-await", "thread-pool"] }
4952
futures-test = { path = "../futures-test" }

futures-util/src/lock/mutex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ mod tests {
553553
fn test_mutex_guard_debug_not_recurse() {
554554
let mutex = Mutex::new(42);
555555
let guard = mutex.try_lock().unwrap();
556-
let _ = format!("{:?}", guard);
556+
let _ = format!("{guard:?}");
557557
let guard = MutexGuard::map(guard, |n| n);
558-
let _ = format!("{:?}", guard);
558+
let _ = format!("{guard:?}");
559559
}
560560
}

futures-util/src/stream/futures_unordered/ready_to_run_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<Fut> ReadyToRunQueue<Fut> {
6969
return Dequeue::Data(tail);
7070
}
7171

72-
if self.head.load(Acquire) as *const _ != tail {
72+
if !core::ptr::eq(self.head.load(Acquire), tail) {
7373
return Dequeue::Inconsistent;
7474
}
7575

futures-util/src/stream/stream/try_for_each_concurrent.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::stream::{FuturesUnordered, StreamExt};
22
use core::fmt;
3-
use core::mem;
43
use core::num::NonZeroUsize;
54
use core::pin::Pin;
65
use futures_core::future::{FusedFuture, Future};
@@ -113,7 +112,7 @@ where
113112
// Empty the stream and futures so that we know
114113
// the future has completed.
115114
this.stream.set(None);
116-
drop(mem::replace(this.futures, FuturesUnordered::new()));
115+
drop(core::mem::take(this.futures));
117116
return Poll::Ready(Err(e));
118117
}
119118
}

futures/tests/oneshot.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ fn oneshot_drop_rx() {
6868
#[test]
6969
fn oneshot_debug() {
7070
let (tx, rx) = oneshot::channel::<i32>();
71-
assert_eq!(format!("{:?}", tx), "Sender { complete: false }");
72-
assert_eq!(format!("{:?}", rx), "Receiver { complete: false }");
71+
assert_eq!(format!("{tx:?}"), "Sender { complete: false }");
72+
assert_eq!(format!("{rx:?}"), "Receiver { complete: false }");
7373
drop(rx);
74-
assert_eq!(format!("{:?}", tx), "Sender { complete: true }");
74+
assert_eq!(format!("{tx:?}"), "Sender { complete: true }");
7575
let (tx, rx) = oneshot::channel::<i32>();
7676
drop(tx);
77-
assert_eq!(format!("{:?}", rx), "Receiver { complete: true }");
77+
assert_eq!(format!("{rx:?}"), "Receiver { complete: true }");
7878
}

0 commit comments

Comments
 (0)