From: Yedidya Feldblum Date: Tue, 26 Dec 2017 21:37:51 +0000 (-0800) Subject: Tweaks to AtomicStruct X-Git-Tag: v2018.01.01.00~14 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=af28f1184cd85cebf83b4d473170535479c29928;p=folly.git Tweaks to AtomicStruct Summary: [Folly] Tweaks to `AtomicStruct`. Reviewed By: Orvid Differential Revision: D6636432 fbshipit-source-id: 274c118a732287219c569d7d3f4e170f275518f1 --- diff --git a/folly/synchronization/AtomicStruct.h b/folly/synchronization/AtomicStruct.h index a774d315..5dd02992 100644 --- a/folly/synchronization/AtomicStruct.h +++ b/folly/synchronization/AtomicStruct.h @@ -16,38 +16,53 @@ #pragma once -#include -#include -#include -#include #include +#include +#include +#include #include +#include +#include +#include + namespace folly { namespace detail { -template struct AtomicStructIntPick {}; +template +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 work like C++ atomics, but can be used on any POD /// type <= 8 bytes. -template < - typename T, - template class Atom = std::atomic, - typename Raw = typename detail::AtomicStructIntPick::type> +template 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::value || - folly::IsTriviallyCopyable::value, + private: + using Raw = _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::value || folly::IsTriviallyCopyable::value, "target type must be trivially copyable"); - union { - Atom data; - T typedData; - }; + Atom 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 const &) = delete; AtomicStruct& operator= (AtomicStruct 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