Chris@16: // Chris@16: // detail/gcc_x86_fenced_block.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP Chris@16: #define BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class gcc_x86_fenced_block Chris@16: : private noncopyable Chris@16: { Chris@16: public: Chris@16: enum half_t { half }; Chris@16: enum full_t { full }; Chris@16: Chris@16: // Constructor for a half fenced block. Chris@16: explicit gcc_x86_fenced_block(half_t) Chris@16: { Chris@16: } Chris@16: Chris@16: // Constructor for a full fenced block. Chris@16: explicit gcc_x86_fenced_block(full_t) Chris@16: { Chris@16: lbarrier(); Chris@16: } Chris@16: Chris@16: // Destructor. Chris@16: ~gcc_x86_fenced_block() Chris@16: { Chris@16: sbarrier(); Chris@16: } Chris@16: Chris@16: private: Chris@16: static int barrier() Chris@16: { Chris@16: int r = 0, m = 1; Chris@16: __asm__ __volatile__ ( Chris@16: "xchgl %0, %1" : Chris@16: "=r"(r), "=m"(m) : Chris@16: "0"(1), "m"(m) : Chris@16: "memory", "cc"); Chris@16: return r; Chris@16: } Chris@16: Chris@16: static void lbarrier() Chris@16: { Chris@16: #if defined(__SSE2__) Chris@101: # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@101: __builtin_ia32_lfence(); Chris@101: # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@16: __asm__ __volatile__ ("lfence" ::: "memory"); Chris@101: # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@16: #else // defined(__SSE2__) Chris@16: barrier(); Chris@16: #endif // defined(__SSE2__) Chris@16: } Chris@16: Chris@16: static void sbarrier() Chris@16: { Chris@16: #if defined(__SSE2__) Chris@101: # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@101: __builtin_ia32_sfence(); Chris@101: # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@16: __asm__ __volatile__ ("sfence" ::: "memory"); Chris@101: # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL) Chris@16: #else // defined(__SSE2__) Chris@16: barrier(); Chris@16: #endif // defined(__SSE2__) Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP