| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkOnce.h" | 8 #include "SkOnce.h" |
| 9 #include "SkThreadPool.h" | 9 #include "SkThreadPool.h" |
| 10 #include "Test.h" | 10 #include "Test.h" |
| 11 #include "TestClassDef.h" | 11 #include "TestClassDef.h" |
| 12 | 12 |
| 13 DEF_SK_ONCE(add_five, int* x) { | 13 static void add_five(int* x) { |
| 14 *x += 5; | 14 *x += 5; |
| 15 } | 15 } |
| 16 | 16 |
| 17 DEF_TEST(SkOnce_Singlethreaded, r) { | 17 DEF_TEST(SkOnce_Singlethreaded, r) { |
| 18 int x = 0; | 18 int x = 0; |
| 19 | 19 |
| 20 SK_DECLARE_STATIC_ONCE(once); |
| 20 // No matter how many times we do this, x will be 5. | 21 // No matter how many times we do this, x will be 5. |
| 21 SK_ONCE(add_five, &x); | 22 SkOnce(&once, add_five, &x); |
| 22 SK_ONCE(add_five, &x); | 23 SkOnce(&once, add_five, &x); |
| 23 SK_ONCE(add_five, &x); | 24 SkOnce(&once, add_five, &x); |
| 24 SK_ONCE(add_five, &x); | 25 SkOnce(&once, add_five, &x); |
| 25 SK_ONCE(add_five, &x); | 26 SkOnce(&once, add_five, &x); |
| 26 | 27 |
| 27 REPORTER_ASSERT(r, 5 == x); | 28 REPORTER_ASSERT(r, 5 == x); |
| 28 } | 29 } |
| 29 | 30 |
| 30 DEF_SK_ONCE(add_six, int* x) { | 31 static void add_six(int* x) { |
| 31 *x += 6; | 32 *x += 6; |
| 32 } | 33 } |
| 33 | 34 |
| 34 class Racer : public SkRunnable { | 35 class Racer : public SkRunnable { |
| 35 public: | 36 public: |
| 37 SkOnceFlag* once; |
| 36 int* ptr; | 38 int* ptr; |
| 39 |
| 37 virtual void run() SK_OVERRIDE { | 40 virtual void run() SK_OVERRIDE { |
| 38 SK_ONCE(add_six, ptr); | 41 SkOnce(once, add_six, ptr); |
| 39 } | 42 } |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 DEF_TEST(SkOnce_Multithreaded, r) { | 45 DEF_TEST(SkOnce_Multithreaded, r) { |
| 43 const int kTasks = 16, kThreads = 4; | 46 const int kTasks = 16, kThreads = 4; |
| 44 | 47 |
| 45 // Make a bunch of tasks that will race to be the first to add six to x. | 48 // Make a bunch of tasks that will race to be the first to add six to x. |
| 46 Racer racers[kTasks]; | 49 Racer racers[kTasks]; |
| 50 SK_DECLARE_STATIC_ONCE(once); |
| 47 int x = 0; | 51 int x = 0; |
| 48 for (int i = 0; i < kTasks; i++) { | 52 for (int i = 0; i < kTasks; i++) { |
| 53 racers[i].once = &once; |
| 49 racers[i].ptr = &x; | 54 racers[i].ptr = &x; |
| 50 } | 55 } |
| 51 | 56 |
| 52 // Let them race. | 57 // Let them race. |
| 53 SkThreadPool pool(kThreads); | 58 SkThreadPool pool(kThreads); |
| 54 for (int i = 0; i < kTasks; i++) { | 59 for (int i = 0; i < kTasks; i++) { |
| 55 pool.add(&racers[i]); | 60 pool.add(&racers[i]); |
| 56 } | 61 } |
| 57 pool.wait(); | 62 pool.wait(); |
| 58 | 63 |
| 59 // Only one should have done the +=. | 64 // Only one should have done the +=. |
| 60 REPORTER_ASSERT(r, 6 == x); | 65 REPORTER_ASSERT(r, 6 == x); |
| 61 } | 66 } |
| OLD | NEW |