| 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 "SkRunnable.h" | |
| 10 #include "SkSemaphore.h" | 9 #include "SkSemaphore.h" |
| 11 #include "SkSpinlock.h" | 10 #include "SkSpinlock.h" |
| 12 #include "SkTArray.h" | 11 #include "SkTArray.h" |
| 13 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
| 14 #include "SkTaskGroup.h" | 13 #include "SkTaskGroup.h" |
| 15 #include "SkThreadUtils.h" | 14 #include "SkThreadUtils.h" |
| 16 | 15 |
| 17 #if defined(SK_BUILD_FOR_WIN32) | 16 #if defined(SK_BUILD_FOR_WIN32) |
| 18 static void query_num_cores(int* num_cores) { | 17 static void query_num_cores(int* num_cores) { |
| 19 SYSTEM_INFO sysinfo; | 18 SYSTEM_INFO sysinfo; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 static int num_cores = 0; | 32 static int num_cores = 0; |
| 34 SkOnce(&g_query_num_cores_once, query_num_cores, &num_cores); | 33 SkOnce(&g_query_num_cores_once, query_num_cores, &num_cores); |
| 35 SkASSERT(num_cores > 0); | 34 SkASSERT(num_cores > 0); |
| 36 return num_cores; | 35 return num_cores; |
| 37 } | 36 } |
| 38 | 37 |
| 39 namespace { | 38 namespace { |
| 40 | 39 |
| 41 class ThreadPool : SkNoncopyable { | 40 class ThreadPool : SkNoncopyable { |
| 42 public: | 41 public: |
| 43 static void Add(SkRunnable* task, SkAtomic<int32_t>* pending) { | |
| 44 if (!gGlobal) { // If we have no threads, run synchronously. | |
| 45 return task->run(); | |
| 46 } | |
| 47 gGlobal->add([task]() { task->run(); }, pending); | |
| 48 } | |
| 49 | |
| 50 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) { |
| 51 if (!gGlobal) { | 43 if (!gGlobal) { |
| 52 return fn(); | 44 return fn(); |
| 53 } | 45 } |
| 54 gGlobal->add(fn, pending); | 46 gGlobal->add(fn, pending); |
| 55 } | 47 } |
| 56 | 48 |
| 57 static void Batch(int N, std::function<void(int)> fn, SkAtomic<int32_t>* pen
ding) { | 49 static void Batch(int N, std::function<void(int)> fn, SkAtomic<int32_t>* pen
ding) { |
| 58 if (!gGlobal) { | 50 if (!gGlobal) { |
| 59 for (int i = 0; i < N; i++) { fn(i); } | 51 for (int i = 0; i < N; i++) { fn(i); } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 84 } |
| 93 | 85 |
| 94 private: | 86 private: |
| 95 struct AutoLock { | 87 struct AutoLock { |
| 96 AutoLock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); } | 88 AutoLock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); } |
| 97 ~AutoLock() { fLock->release(); } | 89 ~AutoLock() { fLock->release(); } |
| 98 private: | 90 private: |
| 99 SkSpinlock* fLock; | 91 SkSpinlock* fLock; |
| 100 }; | 92 }; |
| 101 | 93 |
| 102 static void CallRunnable(void* arg) { static_cast<SkRunnable*>(arg)->run();
} | |
| 103 | |
| 104 struct Work { | 94 struct Work { |
| 105 std::function<void(void)> fn; // A function to call | 95 std::function<void(void)> fn; // A function to call |
| 106 SkAtomic<int32_t>* pending; // then decrement pending afterwards. | 96 SkAtomic<int32_t>* pending; // then decrement pending afterwards. |
| 107 }; | 97 }; |
| 108 | 98 |
| 109 explicit ThreadPool(int threads) { | 99 explicit ThreadPool(int threads) { |
| 110 if (threads == -1) { | 100 if (threads == -1) { |
| 111 threads = sk_num_cores(); | 101 threads = sk_num_cores(); |
| 112 } | 102 } |
| 113 for (int i = 0; i < threads; i++) { | 103 for (int i = 0; i < threads; i++) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 if (threads != 0) { | 196 if (threads != 0) { |
| 207 ThreadPool::gGlobal = new ThreadPool(threads); | 197 ThreadPool::gGlobal = new ThreadPool(threads); |
| 208 } | 198 } |
| 209 } | 199 } |
| 210 | 200 |
| 211 SkTaskGroup::Enabler::~Enabler() { delete ThreadPool::gGlobal; } | 201 SkTaskGroup::Enabler::~Enabler() { delete ThreadPool::gGlobal; } |
| 212 | 202 |
| 213 SkTaskGroup::SkTaskGroup() : fPending(0) {} | 203 SkTaskGroup::SkTaskGroup() : fPending(0) {} |
| 214 | 204 |
| 215 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending
); } | 205 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending
); } |
| 216 void SkTaskGroup::add(SkRunnable* task) { ThreadPool::Add(task, &fPe
nding); } | |
| 217 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); } |
| 218 void SkTaskGroup::batch(int N, std::function<void(int)> fn) { | 207 void SkTaskGroup::batch(int N, std::function<void(int)> fn) { |
| 219 ThreadPool::Batch(N, fn, &fPending); | 208 ThreadPool::Batch(N, fn, &fPending); |
| 220 } | 209 } |
| 221 | 210 |
| OLD | NEW |