| 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 "SkTaskGroup.h" | 9 #include "SkThreadPool.h" |
| 10 #include "Test.h" | 10 #include "Test.h" |
| 11 | 11 |
| 12 static void add_five(int* x) { | 12 static void add_five(int* x) { |
| 13 *x += 5; | 13 *x += 5; |
| 14 } | 14 } |
| 15 | 15 |
| 16 DEF_TEST(SkOnce_Singlethreaded, r) { | 16 DEF_TEST(SkOnce_Singlethreaded, r) { |
| 17 int x = 0; | 17 int x = 0; |
| 18 | 18 |
| 19 SK_DECLARE_STATIC_ONCE(once); | 19 SK_DECLARE_STATIC_ONCE(once); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 35 public: | 35 public: |
| 36 SkOnceFlag* once; | 36 SkOnceFlag* once; |
| 37 int* ptr; | 37 int* ptr; |
| 38 | 38 |
| 39 virtual void run() SK_OVERRIDE { | 39 virtual void run() SK_OVERRIDE { |
| 40 SkOnce(once, add_six, ptr); | 40 SkOnce(once, add_six, ptr); |
| 41 } | 41 } |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 DEF_TEST(SkOnce_Multithreaded, r) { | 44 DEF_TEST(SkOnce_Multithreaded, r) { |
| 45 const int kTasks = 16; | 45 const int kTasks = 16, kThreads = 4; |
| 46 | 46 |
| 47 // Make a bunch of tasks that will race to be the first to add six to x. | 47 // Make a bunch of tasks that will race to be the first to add six to x. |
| 48 Racer racers[kTasks]; | 48 Racer racers[kTasks]; |
| 49 SK_DECLARE_STATIC_ONCE(once); | 49 SK_DECLARE_STATIC_ONCE(once); |
| 50 int x = 0; | 50 int x = 0; |
| 51 for (int i = 0; i < kTasks; i++) { | 51 for (int i = 0; i < kTasks; i++) { |
| 52 racers[i].once = &once; | 52 racers[i].once = &once; |
| 53 racers[i].ptr = &x; | 53 racers[i].ptr = &x; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Let them race. | 56 // Let them race. |
| 57 SkTaskGroup tg; | 57 SkThreadPool pool(kThreads); |
| 58 for (int i = 0; i < kTasks; i++) { | 58 for (int i = 0; i < kTasks; i++) { |
| 59 tg.add(&racers[i]); | 59 pool.add(&racers[i]); |
| 60 } | 60 } |
| 61 tg.wait(); | 61 pool.wait(); |
| 62 | 62 |
| 63 // Only one should have done the +=. | 63 // Only one should have done the +=. |
| 64 REPORTER_ASSERT(r, 6 == x); | 64 REPORTER_ASSERT(r, 6 == x); |
| 65 } | 65 } |
| 66 | 66 |
| 67 static int gX = 0; | 67 static int gX = 0; |
| 68 static void inc_gX() { gX++; } | 68 static void inc_gX() { gX++; } |
| 69 | 69 |
| 70 DEF_TEST(SkOnce_NoArg, r) { | 70 DEF_TEST(SkOnce_NoArg, r) { |
| 71 SK_DECLARE_STATIC_ONCE(once); | 71 SK_DECLARE_STATIC_ONCE(once); |
| 72 SkOnce(&once, inc_gX); | 72 SkOnce(&once, inc_gX); |
| 73 SkOnce(&once, inc_gX); | 73 SkOnce(&once, inc_gX); |
| 74 SkOnce(&once, inc_gX); | 74 SkOnce(&once, inc_gX); |
| 75 REPORTER_ASSERT(r, 1 == gX); | 75 REPORTER_ASSERT(r, 1 == gX); |
| 76 } | 76 } |
| OLD | NEW |