| 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 "SkRunnable.h" | 9 #include "SkRunnable.h" |
| 10 #include "SkTaskGroup.h" | 10 #include "SkTaskGroup.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // 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. |
| 22 SkOnce(&st_once, add_five, &x); | 22 SkOnce(&st_once, add_five, &x); |
| 23 SkOnce(&st_once, add_five, &x); | 23 SkOnce(&st_once, add_five, &x); |
| 24 SkOnce(&st_once, add_five, &x); | 24 SkOnce(&st_once, add_five, &x); |
| 25 SkOnce(&st_once, add_five, &x); | 25 SkOnce(&st_once, add_five, &x); |
| 26 SkOnce(&st_once, add_five, &x); | 26 SkOnce(&st_once, add_five, &x); |
| 27 | 27 |
| 28 REPORTER_ASSERT(r, 5 == x); | 28 REPORTER_ASSERT(r, 5 == x); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static void add_six(int* x) { | |
| 32 *x += 6; | |
| 33 } | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 class Racer : public SkRunnable { | |
| 38 public: | |
| 39 SkOnceFlag* once; | |
| 40 int* ptr; | |
| 41 | |
| 42 void run() override { | |
| 43 SkOnce(once, add_six, ptr); | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 SK_DECLARE_STATIC_ONCE(mt_once); | 31 SK_DECLARE_STATIC_ONCE(mt_once); |
| 50 DEF_TEST(SkOnce_Multithreaded, r) { | 32 DEF_TEST(SkOnce_Multithreaded, r) { |
| 51 const int kTasks = 16; | |
| 52 | |
| 53 // Make a bunch of tasks that will race to be the first to add six to x. | |
| 54 Racer racers[kTasks]; | |
| 55 int x = 0; | 33 int x = 0; |
| 56 for (int i = 0; i < kTasks; i++) { | 34 // Run a bunch of tasks to be the first to add six to x. |
| 57 racers[i].once = &mt_once; | 35 sk_parallel_for(1021, [&](int) { |
| 58 racers[i].ptr = &x; | 36 void(*add_six)(int*) = [](int* p) { *p += 6; }; |
| 59 } | 37 SkOnce(&mt_once, add_six, &x); |
| 60 | 38 }); |
| 61 // Let them race. | |
| 62 SkTaskGroup tg; | |
| 63 for (int i = 0; i < kTasks; i++) { | |
| 64 tg.add(&racers[i]); | |
| 65 } | |
| 66 tg.wait(); | |
| 67 | 39 |
| 68 // Only one should have done the +=. | 40 // Only one should have done the +=. |
| 69 REPORTER_ASSERT(r, 6 == x); | 41 REPORTER_ASSERT(r, 6 == x); |
| 70 } | 42 } |
| 71 | 43 |
| 72 static int gX = 0; | 44 static int gX = 0; |
| 73 static void inc_gX() { gX++; } | 45 static void inc_gX() { gX++; } |
| 74 | 46 |
| 75 SK_DECLARE_STATIC_ONCE(noarg_once); | 47 SK_DECLARE_STATIC_ONCE(noarg_once); |
| 76 DEF_TEST(SkOnce_NoArg, r) { | 48 DEF_TEST(SkOnce_NoArg, r) { |
| 77 SkOnce(&noarg_once, inc_gX); | 49 SkOnce(&noarg_once, inc_gX); |
| 78 SkOnce(&noarg_once, inc_gX); | 50 SkOnce(&noarg_once, inc_gX); |
| 79 SkOnce(&noarg_once, inc_gX); | 51 SkOnce(&noarg_once, inc_gX); |
| 80 REPORTER_ASSERT(r, 1 == gX); | 52 REPORTER_ASSERT(r, 1 == gX); |
| 81 } | 53 } |
| OLD | NEW |