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

Side by Side Diff: include/private/SkOncePtr.h

Issue 1904483003: SkOnce: 2 bytes -> 1 byte (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: basic uint8_t type Created 4 years, 8 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/private/SkOnce.h ('k') | no next file » | 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 2015 Google Inc. 2 * Copyright 2015 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 SkOncePtr_DEFINED 8 #ifndef SkOncePtr_DEFINED
9 #define SkOncePtr_DEFINED 9 #define SkOncePtr_DEFINED
10 10
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 template <typename T> 59 template <typename T>
60 class SkBaseOncePtr { 60 class SkBaseOncePtr {
61 public: 61 public:
62 template <typename F> 62 template <typename F>
63 T* get(const F& f) const { 63 T* get(const F& f) const {
64 uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire); 64 uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
65 if (state < 2) { 65 if (state < 2) {
66 if (state == 0) { 66 if (state == 0) {
67 // It looks like no one has tried to create our pointer yet. 67 // It looks like no one has tried to create our pointer yet.
68 // We try to claim that task by atomically swapping our state fr om '0' to '1'. 68 // We try to claim that task by atomically swapping our state fr om '0' to '1'.
69 // See SkOnce.h for why we use an acquire memory order here rath er than relaxed.
69 if (sk_atomic_compare_exchange( 70 if (sk_atomic_compare_exchange(
70 &fState, &state, (uintptr_t)1, sk_memory_order_relaxed, sk_m emory_order_relaxed)) { 71 &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_m emory_order_acquire)) {
71 // We've claimed it. Create our pointer and store it into f State. 72 // We've claimed it. Create our pointer and store it into f State.
72 state = (uintptr_t)f(); 73 state = (uintptr_t)f();
73 SkASSERT(state > 1); 74 SkASSERT(state > 1);
74 sk_atomic_store(&fState, state, sk_memory_order_release); 75 sk_atomic_store(&fState, state, sk_memory_order_release);
75 } else { 76 } else {
76 // Someone else claimed it. 77 // Someone else claimed it.
77 // We fall through to the spin loop just below to wait for t hem to finish. 78 // We fall through to the spin loop just below to wait for t hem to finish.
78 } 79 }
79 } 80 }
80 81
(...skipping 16 matching lines...) Expand all
97 // TODO: If state == 1 spin until it's not? 98 // TODO: If state == 1 spin until it's not?
98 } 99 }
99 100
100 // fState == 0 --> we have not created our ptr yet 101 // fState == 0 --> we have not created our ptr yet
101 // fState == 1 --> someone is in the middle of creating our ptr 102 // fState == 1 --> someone is in the middle of creating our ptr
102 // else --> (T*)fState is our ptr 103 // else --> (T*)fState is our ptr
103 mutable uintptr_t fState; 104 mutable uintptr_t fState;
104 }; 105 };
105 106
106 #endif//SkOncePtr_DEFINED 107 #endif//SkOncePtr_DEFINED
OLDNEW
« no previous file with comments | « include/private/SkOnce.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698