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