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