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

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

Issue 26491003: SK_ONCE for SkData (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: dropped assert Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « src/core/SkMatrix.cpp ('k') | src/core/SkPathRef.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 // SkOnce.h defines two macros, DEF_SK_ONCE and SK_ONCE. 11 // SkOnce.h defines SK_DECLARE_STATIC_ONCE and SkOnce(), which you can use
12 // You can use these macros together to create a threadsafe block of code that 12 // together to create a threadsafe way to call a function just once. This
13 // runs at most once, no matter how many times you call it. This is 13 // is particularly useful for lazy singleton initialization. E.g.
14 // particularly useful for lazy singleton initialization. E.g.
15 // 14 //
16 // DEF_SK_ONCE(set_up_my_singleton, SingletonType* singleton) { 15 // static void set_up_my_singleton(Singleton** singleton) {
17 // // Code in this block will run at most once.
18 // *singleton = new Singleton(...); 16 // *singleton = new Singleton(...);
19 // } 17 // }
20 // ... 18 // ...
21 // const Singleton& getSingleton() { 19 // const Singleton& GetSingleton() {
22 // static Singleton* singleton = NULL; 20 // static Singleton* singleton = NULL;
23 // // Always call SK_ONCE. It's very cheap to call after the first time. 21 // SK_DECLARE_STATIC_ONCE(once);
24 // SK_ONCE(set_up_my_singleton, singleton); 22 // SkOnce(&once, set_up_my_singleton, &singleton);
25 // SkASSERT(NULL != singleton); 23 // SkASSERT(NULL != singleton);
26 // return *singleton; 24 // return *singleton;
27 // } 25 // }
28 // 26 //
29 // OnceTest.cpp also should serve as another simple example. 27 // OnceTest.cpp also should serve as a few other simple examples.
30 28
31 #include "SkThread.h" 29 #include "SkThread.h"
32 #include "SkTypes.h" 30 #include "SkTypes.h"
33 31
32 #ifdef SK_USE_POSIX_THREADS
33 #define SK_DECLARE_STATIC_ONCE(name) \
34 static SkOnceFlag name = { false, { PTHREAD_MUTEX_INITIALIZER } }
35 #else
36 #define SK_DECLARE_STATIC_ONCE(name) \
37 static SkOnceFlag name = { false, SkBaseMutex() }
38 #endif
34 39
35 // Pass a unique name (at least in this scope) for name, and a type and name 40 struct SkOnceFlag;
36 // for arg (as if writing a function declaration).
37 // E.g.
38 // DEF_SK_ONCE(my_onetime_setup, int* foo) {
39 // *foo += 5;
40 // }
41 #define DEF_SK_ONCE(name, arg) \
42 static bool sk_once_##name##_done = false; \
43 SK_DECLARE_STATIC_MUTEX(sk_once_##name##_mutex); \
44 static void sk_once_##name##_function(arg)
45 41
46 // Call this anywhere you need to guarantee that the corresponding DEF_SK_ONCE 42 template <typename Arg>
47 // block of code has run. name should match the DEF_SK_ONCE, and here you pass 43 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg);
48 // the actual value of the argument.
49 // E.g
50 // int foo = 0;
51 // SK_ONCE(my_onetime_setup, &foo);
52 // SkASSERT(5 == foo);
53 #define SK_ONCE(name, arg) \
54 sk_once(&sk_once_##name##_done, &sk_once_##name##_mutex, sk_once_##name##_fu nction, arg)
55
56 44
57 // ---------------------- Implementation details below here. ----------------- ------------ 45 // ---------------------- Implementation details below here. ----------------- ------------
58 46
47 struct SkOnceFlag {
48 bool done;
49 SkBaseMutex mutex;
50 };
59 51
60 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. 52 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands.
61 53
62 #ifdef SK_BUILD_FOR_WIN 54 #ifdef SK_BUILD_FOR_WIN
63 #include <intrin.h> 55 #include <intrin.h>
64 inline static void compiler_barrier() { 56 inline static void compiler_barrier() {
65 _ReadWriteBarrier(); 57 _ReadWriteBarrier();
66 } 58 }
67 #else 59 #else
68 inline static void compiler_barrier() { 60 inline static void compiler_barrier() {
(...skipping 22 matching lines...) Expand all
91 inline static void acquire_barrier() { 83 inline static void acquire_barrier() {
92 compiler_barrier(); 84 compiler_barrier();
93 full_barrier_on_arm(); 85 full_barrier_on_arm();
94 } 86 }
95 87
96 // We've pulled a pretty standard double-checked locking implementation apart 88 // We've pulled a pretty standard double-checked locking implementation apart
97 // into its main fast path and a slow path that's called when we suspect the 89 // into its main fast path and a slow path that's called when we suspect the
98 // one-time code hasn't run yet. 90 // one-time code hasn't run yet.
99 91
100 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 92 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
101 // This should be rarely called, so we separate it from sk_once and don't mark i t as inline. 93 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
102 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) 94 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.)
103 template <typename Arg> 95 template <typename Arg>
104 static void sk_once_slow(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Arg arg) { 96 static void sk_once_slow(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
105 const SkAutoMutexAcquire lock(*mutex); 97 const SkAutoMutexAcquire lock(once->mutex);
106 if (!*done) { 98 if (!once->done) {
107 once(arg); 99 f(arg);
108 // Also known as a store-store/load-store barrier, this makes sure that the writes 100 // Also known as a store-store/load-store barrier, this makes sure that the writes
109 // done before here---in particular, those done by calling once(arg)---a re observable 101 // done before here---in particular, those done by calling once(arg)---a re observable
110 // before the writes after the line, *done = true. 102 // before the writes after the line, *done = true.
111 // 103 //
112 // In version control terms this is like saying, "check in the work up 104 // In version control terms this is like saying, "check in the work up
113 // to and including once(arg), then check in *done=true as a subsequent change". 105 // to and including once(arg), then check in *done=true as a subsequent change".
114 // 106 //
115 // We'll use this in the fast path to make sure once(arg)'s effects are 107 // We'll use this in the fast path to make sure once(arg)'s effects are
116 // observable whenever we observe *done == true. 108 // observable whenever we observe *done == true.
117 release_barrier(); 109 release_barrier();
118 *done = true; 110 once->done = true;
119 } 111 }
120 } 112 }
121 113
122 // We nabbed this code from the dynamic_annotations library, and in their honor 114 // We nabbed this code from the dynamic_annotations library, and in their honor
123 // we check the same define. If you find yourself wanting more than just 115 // we check the same define. If you find yourself wanting more than just
124 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency 116 // ANNOTATE_BENIGN_RACE, it might make sense to pull that in as a dependency
125 // rather than continue to reproduce it here. 117 // rather than continue to reproduce it here.
126 118
127 #if DYNAMIC_ANNOTATIONS_ENABLED 119 #if DYNAMIC_ANNOTATIONS_ENABLED
128 // TSAN provides this hook to supress a known-safe apparent race. 120 // TSAN provides this hook to supress a known-safe apparent race.
129 extern "C" { 121 extern "C" {
130 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc); 122 void AnnotateBenignRace(const char* file, int line, const volatile void* mem, co nst char* desc);
131 } 123 }
132 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc) 124 #define ANNOTATE_BENIGN_RACE(mem, desc) AnnotateBenignRace(__FILE__, __LINE__, m em, desc)
133 #else 125 #else
134 #define ANNOTATE_BENIGN_RACE(mem, desc) 126 #define ANNOTATE_BENIGN_RACE(mem, desc)
135 #endif 127 #endif
136 128
137 // This is our fast path, called all the time. We do really want it to be inlin ed. 129 // This is our fast path, called all the time. We do really want it to be inlin ed.
138 template <typename Arg> 130 template <typename Arg>
139 inline static void sk_once(bool* done, SkBaseMutex* mutex, void (*once)(Arg), Ar g arg) { 131 inline void SkOnce(SkOnceFlag* once, void (*f)(Arg), Arg arg) {
140 ANNOTATE_BENIGN_RACE(done, "Don't worry TSAN, we're sure this is safe."); 132 ANNOTATE_BENIGN_RACE(once->done, "Don't worry TSAN, we're sure this is safe. ");
141 if (!*done) { 133 if (!once->done) {
142 sk_once_slow(done, mutex, once, arg); 134 sk_once_slow(once, f, arg);
143 } 135 }
144 // Also known as a load-load/load-store barrier, this acquire barrier makes 136 // Also known as a load-load/load-store barrier, this acquire barrier makes
145 // sure that anything we read from memory---in particular, memory written by 137 // sure that anything we read from memory---in particular, memory written by
146 // calling once(arg)---is at least as current as the value we read from done . 138 // calling f(arg)---is at least as current as the value we read from once->d one.
147 // 139 //
148 // In version control terms, this is a lot like saying "sync up to the 140 // In version control terms, this is a lot like saying "sync up to the
149 // commit where we wrote *done = true". 141 // commit where we wrote once->done = true".
150 // 142 //
151 // The release barrier in sk_once_slow guaranteed that *done = true 143 // The release barrier in sk_once_slow guaranteed that once->done = true
152 // happens after once(arg), so by syncing to *done = true here we're 144 // happens after f(arg), so by syncing to once->done = true here we're
153 // forcing ourselves to also wait until the effects of once(arg) are readble . 145 // forcing ourselves to also wait until the effects of f(arg) are readble.
154 acquire_barrier(); 146 acquire_barrier();
155 } 147 }
156 148
157 #undef ANNOTATE_BENIGN_RACE 149 #undef ANNOTATE_BENIGN_RACE
158 150
159
160 #endif // SkOnce_DEFINED 151 #endif // SkOnce_DEFINED
OLDNEW
« no previous file with comments | « src/core/SkMatrix.cpp ('k') | src/core/SkPathRef.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698