aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2016-05-05 01:36:43 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-05 01:36:43 -0700
commit959a2937d55279c6d020f2511007bb39ed322ace (patch)
treebe843fd0a48d47d9ed0d2cc7ef61a4821208c33a /include
parent82da9a8aa0ce648f36882830765b42e0ada6c0fa (diff)
Revert of SkOncePtr -> SkOnce (patchset #1 id:1 of https://codereview.chromium.org/1949313003/ )
Reason for revert: ok, I have no idea what's going on Original issue's description: > Reland of SkOncePtr -> SkOnce (patchset #1 id:1 of https://codereview.chromium.org/1945293004/ ) > > Reason for revert: > previous revert unneeded (I think) > > Original issue's description: > > Revert of SkOncePtr -> SkOnce (patchset #4 id:60001 of https://codereview.chromium.org/1953533002/ ) > > > > Reason for revert: > > broken the Mac and Linux builders, e.g.: > > > > https://build.chromium.org/p/chromium/builders/Mac/builds/15151 > > https://build.chromium.org/p/chromium/builders/Linux%20x64/builds/19052 > > > > Original issue's description: > > > SkOncePtr -> SkOnce > > > > > > It's always nice to kill off a synchronization primitive. > > > And while less terse, I think this new code reads more clearly. > > > > > > ... and, SkOncePtr's tests were the only thing now using sk_num_cores() > > > outside of SkTaskGroup, so I've hidden it as static inside SkTaskGroup.cpp. > > > > > > BUG=skia: > > > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1953533002 > > > CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot > > > > > > Committed: https://skia.googlesource.com/skia/+/9bd3fc7fa9bb7cad050bd619aa93d4c48ebb5c02 > > > > TBR=herb@google.com,mtklein@chromium.org > > # Skipping CQ checks because original CL landed less than 1 days ago. > > NOPRESUBMIT=true > > NOTREECHECKS=true > > NOTRY=true > > BUG=skia: > > > > Committed: https://skia.googlesource.com/skia/+/7eb33da7eede34c050b865dbb1b60c3dcea7191b > > TBR=herb@google.com,mtklein@chromium.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=skia: > > Committed: https://skia.googlesource.com/skia/+/82da9a8aa0ce648f36882830765b42e0ada6c0fa TBR=herb@google.com,mtklein@chromium.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=skia: Review-Url: https://codereview.chromium.org/1948313002
Diffstat (limited to 'include')
-rw-r--r--include/private/SkOncePtr.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/include/private/SkOncePtr.h b/include/private/SkOncePtr.h
new file mode 100644
index 0000000000..921c6a6534
--- /dev/null
+++ b/include/private/SkOncePtr.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkOncePtr_DEFINED
+#define SkOncePtr_DEFINED
+
+#include "../private/SkAtomics.h"
+#include <memory>
+
+// Use this to create a global static pointer that's intialized exactly once when you call get().
+#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name;
+
+template <typename T>
+class SkBaseOncePtr {
+public:
+ template <typename F>
+ T* get(const F& f) const {
+ uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ if (state < 2) {
+ if (state == 0) {
+ // It looks like no one has tried to create our pointer yet.
+ // We try to claim that task by atomically swapping our state from '0' to '1'.
+ // See SkOnce.h for why we use an acquire memory order here rather than relaxed.
+ if (sk_atomic_compare_exchange(
+ &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_memory_order_acquire)) {
+ // We've claimed it. Create our pointer and store it into fState.
+ state = (uintptr_t)f();
+ SkASSERT(state > 1);
+ sk_atomic_store(&fState, state, sk_memory_order_release);
+ } else {
+ // Someone else claimed it.
+ // We fall through to the spin loop just below to wait for them to finish.
+ }
+ }
+
+ while (state == 1) {
+ // State '1' is our busy-but-not-done state.
+ // Some other thread has claimed the job of creating our pointer.
+ // We just need to wait for it to finish.
+ state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ }
+
+ // We shouldn't be able to get here without having created our pointer.
+ SkASSERT(state > 1);
+ }
+ return (T*)state;
+ }
+
+ operator T*() const {
+ auto state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ return state < 2 ? nullptr : (T*)state;
+ // TODO: If state == 1 spin until it's not?
+ }
+
+ // fState == 0 --> we have not created our ptr yet
+ // fState == 1 --> someone is in the middle of creating our ptr
+ // else --> (T*)fState is our ptr
+ mutable uintptr_t fState;
+};
+
+#endif//SkOncePtr_DEFINED