OLD | NEW |
---|---|
1 /* | |
2 * 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.
| |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
1 #include "SkTaskGroup.h" | 8 #include "SkTaskGroup.h" |
2 | 9 |
3 #include "SkCondVar.h" | 10 #include "SkCondVar.h" |
11 #include "SkOnce.h" | |
4 #include "SkRunnable.h" | 12 #include "SkRunnable.h" |
5 #include "SkTDArray.h" | 13 #include "SkTDArray.h" |
6 #include "SkThread.h" | 14 #include "SkThread.h" |
7 #include "SkThreadUtils.h" | 15 #include "SkThreadUtils.h" |
8 | 16 |
9 #if defined(SK_BUILD_FOR_WIN32) | 17 #if defined(SK_BUILD_FOR_WIN32) |
10 static inline int num_cores() { | 18 static void query_num_cores(int* num_cores) { |
11 SYSTEM_INFO sysinfo; | 19 SYSTEM_INFO sysinfo; |
12 GetSystemInfo(&sysinfo); | 20 GetSystemInfo(&sysinfo); |
13 return sysinfo.dwNumberOfProcessors; | 21 *num_cores = sysinfo.dwNumberOfProcessors; |
14 } | 22 } |
15 #else | 23 #else |
16 #include <unistd.h> | 24 #include <unistd.h> |
17 static inline int num_cores() { | 25 static void query_num_cores(int* num_cores) { |
18 return (int) sysconf(_SC_NPROCESSORS_ONLN); | 26 *num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
19 } | 27 } |
20 #endif | 28 #endif |
21 | 29 |
30 // We cache sk_num_cores() so we only query the OS once. | |
31 SK_DECLARE_STATIC_ONCE(g_query_num_cores_once); | |
32 int sk_num_cores() { | |
33 static int num_cores = 0; | |
34 SkOnce(&g_query_num_cores_once, query_num_cores, &num_cores); | |
35 SkASSERT(num_cores > 0); | |
36 return num_cores; | |
37 } | |
38 | |
22 namespace { | 39 namespace { |
23 | 40 |
24 class ThreadPool : SkNoncopyable { | 41 class ThreadPool : SkNoncopyable { |
25 public: | 42 public: |
26 static void Add(SkRunnable* task, int32_t* pending) { | 43 static void Add(SkRunnable* task, int32_t* pending) { |
27 if (!gGlobal) { // If we have no threads, run synchronously. | 44 if (!gGlobal) { // If we have no threads, run synchronously. |
28 return task->run(); | 45 return task->run(); |
29 } | 46 } |
30 gGlobal->add(&CallRunnable, task, pending); | 47 gGlobal->add(&CallRunnable, task, pending); |
31 } | 48 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 static void CallRunnable(void* arg) { static_cast<SkRunnable*>(arg)->run(); } | 97 static void CallRunnable(void* arg) { static_cast<SkRunnable*>(arg)->run(); } |
81 | 98 |
82 struct Work { | 99 struct Work { |
83 void (*fn)(void*); // A function to call, | 100 void (*fn)(void*); // A function to call, |
84 void* arg; // its argument, | 101 void* arg; // its argument, |
85 int32_t* pending; // then sk_atomic_dec(pending) afterwards. | 102 int32_t* pending; // then sk_atomic_dec(pending) afterwards. |
86 }; | 103 }; |
87 | 104 |
88 explicit ThreadPool(int threads) : fDraining(false) { | 105 explicit ThreadPool(int threads) : fDraining(false) { |
89 if (threads == -1) { | 106 if (threads == -1) { |
90 threads = num_cores(); | 107 threads = sk_num_cores(); |
91 } | 108 } |
92 for (int i = 0; i < threads; i++) { | 109 for (int i = 0; i < threads; i++) { |
93 fThreads.push(SkNEW_ARGS(SkThread, (&ThreadPool::Loop, this))); | 110 fThreads.push(SkNEW_ARGS(SkThread, (&ThreadPool::Loop, this))); |
94 fThreads.top()->start(); | 111 fThreads.top()->start(); |
95 } | 112 } |
96 } | 113 } |
97 | 114 |
98 ~ThreadPool() { | 115 ~ThreadPool() { |
99 SkASSERT(fWork.isEmpty()); // All SkTaskGroups should be destroyed by n ow. | 116 SkASSERT(fWork.isEmpty()); // All SkTaskGroups should be destroyed by n ow. |
100 { | 117 { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 | 193 |
177 SkTaskGroup::SkTaskGroup() : fPending(0) {} | 194 SkTaskGroup::SkTaskGroup() : fPending(0) {} |
178 | 195 |
179 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending ); } | 196 void SkTaskGroup::wait() { ThreadPool::Wait(&fPending ); } |
180 void SkTaskGroup::add(SkRunnable* task) { ThreadPool::Add(task, &fPe nding); } | 197 void SkTaskGroup::add(SkRunnable* task) { ThreadPool::Add(task, &fPe nding); } |
181 void SkTaskGroup::add(void (*fn)(void*), void* arg) { ThreadPool::Add(fn, arg, & fPending); } | 198 void SkTaskGroup::add(void (*fn)(void*), void* arg) { ThreadPool::Add(fn, arg, & fPending); } |
182 void SkTaskGroup::batch (void (*fn)(void*), void* args, int N, size_t stride) { | 199 void SkTaskGroup::batch (void (*fn)(void*), void* args, int N, size_t stride) { |
183 ThreadPool::Batch(fn, args, N, stride, &fPending); | 200 ThreadPool::Batch(fn, args, N, stride, &fPending); |
184 } | 201 } |
185 | 202 |
OLD | NEW |