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

Side by Side Diff: include/core/SkOnce.h

Issue 302083003: Clean up SkOnce: (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 6 months 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 unified diff | Download patch
« no previous file with comments | « include/core/SkInstCnt.h ('k') | src/ports/SkGlobalInitialization_chromium.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkOnce_DEFINED 8 #ifndef SkOnce_DEFINED
9 #define SkOnce_DEFINED 9 #define SkOnce_DEFINED
10 10
11 // Before trying SkOnce, see if SkLazyPtr or SkLazyFnPtr will work for you.
12 // They're smaller and faster, if slightly less versatile.
13
14
11 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use 15 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use
12 // together to create a threadsafe way to call a function just once. This 16 // together to create a threadsafe way to call a function just once. E.g.
13 // is particularly useful for lazy singleton initialization. E.g.
14 // 17 //
15 // static void set_up_my_singleton(Singleton** singleton) { 18 // static void register_my_stuff(GlobalRegistry* registry) {
16 // *singleton = new Singleton(...); 19 // registry->register(...);
17 // } 20 // }
18 // ... 21 // ...
19 // const Singleton& GetSingleton() { 22 // void EnsureRegistered() {
20 // static Singleton* singleton = NULL;
21 // SK_DECLARE_STATIC_ONCE(once); 23 // SK_DECLARE_STATIC_ONCE(once);
22 // SkOnce(&once, set_up_my_singleton, &singleton); 24 // SkOnce(&once, register_my_stuff, GetGlobalRegistry());
23 // SkASSERT(NULL != singleton);
24 // return *singleton;
25 // } 25 // }
26 // 26 //
27 // No matter how many times you call EnsureRegistered(), register_my_stuff will be called just once.
27 // OnceTest.cpp also should serve as a few other simple examples. 28 // OnceTest.cpp also should serve as a few other simple examples.
28 //
29 // You may optionally pass SkOnce a second function to be called at exit for cle anup.
30 29
31 #include "SkDynamicAnnotations.h" 30 #include "SkDynamicAnnotations.h"
32 #include "SkThread.h" 31 #include "SkThread.h"
33 #include "SkTypes.h" 32 #include "SkTypes.h"
34 33
35 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } } 34 // This must be used in a global or function scope, not as a class member.
36 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT 35 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name
37 36
38 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK _ONCE_INIT 37 class SkOnceFlag;
39 38
40 template <typename Func, typename Arg> 39 inline void SkOnce(SkOnceFlag* once, void (*f)());
41 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL); 40
41 template <typename Arg>
42 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg);
42 43
43 // If you've already got a lock and a flag to use, this variant lets you avoid a n extra SkOnceFlag. 44 // If you've already got a lock and a flag to use, this variant lets you avoid a n extra SkOnceFlag.
44 template <typename Lock, typename Func, typename Arg> 45 template <typename Lock>
45 inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)() = NU LL); 46 inline void SkOnce(bool* done, Lock* lock, void (*f)());
47
48 template <typename Lock, typename Arg>
49 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg);
46 50
47 // ---------------------- Implementation details below here. ----------------- ------------ 51 // ---------------------- Implementation details below here. ----------------- ------------
48 52
49 // This is POD and must be zero-initialized. 53 // This class has no constructor and must be zero-initialized (the macro above d oes this).
50 struct SkSpinlock { 54 class SkOnceFlag {
55 public:
56 bool* mutableDone() { return &fDone; }
57
51 void acquire() { 58 void acquire() {
52 SkASSERT(shouldBeZero == 0); 59 // To act as a mutex, this needs an acquire barrier on success.
53 // No memory barrier needed, but sk_atomic_cas gives us at least release anyway. 60 // sk_atomic_cas doesn't guarantee this ...
54 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) { 61 while (!sk_atomic_cas(&fSpinlock, 0, 1)) {
55 // spin 62 // spin
56 } 63 }
64 // ... so make sure to issue one of our own.
65 SkAssertResult(1 == sk_acquire_load(&fSpinlock));
57 } 66 }
58 67
59 void release() { 68 void release() {
60 SkASSERT(shouldBeZero == 0); 69 // To act as a mutex, this needs a release barrier. sk_atomic_cas guara ntees this.
61 // This requires a release memory barrier before storing, which sk_atomi c_cas guarantees. 70 SkAssertResult(1 == sk_atomic_cas(&fSpinlock, 1, 0));
62 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0));
63 } 71 }
64 72
65 int32_t thisIsPrivate;
66 SkDEBUGCODE(int32_t shouldBeZero;)
67 };
68
69 struct SkOnceFlag {
70 bool done;
71 SkSpinlock lock;
72 };
73
74 // Works with SkSpinlock or SkMutex.
75 template <typename Lock>
76 class SkAutoLockAcquire {
77 public:
78 explicit SkAutoLockAcquire(Lock* lock) : fLock(lock) { fLock->acquire(); }
79 ~SkAutoLockAcquire() { fLock->release(); }
80 private: 73 private:
81 Lock* fLock; 74 bool fDone;
75 int32_t fSpinlock;
82 }; 76 };
83 77
84 // We've pulled a pretty standard double-checked locking implementation apart 78 // We've pulled a pretty standard double-checked locking implementation apart
85 // into its main fast path and a slow path that's called when we suspect the 79 // into its main fast path and a slow path that's called when we suspect the
86 // one-time code hasn't run yet. 80 // one-time code hasn't run yet.
87 81
88 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 82 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
89 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline. 83 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
90 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) 84 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.)
91 template <typename Lock, typename Func, typename Arg> 85 template <typename Lock, typename Arg>
92 static void sk_once_slow(bool* done, Lock* lock, Func f, Arg arg, void (*atExit) ()) { 86 static void sk_once_slow(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
93 const SkAutoLockAcquire<Lock> locked(lock); 87 lock->acquire();
94 if (!*done) { 88 if (!*done) {
95 f(arg); 89 f(arg);
96 if (atExit != NULL) {
97 atexit(atExit);
98 }
99 // Also known as a store-store/load-store barrier, this makes sure that the writes 90 // Also known as a store-store/load-store barrier, this makes sure that the writes
100 // done before here---in particular, those done by calling f(arg)---are observable 91 // done before here---in particular, those done by calling f(arg)---are observable
101 // before the writes after the line, *done = true. 92 // before the writes after the line, *done = true.
102 // 93 //
103 // In version control terms this is like saying, "check in the work up 94 // In version control terms this is like saying, "check in the work up
104 // to and including f(arg), then check in *done=true as a subsequent cha nge". 95 // to and including f(arg), then check in *done=true as a subsequent cha nge".
105 // 96 //
106 // We'll use this in the fast path to make sure f(arg)'s effects are 97 // We'll use this in the fast path to make sure f(arg)'s effects are
107 // observable whenever we observe *done == true. 98 // observable whenever we observe *done == true.
108 sk_release_store(done, true); 99 sk_release_store(done, true);
109 } 100 }
101 lock->release();
110 } 102 }
111 103
112 // This is our fast path, called all the time. We do really want it to be inlin ed. 104 // This is our fast path, called all the time. We do really want it to be inlin ed.
113 template <typename Lock, typename Func, typename Arg> 105 template <typename Lock, typename Arg>
114 inline void SkOnce(bool* done, Lock* lock, Func f, Arg arg, void(*atExit)()) { 106 inline void SkOnce(bool* done, Lock* lock, void (*f)(Arg), Arg arg) {
115 if (!SK_ANNOTATE_UNPROTECTED_READ(*done)) { 107 if (!SK_ANNOTATE_UNPROTECTED_READ(*done)) {
116 sk_once_slow(done, lock, f, arg, atExit); 108 sk_once_slow(done, lock, f, arg);
117 } 109 }
118 // Also known as a load-load/load-store barrier, this acquire barrier makes 110 // Also known as a load-load/load-store barrier, this acquire barrier makes
119 // sure that anything we read from memory---in particular, memory written by 111 // sure that anything we read from memory---in particular, memory written by
120 // calling f(arg)---is at least as current as the value we read from done. 112 // calling f(arg)---is at least as current as the value we read from done.
121 // 113 //
122 // In version control terms, this is a lot like saying "sync up to the 114 // In version control terms, this is a lot like saying "sync up to the
123 // commit where we wrote done = true". 115 // commit where we wrote done = true".
124 // 116 //
125 // The release barrier in sk_once_slow guaranteed that done = true 117 // The release barrier in sk_once_slow guaranteed that done = true
126 // happens after f(arg), so by syncing to done = true here we're 118 // happens after f(arg), so by syncing to done = true here we're
127 // forcing ourselves to also wait until the effects of f(arg) are readble. 119 // forcing ourselves to also wait until the effects of f(arg) are readble.
128 SkAssertResult(sk_acquire_load(done)); 120 SkAssertResult(sk_acquire_load(done));
129 } 121 }
130 122
131 template <typename Func, typename Arg> 123 template <typename Arg>
132 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) { 124 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
133 return SkOnce(&once->done, &once->lock, f, arg, atExit); 125 return SkOnce(once->mutableDone(), once, f, arg);
134 } 126 }
135 127
136 #undef SK_ANNOTATE_BENIGN_RACE 128 // Calls its argument.
129 // This lets us use functions that take no arguments with SkOnce methods above.
130 // (We pass _this_ as the function and the no-arg function as its argument. Cut e eh?)
131 static void no_arg_adaptor(void (*f)()) {
bungeman-skia 2014/05/30 21:13:18 Can the name be hidden so that 'no_arg_adaptor' do
mtklein 2014/06/02 16:11:39 Done.
132 f();
133 }
134
135 inline void SkOnce(SkOnceFlag* once, void (*func)()) {
136 return SkOnce(once, no_arg_adaptor, func);
137 }
138
139 template <typename Lock>
140 inline void SkOnce(bool* done, Lock* lock, void (*func)()) {
141 return SkOnce(done, lock, no_arg_adaptor, func);
142 }
137 143
138 #endif // SkOnce_DEFINED 144 #endif // SkOnce_DEFINED
OLDNEW
« no previous file with comments | « include/core/SkInstCnt.h ('k') | src/ports/SkGlobalInitialization_chromium.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698