blob: 8b5ac9b67931c674f8b07d1d8377ddadb2718893 [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"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
scroggo@google.com50ccb0a2012-07-16 16:51:28 +000012
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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000034DEF_TEST(Atomic, reporter) {
scroggo@google.com50ccb0a2012-07-16 16:51:28 +000035 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}