This commit is contained in:
Sean McArthur
2015-03-16 11:42:22 -07:00
commit 1aeb70231f
7 changed files with 141 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
target
Cargo.lock
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "num_cpus"
version = "0.1.0"
authors = ["Sean McArthur <sean.monstar@gmail.com>"]
license = "MIT"
repository = "https://github.com/seanmonstar/num_cpus"
build = "build.rs"
[dependencies]
libc = "*"
[build-dependencies]
gcc = "*"
+20
View File
@@ -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.
+25
View File
@@ -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
+10
View File
@@ -0,0 +1,10 @@
#![deny(warnings)]
extern crate gcc;
fn main() {
gcc::compile_library(
"libnumcpus.a",
&["extern/num_cpus.c"]
);
}
+47
View File
@@ -0,0 +1,47 @@
#if !defined(__WIN32__)
#include <unistd.h>
#else
#include <windows.h>
#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();
}
+22
View File
@@ -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);
}