2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkTaskGroup.h"
12 static void add_five(int* x) {
16 DEF_TEST(SkOnce_Singlethreaded, r) {
19 SK_DECLARE_STATIC_ONCE(once);
20 // No matter how many times we do this, x will be 5.
21 SkOnce(&once, add_five, &x);
22 SkOnce(&once, add_five, &x);
23 SkOnce(&once, add_five, &x);
24 SkOnce(&once, add_five, &x);
25 SkOnce(&once, add_five, &x);
27 REPORTER_ASSERT(r, 5 == x);
30 static void add_six(int* x) {
34 class Racer : public SkRunnable {
39 virtual void run() SK_OVERRIDE {
40 SkOnce(once, add_six, ptr);
44 DEF_TEST(SkOnce_Multithreaded, r) {
45 const int kTasks = 16;
47 // Make a bunch of tasks that will race to be the first to add six to x.
49 SK_DECLARE_STATIC_ONCE(once);
51 for (int i = 0; i < kTasks; i++) {
52 racers[i].once = &once;
58 for (int i = 0; i < kTasks; i++) {
63 // Only one should have done the +=.
64 REPORTER_ASSERT(r, 6 == x);
68 static void inc_gX() { gX++; }
70 DEF_TEST(SkOnce_NoArg, r) {
71 SK_DECLARE_STATIC_ONCE(once);
72 SkOnce(&once, inc_gX);
73 SkOnce(&once, inc_gX);
74 SkOnce(&once, inc_gX);
75 REPORTER_ASSERT(r, 1 == gX);