Add sk_parallel_for()
This should be a drop-in replacement for most for-loops to make them run in parallel:
for (int i = 0; i < N; i++) { code... }
~~~>
sk_parallel_for(N, [&](int i) { code... });
This is just syntax sugar over SkTaskGroup to make this use case really easy to write.
There's no more overhead that we weren't already forced to add using an interface like batch(),
and no extra heap allocations.
I've replaced 3 uses of SkTaskGroup with sk_parallel_for:
1) My unit tests for SkOnce.
2) Cary's path fuzzer.
3) SkMultiPictureDraw.
Performance should be the same. Please compare left and right for readability. :)
BUG=skia:
No public API changes.
TBR=reed@google.com
Review URL: https://codereview.chromium.org/1184373003
diff --git a/tests/LazyPtrTest.cpp b/tests/LazyPtrTest.cpp
index 1b845bc..89443f9 100644
--- a/tests/LazyPtrTest.cpp
+++ b/tests/LazyPtrTest.cpp
@@ -1,3 +1,10 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
#include "Test.h"
#include "SkLazyPtr.h"
#include "SkRunnable.h"
@@ -44,37 +51,20 @@
SkDELETE(ptr);
}
-namespace {
-
-struct Racer : public SkRunnable {
- Racer() : fLazy(NULL), fSeen(NULL) {}
-
- void run() override { fSeen = fLazy->get(); }
-
- SkLazyPtr<int>* fLazy;
- int* fSeen;
-};
-
-} // namespace
-
DEF_TEST(LazyPtr_Threaded, r) {
static const int kRacers = 321;
+ // Race to intialize the pointer by calling .get().
SkLazyPtr<int> lazy;
+ int* seen[kRacers];
- Racer racers[kRacers];
- for (int i = 0; i < kRacers; i++) {
- racers[i].fLazy = &lazy;
- }
+ sk_parallel_for(kRacers, [&](int i) {
+ seen[i] = lazy.get();
+ });
- SkTaskGroup tg;
- for (int i = 0; i < kRacers; i++) {
- tg.add(racers + i);
- }
- tg.wait();
-
+ // lazy.get() should return the same pointer to all threads.
for (int i = 1; i < kRacers; i++) {
- REPORTER_ASSERT(r, racers[i].fSeen);
- REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen);
+ REPORTER_ASSERT(r, seen[i] != nullptr);
+ REPORTER_ASSERT(r, seen[i] == seen[0]);
}
}