diff --git a/include/def.h b/include/def.h index a8a5a47..ccd30ca 100644 --- a/include/def.h +++ b/include/def.h @@ -30,66 +30,3 @@ enum : std::size_t { }; } // namespace ipc - -//////////////////////////////////////////////////////////////// -/// Gives hint to processor that improves performance of spin-wait loops. -//////////////////////////////////////////////////////////////// - -#pragma push_macro("IPC_LOCK_PAUSE_") -#undef IPC_LOCK_PAUSE_ - -#if defined(_MSC_VER) -#include // YieldProcessor -/* - See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms687419(v=vs.85).aspx - Not for intel c++ compiler, so ignore http://software.intel.com/en-us/forums/topic/296168 -*/ -# define IPC_LOCK_PAUSE_() YieldProcessor() -#elif defined(__GNUC__) -#if defined(__i386__) || defined(__x86_64__) -/* - See: Intel(R) 64 and IA-32 Architectures Software Developer's Manual V2 - PAUSE-Spin Loop Hint, 4-57 - http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.html?wapkw=instruction+set+reference -*/ -# define IPC_LOCK_PAUSE_() __asm__ __volatile__("pause") -#elif defined(__ia64__) || defined(__ia64) -/* - See: Intel(R) Itanium(R) Architecture Developer's Manual, Vol.3 - hint - Performance Hint, 3:145 - http://www.intel.com/content/www/us/en/processors/itanium/itanium-architecture-vol-3-manual.html -*/ -# define IPC_LOCK_PAUSE_() __asm__ __volatile__ ("hint @pause") -#elif defined(__arm__) -/* - See: ARM Architecture Reference Manuals (YIELD) - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html -*/ -# define IPC_LOCK_PAUSE_() __asm__ __volatile__ ("yield") -#endif -#endif/*compilers*/ - -#if !defined(IPC_LOCK_PAUSE_) -/* - Just use a compiler fence, prevent compiler from optimizing loop -*/ -# define IPC_LOCK_PAUSE_() std::atomic_signal_fence(std::memory_order_seq_cst) -#endif/*!defined(IPC_LOCK_PAUSE_)*/ - -//////////////////////////////////////////////////////////////// -/// Yield to other threads -//////////////////////////////////////////////////////////////// - -namespace ipc { - -inline void yield(unsigned k) { - if (k < 4) { /* Do nothing */ } - else - if (k < 16) { IPC_LOCK_PAUSE_(); } - else - { std::this_thread::yield(); } -} - -} // namespace ipc - -#pragma pop_macro("IPC_LOCK_PAUSE_") diff --git a/include/rw_lock.h b/include/rw_lock.h index 4f8405f..6c0c20d 100644 --- a/include/rw_lock.h +++ b/include/rw_lock.h @@ -4,7 +4,68 @@ #include #include -#include "def.h" +//////////////////////////////////////////////////////////////// +/// Gives hint to processor that improves performance of spin-wait loops. +//////////////////////////////////////////////////////////////// + +#pragma push_macro("IPC_LOCK_PAUSE_") +#undef IPC_LOCK_PAUSE_ + +#if defined(_MSC_VER) +#include // YieldProcessor +/* + See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms687419(v=vs.85).aspx + Not for intel c++ compiler, so ignore http://software.intel.com/en-us/forums/topic/296168 +*/ +# define IPC_LOCK_PAUSE_() YieldProcessor() +#elif defined(__GNUC__) +#if defined(__i386__) || defined(__x86_64__) +/* + See: Intel(R) 64 and IA-32 Architectures Software Developer's Manual V2 + PAUSE-Spin Loop Hint, 4-57 + http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.html?wapkw=instruction+set+reference +*/ +# define IPC_LOCK_PAUSE_() __asm__ __volatile__("pause") +#elif defined(__ia64__) || defined(__ia64) +/* + See: Intel(R) Itanium(R) Architecture Developer's Manual, Vol.3 + hint - Performance Hint, 3:145 + http://www.intel.com/content/www/us/en/processors/itanium/itanium-architecture-vol-3-manual.html +*/ +# define IPC_LOCK_PAUSE_() __asm__ __volatile__ ("hint @pause") +#elif defined(__arm__) +/* + See: ARM Architecture Reference Manuals (YIELD) + http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html +*/ +# define IPC_LOCK_PAUSE_() __asm__ __volatile__ ("yield") +#endif +#endif/*compilers*/ + +#if !defined(IPC_LOCK_PAUSE_) +/* + Just use a compiler fence, prevent compiler from optimizing loop +*/ +# define IPC_LOCK_PAUSE_() std::atomic_signal_fence(std::memory_order_seq_cst) +#endif/*!defined(IPC_LOCK_PAUSE_)*/ + +//////////////////////////////////////////////////////////////// +/// Yield to other threads +//////////////////////////////////////////////////////////////// + +namespace ipc { + +inline void yield(unsigned k) { + if (k < 4) { /* Do nothing */ } + else + if (k < 16) { IPC_LOCK_PAUSE_(); } + else + { std::this_thread::yield(); } +} + +} // namespace ipc + +#pragma pop_macro("IPC_LOCK_PAUSE_") namespace ipc {