/* MemoryTransactions.hpp Provides yet another mechanism for serialising memory (C) 2013 Niall Douglas http://www.nedprod.com/ File Created: Sept 2013 */ #ifndef BOOST_AFIO_MEMORY_TRANSACTIONS_HPP #define BOOST_AFIO_MEMORY_TRANSACTIONS_HPP // Turn this on if you have a compiler which understands __transaction_relaxed //#define BOOST_HAVE_TRANSACTIONAL_MEMORY_COMPILER #ifndef BOOST_AFIO_COMPILING_FOR_GCOV // transaction support murders poor old gcov // Turn this on if you want to use Haswell TSX where available #if (defined(_MSC_VER) && _MSC_VER >= 1700 && ( defined(_M_IX86) || defined(_M_X64) )) || (defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )) #define BOOST_USING_INTEL_TSX #endif #endif #include "std_atomic_mutex_chrono.hpp" #ifndef BOOST_SMT_PAUSE #if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) ) extern "C" void _mm_pause(); #pragma intrinsic( _mm_pause ) #define BOOST_SMT_PAUSE _mm_pause(); #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) #define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" ); #endif #endif namespace boost { namespace afio { namespace detail { /*! \class spinlock So what's wrong with boost/smart_ptr/detail/spinlock.hpp then, and why reinvent the wheel? 1. Non-configurable spin. AFIO needs a bigger spin than smart_ptr provides. 2. AFIO is C++ 11, and therefore can implement this in pure C++ 11 atomics. 3. I don't much care for doing writes during the spin. It generates an unnecessary amount of cache line invalidation traffic. Better to spin-read and only write when the read suggests you might have a chance. 4. The other spin lock doesn't use Intel TSX yet. */ template struct spinlockbase { volatile atomic v; spinlockbase() BOOST_NOEXCEPT_OR_NOTHROW : v(0) { } bool try_lock() BOOST_NOEXCEPT_OR_NOTHROW { if(v.load()) // Avoid unnecessary cache line invalidation traffic return false; T expected=0; return v.compare_exchange_weak(expected, 1); } void unlock() BOOST_NOEXCEPT_OR_NOTHROW { v.store(0); } bool int_yield(size_t) BOOST_NOEXCEPT_OR_NOTHROW{ return false; } }; template struct spins_to_transact { template struct policy : parenttype { static BOOST_CONSTEXPR_OR_CONST size_t spins_to_transact=spins; }; }; template struct spins_to_loop { template struct policy : parenttype { static BOOST_CONSTEXPR_OR_CONST size_t spins_to_loop=spins; bool int_yield(size_t n) BOOST_NOEXCEPT_OR_NOTHROW { if(parenttype::int_yield(n)) return true; if(n>=spins) return false; if(use_pause) { #ifdef BOOST_SMT_PAUSE BOOST_SMT_PAUSE; #endif } return true; } }; }; template struct spins_to_yield { template struct policy : parenttype { static BOOST_CONSTEXPR_OR_CONST size_t spins_to_yield=spins; bool int_yield(size_t n) BOOST_NOEXCEPT_OR_NOTHROW { if(parenttype::int_yield(n)) return true; if(n>=spins) return false; boost::afio::this_thread::yield(); return true; } }; }; struct spins_to_sleep { template struct policy : parenttype { bool int_yield(size_t n) BOOST_NOEXCEPT_OR_NOTHROW { if(parenttype::int_yield(n)) return true; boost::afio::this_thread::sleep_for(boost::afio::chrono::milliseconds(1)); return true; } }; }; struct null_spin_policy { template struct policy : parenttype { }; }; template class spinpolicy1=spins_to_transact<5>::policy, template class spinpolicy2=spins_to_loop<125>::policy, template class spinpolicy3=spins_to_yield<250>::policy, template class spinpolicy4=spins_to_sleep::policy> class spinlock : public spinpolicy4>>>> { typedef spinpolicy4>>>> parenttype; public: void lock() BOOST_NOEXCEPT_OR_NOTHROW { for(size_t n=0;; n++) { if(parenttype::try_lock()) return; parenttype::int_yield(n); } } }; #ifndef BOOST_BEGIN_MEMORY_TRANSACTION #ifdef BOOST_HAVE_TRANSACTIONAL_MEMORY_COMPILER #define BOOST_BEGIN_MEMORY_TRANSACTION(lockable) __transaction_relaxed #define BOOST_END_MEMORY_TRANSACTION(lockable) #elif defined(BOOST_USING_INTEL_TSX) #define BOOST_AFIO_XBEGIN_STARTED (~0u) #define BOOST_AFIO_XABORT_EXPLICIT (1 << 0) #define BOOST_AFIO_XABORT_RETRY (1 << 1) #define BOOST_AFIO_XABORT_CONFLICT (1 << 2) #define BOOST_AFIO_XABORT_CAPACITY (1 << 3) #define BOOST_AFIO_XABORT_DEBUG (1 << 4) #define BOOST_AFIO_XABORT_NESTED (1 << 5) #define BOOST_AFIO_XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF)) #if defined(_MSC_VER) // Declare MSVC intrinsics extern "C" unsigned int _xbegin(void); extern "C" void _xend(void); extern "C" void _xabort(const unsigned int); extern "C" unsigned char _xtest(void); #define BOOST_AFIO_XBEGIN(...) _xbegin(__VA_ARGS__) #define BOOST_AFIO_XEND(...) _xend(__VA_ARGS__) #define BOOST_AFIO_XABORT(...) _xabort(__VA_ARGS__) #define BOOST_AFIO_XTEST(...) _xtest(__VA_ARGS__) #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) // Hack the bytes codes in for older compilers to avoid needing to compile with -mrtm namespace { // prevent any collisions with static __attribute__((__always_inline__)) inline unsigned int _xbegin() BOOST_NOEXCEPT_OR_NOTHROW { unsigned int ret = BOOST_AFIO_XBEGIN_STARTED; asm volatile(".byte 0xc7,0xf8 ; .long 0" : "+a" (ret) :: "memory"); return ret; } static __attribute__((__always_inline__)) inline unsigned char _xtest() BOOST_NOEXCEPT_OR_NOTHROW { unsigned char out; asm volatile(".byte 0x0f,0x01,0xd6 ; setnz %0" : "=r" (out) :: "memory"); return out; } } #define BOOST_AFIO_XBEGIN(...) _xbegin(__VA_ARGS__) #define BOOST_AFIO_XEND(...) asm volatile(".byte 0x0f,0x01,0xd5" ::: "memory") #define BOOST_AFIO_XABORT(status) asm volatile(".byte 0xc6,0xf8,%P0" :: "i" (status) : "memory") #define BOOST_AFIO_XTEST(...) _xtest(__VA_ARGS__) #endif // Adapted from http://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family namespace intel_stuff { inline void run_cpuid(uint32_t eax, uint32_t ecx, uint32_t* abcd) BOOST_NOEXCEPT_OR_NOTHROW { #if defined(_MSC_VER) __cpuidex((int *) abcd, eax, ecx); #else uint32_t ebx=0, edx=0; # if defined( __i386__ ) && defined ( __PIC__ ) /* in case of PIC under 32-bit EBX cannot be clobbered */ __asm__("movl %%ebx, %%edi \n\t cpuid \n\t xchgl %%ebx, %%edi" : "=D" (ebx), # else __asm__("cpuid" : "+b" (ebx), # endif "+a" (eax), "+c" (ecx), "=d" (edx)); abcd[0] = eax; abcd[1] = ebx; abcd[2] = ecx; abcd[3] = edx; #endif } inline bool have_intel_tsx_support() BOOST_NOEXCEPT_OR_NOTHROW { static int result; if(result) return result==2; uint32_t abcd[4]; uint32_t rtm_mask = (1 << 11); /* CPUID.(EAX=07H, ECX=0H):EBX.RTM[bit 11]==1 */ run_cpuid(7, 0, abcd); if((abcd[1] & rtm_mask) != rtm_mask) result=1; else result=2; return result==2; } } template struct get_spins_to_transact { static BOOST_CONSTEXPR_OR_CONST size_t value=4; }; template class spinpolicy1, template class spinpolicy2, template class spinpolicy3, template class spinpolicy4> struct get_spins_to_transact> { static BOOST_CONSTEXPR_OR_CONST size_t value=spinlock::spins_to_transact; }; template inline bool is_lockable_locked(T &lockable) BOOST_NOEXCEPT_OR_NOTHROW { if(lockable.try_lock()) { lockable.unlock(); return true; } return false; } template class spinpolicy1, template class spinpolicy2, template class spinpolicy3, template class spinpolicy4> inline bool is_lockable_locked(spinlock &lockable) BOOST_NOEXCEPT_OR_NOTHROW { return 1==lockable.v.load(); } template struct intel_tsx_transaction { T &lockable; int dismissed; // 0=disabled, 1=abort on exception throw, 2=commit, 3=traditional locks intel_tsx_transaction(T &_lockable) : lockable(_lockable), dismissed(0) { // Try four times size_t spins_to_transact=get_spins_to_transact::value; for(size_t n=0; n inline intel_tsx_transaction make_intel_tsx_transaction(T &lockable) BOOST_NOEXCEPT_OR_NOTHROW { return intel_tsx_transaction(lockable); } #define BOOST_BEGIN_MEMORY_TRANSACTION(lockable) { auto __tsx_transaction=boost::afio::detail::make_intel_tsx_transaction(lockable); { #define BOOST_END_MEMORY_TRANSACTION(lockable) } __tsx_transaction.commit(); } #endif // BOOST_USING_INTEL_TSX #endif // BOOST_BEGIN_MEMORY_TRANSACTION #ifndef BOOST_BEGIN_MEMORY_TRANSACTION #define BOOST_BEGIN_MEMORY_TRANSACTION(lockable) { boost::lock_guard __tsx_transaction(lockable); #define BOOST_END_MEMORY_TRANSACTION(lockable) } #endif // BOOST_BEGIN_MEMORY_TRANSACTION } } } #endif // BOOST_AFIO_MEMORY_TRANSACTIONS_HPP