blob: 6a1e0dc2fee3e3c72adb59531ff5a4e9abf36b02 [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
bungeman@google.com55487522012-05-14 14:09:24 +00008#include "SkRefCnt.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "SkThreadUtils.h"
10#include "SkTypes.h"
11#include "SkWeakRefCnt.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
bungeman@google.com55487522012-05-14 14:09:24 +000013
14static 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
22static 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
bungeman@google.com55487522012-05-14 14:09:24 +000028 SkASSERT(thing1.start());
29 SkASSERT(thing2.start());
30
31 thing1.join();
32 thing2.join();
33
mtkleinbbb61d72014-11-24 13:09:39 -080034 REPORTER_ASSERT(reporter, ref->unique());
bungeman@google.com55487522012-05-14 14:09:24 +000035 ref->unref();
36}
37
bungeman@google.coma02bc152012-05-16 18:21:56 +000038static void bounce_weak_ref(void* data) {
39 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
40 for (int i = 0; i < 100000; ++i) {
41 if (ref->try_ref()) {
42 ref->unref();
43 }
44 }
45}
46
47static void bounce_weak_weak_ref(void* data) {
48 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
49 for (int i = 0; i < 100000; ++i) {
50 ref->weak_ref();
51 ref->weak_unref();
52 }
53}
54
55static void test_weakRefCnt(skiatest::Reporter* reporter) {
56 SkWeakRefCnt* ref = new SkWeakRefCnt();
57
58 SkThread thing1(bounce_ref, ref);
59 SkThread thing2(bounce_ref, ref);
60 SkThread thing3(bounce_weak_ref, ref);
61 SkThread thing4(bounce_weak_weak_ref, ref);
62
bungeman@google.coma02bc152012-05-16 18:21:56 +000063 SkASSERT(thing1.start());
64 SkASSERT(thing2.start());
65 SkASSERT(thing3.start());
66 SkASSERT(thing4.start());
67
68 thing1.join();
69 thing2.join();
70 thing3.join();
71 thing4.join();
72
mtkleinbbb61d72014-11-24 13:09:39 -080073 REPORTER_ASSERT(reporter, ref->unique());
Mike Klein874a62a2014-07-09 09:04:07 -040074 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1);
bungeman@google.coma02bc152012-05-16 18:21:56 +000075 ref->unref();
76}
77
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000078DEF_TEST(RefCnt, reporter) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000079 test_refCnt(reporter);
80 test_weakRefCnt(reporter);
81}