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 #ifndef SkTaskGroup_DEFINED | 8 #ifndef SkTaskGroup_DEFINED |
9 #define SkTaskGroup_DEFINED | 9 #define SkTaskGroup_DEFINED |
10 | 10 |
| 11 #include <functional> |
| 12 |
11 #include "SkTypes.h" | 13 #include "SkTypes.h" |
12 #include "SkAtomics.h" | 14 #include "SkAtomics.h" |
13 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
14 | 16 |
15 struct SkRunnable; | 17 struct SkRunnable; |
16 | 18 |
17 class SkTaskGroup : SkNoncopyable { | 19 class SkTaskGroup : SkNoncopyable { |
18 public: | 20 public: |
19 // Create one of these in main() to enable SkTaskGroups globally. | 21 // Create one of these in main() to enable SkTaskGroups globally. |
20 struct Enabler : SkNoncopyable { | 22 struct Enabler : SkNoncopyable { |
21 explicit Enabler(int threads = -1); // Default is system-reported core
count. | 23 explicit Enabler(int threads = -1); // Default is system-reported core
count. |
22 ~Enabler(); | 24 ~Enabler(); |
23 }; | 25 }; |
24 | 26 |
25 SkTaskGroup(); | 27 SkTaskGroup(); |
26 ~SkTaskGroup() { this->wait(); } | 28 ~SkTaskGroup() { this->wait(); } |
27 | 29 |
28 // Add a task to this SkTaskGroup. It will likely run on another thread. | 30 // Add a task to this SkTaskGroup. It will likely run on another thread. |
29 // Neither add() method takes owership of any of its parameters. | 31 // Neither add() method takes owership of any of its parameters. |
30 void add(SkRunnable*); | 32 void add(SkRunnable*); |
31 | 33 |
32 template <typename T> | 34 void add(std::function<void(void)> fn); |
33 void add(void (*fn)(T*), T* arg) { this->add((void_fn)fn, (void*)arg); } | |
34 | 35 |
35 // Add a batch of N tasks, all calling fn with different arguments. | 36 // Add a batch of N tasks, all calling fn with different arguments. |
36 // Equivalent to a loop over add(fn, arg), but with perhaps less synchroniza
tion overhead. | 37 void batch(std::function<void(int)> fn, int N); |
37 template <typename T> | |
38 void batch(void (*fn)(T*), T* args, int N) { this->batch((void_fn)fn, args,
N, sizeof(T)); } | |
39 | 38 |
40 // Block until all Tasks previously add()ed to this SkTaskGroup have run. | 39 // Block until all Tasks previously add()ed to this SkTaskGroup have run. |
41 // You may safely reuse this SkTaskGroup after wait() returns. | 40 // You may safely reuse this SkTaskGroup after wait() returns. |
42 void wait(); | 41 void wait(); |
43 | 42 |
44 private: | 43 private: |
45 typedef void(*void_fn)(void*); | |
46 | |
47 void add (void_fn, void* arg); | |
48 void batch(void_fn, void* args, int N, size_t stride); | |
49 | |
50 SkAtomic<int32_t> fPending; | 44 SkAtomic<int32_t> fPending; |
51 }; | 45 }; |
52 | 46 |
53 // Returns best estimate of number of CPU cores available to use. | 47 // Returns best estimate of number of CPU cores available to use. |
54 int sk_num_cores(); | 48 int sk_num_cores(); |
55 | 49 |
56 int sk_parallel_for_thread_count(); | 50 int sk_parallel_for_thread_count(); |
57 | 51 |
58 // Call f(i) for i in [0, end). | 52 // Call f(i) for i in [0, end). |
59 template <typename Func> | 53 template <typename Func> |
(...skipping 14 matching lines...) Expand all Loading... |
74 #if defined(GOOGLE3) | 68 #if defined(GOOGLE3) |
75 // Stack frame size is limited in GOOGLE3. | 69 // Stack frame size is limited in GOOGLE3. |
76 SkAutoSTMalloc<512, Chunk> chunks(nchunks); | 70 SkAutoSTMalloc<512, Chunk> chunks(nchunks); |
77 #else | 71 #else |
78 // With the chunking strategy above this won't malloc until we have a machin
e with >512 cores. | 72 // With the chunking strategy above this won't malloc until we have a machin
e with >512 cores. |
79 SkAutoSTMalloc<1024, Chunk> chunks(nchunks); | 73 SkAutoSTMalloc<1024, Chunk> chunks(nchunks); |
80 #endif | 74 #endif |
81 | 75 |
82 for (int i = 0; i < nchunks; i++) { | 76 for (int i = 0; i < nchunks; i++) { |
83 Chunk& c = chunks[i]; | 77 Chunk& c = chunks[i]; |
84 c.f = &f; | 78 c.f = &f; |
85 c.start = i * stride; | 79 c.start = i * stride; |
86 c.end = SkTMin(c.start + stride, end); | 80 c.end = SkTMin(c.start + stride, end); |
87 SkASSERT(c.start < c.end); // Nothing will break if start >= end, but i
t's a wasted chunk. | 81 SkASSERT(c.start < c.end); // Nothing will break if start >= end, but i
t's a wasted chunk. |
88 } | 82 } |
89 | 83 |
90 void(*run_chunk)(Chunk*) = [](Chunk* c) { | 84 Chunk* chunkBase = chunks.get(); |
91 for (int i = c->start; i < c->end; i++) { | 85 auto run_chunk = [chunkBase](int i) { |
92 (*c->f)(i); | 86 Chunk& c = chunkBase[i]; |
| 87 for (int i = c.start; i < c.end; i++) { |
| 88 (*c.f)(i); |
93 } | 89 } |
94 }; | 90 }; |
95 SkTaskGroup().batch(run_chunk, chunks.get(), nchunks); | 91 SkTaskGroup().batch(run_chunk, nchunks); |
96 } | 92 } |
97 | 93 |
98 #endif//SkTaskGroup_DEFINED | 94 #endif//SkTaskGroup_DEFINED |
OLD | NEW |