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

Side by Side Diff: src/core/SkLazyFnPtr.h

Issue 1317263007: Port SkLazyFnPtr users to SkOncePtr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 | src/ports/SkFontHost_mac.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 2014 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 SkLazyFnPtr_DEFINED
9 #define SkLazyFnPtr_DEFINED
10
11 /** Declare a lazily-chosen static function pointer of type F.
12 *
13 * Example usage:
14 *
15 * typedef int (*FooImpl)(int, int);
16 *
17 * static FooImpl choose_foo() { return ... };
18 *
19 * int Foo(int a, int b) {
20 * SK_DECLARE_STATIC_LAZY_FN_PTR(FooImpl, foo, choose_foo);
21 * return foo.get()(a, b);
22 * }
23 *
24 * You can think of SK_DECLARE_STATIC_LAZY_FN_PTR as a cheaper specialization o f SkOnce.
25 * There is no mutex, and in the fast path, no memory barriers are issued.
26 *
27 * This must be used in a global or function scope, not as a class member.
28 */
29 #define SK_DECLARE_STATIC_LAZY_FN_PTR(F, name, Choose) static Private::SkLazyFnP tr<F, Choose> name
30
31
32 // Everything below here is private implementation details. Don't touch, don't even look.
33
34 #include "SkAtomics.h"
35
36 namespace Private {
37
38 // This has no constructor and must be zero-initialized (the macro above does th is).
39 template <typename F, F (*Choose)()>
40 class SkLazyFnPtr {
41 public:
42 F get() {
43 // First, try reading to see if it's already set.
44 F fn = (F)sk_atomic_load(&fPtr, sk_memory_order_relaxed);
45 if (fn != nullptr) {
46 return fn;
47 }
48
49 // We think it's not already set.
50 fn = Choose();
51
52 // No particular memory barriers needed; we're not guarding anything but the pointer itself.
53 F prev = (F)sk_atomic_cas(&fPtr, nullptr, (void*)fn);
54
55 // If prev != nullptr, someone snuck in and set fPtr concurrently.
56 // If prev == nullptr, we did write fn to fPtr.
57 return prev != nullptr ? prev : fn;
58 }
59
60 private:
61 void* fPtr;
62 };
63
64 } // namespace Private
65
66 #endif//SkLazyFnPtr_DEFINED
OLDNEW
« no previous file with comments | « no previous file | src/ports/SkFontHost_mac.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698