From 64f9370fffe9581a4ae8a2d285243f4014dc717e Mon Sep 17 00:00:00 2001 From: joaquintides Date: Wed, 7 Jun 2023 11:30:52 +0200 Subject: [PATCH] implemented cacheline alignment without extended alignas --- .../unordered/detail/foa/concurrent_table.hpp | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/include/boost/unordered/detail/foa/concurrent_table.hpp b/include/boost/unordered/detail/foa/concurrent_table.hpp index ad341e64..2bc55744 100644 --- a/include/boost/unordered/detail/foa/concurrent_table.hpp +++ b/include/boost/unordered/detail/foa/concurrent_table.hpp @@ -64,20 +64,33 @@ using is_execution_policy=std::false_type; namespace foa{ -#if defined(BOOST_MSVC) -#pragma warning(push) -#pragma warning(disable:4324) /* padded structure due to alignas */ -#endif - -template -struct alignas(64) cacheline_protected:T +template +class cache_aligned_array { - using T::T; -}; +public: + cache_aligned_array(){for(std::size_t n=0;n0;)data(n--)->~T();} + cache_aligned_array(const cache_aligned_array&)=delete; + cache_aligned_array& operator=(const cache_aligned_array&)=delete; -#if defined(BOOST_MSVC) -#pragma warning(pop) /* C4324 */ -#endif + T& operator[](std::size_t pos)noexcept{return *data(pos);} + +private: + static constexpr std::size_t cacheline=64; + static constexpr std::size_t element_offset= + (sizeof(T)+cacheline-1)/cacheline*cacheline; + + BOOST_STATIC_ASSERT(alignof(T)<=cacheline); + + T* data(std::size_t pos)noexcept + { + return reinterpret_cast( + (reinterpret_cast(&buf)+cacheline-1)/cacheline*cacheline+ + pos*element_offset); + } + + unsigned char buf[element_offset*N+cacheline-1]; +}; template class multimutex @@ -95,7 +108,7 @@ public: void unlock()noexcept{for(auto n=N;n>0;)mutexes[--n].unlock();} private: - Mutex mutexes[N]; + cache_aligned_array mutexes; }; /* std::shared_lock is C++14 */ @@ -785,7 +798,7 @@ public: } private: - using mutex_type=cacheline_protected; + using mutex_type=rw_spinlock; using multimutex_type=multimutex; // TODO: adapt 128 to the machine using shared_lock_guard=shared_lock; using exclusive_lock_guard=lock_guard;