Skip to content

Commit

Permalink
Multithreaded component refresh (#1232)
Browse files Browse the repository at this point in the history
* Multithreaded component refresh
* Create utils::into_iter_mut to complement utils::into_iter.
* Fix invalid parallelization `cfg`, preventing multi-threading on linux
  • Loading branch information
softdevca committed Mar 20, 2024
1 parent 42e30c8 commit b7d7f31
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
14 changes: 9 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
ComponentInner, ComponentsInner, CpuInner, NetworkDataInner, NetworksInner, ProcessInner,
SystemInner, UserInner,
utils::into_iter_mut, ComponentInner, ComponentsInner, CpuInner, NetworkDataInner,
NetworksInner, ProcessInner, SystemInner, UserInner,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -3822,9 +3822,13 @@ impl Components {
/// components.refresh();
/// ```
pub fn refresh(&mut self) {
for component in self.list_mut() {
component.refresh();
}
#[cfg(all(
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
use rayon::iter::ParallelIterator;
into_iter_mut(self.list_mut()).for_each(|component| component.refresh());
}

/// The component list will be emptied then completely recomputed.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod common;
mod debug;
#[cfg(feature = "serde")]
mod serde;
mod utils;
pub(crate) mod utils;

/// This function is only used on Linux targets, on the other platforms it does nothing and returns
/// `false`.
Expand Down
56 changes: 43 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
// Take a look at the license at the top of the repository in the LICENSE file.

/// Converts the value into a parallel iterator (if the multi-thread feature is enabled).
/// Converts the value into a parallel iterator if the `multithread` feature is enabled.
/// Uses the `rayon::iter::IntoParallelIterator` trait.
#[cfg(all(
all(
any(target_os = "macos", target_os = "windows", target_os = "freebsd",),
feature = "multithread"
),
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox")),
not(feature = "unknown-ci")
))]
#[allow(dead_code)]
pub(crate) fn into_iter<T>(val: T) -> T::Iter
where
T: rayon::iter::IntoParallelIterator,
{
val.into_par_iter()
}

/// Converts the value into a sequential iterator (if the multithread feature is disabled).
/// Converts the value into a sequential iterator if the `multithread` feature is disabled.
/// Uses the `std::iter::IntoIterator` trait.
#[cfg(any(
not(feature = "multithread"),
feature = "unknown-ci",
all(target_os = "macos", feature = "apple-sandbox")
))]
#[allow(dead_code)]
pub(crate) fn into_iter<T>(val: T) -> T::IntoIter
where
T: IntoIterator,
{
val.into_iter()
}

/// Converts the value into a parallel mutable iterator if the `multithread` feature is enabled.
/// Uses the `rayon::iter::IntoParallelRefMutIterator` trait.
#[cfg(all(
all(
any(target_os = "macos", target_os = "windows", target_os = "freebsd",),
not(feature = "multithread")
),
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox"))
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
pub(crate) fn into_iter<T>(val: T) -> T::IntoIter
pub(crate) fn into_iter_mut<'a, T>(
val: &'a mut T,
) -> <T as rayon::iter::IntoParallelRefMutIterator<'a>>::Iter
where
T: rayon::iter::IntoParallelRefMutIterator<'a> + ?Sized,
{
val.par_iter_mut()
}

// In the multithreaded version of `into_iter_mut` above, the `&mut` on the argument is indicating
// the parallel iterator is an exclusive reference. In the non-multithreaded case, the `&mut` is
// already part of `T` and specifying it will result in the argument being `&mut &mut T`.

/// Converts the value into a sequential mutable iterator if the `multithread` feature is disabled.
/// Uses the `std::iter::IntoIterator` trait.
#[cfg(any(
not(feature = "multithread"),
feature = "unknown-ci",
all(target_os = "macos", feature = "apple-sandbox")
))]
pub(crate) fn into_iter_mut<T>(val: T) -> T::IntoIter
where
T: IntoIterator,
{
Expand Down

0 comments on commit b7d7f31

Please sign in to comment.