| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkOnce.h" | 8 #include "SkOnce.h" |
| 9 #include "SkSemaphore.h" | 9 #include "SkSemaphore.h" |
| 10 #include "SkSpinlock.h" | 10 #include "SkSpinlock.h" |
| 11 #include "SkTArray.h" | 11 #include "SkTArray.h" |
| 12 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
| 13 #include "SkTaskGroup.h" | 13 #include "SkTaskGroup.h" |
| 14 #include "SkThreadUtils.h" | 14 #include "SkThreadUtils.h" |
| 15 | 15 |
| 16 #if defined(SK_BUILD_FOR_WIN32) | 16 #if defined(SK_BUILD_FOR_WIN32) |
| 17 static void query_num_cores(int* cores) { | 17 static void query_num_cores(int* num_cores) { |
| 18 SYSTEM_INFO sysinfo; | 18 SYSTEM_INFO sysinfo; |
| 19 GetNativeSystemInfo(&sysinfo); | 19 GetNativeSystemInfo(&sysinfo); |
| 20 *cores = sysinfo.dwNumberOfProcessors; | 20 *num_cores = sysinfo.dwNumberOfProcessors; |
| 21 } | 21 } |
| 22 #else | 22 #else |
| 23 #include <unistd.h> | 23 #include <unistd.h> |
| 24 static void query_num_cores(int* cores) { | 24 static void query_num_cores(int* num_cores) { |
| 25 *cores = (int)sysconf(_SC_NPROCESSORS_ONLN); | 25 *num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 26 } | 26 } |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 static int num_cores() { | 29 int sk_num_cores() { |
| 30 // We cache num_cores() so we only query the OS once. | 30 // We cache sk_num_cores() so we only query the OS once. |
| 31 static int cores = 0; | 31 static int num_cores = 0; |
| 32 static SkOnce once; | 32 static SkOnce once; |
| 33 once(query_num_cores, &cores); | 33 once(query_num_cores, &num_cores); |
| 34 SkASSERT(cores > 0); | 34 SkASSERT(num_cores > 0); |
| 35 return cores; | 35 return num_cores; |
| 36 } | 36 } |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 class ThreadPool : SkNoncopyable { | 40 class ThreadPool : SkNoncopyable { |
| 41 public: | 41 public: |
| 42 static void Add(std::function<void(void)> fn, SkAtomic<int32_t>* pending) { | 42 static void Add(std::function<void(void)> fn, SkAtomic<int32_t>* pending) { |
| 43 if (!gGlobal) { | 43 if (!gGlobal) { |
| 44 return fn(); | 44 return fn(); |
| 45 } | 45 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 SkSpinlock* fLock; | 91 SkSpinlock* fLock; |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 struct Work { | 94 struct Work { |
| 95 std::function<void(void)> fn; // A function to call | 95 std::function<void(void)> fn; // A function to call |
| 96 SkAtomic<int32_t>* pending; // then decrement pending afterwards. | 96 SkAtomic<int32_t>* pending; // then decrement pending afterwards. |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 explicit ThreadPool(int threads) { | 99 explicit ThreadPool(int threads) { |
| 100 if (threads == -1) { | 100 if (threads == -1) { |
| 101 threads = num_cores(); | 101 threads = sk_num_cores(); |
| 102 } | 102 } |
| 103 for (int i = 0; i < threads; i++) { | 103 for (int i = 0; i < threads; i++) { |
| 104 fThreads.push(new SkThread(&ThreadPool::Loop, this)); | 104 fThreads.push(new SkThread(&ThreadPool::Loop, this)); |
| 105 fThreads.top()->start(); | 105 fThreads.top()->start(); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 ~ThreadPool() { | 109 ~ThreadPool() { |
| 110 SkASSERT(fWork.empty()); // All SkTaskGroups should be destroyed by now
. | 110 SkASSERT(fWork.empty()); // All SkTaskGroups should be destroyed by now
. |
| 111 | 111 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 200 |
| 201 SkTaskGroup::Enabler::~Enabler() { delete ThreadPool::gGlobal; } | 201 SkTaskGroup::Enabler::~Enabler() { delete ThreadPool::gGlobal; } |
| 202 | 202 |
| 203 SkTaskGroup::SkTaskGroup() : fPending(0) {} | 203 SkTaskGroup::SkTaskGroup() : fPending(0) {} |
| 204 | 204 |
| 205 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending
); } | 205 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending
); } |
| 206 void SkTaskGroup::add(std::function<void(void)> fn) { ThreadPool::Add(fn, &fPend
ing); } | 206 void SkTaskGroup::add(std::function<void(void)> fn) { ThreadPool::Add(fn, &fPend
ing); } |
| 207 void SkTaskGroup::batch(int N, std::function<void(int)> fn) { | 207 void SkTaskGroup::batch(int N, std::function<void(int)> fn) { |
| 208 ThreadPool::Batch(N, fn, &fPending); | 208 ThreadPool::Batch(N, fn, &fPending); |
| 209 } | 209 } |
| OLD | NEW |