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

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

Issue 1301323006: SkOncePtr (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: drop destructor 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 | « no previous file | tests/OncePtrTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkOncePtr_DEFINED
9 #define SkOncePtr_DEFINED
10
11 #include "SkAtomics.h"
12
13 template <typename T> class SkStaticOnce;
14
15 // 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
18 // Use this for a local or member pointer that's initialized exactly once when y ou call get().
19 template <typename T>
20 class SkOncePtr : SkNoncopyable {
21 public:
22 SkOncePtr() { sk_bzero(this, sizeof(*this)); }
23
24 // SkOncePtr does not have a destructor and does not clean up the pointer. But you may, e.g.
25 // delete (T*)fOncePtr;
26 // SkSafeUnref((T*)fOncePtr);
27 // etc.
28
29 template <typename F>
30 T* get(const F& f) const {
31 return fOnce.get(f);
32 }
33
34 operator T*() const {
35 return (T*)fOnce;
36 }
37
38 private:
39 SkStaticOnce<T> fOnce;
40 };
41
42 /* TODO(mtklein): in next CL
43 typedef SkStaticOnce<void> SkOnceFlag;
44 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
45
46 template <typename F>
47 inline void SkOnce(SkOnceFlag* once, const F& f) {
48 once->get([&]{ f(); return (void*)2; });
49 }
50 */
51
52 // Implementation details below here! No peeking!
53
54 template <typename T>
55 class SkStaticOnce {
56 public:
57 template <typename F>
58 T* get(const F& f) const {
59 uintptr_t state = fState.load(sk_memory_order_acquire);
60 if (state < 2) {
61 if (state == 0) {
62 // 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 if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed,
65 sk_memory_order_relaxed)) {
66 // We've claimed it. Create our pointer and store it into f State.
67 state = (uintptr_t)f();
68 SkASSERT(state > 1);
69 fState.store(state, sk_memory_order_release);
70 } else {
71 // Someone else claimed it.
72 // We fall through to the spin loop just below to wait for t hem to finish.
73 }
74 }
75
76 while (state == 1) {
77 // State '1' is our busy-but-not-done state.
78 // Some other thread has claimed the job of creating our pointer .
79 // We just need to wait for it to finish.
80 state = fState.load(sk_memory_order_acquire);
81 }
82
83 // We shouldn't be able to get here without having created our point er.
84 SkASSERT(state > 1);
85 }
86 return (T*)state;
87 }
88
89 operator T*() const {
90 auto state = fState.load(sk_memory_order_acquire);
91 return state < 2 ? nullptr : (T*)state;
92 // TODO: If state == 1 spin until it's not?
93 }
94
95 private:
96 // fState == 0 --> we have not created our ptr yet
97 // fState == 1 --> someone is in the middle of creating our ptr
98 // else --> (T*)fState is our ptr
99 mutable SkAtomic<uintptr_t> fState;
100 };
101
102 #endif//SkOncePtr_DEFINED
OLDNEW
« no previous file with comments | « no previous file | tests/OncePtrTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698