mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 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 | |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkLazyPtr.h" |
reed | 89889b6 | 2014-10-29 12:36:45 -0700 | [diff] [blame] | 10 | #include "SkRunnable.h" |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 11 | #include "SkTaskGroup.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | struct CreateIntFromFloat { |
| 16 | CreateIntFromFloat(float val) : fVal(val) {} |
| 17 | int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); } |
| 18 | float fVal; |
| 19 | }; |
| 20 | |
| 21 | // As a template argument this must have external linkage. |
| 22 | void custom_destroy(int* ptr) { *ptr = 99; } |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | DEF_TEST(LazyPtr, r) { |
| 27 | // Basic usage: calls SkNEW(int). |
| 28 | SkLazyPtr<int> lazy; |
| 29 | int* ptr = lazy.get(); |
| 30 | REPORTER_ASSERT(r, ptr); |
| 31 | REPORTER_ASSERT(r, lazy.get() == ptr); |
| 32 | |
| 33 | // Advanced usage: calls a functor. |
| 34 | SkLazyPtr<int> lazyFunctor; |
| 35 | int* six = lazyFunctor.get(CreateIntFromFloat(6.4f)); |
| 36 | REPORTER_ASSERT(r, six); |
| 37 | REPORTER_ASSERT(r, 6 == *six); |
| 38 | |
| 39 | // Just makes sure this is safe. |
| 40 | SkLazyPtr<double> neverRead; |
| 41 | |
| 42 | // SkLazyPtr supports custom destroy methods. |
| 43 | { |
| 44 | SkLazyPtr<int, custom_destroy> customDestroy; |
| 45 | ptr = customDestroy.get(); |
| 46 | // custom_destroy called here. |
| 47 | } |
| 48 | REPORTER_ASSERT(r, ptr); |
| 49 | REPORTER_ASSERT(r, 99 == *ptr); |
| 50 | // Since custom_destroy didn't actually delete ptr, we do now. |
| 51 | SkDELETE(ptr); |
| 52 | } |
| 53 | |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 54 | DEF_TEST(LazyPtr_Threaded, r) { |
| 55 | static const int kRacers = 321; |
| 56 | |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 57 | // Race to intialize the pointer by calling .get(). |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 58 | SkLazyPtr<int> lazy; |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 59 | int* seen[kRacers]; |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 60 | |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 61 | sk_parallel_for(kRacers, [&](int i) { |
| 62 | seen[i] = lazy.get(); |
| 63 | }); |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 64 | |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 65 | // lazy.get() should return the same pointer to all threads. |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 66 | for (int i = 1; i < kRacers; i++) { |
mtklein | 00b621c | 2015-06-17 15:26:15 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(r, seen[i] != nullptr); |
| 68 | REPORTER_ASSERT(r, seen[i] == seen[0]); |
mtklein | e9e0dea | 2014-10-21 12:20:04 -0700 | [diff] [blame] | 69 | } |
| 70 | } |