[Bf-blender-cvs] [aeda4dc] master: Threads: Cache result of syscall when querying number of system threads

Sergey Sharybin noreply at git.blender.org
Sat Jun 20 22:11:13 CEST 2015


Commit: aeda4dca779da03c610c76b8cd0ea2cbdf744f38
Author: Sergey Sharybin
Date:   Sat Jun 20 22:10:30 2015 +0200
Branches: master
https://developer.blender.org/rBaeda4dca779da03c610c76b8cd0ea2cbdf744f38

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

===================================================================

M	source/blender/blenlib/intern/threads.c

===================================================================

diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c
index 73cadea..42f7a74 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;
 }




More information about the Bf-blender-cvs mailing list