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