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

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

Issue 1898413004: Revert of SkOnce: 2 bytes -> 1 byte (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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.
70 if (sk_atomic_compare_exchange( 69 if (sk_atomic_compare_exchange(
71 &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_m emory_order_acquire)) { 70 &fState, &state, (uintptr_t)1, sk_memory_order_relaxed, sk_m emory_order_relaxed)) {
72 // We've claimed it. Create our pointer and store it into f State. 71 // We've claimed it. Create our pointer and store it into f State.
73 state = (uintptr_t)f(); 72 state = (uintptr_t)f();
74 SkASSERT(state > 1); 73 SkASSERT(state > 1);
75 sk_atomic_store(&fState, state, sk_memory_order_release); 74 sk_atomic_store(&fState, state, sk_memory_order_release);
76 } else { 75 } else {
77 // Someone else claimed it. 76 // Someone else claimed it.
78 // We fall through to the spin loop just below to wait for t hem to finish. 77 // We fall through to the spin loop just below to wait for t hem to finish.
79 } 78 }
80 } 79 }
81 80
(...skipping 16 matching lines...) Expand all
98 // TODO: If state == 1 spin until it's not? 97 // TODO: If state == 1 spin until it's not?
99 } 98 }
100 99
101 // fState == 0 --> we have not created our ptr yet 100 // fState == 0 --> we have not created our ptr yet
102 // fState == 1 --> someone is in the middle of creating our ptr 101 // fState == 1 --> someone is in the middle of creating our ptr
103 // else --> (T*)fState is our ptr 102 // else --> (T*)fState is our ptr
104 mutable uintptr_t fState; 103 mutable uintptr_t fState;
105 }; 104 };
106 105
107 #endif//SkOncePtr_DEFINED 106 #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