2015-06-17 22:26:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 20:46:11 +00:00
|
|
|
#include "Test.h"
|
|
|
|
#include "SkLazyPtr.h"
|
2014-10-29 19:36:45 +00:00
|
|
|
#include "SkRunnable.h"
|
2014-10-20 20:46:11 +00:00
|
|
|
#include "SkTaskGroup.h"
|
|
|
|
|
2014-10-21 19:20:04 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct CreateIntFromFloat {
|
|
|
|
CreateIntFromFloat(float val) : fVal(val) {}
|
|
|
|
int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); }
|
|
|
|
float fVal;
|
|
|
|
};
|
|
|
|
|
|
|
|
// As a template argument this must have external linkage.
|
|
|
|
void custom_destroy(int* ptr) { *ptr = 99; }
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-10-20 20:46:11 +00:00
|
|
|
DEF_TEST(LazyPtr, r) {
|
2014-10-21 19:20:04 +00:00
|
|
|
// Basic usage: calls SkNEW(int).
|
2014-10-20 20:46:11 +00:00
|
|
|
SkLazyPtr<int> lazy;
|
|
|
|
int* ptr = lazy.get();
|
|
|
|
REPORTER_ASSERT(r, ptr);
|
|
|
|
REPORTER_ASSERT(r, lazy.get() == ptr);
|
|
|
|
|
2014-10-21 19:20:04 +00:00
|
|
|
// Advanced usage: calls a functor.
|
|
|
|
SkLazyPtr<int> lazyFunctor;
|
|
|
|
int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
|
|
|
|
REPORTER_ASSERT(r, six);
|
|
|
|
REPORTER_ASSERT(r, 6 == *six);
|
|
|
|
|
|
|
|
// Just makes sure this is safe.
|
2014-10-20 20:46:11 +00:00
|
|
|
SkLazyPtr<double> neverRead;
|
2014-10-21 19:20:04 +00:00
|
|
|
|
|
|
|
// SkLazyPtr supports custom destroy methods.
|
|
|
|
{
|
|
|
|
SkLazyPtr<int, custom_destroy> customDestroy;
|
|
|
|
ptr = customDestroy.get();
|
|
|
|
// custom_destroy called here.
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, ptr);
|
|
|
|
REPORTER_ASSERT(r, 99 == *ptr);
|
|
|
|
// Since custom_destroy didn't actually delete ptr, we do now.
|
|
|
|
SkDELETE(ptr);
|
2014-10-20 20:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(LazyPtr_Threaded, r) {
|
|
|
|
static const int kRacers = 321;
|
|
|
|
|
2015-06-17 22:26:15 +00:00
|
|
|
// Race to intialize the pointer by calling .get().
|
2014-10-20 20:46:11 +00:00
|
|
|
SkLazyPtr<int> lazy;
|
2015-06-17 22:26:15 +00:00
|
|
|
int* seen[kRacers];
|
2014-10-20 20:46:11 +00:00
|
|
|
|
2015-06-17 22:26:15 +00:00
|
|
|
sk_parallel_for(kRacers, [&](int i) {
|
|
|
|
seen[i] = lazy.get();
|
|
|
|
});
|
2014-10-20 20:46:11 +00:00
|
|
|
|
2015-06-17 22:26:15 +00:00
|
|
|
// lazy.get() should return the same pointer to all threads.
|
2014-10-20 20:46:11 +00:00
|
|
|
for (int i = 1; i < kRacers; i++) {
|
2015-06-17 22:26:15 +00:00
|
|
|
REPORTER_ASSERT(r, seen[i] != nullptr);
|
|
|
|
REPORTER_ASSERT(r, seen[i] == seen[0]);
|
2014-10-20 20:46:11 +00:00
|
|
|
}
|
|
|
|
}
|