blob: 4d4ae3f54a44c10c2ab57e2ae72fb38920e3b41d [file] [log] [blame]
bungeman@google.com55487522012-05-14 14:09:24 +00001/*
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.coma02bc152012-05-16 18:21:56 +000013#include "SkWeakRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000014
15///////////////////////////////////////////////////////////////////////////////
16
17static 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
25static 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.coma02bc152012-05-16 18:21:56 +000044static 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
53static 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
61static 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
89static void test_refCntTests(skiatest::Reporter* reporter) {
90 test_refCnt(reporter);
91 test_weakRefCnt(reporter);
92}
93
bungeman@google.com55487522012-05-14 14:09:24 +000094#include "TestClassDef.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000095DEFINE_TESTCLASS("RefCnt", RefCntTestClass, test_refCntTests)