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

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

Issue 123093002: Use a spinlock in SkOnce. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: comments Created 6 years, 11 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/SkThread.h ('k') | src/ports/SkAtomics_android.h » ('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
(...skipping 11 matching lines...) Expand all
22 // SkOnce(&once, set_up_my_singleton, &singleton); 22 // SkOnce(&once, set_up_my_singleton, &singleton);
23 // SkASSERT(NULL != singleton); 23 // SkASSERT(NULL != singleton);
24 // return *singleton; 24 // return *singleton;
25 // } 25 // }
26 // 26 //
27 // OnceTest.cpp also should serve as a few other simple examples. 27 // OnceTest.cpp also should serve as a few other simple examples.
28 28
29 #include "SkThread.h" 29 #include "SkThread.h"
30 #include "SkTypes.h" 30 #include "SkTypes.h"
31 31
32 #ifdef SK_USE_POSIX_THREADS 32 #define SK_ONCE_INIT { false, { 0, SkDEBUGCODE(0) } }
33 # define SK_ONCE_INIT { false, { PTHREAD_MUTEX_INITIALIZER } }
34 #else
35 # define SK_ONCE_INIT { false, SkBaseMutex() }
36 #endif
37
38 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT 33 #define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name = SK_ONCE_INIT
39 34
40 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK _ONCE_INIT 35 struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK _ONCE_INIT
41 36
42 template <typename Func, typename Arg> 37 template <typename Func, typename Arg>
43 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg); 38 inline void SkOnce(SkOnceFlag* once, Func f, Arg arg);
44 39
45 // ---------------------- Implementation details below here. ----------------- ------------ 40 // ---------------------- Implementation details below here. ----------------- ------------
46 41
47 struct SkOnceFlag { 42 struct SkOnceFlag {
48 bool done; 43 bool done;
49 SkBaseMutex mutex; 44 SkSpinlock lock;
50 }; 45 };
51 46
52 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands. 47 // TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands.
53 48
54 #ifdef SK_BUILD_FOR_WIN 49 #ifdef SK_BUILD_FOR_WIN
55 #include <intrin.h> 50 # include <intrin.h>
56 inline static void compiler_barrier() { 51 inline static void compiler_barrier() {
57 _ReadWriteBarrier(); 52 _ReadWriteBarrier();
58 } 53 }
59 #else 54 #else
60 inline static void compiler_barrier() { 55 inline static void compiler_barrier() {
61 asm volatile("" : : : "memory"); 56 asm volatile("" : : : "memory");
62 } 57 }
63 #endif 58 #endif
64 59
65 inline static void full_barrier_on_arm() { 60 inline static void full_barrier_on_arm() {
66 #ifdef SK_CPU_ARM 61 #ifdef SK_CPU_ARM
67 #if SK_ARM_ARCH >= 7 62 # if SK_ARM_ARCH >= 7
68 asm volatile("dmb" : : : "memory"); 63 asm volatile("dmb" : : : "memory");
69 #else 64 # else
70 asm volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory"); 65 asm volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory");
71 #endif 66 # endif
72 #endif 67 #endif
73 } 68 }
74 69
75 // On every platform, we issue a compiler barrier to prevent it from reordering 70 // On every platform, we issue a compiler barrier to prevent it from reordering
76 // code. That's enough for platforms like x86 where release and acquire 71 // code. That's enough for platforms like x86 where release and acquire
77 // barriers are no-ops. On other platforms we may need to be more careful; 72 // barriers are no-ops. On other platforms we may need to be more careful;
78 // ARM, in particular, needs real code for both acquire and release. We use a 73 // ARM, in particular, needs real code for both acquire and release. We use a
79 // full barrier, which acts as both, because that the finest precision ARM 74 // full barrier, which acts as both, because that the finest precision ARM
80 // provides. 75 // provides.
81 76
82 inline static void release_barrier() { 77 inline static void release_barrier() {
83 compiler_barrier(); 78 compiler_barrier();
84 full_barrier_on_arm(); 79 full_barrier_on_arm();
85 } 80 }
86 81
87 inline static void acquire_barrier() { 82 inline static void acquire_barrier() {
88 compiler_barrier(); 83 compiler_barrier();
89 full_barrier_on_arm(); 84 full_barrier_on_arm();
90 } 85 }
91 86
92 // We've pulled a pretty standard double-checked locking implementation apart 87 // We've pulled a pretty standard double-checked locking implementation apart
93 // into its main fast path and a slow path that's called when we suspect the 88 // into its main fast path and a slow path that's called when we suspect the
94 // one-time code hasn't run yet. 89 // one-time code hasn't run yet.
95 90
96 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet. 91 // This is the guts of the code, called when we suspect the one-time code hasn't been run yet.
97 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline. 92 // This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
98 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.) 93 // (We don't mind if this is an actual function call, but odds are it'll be inli ned anyway.)
99 template <typename Func, typename Arg> 94 template <typename Func, typename Arg>
100 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) { 95 static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) {
101 const SkAutoMutexAcquire lock(once->mutex); 96 const SkAutoSpinlock lock(&once->lock);
102 if (!once->done) { 97 if (!once->done) {
103 f(arg); 98 f(arg);
104 // Also known as a store-store/load-store barrier, this makes sure that the writes 99 // Also known as a store-store/load-store barrier, this makes sure that the writes
105 // done before here---in particular, those done by calling f(arg)---are observable 100 // done before here---in particular, those done by calling f(arg)---are observable
106 // before the writes after the line, *done = true. 101 // before the writes after the line, *done = true.
107 // 102 //
108 // In version control terms this is like saying, "check in the work up 103 // In version control terms this is like saying, "check in the work up
109 // to and including f(arg), then check in *done=true as a subsequent cha nge". 104 // to and including f(arg), then check in *done=true as a subsequent cha nge".
110 // 105 //
111 // We'll use this in the fast path to make sure f(arg)'s effects are 106 // We'll use this in the fast path to make sure f(arg)'s effects are
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // 141 //
147 // The release barrier in sk_once_slow guaranteed that once->done = true 142 // The release barrier in sk_once_slow guaranteed that once->done = true
148 // happens after f(arg), so by syncing to once->done = true here we're 143 // happens after f(arg), so by syncing to once->done = true here we're
149 // forcing ourselves to also wait until the effects of f(arg) are readble. 144 // forcing ourselves to also wait until the effects of f(arg) are readble.
150 acquire_barrier(); 145 acquire_barrier();
151 } 146 }
152 147
153 #undef ANNOTATE_BENIGN_RACE 148 #undef ANNOTATE_BENIGN_RACE
154 149
155 #endif // SkOnce_DEFINED 150 #endif // SkOnce_DEFINED
OLDNEW
« no previous file with comments | « include/core/SkThread.h ('k') | src/ports/SkAtomics_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698