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" |
(...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(&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 SkOnce(&once, add_five, &x); | 26 SkOnce(&once, add_five, &x); |
27 | 27 |
28 REPORTER_ASSERT(r, 5 == x); | 28 REPORTER_ASSERT(r, 5 == x); |
29 } | 29 } |
30 | 30 |
| 31 struct AddFour { void operator()(int* x) { *x += 4; } }; |
| 32 |
| 33 DEF_TEST(SkOnce_MiscFeatures, r) { |
| 34 // Tests that we support functors and explicit SkOnceFlags. |
| 35 int x = 0; |
| 36 |
| 37 SkOnceFlag once = SK_ONCE_INIT; |
| 38 SkOnce(&once, AddFour(), &x); |
| 39 SkOnce(&once, AddFour(), &x); |
| 40 SkOnce(&once, AddFour(), &x); |
| 41 |
| 42 REPORTER_ASSERT(r, 4 == x); |
| 43 } |
| 44 |
31 static void add_six(int* x) { | 45 static void add_six(int* x) { |
32 *x += 6; | 46 *x += 6; |
33 } | 47 } |
34 | 48 |
35 class Racer : public SkRunnable { | 49 class Racer : public SkRunnable { |
36 public: | 50 public: |
37 SkOnceFlag* once; | 51 SkOnceFlag* once; |
38 int* ptr; | 52 int* ptr; |
39 | 53 |
40 virtual void run() SK_OVERRIDE { | 54 virtual void run() SK_OVERRIDE { |
(...skipping 16 matching lines...) Expand all Loading... |
57 // Let them race. | 71 // Let them race. |
58 SkThreadPool pool(kThreads); | 72 SkThreadPool pool(kThreads); |
59 for (int i = 0; i < kTasks; i++) { | 73 for (int i = 0; i < kTasks; i++) { |
60 pool.add(&racers[i]); | 74 pool.add(&racers[i]); |
61 } | 75 } |
62 pool.wait(); | 76 pool.wait(); |
63 | 77 |
64 // Only one should have done the +=. | 78 // Only one should have done the +=. |
65 REPORTER_ASSERT(r, 6 == x); | 79 REPORTER_ASSERT(r, 6 == x); |
66 } | 80 } |
OLD | NEW |