blob: e2711f00bb2b3d58d0e670c4461fbbc0430e7855 [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
mtklein1d932662014-10-20 13:46:11 -070034namespace {
35
mtklein@google.com3a19fb52013-10-09 16:12:23 +000036class Racer : public SkRunnable {
37public:
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000038 SkOnceFlag* once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000039 int* ptr;
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000040
mtklein@google.com3a19fb52013-10-09 16:12:23 +000041 virtual void run() SK_OVERRIDE {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000042 SkOnce(once, add_six, ptr);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000043 }
44};
45
mtklein1d932662014-10-20 13:46:11 -070046} // namespace
47
mtklein@google.com3a19fb52013-10-09 16:12:23 +000048DEF_TEST(SkOnce_Multithreaded, r) {
mtklein406654b2014-09-03 15:34:37 -070049 const int kTasks = 16;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000050
51 // Make a bunch of tasks that will race to be the first to add six to x.
52 Racer racers[kTasks];
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000053 SK_DECLARE_STATIC_ONCE(once);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000054 int x = 0;
55 for (int i = 0; i < kTasks; i++) {
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000056 racers[i].once = &once;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000057 racers[i].ptr = &x;
58 }
59
60 // Let them race.
mtklein406654b2014-09-03 15:34:37 -070061 SkTaskGroup tg;
mtklein@google.com3a19fb52013-10-09 16:12:23 +000062 for (int i = 0; i < kTasks; i++) {
mtklein406654b2014-09-03 15:34:37 -070063 tg.add(&racers[i]);
mtklein@google.com3a19fb52013-10-09 16:12:23 +000064 }
mtklein406654b2014-09-03 15:34:37 -070065 tg.wait();
mtklein@google.com3a19fb52013-10-09 16:12:23 +000066
67 // Only one should have done the +=.
68 REPORTER_ASSERT(r, 6 == x);
69}
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000070
mtklein1b818772014-06-02 11:26:59 -070071static int gX = 0;
72static void inc_gX() { gX++; }
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000073
mtklein1b818772014-06-02 11:26:59 -070074DEF_TEST(SkOnce_NoArg, r) {
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000075 SK_DECLARE_STATIC_ONCE(once);
mtklein1b818772014-06-02 11:26:59 -070076 SkOnce(&once, inc_gX);
77 SkOnce(&once, inc_gX);
78 SkOnce(&once, inc_gX);
79 REPORTER_ASSERT(r, 1 == gX);
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000080}