Index: src/core/SkTaskGroup.cpp |
diff --git a/src/core/SkTaskGroup.cpp b/src/core/SkTaskGroup.cpp |
index 345fd2fcb9a7fcb59ac2ca2f5de088c92315d344..d2f1a3932eeed6294f9a1f3620109dc9a9c5c04e 100644 |
--- a/src/core/SkTaskGroup.cpp |
+++ b/src/core/SkTaskGroup.cpp |
@@ -1,24 +1,41 @@ |
+/* |
+ * Copyright 2014 Google Inc. |
caryclark
2015/06/16 19:04:25
2015? (Not sure whether its appropriate to predate
mtklein
2015/06/16 19:16:21
I have no idea. I did write it in 2014.
|
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
#include "SkTaskGroup.h" |
#include "SkCondVar.h" |
+#include "SkOnce.h" |
#include "SkRunnable.h" |
#include "SkTDArray.h" |
#include "SkThread.h" |
#include "SkThreadUtils.h" |
#if defined(SK_BUILD_FOR_WIN32) |
- static inline int num_cores() { |
+ static void query_num_cores(int* num_cores) { |
SYSTEM_INFO sysinfo; |
GetSystemInfo(&sysinfo); |
- return sysinfo.dwNumberOfProcessors; |
+ *num_cores = sysinfo.dwNumberOfProcessors; |
} |
#else |
#include <unistd.h> |
- static inline int num_cores() { |
- return (int) sysconf(_SC_NPROCESSORS_ONLN); |
+ static void query_num_cores(int* num_cores) { |
+ *num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
} |
#endif |
+// We cache sk_num_cores() so we only query the OS once. |
+SK_DECLARE_STATIC_ONCE(g_query_num_cores_once); |
+int sk_num_cores() { |
+ static int num_cores = 0; |
+ SkOnce(&g_query_num_cores_once, query_num_cores, &num_cores); |
+ SkASSERT(num_cores > 0); |
+ return num_cores; |
+} |
+ |
namespace { |
class ThreadPool : SkNoncopyable { |
@@ -87,7 +104,7 @@ private: |
explicit ThreadPool(int threads) : fDraining(false) { |
if (threads == -1) { |
- threads = num_cores(); |
+ threads = sk_num_cores(); |
} |
for (int i = 0; i < threads; i++) { |
fThreads.push(SkNEW_ARGS(SkThread, (&ThreadPool::Loop, this))); |