skia2/tests/OnceTest.cpp
mtklein 00b621cfc0 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
2015-06-17 15:26:15 -07:00

54 lines
1.2 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkOnce.h"
#include "SkRunnable.h"
#include "SkTaskGroup.h"
#include "Test.h"
static void add_five(int* x) {
*x += 5;
}
SK_DECLARE_STATIC_ONCE(st_once);
DEF_TEST(SkOnce_Singlethreaded, r) {
int x = 0;
// No matter how many times we do this, x will be 5.
SkOnce(&st_once, add_five, &x);
SkOnce(&st_once, add_five, &x);
SkOnce(&st_once, add_five, &x);
SkOnce(&st_once, add_five, &x);
SkOnce(&st_once, add_five, &x);
REPORTER_ASSERT(r, 5 == x);
}
SK_DECLARE_STATIC_ONCE(mt_once);
DEF_TEST(SkOnce_Multithreaded, r) {
int x = 0;
// Run a bunch of tasks to be the first to add six to x.
sk_parallel_for(1021, [&](int) {
void(*add_six)(int*) = [](int* p) { *p += 6; };
SkOnce(&mt_once, add_six, &x);
});
// Only one should have done the +=.
REPORTER_ASSERT(r, 6 == x);
}
static int gX = 0;
static void inc_gX() { gX++; }
SK_DECLARE_STATIC_ONCE(noarg_once);
DEF_TEST(SkOnce_NoArg, r) {
SkOnce(&noarg_once, inc_gX);
SkOnce(&noarg_once, inc_gX);
SkOnce(&noarg_once, inc_gX);
REPORTER_ASSERT(r, 1 == gX);
}