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 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 31 | static void add_six(int* x) { |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 32 | *x += 6; |
| 33 | } |
| 34 | |
mtklein | 1d93266 | 2014-10-20 13:46:11 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 37 | class Racer : public SkRunnable { |
| 38 | public: |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 39 | SkOnceFlag* once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 40 | int* ptr; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | void run() override { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 43 | SkOnce(once, add_six, ptr); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 44 | } |
| 45 | }; |
| 46 | |
mtklein | 1d93266 | 2014-10-20 13:46:11 -0700 | [diff] [blame] | 47 | } // namespace |
| 48 | |
mtklein | 199ba8e | 2015-01-13 08:40:23 -0800 | [diff] [blame] | 49 | SK_DECLARE_STATIC_ONCE(mt_once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 50 | DEF_TEST(SkOnce_Multithreaded, r) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 51 | const int kTasks = 16; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 52 | |
| 53 | // Make a bunch of tasks that will race to be the first to add six to x. |
| 54 | Racer racers[kTasks]; |
| 55 | int x = 0; |
| 56 | for (int i = 0; i < kTasks; i++) { |
mtklein | 199ba8e | 2015-01-13 08:40:23 -0800 | [diff] [blame] | 57 | racers[i].once = &mt_once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 58 | racers[i].ptr = &x; |
| 59 | } |
| 60 | |
| 61 | // Let them race. |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 62 | SkTaskGroup tg; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 63 | for (int i = 0; i < kTasks; i++) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 64 | tg.add(&racers[i]); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 65 | } |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 66 | tg.wait(); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 67 | |
| 68 | // Only one should have done the +=. |
| 69 | REPORTER_ASSERT(r, 6 == x); |
| 70 | } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 71 | |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 72 | static int gX = 0; |
| 73 | static void inc_gX() { gX++; } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 74 | |
mtklein | 199ba8e | 2015-01-13 08:40:23 -0800 | [diff] [blame] | 75 | SK_DECLARE_STATIC_ONCE(noarg_once); |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 76 | DEF_TEST(SkOnce_NoArg, r) { |
mtklein | 199ba8e | 2015-01-13 08:40:23 -0800 | [diff] [blame] | 77 | SkOnce(&noarg_once, inc_gX); |
| 78 | SkOnce(&noarg_once, inc_gX); |
| 79 | SkOnce(&noarg_once, inc_gX); |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 80 | REPORTER_ASSERT(r, 1 == gX); |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 81 | } |