Tweaks to AtomicStruct
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 26 Dec 2017 21:37:51 +0000 (13:37 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 26 Dec 2017 22:02:35 +0000 (14:02 -0800)
Summary: [Folly] Tweaks to `AtomicStruct`.

Reviewed By: Orvid

Differential Revision: D6636432

fbshipit-source-id: 274c118a732287219c569d7d3f4e170f275518f1

folly/synchronization/AtomicStruct.h

index a774d31500fd952afd49e1a326560d30e3c5eaa6..5dd02992cdc41adc7d77296578cbccac706492ae 100644 (file)
 
 #pragma once
 
-#include <folly/Traits.h>
-#include <folly/synchronization/detail/AtomicUtils.h>
-#include <stdint.h>
-#include <string.h>
 #include <atomic>
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
 #include <type_traits>
 
+#include <folly/ConstexprMath.h>
+#include <folly/Traits.h>
+#include <folly/synchronization/detail/AtomicUtils.h>
+
 namespace folly {
 
 namespace detail {
-template <int N> struct AtomicStructIntPick {};
+template <size_t>
+struct AtomicStructRaw;
+template <>
+struct AtomicStructRaw<0> {
+  using type = uint8_t;
+};
+template <>
+struct AtomicStructRaw<1> {
+  using type = uint16_t;
+};
+template <>
+struct AtomicStructRaw<2> {
+  using type = uint32_t;
+};
+template <>
+struct AtomicStructRaw<3> {
+  using type = uint64_t;
+};
 } // namespace detail
 
 /// AtomicStruct<T> work like C++ atomics, but can be used on any POD
 /// type <= 8 bytes.
-template <
-    typename T,
-    template <typename> class Atom = std::atomic,
-    typename Raw = typename detail::AtomicStructIntPick<sizeof(T)>::type>
+template <typename T, template <typename> class Atom = std::atomic>
 class AtomicStruct {
-  static_assert(alignof(T) <= alignof(Raw),
-      "target type can't have stricter alignment than matching int");
-  static_assert(sizeof(T) <= sizeof(Raw),
-      "underlying type isn't big enough");
-  static_assert(std::is_trivial<T>::value ||
-                folly::IsTriviallyCopyable<T>::value,
+ private:
+  using Raw = _t<detail::AtomicStructRaw<constexpr_log2_ceil(sizeof(T))>>;
+
+  static_assert(alignof(T) <= alignof(Raw), "underlying type is under-aligned");
+  static_assert(sizeof(T) <= sizeof(Raw), "underlying type is under-sized");
+  static_assert(
+      std::is_trivial<T>::value || folly::IsTriviallyCopyable<T>::value,
       "target type must be trivially copyable");
 
-  union {
-    Atom<Raw> data;
-    T typedData;
-  };
+  Atom<Raw> data;
 
   static Raw encode(T v) noexcept {
     // we expect the compiler to optimize away the memcpy, but without
@@ -69,7 +84,7 @@ class AtomicStruct {
   AtomicStruct(AtomicStruct<T> const &) = delete;
   AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
 
-  constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {}
+  constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
 
   bool is_lock_free() const noexcept {
     return data.is_lock_free();
@@ -142,17 +157,4 @@ class AtomicStruct {
   // by duplicating the above code and the corresponding unit tests.
 };
 
-namespace detail {
-
-template <> struct AtomicStructIntPick<1> { typedef uint8_t type; };
-template <> struct AtomicStructIntPick<2> { typedef uint16_t type; };
-template <> struct AtomicStructIntPick<3> { typedef uint32_t type; };
-template <> struct AtomicStructIntPick<4> { typedef uint32_t type; };
-template <> struct AtomicStructIntPick<5> { typedef uint64_t type; };
-template <> struct AtomicStructIntPick<6> { typedef uint64_t type; };
-template <> struct AtomicStructIntPick<7> { typedef uint64_t type; };
-template <> struct AtomicStructIntPick<8> { typedef uint64_t type; };
-
-} // namespace detail
-
 } // namespace folly