scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 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 | |
jvanverth | 02802f6 | 2015-07-02 06:42:49 -0700 | [diff] [blame] | 8 | #include "SkThread.h" |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 9 | #include "SkThreadUtils.h" |
| 10 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 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 | |
| 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 34 | DEF_TEST(Atomic, reporter) { |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 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 | } |