Skip to content

Commit

Permalink
refactor(bindings/java): replace num_cpus with `std::thread::availa…
Browse files Browse the repository at this point in the history
…ble_parallelism` (#5080)

refactor(bindings/java): replace num_cpus with std available_parallelism
  • Loading branch information
miroim committed Aug 31, 2024
1 parent 48e3bf7 commit d71bd8e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 0 additions & 1 deletion bindings/java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ services-yandex-disk = ["opendal/services-yandex-disk"]
[dependencies]
anyhow = "1.0.71"
jni = "0.21.1"
num_cpus = "1.15.0"
once_cell = "1.19.0"
# this crate won't be published, we always use the local version
opendal = { version = ">=0", path = "../../core", features = [
Expand Down
9 changes: 8 additions & 1 deletion bindings/java/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use std::cell::RefCell;
use std::ffi::c_void;
use std::future::Future;
use std::num::NonZeroUsize;
use std::thread::available_parallelism;

use jni::objects::JClass;
use jni::objects::JObject;
Expand Down Expand Up @@ -152,5 +154,10 @@ pub(crate) fn executor_or_default<'a>(
///
/// This function could be only when the lib is loaded.
unsafe fn default_executor<'a>(env: &mut JNIEnv<'a>) -> Result<&'a Executor> {
RUNTIME.get_or_try_init(|| make_tokio_executor(env, num_cpus::get()))
RUNTIME.get_or_try_init(|| {
make_tokio_executor(
env,
available_parallelism().map(NonZeroUsize::get).unwrap_or(1),
)
})
}

0 comments on commit d71bd8e

Please sign in to comment.