bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +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 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 8 | #include "SkRefCnt.h" |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 9 | #include "SkTRefArray.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 10 | #include "SkThreadUtils.h" |
| 11 | #include "SkTypes.h" |
| 12 | #include "SkWeakRefCnt.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 14 | |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 15 | class InstCounterClass { |
| 16 | public: |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 17 | InstCounterClass() { fCount = gInstCounter++; } |
| 18 | InstCounterClass(const InstCounterClass& src) { |
| 19 | fCount = src.fCount; |
| 20 | gInstCounter += 1; |
| 21 | } |
| 22 | virtual ~InstCounterClass() { gInstCounter -= 1; } |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 23 | |
| 24 | static int gInstCounter; |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 25 | int fCount; |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | int InstCounterClass::gInstCounter; |
| 29 | |
| 30 | static void test_refarray(skiatest::Reporter* reporter) { |
| 31 | REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); |
| 32 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 33 | const int N = 10; |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 34 | SkTRefArray<InstCounterClass>* array = SkTRefArray<InstCounterClass>::Create(N); |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 35 | |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, N == array->count()); |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 38 | |
| 39 | REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 40 | array->unref(); |
| 41 | REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 43 | // Now test the copy factory |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 44 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 45 | int i; |
| 46 | InstCounterClass* src = new InstCounterClass[N]; |
| 47 | REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
| 48 | for (i = 0; i < N; ++i) { |
| 49 | REPORTER_ASSERT(reporter, i == src[i].fCount); |
| 50 | } |
| 51 | |
| 52 | array = SkTRefArray<InstCounterClass>::Create(src, N); |
| 53 | REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); |
| 54 | REPORTER_ASSERT(reporter, N == array->count()); |
| 55 | |
| 56 | REPORTER_ASSERT(reporter, 2*N == InstCounterClass::gInstCounter); |
| 57 | for (i = 0; i < N; ++i) { |
| 58 | REPORTER_ASSERT(reporter, i == (*array)[i].fCount); |
| 59 | } |
| 60 | |
| 61 | delete[] src; |
| 62 | REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
| 63 | |
| 64 | for (i = 0; i < N; ++i) { |
| 65 | REPORTER_ASSERT(reporter, i == (*array)[i].fCount); |
| 66 | } |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 67 | array->unref(); |
| 68 | REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); |
| 69 | } |
| 70 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 71 | static void bounce_ref(void* data) { |
| 72 | SkRefCnt* ref = static_cast<SkRefCnt*>(data); |
| 73 | for (int i = 0; i < 100000; ++i) { |
| 74 | ref->ref(); |
| 75 | ref->unref(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void test_refCnt(skiatest::Reporter* reporter) { |
| 80 | SkRefCnt* ref = new SkRefCnt(); |
| 81 | |
| 82 | SkThread thing1(bounce_ref, ref); |
| 83 | SkThread thing2(bounce_ref, ref); |
| 84 | |
| 85 | thing1.setProcessorAffinity(0); |
| 86 | thing2.setProcessorAffinity(23); |
| 87 | |
| 88 | SkASSERT(thing1.start()); |
| 89 | SkASSERT(thing2.start()); |
| 90 | |
| 91 | thing1.join(); |
| 92 | thing2.join(); |
| 93 | |
| 94 | REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
| 95 | ref->unref(); |
| 96 | } |
| 97 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 98 | static void bounce_weak_ref(void* data) { |
| 99 | SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); |
| 100 | for (int i = 0; i < 100000; ++i) { |
| 101 | if (ref->try_ref()) { |
| 102 | ref->unref(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void bounce_weak_weak_ref(void* data) { |
| 108 | SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); |
| 109 | for (int i = 0; i < 100000; ++i) { |
| 110 | ref->weak_ref(); |
| 111 | ref->weak_unref(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void test_weakRefCnt(skiatest::Reporter* reporter) { |
| 116 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
| 117 | |
| 118 | SkThread thing1(bounce_ref, ref); |
| 119 | SkThread thing2(bounce_ref, ref); |
| 120 | SkThread thing3(bounce_weak_ref, ref); |
| 121 | SkThread thing4(bounce_weak_weak_ref, ref); |
| 122 | |
| 123 | thing1.setProcessorAffinity(0); |
| 124 | thing2.setProcessorAffinity(23); |
| 125 | thing3.setProcessorAffinity(2); |
| 126 | thing4.setProcessorAffinity(17); |
| 127 | |
| 128 | SkASSERT(thing1.start()); |
| 129 | SkASSERT(thing2.start()); |
| 130 | SkASSERT(thing3.start()); |
| 131 | SkASSERT(thing4.start()); |
| 132 | |
| 133 | thing1.join(); |
| 134 | thing2.join(); |
| 135 | thing3.join(); |
| 136 | thing4.join(); |
| 137 | |
| 138 | REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
| 139 | REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
| 140 | ref->unref(); |
| 141 | } |
| 142 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 143 | DEF_TEST(RefCnt, reporter) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 144 | test_refCnt(reporter); |
| 145 | test_weakRefCnt(reporter); |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 146 | test_refarray(reporter); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 147 | } |