OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "Test.h" | 8 #include "Test.h" |
9 #include "SkCommonFlags.h" | |
9 #include "SkOncePtr.h" | 10 #include "SkOncePtr.h" |
10 #include "SkTaskGroup.h" | 11 #include "SkTaskGroup.h" |
11 | 12 |
12 DEF_TEST(OncePtr, r) { | 13 DEF_TEST(OncePtr, r) { |
14 if (FLAGS_threads > 0 && FLAGS_threads < sk_num_cores()) { | |
15 return; | |
16 } | |
17 | |
13 SkOncePtr<int> once; | 18 SkOncePtr<int> once; |
14 | 19 |
15 static SkAtomic<int> calls(0); | 20 static SkAtomic<int> calls(0); |
16 auto create = [&] { | 21 auto create = [&] { |
17 calls.fetch_add(1); | 22 calls.fetch_add(1); |
18 return new int(5); | 23 return new int(5); |
19 }; | 24 }; |
20 | 25 |
21 SkAtomic<int> force_a_race(sk_num_cores()); | 26 SkAtomic<int> force_a_race(sk_num_cores()); |
mtklein
2015/10/26 13:36:37
Does this work correctly if we change the initial
herb_g
2015/10/26 14:32:56
I agree with Mike on this. I don't think the new g
Kimmo Kinnunen
2015/10/27 06:44:41
Sure..
| |
22 | 27 |
23 sk_parallel_for(sk_num_cores()*4, [&](size_t) { | 28 sk_parallel_for(sk_num_cores()*4, [&](size_t) { |
24 force_a_race.fetch_add(-1); | 29 force_a_race.fetch_add(-1); |
25 while (force_a_race.load() > 0); | 30 while (force_a_race.load() > 0); |
26 | 31 |
27 int* n = once.get(create); | 32 SkAutoTDelete<int> n(once.get(create)); |
mtklein
2015/10/26 13:36:37
Pretty sure ~SkOncePtr deletes the pointer and thi
Kimmo Kinnunen
2015/10/27 06:44:41
I see. I didn't think this through, sorry.
| |
28 REPORTER_ASSERT(r, *n == 5); | 33 REPORTER_ASSERT(r, *n == 5); |
29 }); | 34 }); |
30 REPORTER_ASSERT(r, calls.load() == 1); | 35 REPORTER_ASSERT(r, calls.load() == 1); |
31 } | 36 } |
32 | 37 |
33 /* TODO(mtklein): next CL | 38 /* TODO(mtklein): next CL |
34 | 39 |
35 SK_DECLARE_STATIC_ONCE(once_noptr); | 40 SK_DECLARE_STATIC_ONCE(once_noptr); |
36 DEF_TEST(OnceNoPtr, r) { | 41 DEF_TEST(OnceNoPtr, r) { |
37 static SkAtomic<int> calls(0); | 42 static SkAtomic<int> calls(0); |
38 | 43 |
39 SkAtomic<int> force_a_race(sk_num_cores()); | 44 SkAtomic<int> force_a_race(sk_num_cores()); |
40 sk_parallel_for(sk_num_cores()*4, [&](size_t) { | 45 sk_parallel_for(sk_num_cores()*4, [&](size_t) { |
41 force_a_race.fetch_add(-1); | 46 force_a_race.fetch_add(-1); |
42 while (force_a_race.load() > 0); | 47 while (force_a_race.load() > 0); |
43 | 48 |
44 SkOnce(&once_noptr, [&] { calls.fetch_add(1); }); | 49 SkOnce(&once_noptr, [&] { calls.fetch_add(1); }); |
45 }); | 50 }); |
46 REPORTER_ASSERT(r, calls.load() == 1); | 51 REPORTER_ASSERT(r, calls.load() == 1); |
47 } | 52 } |
48 */ | 53 */ |
OLD | NEW |