diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-06-20 23:10:30 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-06-20 23:10:30 +0300 |
commit | aeda4dca779da03c610c76b8cd0ea2cbdf744f38 (patch) | |
tree | 24e355aaeccea8a02d8730b1c8914445f458eb47 /source/blender | |
parent | a95b0e0e9df75a2128e2276e88869547e872b7a1 (diff) |
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
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/intern/threads.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 73cadea71c0..42f7a744b94 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -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; } |