1 //
2 // detail/gcc_x86_fenced_block.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
12 #define BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
21 
22 #include <boost/asio/detail/noncopyable.hpp>
23 
24 #include <boost/asio/detail/push_options.hpp>
25 
26 namespace boost {
27 namespace asio {
28 namespace detail {
29 
30 class gcc_x86_fenced_block
31   : private noncopyable
32 {
33 public:
34   enum half_t { half };
35   enum full_t { full };
36 
37   // Constructor for a half fenced block.
gcc_x86_fenced_block(half_t)38   explicit gcc_x86_fenced_block(half_t)
39   {
40   }
41 
42   // Constructor for a full fenced block.
gcc_x86_fenced_block(full_t)43   explicit gcc_x86_fenced_block(full_t)
44   {
45     lbarrier();
46   }
47 
48   // Destructor.
~gcc_x86_fenced_block()49   ~gcc_x86_fenced_block()
50   {
51     sbarrier();
52   }
53 
54 private:
barrier()55   static int barrier()
56   {
57     int r = 0, m = 1;
58     __asm__ __volatile__ (
59         "xchgl %0, %1" :
60         "=r"(r), "=m"(m) :
61         "0"(1), "m"(m) :
62         "memory", "cc");
63     return r;
64   }
65 
lbarrier()66   static void lbarrier()
67   {
68 #if defined(__SSE2__)
69 # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
70     __builtin_ia32_lfence();
71 # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
72     __asm__ __volatile__ ("lfence" ::: "memory");
73 # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
74 #else // defined(__SSE2__)
75     barrier();
76 #endif // defined(__SSE2__)
77   }
78 
sbarrier()79   static void sbarrier()
80   {
81 #if defined(__SSE2__)
82 # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
83     __builtin_ia32_sfence();
84 # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
85     __asm__ __volatile__ ("sfence" ::: "memory");
86 # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
87 #else // defined(__SSE2__)
88     barrier();
89 #endif // defined(__SSE2__)
90   }
91 };
92 
93 } // namespace detail
94 } // namespace asio
95 } // namespace boost
96 
97 #include <boost/asio/detail/pop_options.hpp>
98 
99 #endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
100 
101 #endif // BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
102