blob: b7b13b3fab8e4cf5f19eea4af7ef332fd2042fe3 [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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
scroggo@google.com50ccb0a2012-07-16 16:51:28 +000010#include "SkThread.h"
11#include "SkThreadUtils.h"
12#include "SkTypes.h"
scroggo@google.com50ccb0a2012-07-16 16:51:28 +000013
14struct AddInfo {
15 int32_t valueToAdd;
16 int timesToAdd;
17 unsigned int processorAffinity;
18};
19
20static int32_t base = 0;
21
22static AddInfo gAdds[] = {
23 { 3, 100, 23 },
24 { 2, 200, 2 },
25 { 7, 150, 17 },
26};
27
28static void addABunchOfTimes(void* data) {
29 AddInfo* addInfo = static_cast<AddInfo*>(data);
30 for (int i = 0; i < addInfo->timesToAdd; i++) {
31 sk_atomic_add(&base, addInfo->valueToAdd);
32 }
33}
34
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000035DEF_TEST(Atomic, reporter) {
scroggo@google.com50ccb0a2012-07-16 16:51:28 +000036 int32_t total = base;
37 SkThread* threads[SK_ARRAY_COUNT(gAdds)];
38 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
39 total += gAdds[i].valueToAdd * gAdds[i].timesToAdd;
40 }
41 // Start the threads
42 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
43 threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]);
44 threads[i]->setProcessorAffinity(gAdds[i].processorAffinity);
45 threads[i]->start();
46 }
47
48 // Now end the threads
49 for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
50 threads[i]->join();
51 delete threads[i];
52 }
53 REPORTER_ASSERT(reporter, total == base);
54 // Ensure that the returned value from sk_atomic_add is correct.
55 int32_t valueToModify = 3;
56 const int32_t originalValue = valueToModify;
57 REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7));
58}