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 "SkRunnable.h"
10 #include "SkTaskGroup.h"
13 static void add_five(int* x) {
17 SK_DECLARE_STATIC_ONCE(st_once);
18 DEF_TEST(SkOnce_Singlethreaded, r) {
21 // No matter how many times we do this, x will be 5.
22 SkOnce(&st_once, add_five, &x);
23 SkOnce(&st_once, add_five, &x);
24 SkOnce(&st_once, add_five, &x);
25 SkOnce(&st_once, add_five, &x);
26 SkOnce(&st_once, add_five, &x);
28 REPORTER_ASSERT(r, 5 == x);
31 static void add_six(int* x) {
37 class Racer : public SkRunnable {
42 void run() SK_OVERRIDE {
43 SkOnce(once, add_six, ptr);
49 SK_DECLARE_STATIC_ONCE(mt_once);
50 DEF_TEST(SkOnce_Multithreaded, r) {
51 const int kTasks = 16;
53 // Make a bunch of tasks that will race to be the first to add six to x.
56 for (int i = 0; i < kTasks; i++) {
57 racers[i].once = &mt_once;
63 for (int i = 0; i < kTasks; i++) {
68 // Only one should have done the +=.
69 REPORTER_ASSERT(r, 6 == x);
73 static void inc_gX() { gX++; }
75 SK_DECLARE_STATIC_ONCE(noarg_once);
76 DEF_TEST(SkOnce_NoArg, r) {
77 SkOnce(&noarg_once, inc_gX);
78 SkOnce(&noarg_once, inc_gX);
79 SkOnce(&noarg_once, inc_gX);
80 REPORTER_ASSERT(r, 1 == gX);