commit 1aeb70231fa9c205c4edfc2c972253601be27de4 Author: Sean McArthur Date: Mon Mar 16 11:42:22 2015 -0700 v0.1.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4d6a22e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] + +name = "num_cpus" +version = "0.1.0" +authors = ["Sean McArthur "] +license = "MIT" +repository = "https://github.com/seanmonstar/num_cpus" +build = "build.rs" + + +[dependencies] +libc = "*" + +[build-dependencies] +gcc = "*" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8e91dc9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2015 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba370c6 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# num_cpus + +A replacement for the deprecated `std::os::num_cpus`. + +## Usage + +Add to Cargo.toml: + +``` +[dependencies] +num_cpus = "*" +``` + +In your `main.rs` or `lib.rs`: + +```rust +extern crate num_cpus; + +// elsewhere +let num = num_cpus::get(); +``` + +## License + +MIT diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..42a7fb5 --- /dev/null +++ b/build.rs @@ -0,0 +1,10 @@ +#![deny(warnings)] + +extern crate gcc; + +fn main() { + gcc::compile_library( + "libnumcpus.a", + &["extern/num_cpus.c"] + ); +} diff --git a/extern/num_cpus.c b/extern/num_cpus.c new file mode 100644 index 0000000..7a7e88a --- /dev/null +++ b/extern/num_cpus.c @@ -0,0 +1,47 @@ +#if !defined(__WIN32__) +#include +#else +#include +#endif + + +static int +num_cpus() { +#if defined(__WIN32__) + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + + return (int) sysinfo.dwNumberOfProcessors; +#elif defined(__BSD__) + /* swiped from http://stackoverflow.com/questions/150355/ + * programmatically-find-the-number-of-cores-on-a-machine */ + + unsigned int numCPU; + int mib[4]; + size_t len = sizeof(numCPU); + + /* set the mib for hw.ncpu */ + mib[0] = CTL_HW; + mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU; + + /* get the number of CPUs from the system */ + sysctl(mib, 2, &numCPU, &len, NULL, 0); + + if( numCPU < 1 ) { + mib[1] = HW_NCPU; + sysctl( mib, 2, &numCPU, &len, NULL, 0 ); + + if( numCPU < 1 ) { + numCPU = 1; + } + } + return numCPU; +#elif defined(__GNUC__) + return sysconf(_SC_NPROCESSORS_ONLN); +#endif +} + +int +crates_io_get_num_cpus() { + return num_cpus(); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..88fd375 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,22 @@ +//! Replaces the deprecated functionality of std::os::num_cpus. + +#![cfg_attr(test, deny(warnings))] +#![deny(missing_docs)] + +extern crate libc; + +/// Returns the number of CPUs of the current machine. +pub fn get() -> usize { + unsafe { + crates_io_get_num_cpus() as usize + } +} + +extern { + fn crates_io_get_num_cpus() -> libc::c_int; +} + +#[test] +fn it_works() { + assert!(get() > 0); +}