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

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

Issue 123093002: Use a spinlock in SkOnce. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 | « no previous file | src/core/SkOnce.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 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
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 SkThread_DEFINED 8 #ifndef SkThread_DEFINED
9 #define SkThread_DEFINED 9 #define SkThread_DEFINED
10 10
(...skipping 15 matching lines...) Expand all
26 * This must act as a release (SL/S) memory barrier and as a compiler barrier. 26 * This must act as a release (SL/S) memory barrier and as a compiler barrier.
27 */ 27 */
28 static int32_t sk_atomic_dec(int32_t* addr); 28 static int32_t sk_atomic_dec(int32_t* addr);
29 29
30 /** Atomically adds one to the int referenced by addr iff the referenced int was not 0 30 /** Atomically adds one to the int referenced by addr iff the referenced int was not 0
31 * and returns the previous value. 31 * and returns the previous value.
32 * No additional memory barrier is required; this must act as a compiler barrie r. 32 * No additional memory barrier is required; this must act as a compiler barrie r.
33 */ 33 */
34 static int32_t sk_atomic_conditional_inc(int32_t* addr); 34 static int32_t sk_atomic_conditional_inc(int32_t* addr);
35 35
36 /** Atomic compare and set.
37 * If *addr == before, set *addr to after and return true, otherwise return fal se.
38 */
39 static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after);
40
36 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, 41 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier,
37 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. 42 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier.
38 */ 43 */
39 static void sk_membar_acquire__after_atomic_dec(); 44 static void sk_membar_acquire__after_atomic_dec();
40 45
41 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, 46 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier,
42 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. 47 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier.
43 */ 48 */
44 static void sk_membar_acquire__after_atomic_conditional_inc(); 49 static void sk_membar_acquire__after_atomic_conditional_inc();
45 50
46 #include SK_ATOMICS_PLATFORM_H 51 #include SK_ATOMICS_PLATFORM_H
47 52
53 // This is POD and must be zero-initialized.
reed1 2014/01/02 13:53:34 Is it possible to add another field, just for debu
mtklein 2014/01/02 16:28:16 Yes indeed. Done.
54 struct SkSpinlock {
55 void acquire() {
56 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) {
57 // spin
58 }
59 }
60
61 void release() {
62 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0));
63 }
64
65 int32_t thisIsPrivate;
66 };
67
68 class SkAutoSpinlock : SkNoncopyable {
69 public:
70 explicit SkAutoSpinlock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); }
71 ~SkAutoSpinlock() { fLock->release(); }
72 private:
73 SkSpinlock* fLock;
74 };
75 #define SkAutoSpinlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoSpinlock)
48 76
49 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. 77 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations.
50 78
51 class SkBaseMutex { 79 class SkBaseMutex {
52 public: 80 public:
53 void acquire(); 81 void acquire();
54 void release(); 82 void release();
55 }; 83 };
56 84
57 class SkMutex : SkBaseMutex { 85 class SkMutex : SkBaseMutex {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 fMutex = NULL; 122 fMutex = NULL;
95 } 123 }
96 } 124 }
97 125
98 private: 126 private:
99 SkBaseMutex* fMutex; 127 SkBaseMutex* fMutex;
100 }; 128 };
101 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) 129 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
102 130
103 #endif 131 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkOnce.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698