atomic_hook.h
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 
00016 #ifndef ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
00017 #define ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
00018 
00019 #include <atomic>
00020 #include <cassert>
00021 #include <cstdint>
00022 #include <utility>
00023 
00024 #ifdef _MSC_FULL_VER
00025 #define ABSL_HAVE_WORKING_ATOMIC_POINTER 0
00026 #else
00027 #define ABSL_HAVE_WORKING_ATOMIC_POINTER 1
00028 #endif
00029 
00030 namespace absl {
00031 namespace base_internal {
00032 
00033 template <typename T>
00034 class AtomicHook;
00035 
00036 // AtomicHook is a helper class, templatized on a raw function pointer type, for
00037 // implementing Abseil customization hooks.  It is a callable object that
00038 // dispatches to the registered hook.
00039 //
00040 // A default constructed object performs a no-op (and returns a default
00041 // constructed object) if no hook has been registered.
00042 //
00043 // Hooks can be pre-registered via constant initialization, for example,
00044 // ABSL_CONST_INIT static AtomicHook<void(*)()> my_hook(DefaultAction);
00045 // and then changed at runtime via a call to Store().
00046 //
00047 // Reads and writes guarantee memory_order_acquire/memory_order_release
00048 // semantics.
00049 template <typename ReturnType, typename... Args>
00050 class AtomicHook<ReturnType (*)(Args...)> {
00051  public:
00052   using FnPtr = ReturnType (*)(Args...);
00053 
00054   // Constructs an object that by default performs a no-op (and
00055   // returns a default constructed object) when no hook as been registered.
00056   constexpr AtomicHook() : AtomicHook(DummyFunction) {}
00057 
00058   // Constructs an object that by default dispatches to/returns the
00059   // pre-registered default_fn when no hook has been registered at runtime.
00060 #if ABSL_HAVE_WORKING_ATOMIC_POINTER
00061   explicit constexpr AtomicHook(FnPtr default_fn)
00062       : hook_(default_fn), default_fn_(default_fn) {}
00063 #else
00064   explicit constexpr AtomicHook(FnPtr default_fn)
00065       : hook_(kUninitialized), default_fn_(default_fn) {}
00066 #endif
00067 
00068   // Stores the provided function pointer as the value for this hook.
00069   //
00070   // This is intended to be called once.  Multiple calls are legal only if the
00071   // same function pointer is provided for each call.  The store is implemented
00072   // as a memory_order_release operation, and read accesses are implemented as
00073   // memory_order_acquire.
00074   void Store(FnPtr fn) {
00075     bool success = DoStore(fn);
00076     static_cast<void>(success);
00077     assert(success);
00078   }
00079 
00080   // Invokes the registered callback.  If no callback has yet been registered, a
00081   // default-constructed object of the appropriate type is returned instead.
00082   template <typename... CallArgs>
00083   ReturnType operator()(CallArgs&&... args) const {
00084     return DoLoad()(std::forward<CallArgs>(args)...);
00085   }
00086 
00087   // Returns the registered callback, or nullptr if none has been registered.
00088   // Useful if client code needs to conditionalize behavior based on whether a
00089   // callback was registered.
00090   //
00091   // Note that atomic_hook.Load()() and atomic_hook() have different semantics:
00092   // operator()() will perform a no-op if no callback was registered, while
00093   // Load()() will dereference a null function pointer.  Prefer operator()() to
00094   // Load()() unless you must conditionalize behavior on whether a hook was
00095   // registered.
00096   FnPtr Load() const {
00097     FnPtr ptr = DoLoad();
00098     return (ptr == DummyFunction) ? nullptr : ptr;
00099   }
00100 
00101  private:
00102   static ReturnType DummyFunction(Args...) {
00103     return ReturnType();
00104   }
00105 
00106   // Current versions of MSVC (as of September 2017) have a broken
00107   // implementation of std::atomic<T*>:  Its constructor attempts to do the
00108   // equivalent of a reinterpret_cast in a constexpr context, which is not
00109   // allowed.
00110   //
00111   // This causes an issue when building with LLVM under Windows.  To avoid this,
00112   // we use a less-efficient, intptr_t-based implementation on Windows.
00113 #if ABSL_HAVE_WORKING_ATOMIC_POINTER
00114   // Return the stored value, or DummyFunction if no value has been stored.
00115   FnPtr DoLoad() const { return hook_.load(std::memory_order_acquire); }
00116 
00117   // Store the given value.  Returns false if a different value was already
00118   // stored to this object.
00119   bool DoStore(FnPtr fn) {
00120     assert(fn);
00121     FnPtr expected = default_fn_;
00122     const bool store_succeeded = hook_.compare_exchange_strong(
00123         expected, fn, std::memory_order_acq_rel, std::memory_order_acquire);
00124     const bool same_value_already_stored = (expected == fn);
00125     return store_succeeded || same_value_already_stored;
00126   }
00127 
00128   std::atomic<FnPtr> hook_;
00129 #else  // !ABSL_HAVE_WORKING_ATOMIC_POINTER
00130   // Use a sentinel value unlikely to be the address of an actual function.
00131   static constexpr intptr_t kUninitialized = 0;
00132 
00133   static_assert(sizeof(intptr_t) >= sizeof(FnPtr),
00134                 "intptr_t can't contain a function pointer");
00135 
00136   FnPtr DoLoad() const {
00137     const intptr_t value = hook_.load(std::memory_order_acquire);
00138     if (value == kUninitialized) {
00139       return default_fn_;
00140     }
00141     return reinterpret_cast<FnPtr>(value);
00142   }
00143 
00144   bool DoStore(FnPtr fn) {
00145     assert(fn);
00146     const auto value = reinterpret_cast<intptr_t>(fn);
00147     intptr_t expected = kUninitialized;
00148     const bool store_succeeded = hook_.compare_exchange_strong(
00149         expected, value, std::memory_order_acq_rel, std::memory_order_acquire);
00150     const bool same_value_already_stored = (expected == value);
00151     return store_succeeded || same_value_already_stored;
00152   }
00153 
00154   std::atomic<intptr_t> hook_;
00155 #endif
00156 
00157   const FnPtr default_fn_;
00158 };
00159 
00160 #undef ABSL_HAVE_WORKING_ATOMIC_POINTER
00161 
00162 }  // namespace base_internal
00163 }  // namespace absl
00164 
00165 #endif  // ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14