Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1424)

Unified Diff: src/core/SkOnce.h

Issue 104433005: SkOnce: let f be any functor, update comments (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: tests SkOnceFlag was uninitialized Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/core/SkScaledImageCache.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkOnce.h
diff --git a/src/core/SkOnce.h b/src/core/SkOnce.h
index a469e22c86246d681f4acbbab0da404223d01e48..89de1124421a69ccd9000e2eb05c6a5a192aa0fd 100644
--- a/src/core/SkOnce.h
+++ b/src/core/SkOnce.h
@@ -13,15 +13,15 @@
// is particularly useful for lazy singleton initialization. E.g.
//
// static void set_up_my_singleton(Singleton** singleton) {
-// *singleton = new Singleton(...);
+// *singleton = new Singleton(...);
// }
// ...
// const Singleton& GetSingleton() {
-// static Singleton* singleton = NULL;
-// SK_DECLARE_STATIC_ONCE(once);
-// SkOnce(&once, set_up_my_singleton, &singleton);
-// SkASSERT(NULL != singleton);
-// return *singleton;
+// static Singleton* singleton = NULL;
+// SK_DECLARE_STATIC_ONCE(once);
+// SkOnce(&once, set_up_my_singleton, &singleton);
+// SkASSERT(NULL != singleton);
+// return *singleton;
// }
//
// OnceTest.cpp also should serve as a few other simple examples.
@@ -30,17 +30,17 @@
#include "SkTypes.h"
#ifdef SK_USE_POSIX_THREADS
-#define SK_DECLARE_STATIC_ONCE(name) \
- static SkOnceFlag name = { false, { PTHREAD_MUTEX_INITIALIZER } }
+# define SK_ONCE_INIT { false, { PTHREAD_MUTEX_INITIALIZER } }
#else
-#define SK_DECLARE_STATIC_ONCE(name) \
- static SkOnceFlag name = { false, SkBaseMutex() }
+# define SK_ONCE_INIT { false, SkBaseMutex() }
#endif
-struct SkOnceFlag;
+#define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT
-template <typename Arg>
-inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg);
+struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK_ONCE_INIT
+
+template <typename Func, typename Arg>
+inline void SkOnce(SkOnceFlag* once, Func f, Arg arg);
// ---------------------- Implementation details below here. -----------------------------
@@ -96,19 +96,19 @@ inline static void acquire_barrier() {
// This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
// This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
// (We don't mind if this is an actual function call, but odds are it'll be inlined anyway.)
-template <typename Arg>
-static void sk_once_slow(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
+template <typename Func, typename Arg>
+static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) {
const SkAutoMutexAcquire lock(once->mutex);
if (!once->done) {
f(arg);
// Also known as a store-store/load-store barrier, this makes sure that the writes
- // done before here---in particular, those done by calling once(arg)---are observable
+ // done before here---in particular, those done by calling f(arg)---are observable
// before the writes after the line, *done = true.
//
// In version control terms this is like saying, "check in the work up
- // to and including once(arg), then check in *done=true as a subsequent change".
+ // to and including f(arg), then check in *done=true as a subsequent change".
//
- // We'll use this in the fast path to make sure once(arg)'s effects are
+ // We'll use this in the fast path to make sure f(arg)'s effects are
// observable whenever we observe *done == true.
release_barrier();
once->done = true;
@@ -131,8 +131,8 @@ void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co
#endif
// This is our fast path, called all the time. We do really want it to be inlined.
-template <typename Arg>
-inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
+template <typename Func, typename Arg>
+inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) {
ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is safe.");
if (!once->done) {
sk_once_slow(once, f, arg);
« no previous file with comments | « no previous file | src/core/SkScaledImageCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698