1 | // Copyright 2017 The Abseil Authors. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | // |
15 | |
16 | #ifndef ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_ |
17 | #define ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_ |
18 | |
19 | #include <atomic> |
20 | #include <cassert> |
21 | #include <cstdint> |
22 | #include <utility> |
23 | |
24 | #ifdef _MSC_FULL_VER |
25 | #define ABSL_HAVE_WORKING_ATOMIC_POINTER 0 |
26 | #else |
27 | #define ABSL_HAVE_WORKING_ATOMIC_POINTER 1 |
28 | #endif |
29 | |
30 | namespace absl { |
31 | namespace base_internal { |
32 | |
33 | template <typename T> |
34 | class AtomicHook; |
35 | |
36 | // AtomicHook is a helper class, templatized on a raw function pointer type, for |
37 | // implementing Abseil customization hooks. It is a callable object that |
38 | // dispatches to the registered hook. |
39 | // |
40 | // A default constructed object performs a no-op (and returns a default |
41 | // constructed object) if no hook has been registered. |
42 | // |
43 | // Hooks can be pre-registered via constant initialization, for example, |
44 | // ABSL_CONST_INIT static AtomicHook<void(*)()> my_hook(DefaultAction); |
45 | // and then changed at runtime via a call to Store(). |
46 | // |
47 | // Reads and writes guarantee memory_order_acquire/memory_order_release |
48 | // semantics. |
49 | template <typename ReturnType, typename... Args> |
50 | class AtomicHook<ReturnType (*)(Args...)> { |
51 | public: |
52 | using FnPtr = ReturnType (*)(Args...); |
53 | |
54 | // Constructs an object that by default performs a no-op (and |
55 | // returns a default constructed object) when no hook as been registered. |
56 | constexpr AtomicHook() : AtomicHook(DummyFunction) {} |
57 | |
58 | // Constructs an object that by default dispatches to/returns the |
59 | // pre-registered default_fn when no hook has been registered at runtime. |
60 | #if ABSL_HAVE_WORKING_ATOMIC_POINTER |
61 | explicit constexpr AtomicHook(FnPtr default_fn) |
62 | : hook_(default_fn), default_fn_(default_fn) {} |
63 | #else |
64 | explicit constexpr AtomicHook(FnPtr default_fn) |
65 | : hook_(kUninitialized), default_fn_(default_fn) {} |
66 | #endif |
67 | |
68 | // Stores the provided function pointer as the value for this hook. |
69 | // |
70 | // This is intended to be called once. Multiple calls are legal only if the |
71 | // same function pointer is provided for each call. The store is implemented |
72 | // as a memory_order_release operation, and read accesses are implemented as |
73 | // memory_order_acquire. |
74 | void Store(FnPtr fn) { |
75 | bool success = DoStore(fn); |
76 | static_cast<void>(success); |
77 | assert(success); |
78 | } |
79 | |
80 | // Invokes the registered callback. If no callback has yet been registered, a |
81 | // default-constructed object of the appropriate type is returned instead. |
82 | template <typename... CallArgs> |
83 | ReturnType operator()(CallArgs&&... args) const { |
84 | return DoLoad()(std::forward<CallArgs>(args)...); |
85 | } |
86 | |
87 | // Returns the registered callback, or nullptr if none has been registered. |
88 | // Useful if client code needs to conditionalize behavior based on whether a |
89 | // callback was registered. |
90 | // |
91 | // Note that atomic_hook.Load()() and atomic_hook() have different semantics: |
92 | // operator()() will perform a no-op if no callback was registered, while |
93 | // Load()() will dereference a null function pointer. Prefer operator()() to |
94 | // Load()() unless you must conditionalize behavior on whether a hook was |
95 | // registered. |
96 | FnPtr Load() const { |
97 | FnPtr ptr = DoLoad(); |
98 | return (ptr == DummyFunction) ? nullptr : ptr; |
99 | } |
100 | |
101 | private: |
102 | static ReturnType DummyFunction(Args...) { |
103 | return ReturnType(); |
104 | } |
105 | |
106 | // Current versions of MSVC (as of September 2017) have a broken |
107 | // implementation of std::atomic<T*>: Its constructor attempts to do the |
108 | // equivalent of a reinterpret_cast in a constexpr context, which is not |
109 | // allowed. |
110 | // |
111 | // This causes an issue when building with LLVM under Windows. To avoid this, |
112 | // we use a less-efficient, intptr_t-based implementation on Windows. |
113 | #if ABSL_HAVE_WORKING_ATOMIC_POINTER |
114 | // Return the stored value, or DummyFunction if no value has been stored. |
115 | FnPtr DoLoad() const { return hook_.load(std::memory_order_acquire); } |
116 | |
117 | // Store the given value. Returns false if a different value was already |
118 | // stored to this object. |
119 | bool DoStore(FnPtr fn) { |
120 | assert(fn); |
121 | FnPtr expected = default_fn_; |
122 | const bool store_succeeded = hook_.compare_exchange_strong( |
123 | expected, fn, std::memory_order_acq_rel, std::memory_order_acquire); |
124 | const bool same_value_already_stored = (expected == fn); |
125 | return store_succeeded || same_value_already_stored; |
126 | } |
127 | |
128 | std::atomic<FnPtr> hook_; |
129 | #else // !ABSL_HAVE_WORKING_ATOMIC_POINTER |
130 | // Use a sentinel value unlikely to be the address of an actual function. |
131 | static constexpr intptr_t kUninitialized = 0; |
132 | |
133 | static_assert(sizeof(intptr_t) >= sizeof(FnPtr), |
134 | "intptr_t can't contain a function pointer" ); |
135 | |
136 | FnPtr DoLoad() const { |
137 | const intptr_t value = hook_.load(std::memory_order_acquire); |
138 | if (value == kUninitialized) { |
139 | return default_fn_; |
140 | } |
141 | return reinterpret_cast<FnPtr>(value); |
142 | } |
143 | |
144 | bool DoStore(FnPtr fn) { |
145 | assert(fn); |
146 | const auto value = reinterpret_cast<intptr_t>(fn); |
147 | intptr_t expected = kUninitialized; |
148 | const bool store_succeeded = hook_.compare_exchange_strong( |
149 | expected, value, std::memory_order_acq_rel, std::memory_order_acquire); |
150 | const bool same_value_already_stored = (expected == value); |
151 | return store_succeeded || same_value_already_stored; |
152 | } |
153 | |
154 | std::atomic<intptr_t> hook_; |
155 | #endif |
156 | |
157 | const FnPtr default_fn_; |
158 | }; |
159 | |
160 | #undef ABSL_HAVE_WORKING_ATOMIC_POINTER |
161 | |
162 | } // namespace base_internal |
163 | } // namespace absl |
164 | |
165 | #endif // ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_ |
166 | |