2012-05-14 14:09:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkThreadUtils.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "SkTypes.h"
|
2012-05-16 18:21:56 +00:00
|
|
|
#include "SkWeakRefCnt.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-05-14 14:09:24 +00:00
|
|
|
|
|
|
|
static void bounce_ref(void* data) {
|
|
|
|
SkRefCnt* ref = static_cast<SkRefCnt*>(data);
|
|
|
|
for (int i = 0; i < 100000; ++i) {
|
|
|
|
ref->ref();
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_refCnt(skiatest::Reporter* reporter) {
|
|
|
|
SkRefCnt* ref = new SkRefCnt();
|
|
|
|
|
|
|
|
SkThread thing1(bounce_ref, ref);
|
|
|
|
SkThread thing2(bounce_ref, ref);
|
|
|
|
|
|
|
|
SkASSERT(thing1.start());
|
|
|
|
SkASSERT(thing2.start());
|
|
|
|
|
|
|
|
thing1.join();
|
|
|
|
thing2.join();
|
|
|
|
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, ref->unique());
|
2012-05-14 14:09:24 +00:00
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:21:56 +00:00
|
|
|
static void bounce_weak_ref(void* data) {
|
|
|
|
SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
|
|
|
|
for (int i = 0; i < 100000; ++i) {
|
|
|
|
if (ref->try_ref()) {
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bounce_weak_weak_ref(void* data) {
|
|
|
|
SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
|
|
|
|
for (int i = 0; i < 100000; ++i) {
|
|
|
|
ref->weak_ref();
|
|
|
|
ref->weak_unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_weakRefCnt(skiatest::Reporter* reporter) {
|
|
|
|
SkWeakRefCnt* ref = new SkWeakRefCnt();
|
|
|
|
|
|
|
|
SkThread thing1(bounce_ref, ref);
|
|
|
|
SkThread thing2(bounce_ref, ref);
|
|
|
|
SkThread thing3(bounce_weak_ref, ref);
|
|
|
|
SkThread thing4(bounce_weak_weak_ref, ref);
|
|
|
|
|
|
|
|
SkASSERT(thing1.start());
|
|
|
|
SkASSERT(thing2.start());
|
|
|
|
SkASSERT(thing3.start());
|
|
|
|
SkASSERT(thing4.start());
|
|
|
|
|
|
|
|
thing1.join();
|
|
|
|
thing2.join();
|
|
|
|
thing3.join();
|
|
|
|
thing4.join();
|
|
|
|
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, ref->unique());
|
2014-07-09 13:04:07 +00:00
|
|
|
REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1);
|
2012-05-16 18:21:56 +00:00
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(RefCnt, reporter) {
|
2012-05-16 18:21:56 +00:00
|
|
|
test_refCnt(reporter);
|
|
|
|
test_weakRefCnt(reporter);
|
|
|
|
}
|