Skip to content

Commit 1aeb702

Browse files
committed
v0.1.0
0 parents  commit 1aeb702

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
3+
name = "num_cpus"
4+
version = "0.1.0"
5+
authors = ["Sean McArthur <[email protected]>"]
6+
license = "MIT"
7+
repository = "https://github.com/seanmonstar/num_cpus"
8+
build = "build.rs"
9+
10+
11+
[dependencies]
12+
libc = "*"
13+
14+
[build-dependencies]
15+
gcc = "*"

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# num_cpus
2+
3+
A replacement for the deprecated `std::os::num_cpus`.
4+
5+
## Usage
6+
7+
Add to Cargo.toml:
8+
9+
```
10+
[dependencies]
11+
num_cpus = "*"
12+
```
13+
14+
In your `main.rs` or `lib.rs`:
15+
16+
```rust
17+
extern crate num_cpus;
18+
19+
// elsewhere
20+
let num = num_cpus::get();
21+
```
22+
23+
## License
24+
25+
MIT

build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![deny(warnings)]
2+
3+
extern crate gcc;
4+
5+
fn main() {
6+
gcc::compile_library(
7+
"libnumcpus.a",
8+
&["extern/num_cpus.c"]
9+
);
10+
}

extern/num_cpus.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#if !defined(__WIN32__)
2+
#include <unistd.h>
3+
#else
4+
#include <windows.h>
5+
#endif
6+
7+
8+
static int
9+
num_cpus() {
10+
#if defined(__WIN32__)
11+
SYSTEM_INFO sysinfo;
12+
GetSystemInfo(&sysinfo);
13+
14+
return (int) sysinfo.dwNumberOfProcessors;
15+
#elif defined(__BSD__)
16+
/* swiped from http://stackoverflow.com/questions/150355/
17+
* programmatically-find-the-number-of-cores-on-a-machine */
18+
19+
unsigned int numCPU;
20+
int mib[4];
21+
size_t len = sizeof(numCPU);
22+
23+
/* set the mib for hw.ncpu */
24+
mib[0] = CTL_HW;
25+
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
26+
27+
/* get the number of CPUs from the system */
28+
sysctl(mib, 2, &numCPU, &len, NULL, 0);
29+
30+
if( numCPU < 1 ) {
31+
mib[1] = HW_NCPU;
32+
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
33+
34+
if( numCPU < 1 ) {
35+
numCPU = 1;
36+
}
37+
}
38+
return numCPU;
39+
#elif defined(__GNUC__)
40+
return sysconf(_SC_NPROCESSORS_ONLN);
41+
#endif
42+
}
43+
44+
int
45+
crates_io_get_num_cpus() {
46+
return num_cpus();
47+
}

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Replaces the deprecated functionality of std::os::num_cpus.
2+
3+
#![cfg_attr(test, deny(warnings))]
4+
#![deny(missing_docs)]
5+
6+
extern crate libc;
7+
8+
/// Returns the number of CPUs of the current machine.
9+
pub fn get() -> usize {
10+
unsafe {
11+
crates_io_get_num_cpus() as usize
12+
}
13+
}
14+
15+
extern {
16+
fn crates_io_get_num_cpus() -> libc::c_int;
17+
}
18+
19+
#[test]
20+
fn it_works() {
21+
assert!(get() > 0);
22+
}

0 commit comments

Comments
 (0)