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