Skip to content

feat(utils): introduce utils crate with compio utilities #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ members = [
"compio-quic",
"compio-runtime",
"compio-signal",
"compio-tls",
"compio-tls",
"compio-utils",
]
resolver = "2"

Expand All @@ -38,6 +39,7 @@ compio-log = { path = "./compio-log", version = "0.1.0" }
compio-tls = { path = "./compio-tls", version = "0.6.0", default-features = false }
compio-process = { path = "./compio-process", version = "0.5.0" }
compio-quic = { path = "./compio-quic", version = "0.4.0", default-features = false }
compio-utils = { path = "./compio-utils", version = "0.1.0" }

bytes = "1.7.1"
flume = "0.11.0"
Expand Down
19 changes: 19 additions & 0 deletions compio-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

[package]
name = "compio-utils"
version = "0.1.0"
description = "Utilities for compio runtime"
edition = { workspace = true }
authors = { workspace = true }
readme = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
nix = { workspace = true, features = ["sched"] }


48 changes: 48 additions & 0 deletions compio-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// Bind error
#[cfg(unix)]
pub type BindError<T> = nix::Result<T>;

/// Bind error
#[cfg(windows)]
pub type BindError<T> = std::io::Result<T>;

/// Bind current thread to given cpus
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))]
pub fn bind_to_cpu_set(cpus: impl IntoIterator<Item = usize>) -> BindError<()> {
let mut cpuset = nix::sched::CpuSet::new();
for cpu in cpus {
cpuset.set(cpu)?;
}
let pid = nix::unistd::Pid::from_raw(0);
nix::sched::sched_setaffinity(pid, &cpuset)
}

/// Bind current thread to given cpus(but not works for non-linux)
#[cfg(all(
unix,
not(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))
))]
pub fn bind_to_cpu_set(_: impl IntoIterator<Item = usize>) -> BindError<()> {
Ok(())
}

/// Bind current thread to given cpus
#[cfg(windows)]
pub fn bind_to_cpu_set(_: impl IntoIterator<Item = usize>) -> BindError<()> {
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bind_cpu() {
assert!(bind_to_cpu_set(Some(0)).is_ok());
#[cfg(all(
unix,
any(target_os = "android", target_os = "dragonfly", target_os = "linux")
))]
assert!(bind_to_cpu_set(Some(100000)).is_err());
}
}
3 changes: 3 additions & 0 deletions compio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ compio-log = { workspace = true }
compio-tls = { workspace = true, optional = true }
compio-process = { workspace = true, optional = true }
compio-quic = { workspace = true, optional = true }
compio-utils = { workspace = true, optional = true }

# Shared dev dependencies for all platforms
[dev-dependencies]
Expand Down Expand Up @@ -109,6 +110,7 @@ tls = ["dep:compio-tls"]
native-tls = ["tls", "compio-tls/native-tls"]
rustls = ["tls", "compio-tls/rustls"]
process = ["dep:compio-process"]
utils = ["dep:compio-utils"]
quic = ["dep:compio-quic"]
h3 = ["quic", "compio-quic/h3"]
all = [
Expand All @@ -121,6 +123,7 @@ all = [
"process",
"quic",
"h3",
"utils",
]

arrayvec = ["compio-buf/arrayvec"]
Expand Down
3 changes: 3 additions & 0 deletions compio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub use compio_signal as signal;
#[cfg(feature = "tls")]
#[doc(inline)]
pub use compio_tls as tls;
#[cfg(feature = "utils")]
#[doc(inline)]
pub use compio_utils as utils;
#[cfg(feature = "event")]
#[doc(no_inline)]
pub use runtime::event;
Expand Down
Loading