1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkThread.h"
9 #include "SkThreadUtils.h"
10 #include "SkTypes.h"
11 #include "Test.h"
12
13 struct AddInfo {
14 int32_t valueToAdd;
15 int timesToAdd;
16 unsigned int processorAffinity;
17 };
18
19 static int32_t base = 0;
20
21 static AddInfo gAdds[] = {
22 { 3, 100, 23 },
23 { 2, 200, 2 },
24 { 7, 150, 17 },
25 };
26
addABunchOfTimes(void * data)27 static void addABunchOfTimes(void* data) {
28 AddInfo* addInfo = static_cast<AddInfo*>(data);
29 for (int i = 0; i < addInfo->timesToAdd; i++) {
30 sk_atomic_add(&base, addInfo->valueToAdd);
31 }
32 }
33
DEF_TEST(Atomic,reporter)34 DEF_TEST(Atomic, reporter) {
35 int32_t total = base;
36 SkThread* threads[SK_ARRAY_COUNT(gAdds)];
37 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
38 total += gAdds[i].valueToAdd * gAdds[i].timesToAdd;
39 }
40 // Start the threads
41 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
42 threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]);
43 threads[i]->setProcessorAffinity(gAdds[i].processorAffinity);
44 threads[i]->start();
45 }
46
47 // Now end the threads
48 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
49 threads[i]->join();
50 delete threads[i];
51 }
52 REPORTER_ASSERT(reporter, total == base);
53 // Ensure that the returned value from sk_atomic_add is correct.
54 int32_t valueToModify = 3;
55 const int32_t originalValue = valueToModify;
56 REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7));
57 }
58