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 | 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@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 35 | class Racer : public SkRunnable { |
| 36 | public: |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 37 | SkOnceFlag* once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 38 | int* ptr; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 39 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 40 | virtual void run() SK_OVERRIDE { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 41 | SkOnce(once, add_six, ptr); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 42 | } |
| 43 | }; |
| 44 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 45 | DEF_TEST(SkOnce_Multithreaded, r) { |
| 46 | const int kTasks = 16, kThreads = 4; |
| 47 | |
| 48 | // Make a bunch of tasks that will race to be the first to add six to x. |
| 49 | Racer racers[kTasks]; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 50 | SK_DECLARE_STATIC_ONCE(once); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 51 | int x = 0; |
| 52 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 53 | racers[i].once = &once; |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 54 | racers[i].ptr = &x; |
| 55 | } |
| 56 | |
| 57 | // Let them race. |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 58 | SkThreadPool pool(kThreads); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 59 | for (int i = 0; i < kTasks; i++) { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 60 | pool.add(&racers[i]); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 61 | } |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 62 | pool.wait(); |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 63 | |
| 64 | // Only one should have done the +=. |
| 65 | REPORTER_ASSERT(r, 6 == x); |
| 66 | } |