blob: d3a0a0066a1d8008537459a328a0a016aaa14c86 [file] [log] [blame]
mtklein@google.com3a19fb52013-10-09 16:12:23 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkOnce.h"
mtklein@google.com3a19fb52013-10-09 16:12:23 +00009#include "SkThreadPool.h"
10#include "Test.h"
11#include "TestClassDef.h"
12
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000013static void add_five(int* x) {
mtklein@google.com3a19fb52013-10-09 16:12:23 +000014 *x += 5;
15}
16
17DEF_TEST(SkOnce_Singlethreaded, r) {
18 int x = 0;
19
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000020 SK_DECLARE_STATIC_ONCE(once);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000021 // No matter how many times we do this, x will be 5.
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000022 SkOnce(&once, add_five, &x);
23 SkOnce(&once, add_five, &x);
24 SkOnce(&once, add_five, &x);
25 SkOnce(&once, add_five, &x);
26 SkOnce(&once, add_five, &x);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000027
28 REPORTER_ASSERT(r, 5 == x);
29}
30
commit-bot@chromium.org1b2c1b82013-12-05 19:20:49 +000031struct AddFour { void operator()(int* x) { *x += 4; } };
32
33DEF_TEST(SkOnce_MiscFeatures, r) {
34 // Tests that we support functors and explicit SkOnceFlags.
35 int x = 0;
36
37 SkOnceFlag once = SK_ONCE_INIT;
38 SkOnce(&once, AddFour(), &x);
39 SkOnce(&once, AddFour(), &x);
40 SkOnce(&once, AddFour(), &x);
41
42 REPORTER_ASSERT(r, 4 == x);
43}
44
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000045static void add_six(int* x) {
mtklein@google.com3a19fb52013-10-09 16:12:23 +000046 *x += 6;
47}
48
mtklein@google.com3a19fb52013-10-09 16:12:23 +000049class Racer : public SkRunnable {
50public:
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000051 SkOnceFlag* once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000052 int* ptr;
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000053
mtklein@google.com3a19fb52013-10-09 16:12:23 +000054 virtual void run() SK_OVERRIDE {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000055 SkOnce(once, add_six, ptr);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000056 }
57};
58
mtklein@google.com3a19fb52013-10-09 16:12:23 +000059DEF_TEST(SkOnce_Multithreaded, r) {
60 const int kTasks = 16, kThreads = 4;
61
62 // Make a bunch of tasks that will race to be the first to add six to x.
63 Racer racers[kTasks];
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000064 SK_DECLARE_STATIC_ONCE(once);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000065 int x = 0;
66 for (int i = 0; i < kTasks; i++) {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000067 racers[i].once = &once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000068 racers[i].ptr = &x;
69 }
70
71 // Let them race.
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +000072 SkThreadPool pool(kThreads);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000073 for (int i = 0; i < kTasks; i++) {
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +000074 pool.add(&racers[i]);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000075 }
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +000076 pool.wait();
mtklein@google.com3a19fb52013-10-09 16:12:23 +000077
78 // Only one should have done the +=.
79 REPORTER_ASSERT(r, 6 == x);
80}