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