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

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

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

Powered by Google App Engine
This is Rietveld 408576698