| OLD | NEW |
| 1 /* |
| 2 * Copyright 2014 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 |
| 1 #include "Test.h" | 8 #include "Test.h" |
| 2 #include "SkLazyPtr.h" | 9 #include "SkLazyPtr.h" |
| 3 #include "SkRunnable.h" | 10 #include "SkRunnable.h" |
| 4 #include "SkTaskGroup.h" | 11 #include "SkTaskGroup.h" |
| 5 | 12 |
| 6 namespace { | 13 namespace { |
| 7 | 14 |
| 8 struct CreateIntFromFloat { | 15 struct CreateIntFromFloat { |
| 9 CreateIntFromFloat(float val) : fVal(val) {} | 16 CreateIntFromFloat(float val) : fVal(val) {} |
| 10 int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); } | 17 int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 SkLazyPtr<int, custom_destroy> customDestroy; | 44 SkLazyPtr<int, custom_destroy> customDestroy; |
| 38 ptr = customDestroy.get(); | 45 ptr = customDestroy.get(); |
| 39 // custom_destroy called here. | 46 // custom_destroy called here. |
| 40 } | 47 } |
| 41 REPORTER_ASSERT(r, ptr); | 48 REPORTER_ASSERT(r, ptr); |
| 42 REPORTER_ASSERT(r, 99 == *ptr); | 49 REPORTER_ASSERT(r, 99 == *ptr); |
| 43 // Since custom_destroy didn't actually delete ptr, we do now. | 50 // Since custom_destroy didn't actually delete ptr, we do now. |
| 44 SkDELETE(ptr); | 51 SkDELETE(ptr); |
| 45 } | 52 } |
| 46 | 53 |
| 47 namespace { | |
| 48 | |
| 49 struct Racer : public SkRunnable { | |
| 50 Racer() : fLazy(NULL), fSeen(NULL) {} | |
| 51 | |
| 52 void run() override { fSeen = fLazy->get(); } | |
| 53 | |
| 54 SkLazyPtr<int>* fLazy; | |
| 55 int* fSeen; | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 DEF_TEST(LazyPtr_Threaded, r) { | 54 DEF_TEST(LazyPtr_Threaded, r) { |
| 61 static const int kRacers = 321; | 55 static const int kRacers = 321; |
| 62 | 56 |
| 57 // Race to intialize the pointer by calling .get(). |
| 63 SkLazyPtr<int> lazy; | 58 SkLazyPtr<int> lazy; |
| 59 int* seen[kRacers]; |
| 64 | 60 |
| 65 Racer racers[kRacers]; | 61 sk_parallel_for(kRacers, [&](int i) { |
| 66 for (int i = 0; i < kRacers; i++) { | 62 seen[i] = lazy.get(); |
| 67 racers[i].fLazy = &lazy; | 63 }); |
| 68 } | |
| 69 | 64 |
| 70 SkTaskGroup tg; | 65 // lazy.get() should return the same pointer to all threads. |
| 71 for (int i = 0; i < kRacers; i++) { | |
| 72 tg.add(racers + i); | |
| 73 } | |
| 74 tg.wait(); | |
| 75 | |
| 76 for (int i = 1; i < kRacers; i++) { | 66 for (int i = 1; i < kRacers; i++) { |
| 77 REPORTER_ASSERT(r, racers[i].fSeen); | 67 REPORTER_ASSERT(r, seen[i] != nullptr); |
| 78 REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen); | 68 REPORTER_ASSERT(r, seen[i] == seen[0]); |
| 79 } | 69 } |
| 80 } | 70 } |
| OLD | NEW |