Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: src/core/SkTaskGroup.h

Issue 1519573003: Change SkTaskGroup to use std::function. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix compiler breakage. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Equivalent to a loop over add(fn, arg), but with perhaps less synchroniza tion overhead.
mtklein 2015/12/10 20:58:35 Might just remove this second line now?
herb_g 2015/12/10 21:58:36 Done
37 template <typename T> 38 void batch(std::function<void(int)> fn, int N);
38 void batch(void (*fn)(T*), T* args, int N) { this->batch((void_fn)fn, args, N, sizeof(T)); }
39 39
40 // Block until all Tasks previously add()ed to this SkTaskGroup have run. 40 // Block until all Tasks previously add()ed to this SkTaskGroup have run.
41 // You may safely reuse this SkTaskGroup after wait() returns. 41 // You may safely reuse this SkTaskGroup after wait() returns.
42 void wait(); 42 void wait();
43 43
44 private: 44 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; 45 SkAtomic<int32_t> fPending;
51 }; 46 };
52 47
53 // Returns best estimate of number of CPU cores available to use. 48 // Returns best estimate of number of CPU cores available to use.
54 int sk_num_cores(); 49 int sk_num_cores();
55 50
56 int sk_parallel_for_thread_count(); 51 int sk_parallel_for_thread_count();
57 52
58 // Call f(i) for i in [0, end). 53 // Call f(i) for i in [0, end).
59 template <typename Func> 54 template <typename Func>
(...skipping 14 matching lines...) Expand all
74 #if defined(GOOGLE3) 69 #if defined(GOOGLE3)
75 // Stack frame size is limited in GOOGLE3. 70 // Stack frame size is limited in GOOGLE3.
76 SkAutoSTMalloc<512, Chunk> chunks(nchunks); 71 SkAutoSTMalloc<512, Chunk> chunks(nchunks);
77 #else 72 #else
78 // With the chunking strategy above this won't malloc until we have a machin e with >512 cores. 73 // With the chunking strategy above this won't malloc until we have a machin e with >512 cores.
79 SkAutoSTMalloc<1024, Chunk> chunks(nchunks); 74 SkAutoSTMalloc<1024, Chunk> chunks(nchunks);
80 #endif 75 #endif
81 76
82 for (int i = 0; i < nchunks; i++) { 77 for (int i = 0; i < nchunks; i++) {
83 Chunk& c = chunks[i]; 78 Chunk& c = chunks[i];
84 c.f = &f; 79 c.f = &f;
85 c.start = i * stride; 80 c.start = i * stride;
86 c.end = SkTMin(c.start + stride, end); 81 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. 82 SkASSERT(c.start < c.end); // Nothing will break if start >= end, but i t's a wasted chunk.
88 } 83 }
89 84
90 void(*run_chunk)(Chunk*) = [](Chunk* c) { 85 Chunk* chunkBase = chunks.get();
91 for (int i = c->start; i < c->end; i++) { 86 auto run_chunk = [chunkBase](int i) {
92 (*c->f)(i); 87 Chunk& c = chunkBase[i];
88 for (int i = c.start; i < c.end; i++) {
89 (*c.f)(i);
93 } 90 }
94 }; 91 };
95 SkTaskGroup().batch(run_chunk, chunks.get(), nchunks); 92 SkTaskGroup().batch(run_chunk, nchunks);
96 } 93 }
97 94
98 #endif//SkTaskGroup_DEFINED 95 #endif//SkTaskGroup_DEFINED
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698