Skip to content

Commit 0ebc14d

Browse files
committed
---
yaml --- r: 4135 b: refs/heads/master c: 49a98f1 h: refs/heads/master i: 4133: ce11f9e 4131: c3ffad7 4127: 9623c94 v: v3
1 parent 4b93e5a commit 0ebc14d

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: e37dd2646a4808fff5647bc8d1a45914cd157c53
2+
refs/heads/master: 49a98f1508054556d1301ded432821f1bc4e1c98

trunk/src/rt/rust.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,46 @@ command_line_args : public kernel_owned<command_line_args>
7575
}
7676
};
7777

78+
79+
#if defined(__WIN32__)
80+
int get_num_cpus() {
81+
SYSTEM_INFO sysinfo;
82+
GetSystemInfo(&sysinfo);
83+
84+
return (int) sysinfo.dwNumberOfProcessors;
85+
}
86+
#elif defined(__BSD__)
87+
int get_num_cpus() {
88+
/* swiped from http://stackoverflow.com/questions/150355/
89+
programmatically-find-the-number-of-cores-on-a-machine */
90+
91+
unsigned int numCPU;
92+
int mib[4];
93+
size_t len = sizeof(numCPU);
94+
95+
/* set the mib for hw.ncpu */
96+
mib[0] = CTL_HW;
97+
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
98+
99+
/* get the number of CPUs from the system */
100+
sysctl(mib, 2, &numCPU, &len, NULL, 0);
101+
102+
if( numCPU < 1 ) {
103+
mib[1] = HW_NCPU;
104+
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
105+
106+
if( numCPU < 1 ) {
107+
numCPU = 1;
108+
}
109+
}
110+
return numCPU;
111+
}
112+
#elif defined(__GNUC__)
113+
int get_num_cpus() {
114+
return sysconf(_SC_NPROCESSORS_ONLN);
115+
}
116+
#endif
117+
78118
int get_num_threads()
79119
{
80120
char *env = getenv("RUST_THREADS");
@@ -83,9 +123,7 @@ int get_num_threads()
83123
if(num > 0)
84124
return num;
85125
}
86-
// FIXME: in this case, determine the number of CPUs present on the
87-
// machine.
88-
return 1;
126+
return get_num_cpus();
89127
}
90128

91129
/**

0 commit comments

Comments
 (0)