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 "SkTaskGroup.h" |
10 #include "Test.h" | 10 #include "Test.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 SkOnce(&once, add_five, &x); | 24 SkOnce(&once, add_five, &x); |
25 SkOnce(&once, add_five, &x); | 25 SkOnce(&once, add_five, &x); |
26 | 26 |
27 REPORTER_ASSERT(r, 5 == x); | 27 REPORTER_ASSERT(r, 5 == x); |
28 } | 28 } |
29 | 29 |
30 static void add_six(int* x) { | 30 static void add_six(int* x) { |
31 *x += 6; | 31 *x += 6; |
32 } | 32 } |
33 | 33 |
| 34 namespace { |
| 35 |
34 class Racer : public SkRunnable { | 36 class Racer : public SkRunnable { |
35 public: | 37 public: |
36 SkOnceFlag* once; | 38 SkOnceFlag* once; |
37 int* ptr; | 39 int* ptr; |
38 | 40 |
39 virtual void run() SK_OVERRIDE { | 41 virtual void run() SK_OVERRIDE { |
40 SkOnce(once, add_six, ptr); | 42 SkOnce(once, add_six, ptr); |
41 } | 43 } |
42 }; | 44 }; |
43 | 45 |
| 46 } // namespace |
| 47 |
44 DEF_TEST(SkOnce_Multithreaded, r) { | 48 DEF_TEST(SkOnce_Multithreaded, r) { |
45 const int kTasks = 16; | 49 const int kTasks = 16; |
46 | 50 |
47 // Make a bunch of tasks that will race to be the first to add six to x. | 51 // Make a bunch of tasks that will race to be the first to add six to x. |
48 Racer racers[kTasks]; | 52 Racer racers[kTasks]; |
49 SK_DECLARE_STATIC_ONCE(once); | 53 SK_DECLARE_STATIC_ONCE(once); |
50 int x = 0; | 54 int x = 0; |
51 for (int i = 0; i < kTasks; i++) { | 55 for (int i = 0; i < kTasks; i++) { |
52 racers[i].once = &once; | 56 racers[i].once = &once; |
53 racers[i].ptr = &x; | 57 racers[i].ptr = &x; |
(...skipping 13 matching lines...) Expand all Loading... |
67 static int gX = 0; | 71 static int gX = 0; |
68 static void inc_gX() { gX++; } | 72 static void inc_gX() { gX++; } |
69 | 73 |
70 DEF_TEST(SkOnce_NoArg, r) { | 74 DEF_TEST(SkOnce_NoArg, r) { |
71 SK_DECLARE_STATIC_ONCE(once); | 75 SK_DECLARE_STATIC_ONCE(once); |
72 SkOnce(&once, inc_gX); | 76 SkOnce(&once, inc_gX); |
73 SkOnce(&once, inc_gX); | 77 SkOnce(&once, inc_gX); |
74 SkOnce(&once, inc_gX); | 78 SkOnce(&once, inc_gX); |
75 REPORTER_ASSERT(r, 1 == gX); | 79 REPORTER_ASSERT(r, 1 == gX); |
76 } | 80 } |
OLD | NEW |