blob: 192abaaee36266c81ceb3e7773b8ae524415a669 [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"
mtklein406654b2014-09-03 15:34:37 -07009#include "SkTaskGroup.h"
mtklein@google.com3a19fb52013-10-09 16:12:23 +000010#include "Test.h"
mtklein@google.com3a19fb52013-10-09 16:12:23 +000011
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000012static void add_five(int* x) {
mtklein@google.com3a19fb52013-10-09 16:12:23 +000013 *x += 5;
14}
15
16DEF_TEST(SkOnce_Singlethreaded, r) {
17 int x = 0;
18
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000019 SK_DECLARE_STATIC_ONCE(once);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000020 // No matter how many times we do this, x will be 5.
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000021 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);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000026
27 REPORTER_ASSERT(r, 5 == x);
28}
29
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000030static void add_six(int* x) {
mtklein@google.com3a19fb52013-10-09 16:12:23 +000031 *x += 6;
32}
33
mtklein@google.com3a19fb52013-10-09 16:12:23 +000034class Racer : public SkRunnable {
35public:
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000036 SkOnceFlag* once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000037 int* ptr;
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000038
mtklein@google.com3a19fb52013-10-09 16:12:23 +000039 virtual void run() SK_OVERRIDE {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000040 SkOnce(once, add_six, ptr);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000041 }
42};
43
mtklein@google.com3a19fb52013-10-09 16:12:23 +000044DEF_TEST(SkOnce_Multithreaded, r) {
mtklein406654b2014-09-03 15:34:37 -070045 const int kTasks = 16;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000046
47 // Make a bunch of tasks that will race to be the first to add six to x.
48 Racer racers[kTasks];
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000049 SK_DECLARE_STATIC_ONCE(once);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000050 int x = 0;
51 for (int i = 0; i < kTasks; i++) {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000052 racers[i].once = &once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000053 racers[i].ptr = &x;
54 }
55
56 // Let them race.
mtklein406654b2014-09-03 15:34:37 -070057 SkTaskGroup tg;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000058 for (int i = 0; i < kTasks; i++) {
mtklein406654b2014-09-03 15:34:37 -070059 tg.add(&racers[i]);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000060 }
mtklein406654b2014-09-03 15:34:37 -070061 tg.wait();
mtklein@google.com3a19fb52013-10-09 16:12:23 +000062
63 // Only one should have done the +=.
64 REPORTER_ASSERT(r, 6 == x);
65}
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000066
mtklein1b818772014-06-02 11:26:59 -070067static int gX = 0;
68static void inc_gX() { gX++; }
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000069
mtklein1b818772014-06-02 11:26:59 -070070DEF_TEST(SkOnce_NoArg, r) {
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000071 SK_DECLARE_STATIC_ONCE(once);
mtklein1b818772014-06-02 11:26:59 -070072 SkOnce(&once, inc_gX);
73 SkOnce(&once, inc_gX);
74 SkOnce(&once, inc_gX);
75 REPORTER_ASSERT(r, 1 == gX);
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000076}