Threads: Cache result of syscall when querying number of system threads

Number of system threads is quite difficult to change without need of blender
restart, so we can cache result of the systcalls (which are not really cheap)
in order to be able to call BLI_system_thread_count() without worrying of
performance issues in that function.

Reviewers: campbellbarton

Differential Revision: https://developer.blender.org/D1342
This commit is contained in:
Sergey Sharybin
2015-06-20 22:10:30 +02:00
parent a95b0e0e9d
commit aeda4dca77

View File

@@ -343,33 +343,37 @@ void BLI_end_threads(ListBase *threadbase)
/* how many threads are native on this system? */
int BLI_system_thread_count(void)
{
int t;
static int t = -1;
if (num_threads_override != 0) {
return num_threads_override;
}
else if (LIKELY(t != -1)) {
return t;
}
{
#ifdef WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
t = (int) info.dwNumberOfProcessors;
SYSTEM_INFO info;
GetSystemInfo(&info);
t = (int) info.dwNumberOfProcessors;
#else
# ifdef __APPLE__
int mib[2];
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
len = sizeof(t);
sysctl(mib, 2, &t, &len, NULL, 0);
int mib[2];
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
len = sizeof(t);
sysctl(mib, 2, &t, &len, NULL, 0);
# else
t = (int)sysconf(_SC_NPROCESSORS_ONLN);
t = (int)sysconf(_SC_NPROCESSORS_ONLN);
# endif
#endif
}
CLAMP(t, 1, RE_MAX_THREAD);
if (num_threads_override > 0)
return num_threads_override;
if (t > RE_MAX_THREAD)
return RE_MAX_THREAD;
if (t < 1)
return 1;
return t;
}