2013-10-09 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkOnce_DEFINED
|
|
|
|
#define SkOnce_DEFINED
|
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
// Before trying SkOnce, see if SkLazyPtr or SkLazyFnPtr will work for you.
|
|
|
|
// They're smaller and faster, if slightly less versatile.
|
|
|
|
|
|
|
|
|
2013-10-23 14:44:08 +00:00
|
|
|
// SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use
|
2014-06-02 18:26:59 +00:00
|
|
|
// together to create a threadsafe way to call a function just once. E.g.
|
2013-10-09 16:12:23 +00:00
|
|
|
//
|
2014-06-02 18:26:59 +00:00
|
|
|
// static void register_my_stuff(GlobalRegistry* registry) {
|
|
|
|
// registry->register(...);
|
2013-10-09 16:12:23 +00:00
|
|
|
// }
|
|
|
|
// ...
|
2014-06-02 18:26:59 +00:00
|
|
|
// void EnsureRegistered() {
|
2013-12-05 19:20:49 +00:00
|
|
|
// SK_DECLARE_STATIC_ONCE(once);
|
2014-06-02 18:26:59 +00:00
|
|
|
// SkOnce(&once, register_my_stuff, GetGlobalRegistry());
|
2013-10-09 16:12:23 +00:00
|
|
|
// }
|
|
|
|
//
|
2014-06-02 18:26:59 +00:00
|
|
|
// No matter how many times you call EnsureRegistered(), register_my_stuff will be called just once.
|
2013-10-23 14:44:08 +00:00
|
|
|
// OnceTest.cpp also should serve as a few other simple examples.
|
2013-10-09 16:12:23 +00:00
|
|
|
|
2015-09-28 18:24:13 +00:00
|
|
|
#include "../private/SkAtomics.h"
|
2015-09-29 18:47:45 +00:00
|
|
|
#include "../private/SkSpinlock.h"
|
2013-10-09 16:12:23 +00:00
|
|
|
|
2015-05-04 17:36:42 +00:00
|
|
|
// This must be used in a global scope, not in function scope or as a class member.
|
2015-01-13 16:40:23 +00:00
|
|
|
#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
|
2013-10-09 16:12:23 +00:00
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
class SkOnceFlag;
|
2013-12-05 19:20:49 +00:00
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
inline void SkOnce(SkOnceFlag* once, void (*f)());
|
|
|
|
|
|
|
|
template <typename Arg>
|
|
|
|
inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg);
|
2013-10-09 16:12:23 +00:00
|
|
|
|
2014-02-10 19:58:49 +00:00
|
|
|
// If you've already got a lock and a flag to use, this variant lets you avoid an extra SkOnceFlag.
|
2014-06-02 18:26:59 +00:00
|
|
|
template <typename Lock>
|
|
|
|
inline void SkOnce(bool* done, Lock* lock, void (*f)());
|
|
|
|
|
|
|
|
template <typename Lock, typename Arg>
|
|
|
|
inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg);
|
2014-02-10 19:58:49 +00:00
|
|
|
|
2013-10-09 16:12:23 +00:00
|
|
|
// ---------------------- Implementation details below here. -----------------------------
|
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
// This class has no constructor and must be zero-initialized (the macro above does this).
|
|
|
|
class SkOnceFlag {
|
|
|
|
public:
|
|
|
|
bool* mutableDone() { return &fDone; }
|
|
|
|
|
2015-03-30 15:13:33 +00:00
|
|
|
void acquire() { fSpinlock.acquire(); }
|
|
|
|
void release() { fSpinlock.release(); }
|
2014-02-10 19:58:49 +00:00
|
|
|
|
|
|
|
private:
|
2014-06-02 18:26:59 +00:00
|
|
|
bool fDone;
|
2015-03-30 15:13:33 +00:00
|
|
|
SkPODSpinlock fSpinlock;
|
2014-02-10 19:58:49 +00:00
|
|
|
};
|
|
|
|
|
2013-10-09 16:12:23 +00:00
|
|
|
// We've pulled a pretty standard double-checked locking implementation apart
|
|
|
|
// into its main fast path and a slow path that's called when we suspect the
|
|
|
|
// one-time code hasn't run yet.
|
|
|
|
|
|
|
|
// This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
|
2013-10-23 14:44:08 +00:00
|
|
|
// This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
|
2013-10-09 16:12:23 +00:00
|
|
|
// (We don't mind if this is an actual function call, but odds are it'll be inlined anyway.)
|
2014-06-02 18:26:59 +00:00
|
|
|
template <typename Lock, typename Arg>
|
|
|
|
static void sk_once_slow(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
|
|
|
|
lock->acquire();
|
2015-03-12 12:27:46 +00:00
|
|
|
if (!sk_atomic_load(done, sk_memory_order_relaxed)) {
|
2013-10-23 14:44:08 +00:00
|
|
|
f(arg);
|
2013-10-09 16:12:23 +00:00
|
|
|
// Also known as a store-store/load-store barrier, this makes sure that the writes
|
2013-12-05 19:20:49 +00:00
|
|
|
// done before here---in particular, those done by calling f(arg)---are observable
|
2013-10-09 16:12:23 +00:00
|
|
|
// before the writes after the line, *done = true.
|
|
|
|
//
|
|
|
|
// In version control terms this is like saying, "check in the work up
|
2013-12-05 19:20:49 +00:00
|
|
|
// to and including f(arg), then check in *done=true as a subsequent change".
|
2013-10-09 16:12:23 +00:00
|
|
|
//
|
2013-12-05 19:20:49 +00:00
|
|
|
// We'll use this in the fast path to make sure f(arg)'s effects are
|
2013-10-09 16:12:23 +00:00
|
|
|
// observable whenever we observe *done == true.
|
2014-05-29 18:24:54 +00:00
|
|
|
sk_release_store(done, true);
|
2013-10-09 16:12:23 +00:00
|
|
|
}
|
2014-06-02 18:26:59 +00:00
|
|
|
lock->release();
|
2013-10-09 16:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is our fast path, called all the time. We do really want it to be inlined.
|
2014-06-02 18:26:59 +00:00
|
|
|
template <typename Lock, typename Arg>
|
|
|
|
inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
|
2015-03-12 12:27:46 +00:00
|
|
|
// When *done == true:
|
|
|
|
// Also known as a load-load/load-store barrier, this acquire barrier makes
|
|
|
|
// sure that anything we read from memory---in particular, memory written by
|
|
|
|
// calling f(arg)---is at least as current as the value we read from done.
|
|
|
|
//
|
|
|
|
// In version control terms, this is a lot like saying "sync up to the
|
|
|
|
// commit where we wrote done = true".
|
2013-10-09 16:12:23 +00:00
|
|
|
//
|
2015-03-12 12:27:46 +00:00
|
|
|
// The release barrier in sk_once_slow guaranteed that done = true
|
|
|
|
// happens after f(arg), so by syncing to done = true here we're
|
|
|
|
// forcing ourselves to also wait until the effects of f(arg) are readble.
|
2013-10-09 16:12:23 +00:00
|
|
|
//
|
2015-03-12 12:27:46 +00:00
|
|
|
// When *done == false:
|
|
|
|
// We'll try to call f(arg) in sk_once_slow.
|
|
|
|
// If we get the lock, great, we call f(arg), release true into done, and drop the lock.
|
|
|
|
// If we race and don't get the lock first, we'll wait for the first guy to finish.
|
|
|
|
// Then lock acquire() will give us at least an acquire memory barrier to get the same
|
|
|
|
// effect as the acquire load in the *done == true fast case. We'll see *done is true,
|
|
|
|
// then just drop the lock and return.
|
|
|
|
if (!sk_atomic_load(done, sk_memory_order_acquire)) {
|
|
|
|
sk_once_slow(done, lock, f, arg);
|
|
|
|
}
|
2013-10-09 16:12:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
template <typename Arg>
|
|
|
|
inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
|
|
|
|
return SkOnce(once->mutableDone(), once, f, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calls its argument.
|
|
|
|
// This lets us use functions that take no arguments with SkOnce methods above.
|
|
|
|
// (We pass _this_ as the function and the no-arg function as its argument. Cute eh?)
|
|
|
|
static void sk_once_no_arg_adaptor(void (*f)()) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SkOnce(SkOnceFlag* once, void (*func)()) {
|
|
|
|
return SkOnce(once, sk_once_no_arg_adaptor, func);
|
2014-02-10 19:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
template <typename Lock>
|
|
|
|
inline void SkOnce(bool* done, Lock* lock, void (*func)()) {
|
|
|
|
return SkOnce(done, lock, sk_once_no_arg_adaptor, func);
|
|
|
|
}
|
2013-10-09 16:12:23 +00:00
|
|
|
|
|
|
|
#endif // SkOnce_DEFINED
|