blob: a9ab8d227972756c64b2cec726cba60f14a1ffb0 [file] [log] [blame]
scroggo@google.com50ccb0a2012-07-16 16:51:28 +00001/*
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
13struct AddInfo {
14 int32_t valueToAdd;
15 int timesToAdd;
16 unsigned int processorAffinity;
17};
18
19static int32_t base = 0;
20
21static AddInfo gAdds[] = {
22 { 3, 100, 23 },
23 { 2, 200, 2 },
24 { 7, 150, 17 },
25};
26
27static 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
34static void test_atomicAddTests(skiatest::Reporter* 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
59#include "TestClassDef.h"
60DEFINE_TESTCLASS("AtomicAdd", AtomicAddTestClass, test_atomicAddTests)