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" |
| 11 | #include "TestClassDef.h" |
| 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 | |
| 17 | DEF_TEST(SkOnce_Singlethreaded, r) { |
| 18 | int x = 0; |
| 19 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 20 | SK_DECLARE_STATIC_ONCE(once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 21 | // 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] | 22 | SkOnce(&once, add_five, &x); |
| 23 | SkOnce(&once, add_five, &x); |
| 24 | SkOnce(&once, add_five, &x); |
| 25 | SkOnce(&once, add_five, &x); |
| 26 | SkOnce(&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 | 1b2c1b8 | 2013-12-05 19:20:49 +0000 | [diff] [blame] | 31 | struct AddFour { void operator()(int* x) { *x += 4; } }; |
| 32 | |
| 33 | DEF_TEST(SkOnce_MiscFeatures, r) { |
| 34 | // Tests that we support functors and explicit SkOnceFlags. |
| 35 | int x = 0; |
| 36 | |
| 37 | SkOnceFlag once = SK_ONCE_INIT; |
| 38 | SkOnce(&once, AddFour(), &x); |
| 39 | SkOnce(&once, AddFour(), &x); |
| 40 | SkOnce(&once, AddFour(), &x); |
| 41 | |
| 42 | REPORTER_ASSERT(r, 4 == x); |
| 43 | } |
| 44 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 45 | static void add_six(int* x) { |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 46 | *x += 6; |
| 47 | } |
| 48 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 49 | class Racer : public SkRunnable { |
| 50 | public: |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 51 | SkOnceFlag* once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 52 | int* ptr; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 53 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 54 | virtual void run() SK_OVERRIDE { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 55 | SkOnce(once, add_six, ptr); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 59 | DEF_TEST(SkOnce_Multithreaded, r) { |
| 60 | const int kTasks = 16, kThreads = 4; |
| 61 | |
| 62 | // Make a bunch of tasks that will race to be the first to add six to x. |
| 63 | Racer racers[kTasks]; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 64 | SK_DECLARE_STATIC_ONCE(once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 65 | int x = 0; |
| 66 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 67 | racers[i].once = &once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 68 | racers[i].ptr = &x; |
| 69 | } |
| 70 | |
| 71 | // Let them race. |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 72 | SkThreadPool pool(kThreads); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 73 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 74 | pool.add(&racers[i]); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 75 | } |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 76 | pool.wait(); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 77 | |
| 78 | // Only one should have done the +=. |
| 79 | REPORTER_ASSERT(r, 6 == x); |
| 80 | } |