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 | |
| 8 | #include "SkTypes.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | #include "SkThreadUtils.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 13 | #include "SkWeakRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 14 | |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | static void bounce_ref(void* data) { |
| 18 | SkRefCnt* ref = static_cast<SkRefCnt*>(data); |
| 19 | for (int i = 0; i < 100000; ++i) { |
| 20 | ref->ref(); |
| 21 | ref->unref(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static void test_refCnt(skiatest::Reporter* reporter) { |
| 26 | SkRefCnt* ref = new SkRefCnt(); |
| 27 | |
| 28 | SkThread thing1(bounce_ref, ref); |
| 29 | SkThread thing2(bounce_ref, ref); |
| 30 | |
| 31 | thing1.setProcessorAffinity(0); |
| 32 | thing2.setProcessorAffinity(23); |
| 33 | |
| 34 | SkASSERT(thing1.start()); |
| 35 | SkASSERT(thing2.start()); |
| 36 | |
| 37 | thing1.join(); |
| 38 | thing2.join(); |
| 39 | |
| 40 | REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
| 41 | ref->unref(); |
| 42 | } |
| 43 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 44 | static void bounce_weak_ref(void* data) { |
| 45 | SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); |
| 46 | for (int i = 0; i < 100000; ++i) { |
| 47 | if (ref->try_ref()) { |
| 48 | ref->unref(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void bounce_weak_weak_ref(void* data) { |
| 54 | SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); |
| 55 | for (int i = 0; i < 100000; ++i) { |
| 56 | ref->weak_ref(); |
| 57 | ref->weak_unref(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void test_weakRefCnt(skiatest::Reporter* reporter) { |
| 62 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
| 63 | |
| 64 | SkThread thing1(bounce_ref, ref); |
| 65 | SkThread thing2(bounce_ref, ref); |
| 66 | SkThread thing3(bounce_weak_ref, ref); |
| 67 | SkThread thing4(bounce_weak_weak_ref, ref); |
| 68 | |
| 69 | thing1.setProcessorAffinity(0); |
| 70 | thing2.setProcessorAffinity(23); |
| 71 | thing3.setProcessorAffinity(2); |
| 72 | thing4.setProcessorAffinity(17); |
| 73 | |
| 74 | SkASSERT(thing1.start()); |
| 75 | SkASSERT(thing2.start()); |
| 76 | SkASSERT(thing3.start()); |
| 77 | SkASSERT(thing4.start()); |
| 78 | |
| 79 | thing1.join(); |
| 80 | thing2.join(); |
| 81 | thing3.join(); |
| 82 | thing4.join(); |
| 83 | |
| 84 | REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
| 85 | REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
| 86 | ref->unref(); |
| 87 | } |
| 88 | |
| 89 | static void test_refCntTests(skiatest::Reporter* reporter) { |
| 90 | test_refCnt(reporter); |
| 91 | test_weakRefCnt(reporter); |
| 92 | } |
| 93 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 94 | #include "TestClassDef.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 95 | DEFINE_TESTCLASS("RefCnt", RefCntTestClass, test_refCntTests) |