Home | History | Annotate | Download | only in tests
      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 
      8 #include "Test.h"
      9 #include "TestClassDef.h"
     10 #include "SkThread.h"
     11 #include "SkThreadUtils.h"
     12 #include "SkTypes.h"
     13 
     14 struct AddInfo {
     15     int32_t valueToAdd;
     16     int timesToAdd;
     17     unsigned int processorAffinity;
     18 };
     19 
     20 static int32_t base = 0;
     21 
     22 static AddInfo gAdds[] = {
     23     { 3, 100, 23 },
     24     { 2, 200, 2 },
     25     { 7, 150, 17 },
     26 };
     27 
     28 static 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 
     35 DEF_TEST(Atomic, reporter) {
     36     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 }
     59