mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 1 | /* |
| 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 | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 9 | #include "SkTaskGroup.h" |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 10 | #include "Test.h" |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 11 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 12 | static void add_five(int* x) { |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 13 | *x += 5; |
| 14 | } |
| 15 | |
| 16 | DEF_TEST(SkOnce_Singlethreaded, r) { |
| 17 | int x = 0; |
| 18 | |
| 19 | // No matter how many times we do this, x will be 5. |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 20 | SkOnce once; |
| 21 | once(add_five, &x); |
| 22 | once(add_five, &x); |
| 23 | once(add_five, &x); |
| 24 | once(add_five, &x); |
| 25 | once(add_five, &x); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 26 | |
| 27 | REPORTER_ASSERT(r, 5 == x); |
| 28 | } |
| 29 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 30 | DEF_TEST(SkOnce_Multithreaded, r) { |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 31 | int x = 0; |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 32 | |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 33 | // Run a bunch of tasks to be the first to add six to x. |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 34 | SkOnce once; |
mtklein | 279c786 | 2016-01-04 19:13:19 -0800 | [diff] [blame] | 35 | SkTaskGroup().batch(1021, [&](int) { |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 36 | once([&] { x += 6; }); |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 37 | }); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 38 | |
| 39 | // Only one should have done the +=. |
| 40 | REPORTER_ASSERT(r, 6 == x); |
| 41 | } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 42 | |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 43 | static int gX = 0; |
| 44 | static void inc_gX() { gX++; } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 45 | |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 46 | DEF_TEST(SkOnce_NoArg, r) { |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 47 | SkOnce once; |
| 48 | once(inc_gX); |
| 49 | once(inc_gX); |
| 50 | once(inc_gX); |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(r, 1 == gX); |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 52 | } |