Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 template <typename ReturnType, typename... Args>
00050 class AtomicHook<ReturnType (*)(Args...)> {
00051 public:
00052 using FnPtr = ReturnType (*)(Args...);
00053
00054
00055
00056 constexpr AtomicHook() : AtomicHook(DummyFunction) {}
00057
00058
00059
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
00069
00070
00071
00072
00073
00074 void Store(FnPtr fn) {
00075 bool success = DoStore(fn);
00076 static_cast<void>(success);
00077 assert(success);
00078 }
00079
00080
00081
00082 template <typename... CallArgs>
00083 ReturnType operator()(CallArgs&&... args) const {
00084 return DoLoad()(std::forward<CallArgs>(args)...);
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
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
00107
00108
00109
00110
00111
00112
00113 #if ABSL_HAVE_WORKING_ATOMIC_POINTER
00114
00115 FnPtr DoLoad() const { return hook_.load(std::memory_order_acquire); }
00116
00117
00118
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
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 }
00163 }
00164
00165 #endif // ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_