void onDraw(const int loops, SkCanvas*) override { for (int i = 0; i < loops; ++i) { SkWeakRefCnt* ref = new SkWeakRefCnt(); for (int j = 0; j < M; ++j) { ref->ref(); ref->unref(); } ref->unref(); } }
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(); } } }
virtual void onDraw(const int loops, SkCanvas*) { for (int i = 0; i < loops; ++i) { SkWeakRefCnt ref; for (int j = 0; j < M; ++j) { ref.ref(); ref.unref(); } } }
static void test_weakRefCnt(skiatest::Reporter* reporter) { SkWeakRefCnt* ref = new SkWeakRefCnt(); std::thread thing1(bounce_ref, ref); std::thread thing2(bounce_ref, ref); std::thread thing3(bounce_weak_ref, ref); std::thread thing4(bounce_weak_weak_ref, ref); thing1.join(); thing2.join(); thing3.join(); thing4.join(); REPORTER_ASSERT(reporter, ref->unique()); SkDEBUGCODE(REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1)); ref->unref(); }