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 | 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); |
20 // No matter how many times we do this, x will be 5. | 20 // No matter how many times we do this, x will be 5. |
21 SkOnce(&once, add_five, &x); | 21 SkOnce(&once, add_five, &x); |
22 SkOnce(&once, add_five, &x); | 22 SkOnce(&once, add_five, &x); |
23 SkOnce(&once, add_five, &x); | 23 SkOnce(&once, add_five, &x); |
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 struct AddFour { void operator()(int* x) { *x += 4; } }; | |
31 | |
32 DEF_TEST(SkOnce_MiscFeatures, r) { | |
33 // Tests that we support functors and explicit SkOnceFlags. | |
34 int x = 0; | |
35 | |
36 SkOnceFlag once = SK_ONCE_INIT; | |
37 SkOnce(&once, AddFour(), &x); | |
38 SkOnce(&once, AddFour(), &x); | |
39 SkOnce(&once, AddFour(), &x); | |
40 | |
41 REPORTER_ASSERT(r, 4 == x); | |
42 } | |
43 | |
44 static void add_six(int* x) { | 30 static void add_six(int* x) { |
45 *x += 6; | 31 *x += 6; |
46 } | 32 } |
47 | 33 |
48 class Racer : public SkRunnable { | 34 class Racer : public SkRunnable { |
49 public: | 35 public: |
50 SkOnceFlag* once; | 36 SkOnceFlag* once; |
51 int* ptr; | 37 int* ptr; |
52 | 38 |
53 virtual void run() SK_OVERRIDE { | 39 virtual void run() SK_OVERRIDE { |
(...skipping 17 matching lines...) Expand all Loading... |
71 SkThreadPool pool(kThreads); | 57 SkThreadPool pool(kThreads); |
72 for (int i = 0; i < kTasks; i++) { | 58 for (int i = 0; i < kTasks; i++) { |
73 pool.add(&racers[i]); | 59 pool.add(&racers[i]); |
74 } | 60 } |
75 pool.wait(); | 61 pool.wait(); |
76 | 62 |
77 // Only one should have done the +=. | 63 // Only one should have done the +=. |
78 REPORTER_ASSERT(r, 6 == x); | 64 REPORTER_ASSERT(r, 6 == x); |
79 } | 65 } |
80 | 66 |
81 // Test that the atExit option works. | 67 static int gX = 0; |
82 static int gToDecrement = 1; | 68 static void inc_gX() { gX++; } |
83 static void noop(int) {} | |
84 static void decrement() { gToDecrement--; } | |
85 static void checkDecremented() { SkASSERT(gToDecrement == 0); } | |
86 | 69 |
87 DEF_TEST(SkOnce_atExit, r) { | 70 DEF_TEST(SkOnce_NoArg, r) { |
88 atexit(checkDecremented); | |
89 SK_DECLARE_STATIC_ONCE(once); | 71 SK_DECLARE_STATIC_ONCE(once); |
90 SkOnce(&once, noop, 0, decrement); | 72 SkOnce(&once, inc_gX); |
| 73 SkOnce(&once, inc_gX); |
| 74 SkOnce(&once, inc_gX); |
| 75 REPORTER_ASSERT(r, 1 == gX); |
91 } | 76 } |
OLD | NEW |