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

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

Issue 1322933005: Port uses of SkLazyPtr to SkOncePtr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: name Created 5 years, 3 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/SkTypeface.h ('k') | src/core/SkBigPicture.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 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
11 #include "SkAtomics.h" 11 #include "SkAtomics.h"
12 #include "SkUniquePtr.h"
12 13
13 template <typename T> class SkStaticOnce; 14 template <typename T> class SkBaseOncePtr;
14 15
15 // Use this to create a global static pointer that's intialized exactly once whe n you call get(). 16 // Use this to create a global static pointer that's intialized exactly once whe n you call get().
16 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkStaticOnce< type> name 17 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr <type> name
17 18
18 // Use this for a local or member pointer that's initialized exactly once when y ou call get(). 19 // Use this for a local or member pointer that's initialized exactly once when y ou call get().
19 template <typename T> 20 template <typename T, typename Delete = skstd::default_delete<T>>
20 class SkOncePtr : SkNoncopyable { 21 class SkOncePtr : SkNoncopyable {
21 public: 22 public:
22 SkOncePtr() { sk_bzero(this, sizeof(*this)); } 23 SkOncePtr() { sk_bzero(this, sizeof(*this)); }
23 24 ~SkOncePtr() {
24 // SkOncePtr does not have a destructor and does not clean up the pointer. But you may, e.g. 25 if (T* ptr = (T*)*this) {
25 // delete (T*)fOncePtr; 26 Delete()(ptr);
26 // SkSafeUnref((T*)fOncePtr); 27 }
27 // etc. 28 }
28 29
29 template <typename F> 30 template <typename F>
30 T* get(const F& f) const { 31 T* get(const F& f) const {
31 return fOnce.get(f); 32 return fOnce.get(f);
32 } 33 }
33 34
34 operator T*() const { 35 operator T*() const {
35 return (T*)fOnce; 36 return (T*)fOnce;
36 } 37 }
37 38
38 private: 39 private:
39 SkStaticOnce<T> fOnce; 40 SkBaseOncePtr<T> fOnce;
40 }; 41 };
41 42
42 /* TODO(mtklein): in next CL 43 /* TODO(mtklein): in next CL
43 typedef SkStaticOnce<void> SkOnceFlag; 44 typedef SkBaseOncePtr<void> SkOnceFlag;
44 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name 45 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
45 46
46 template <typename F> 47 template <typename F>
47 inline void SkOnce(SkOnceFlag* once, const F& f) { 48 inline void SkOnce(SkOnceFlag* once, const F& f) {
48 once->get([&]{ f(); return (void*)2; }); 49 once->get([&]{ f(); return (void*)2; });
49 } 50 }
50 */ 51 */
51 52
52 // Implementation details below here! No peeking! 53 // Implementation details below here! No peeking!
53 54
54 template <typename T> 55 template <typename T>
55 class SkStaticOnce { 56 class SkBaseOncePtr {
56 public: 57 public:
57 template <typename F> 58 template <typename F>
58 T* get(const F& f) const { 59 T* get(const F& f) const {
59 uintptr_t state = fState.load(sk_memory_order_acquire); 60 uintptr_t state = fState.load(sk_memory_order_acquire);
60 if (state < 2) { 61 if (state < 2) {
61 if (state == 0) { 62 if (state == 0) {
62 // It looks like no one has tried to create our pointer yet. 63 // It looks like no one has tried to create our pointer yet.
63 // We try to claim that task by atomically swapping our state fr om '0' to '1'. 64 // We try to claim that task by atomically swapping our state fr om '0' to '1'.
64 if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed, 65 if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed,
65 sk_memory_order_relaxed)) { 66 sk_memory_order_relaxed)) {
(...skipping 27 matching lines...) Expand all
93 } 94 }
94 95
95 private: 96 private:
96 // fState == 0 --> we have not created our ptr yet 97 // fState == 0 --> we have not created our ptr yet
97 // fState == 1 --> someone is in the middle of creating our ptr 98 // fState == 1 --> someone is in the middle of creating our ptr
98 // else --> (T*)fState is our ptr 99 // else --> (T*)fState is our ptr
99 mutable SkAtomic<uintptr_t> fState; 100 mutable SkAtomic<uintptr_t> fState;
100 }; 101 };
101 102
102 #endif//SkOncePtr_DEFINED 103 #endif//SkOncePtr_DEFINED
OLDNEW
« no previous file with comments | « include/core/SkTypeface.h ('k') | src/core/SkBigPicture.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698