Realistic 3D camera system
3D camera system components
gcc_x86_fenced_block.hpp
Go to the documentation of this file.
1 //
2 // detail/gcc_x86_fenced_block.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
12 #define 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 "asio/detail/config.hpp"
19 
20 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
21 
23 
24 namespace asio {
25 namespace detail {
26 
27 class gcc_x86_fenced_block
28  : private noncopyable
29 {
30 public:
31  enum half_t { half };
32  enum full_t { full };
33 
34  // Constructor for a half fenced block.
35  explicit gcc_x86_fenced_block(half_t)
36  {
37  }
38 
39  // Constructor for a full fenced block.
40  explicit gcc_x86_fenced_block(full_t)
41  {
42  lbarrier();
43  }
44 
45  // Destructor.
46  ~gcc_x86_fenced_block()
47  {
48  sbarrier();
49  }
50 
51 private:
52  static int barrier()
53  {
54  int r = 0, m = 1;
55  __asm__ __volatile__ (
56  "xchgl %0, %1" :
57  "=r"(r), "=m"(m) :
58  "0"(1), "m"(m) :
59  "memory", "cc");
60  return r;
61  }
62 
63  static void lbarrier()
64  {
65 #if defined(__SSE2__)
66 # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
67  __builtin_ia32_lfence();
68 # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
69  __asm__ __volatile__ ("lfence" ::: "memory");
70 # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
71 #else // defined(__SSE2__)
72  barrier();
73 #endif // defined(__SSE2__)
74  }
75 
76  static void sbarrier()
77  {
78 #if defined(__SSE2__)
79 # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
80  __builtin_ia32_sfence();
81 # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
82  __asm__ __volatile__ ("sfence" ::: "memory");
83 # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
84 #else // defined(__SSE2__)
85  barrier();
86 #endif // defined(__SSE2__)
87  }
88 };
89 
90 } // namespace detail
91 } // namespace asio
92 
94 
95 #endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
96 
97 #endif // ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP