aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/AtomicTest.cpp
blob: a9ab8d227972756c64b2cec726cba60f14a1ffb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkThread.h"
#include "SkThreadUtils.h"
#include "SkTypes.h"
#include "Test.h"

struct AddInfo {
    int32_t valueToAdd;
    int timesToAdd;
    unsigned int processorAffinity;
};

static int32_t base = 0;

static AddInfo gAdds[] = {
    { 3, 100, 23 },
    { 2, 200, 2 },
    { 7, 150, 17 },
};

static void addABunchOfTimes(void* data) {
    AddInfo* addInfo = static_cast<AddInfo*>(data);
    for (int i = 0; i < addInfo->timesToAdd; i++) {
        sk_atomic_add(&base, addInfo->valueToAdd);
    }
}

static void test_atomicAddTests(skiatest::Reporter* reporter) {
    int32_t total = base;
    SkThread* threads[SK_ARRAY_COUNT(gAdds)];
    for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
        total += gAdds[i].valueToAdd * gAdds[i].timesToAdd;
    }
    // Start the threads
    for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
        threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]);
        threads[i]->setProcessorAffinity(gAdds[i].processorAffinity);
        threads[i]->start();
    }

    // Now end the threads
    for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
        threads[i]->join();
        delete threads[i];
    }
    REPORTER_ASSERT(reporter, total == base);
    // Ensure that the returned value from sk_atomic_add is correct.
    int32_t valueToModify = 3;
    const int32_t originalValue = valueToModify;
    REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7));
}

#include "TestClassDef.h"
DEFINE_TESTCLASS("AtomicAdd", AtomicAddTestClass, test_atomicAddTests)