OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkAtomics.h" | 8 #include "SkAtomics.h" |
9 #include "SkThreadUtils.h" | 9 #include "SkThreadUtils.h" |
10 #include "SkTypes.h" | 10 #include "SkTypes.h" |
11 #include "Test.h" | 11 #include "Test.h" |
12 | 12 |
13 struct AddInfo { | 13 struct AddInfo { |
14 int32_t valueToAdd; | 14 int32_t valueToAdd; |
15 int timesToAdd; | 15 int timesToAdd; |
16 unsigned int processorAffinity; | |
17 }; | 16 }; |
18 | 17 |
19 static int32_t base = 0; | 18 static int32_t base = 0; |
20 | 19 |
21 static AddInfo gAdds[] = { | 20 static AddInfo gAdds[] = { |
22 { 3, 100, 23 }, | 21 { 3, 100 }, |
23 { 2, 200, 2 }, | 22 { 2, 200 }, |
24 { 7, 150, 17 }, | 23 { 7, 150 }, |
25 }; | 24 }; |
26 | 25 |
27 static void addABunchOfTimes(void* data) { | 26 static void addABunchOfTimes(void* data) { |
28 AddInfo* addInfo = static_cast<AddInfo*>(data); | 27 AddInfo* addInfo = static_cast<AddInfo*>(data); |
29 for (int i = 0; i < addInfo->timesToAdd; i++) { | 28 for (int i = 0; i < addInfo->timesToAdd; i++) { |
30 sk_atomic_add(&base, addInfo->valueToAdd); | 29 sk_atomic_add(&base, addInfo->valueToAdd); |
31 } | 30 } |
32 } | 31 } |
33 | 32 |
34 DEF_TEST(Atomic, reporter) { | 33 DEF_TEST(Atomic, reporter) { |
35 int32_t total = base; | 34 int32_t total = base; |
36 SkThread* threads[SK_ARRAY_COUNT(gAdds)]; | 35 SkThread* threads[SK_ARRAY_COUNT(gAdds)]; |
37 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { | 36 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { |
38 total += gAdds[i].valueToAdd * gAdds[i].timesToAdd; | 37 total += gAdds[i].valueToAdd * gAdds[i].timesToAdd; |
39 } | 38 } |
40 // Start the threads | 39 // Start the threads |
41 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { | 40 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { |
42 threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]); | 41 threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]); |
43 threads[i]->setProcessorAffinity(gAdds[i].processorAffinity); | |
44 threads[i]->start(); | 42 threads[i]->start(); |
45 } | 43 } |
46 | 44 |
47 // Now end the threads | 45 // Now end the threads |
48 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { | 46 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) { |
49 threads[i]->join(); | 47 threads[i]->join(); |
50 delete threads[i]; | 48 delete threads[i]; |
51 } | 49 } |
52 REPORTER_ASSERT(reporter, total == base); | 50 REPORTER_ASSERT(reporter, total == base); |
53 // Ensure that the returned value from sk_atomic_add is correct. | 51 // Ensure that the returned value from sk_atomic_add is correct. |
54 int32_t valueToModify = 3; | 52 int32_t valueToModify = 3; |
55 const int32_t originalValue = valueToModify; | 53 const int32_t originalValue = valueToModify; |
56 REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7))
; | 54 REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7))
; |
57 | 55 |
58 { | 56 { |
59 SkAtomic<int> v {0}; | 57 SkAtomic<int> v {0}; |
60 REPORTER_ASSERT(reporter, 0 == v.load()); | 58 REPORTER_ASSERT(reporter, 0 == v.load()); |
61 v = 10; | 59 v = 10; |
62 REPORTER_ASSERT(reporter, 10 == v.load()); | 60 REPORTER_ASSERT(reporter, 10 == v.load()); |
63 int q = v; | 61 int q = v; |
64 REPORTER_ASSERT(reporter, 10 == q); | 62 REPORTER_ASSERT(reporter, 10 == q); |
65 } | 63 } |
66 } | 64 } |
OLD | NEW |