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@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 9 | #include "SkThreadPool.h" |
| 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 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 19 | SK_DECLARE_STATIC_ONCE(once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 20 | // No matter how many times we do this, x will be 5. |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 21 | 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.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 26 | |
| 27 | REPORTER_ASSERT(r, 5 == x); |
| 28 | } |
| 29 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 30 | static void add_six(int* x) { |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 31 | *x += 6; |
| 32 | } |
| 33 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 34 | class Racer : public SkRunnable { |
| 35 | public: |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 36 | SkOnceFlag* once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 37 | int* ptr; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 38 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 39 | virtual void run() SK_OVERRIDE { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 40 | SkOnce(once, add_six, ptr); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 41 | } |
| 42 | }; |
| 43 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 44 | DEF_TEST(SkOnce_Multithreaded, r) { |
| 45 | const int kTasks = 16, kThreads = 4; |
| 46 | |
| 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.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 49 | SK_DECLARE_STATIC_ONCE(once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 50 | int x = 0; |
| 51 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 52 | racers[i].once = &once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 53 | racers[i].ptr = &x; |
| 54 | } |
| 55 | |
| 56 | // Let them race. |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 57 | SkThreadPool pool(kThreads); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 58 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 59 | pool.add(&racers[i]); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 60 | } |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 61 | pool.wait(); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 62 | |
| 63 | // Only one should have done the +=. |
| 64 | REPORTER_ASSERT(r, 6 == x); |
| 65 | } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 66 | |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 67 | static int gX = 0; |
| 68 | static void inc_gX() { gX++; } |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 69 | |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 70 | DEF_TEST(SkOnce_NoArg, r) { |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 71 | SK_DECLARE_STATIC_ONCE(once); |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 72 | SkOnce(&once, inc_gX); |
| 73 | SkOnce(&once, inc_gX); |
| 74 | SkOnce(&once, inc_gX); |
| 75 | REPORTER_ASSERT(r, 1 == gX); |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 76 | } |