generic-cas.hpp
Go to the documentation of this file.
1 #ifndef BOOST_DETAIL_ATOMIC_GENERIC_CAS_HPP
2 #define BOOST_DETAIL_ATOMIC_GENERIC_CAS_HPP
3 
4 // Copyright (c) 2009 Helge Bahmann
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <stdint.h>
11 
12 #include <boost/memory_order.hpp>
14 #include <boost/atomic/detail/builder.hpp>
15 
16 /* fallback implementation for various compilation targets;
17 this is *not* efficient, particularly because all operations
18 are fully fenced (full memory barriers before and after
19 each operation) */
20 
21 #if defined(__GNUC__)
22  namespace boost { namespace detail { namespace atomic {
23  static inline int32_t
24  fenced_compare_exchange_strong_32(volatile int32_t *ptr, int32_t expected, int32_t desired)
25  {
26  return __sync_val_compare_and_swap_4(ptr, expected, desired);
27  }
28  #define BOOST_ATOMIC_HAVE_CAS32 1
29 
30  #if defined(__amd64__) || defined(__i686__)
31  static inline int64_t
32  fenced_compare_exchange_strong_64(int64_t *ptr, int64_t expected, int64_t desired)
33  {
34  return __sync_val_compare_and_swap_8(ptr, expected, desired);
35  }
36  #define BOOST_ATOMIC_HAVE_CAS64 1
37  #endif
38  }}}
39 
40 #elif defined(__ICL) || defined(_MSC_VER)
41 
42  #if defined(_MSC_VER)
43  #include <Windows.h>
44  #include <intrin.h>
45  #endif
46 
47  namespace boost { namespace detail { namespace atomic {
48  static inline int32_t
49  fenced_compare_exchange_strong(int32_t *ptr, int32_t expected, int32_t desired)
50  {
51  return _InterlockedCompareExchange(reinterpret_cast<volatile long*>(ptr), desired, expected);
52  }
53  #define BOOST_ATOMIC_HAVE_CAS32 1
54  #if defined(_WIN64)
55  static inline int64_t
56  fenced_compare_exchange_strong(int64_t *ptr, int64_t expected, int64_t desired)
57  {
58  return _InterlockedCompareExchange64(ptr, desired, expected);
59  }
60  #define BOOST_ATOMIC_HAVE_CAS64 1
61  #endif
62  }}}
63 
64 #elif (defined(__ICC) || defined(__ECC))
65  namespace boost { namespace detail { namespace atomic {
66  static inline int32_t
67  fenced_compare_exchange_strong_32(int32_t *ptr, int32_t expected, int32_t desired)
68  {
69  return _InterlockedCompareExchange((void*)ptr, desired, expected);
70  }
71  #define BOOST_ATOMIC_HAVE_CAS32 1
72  #if defined(__x86_64)
73  static inline int64_t
74  fenced_compare_exchange_strong(int64_t *ptr, int64_t expected, int64_t desired)
75  {
76  return cas64<int>(ptr, expected, desired);
77  }
78  #define BOOST_ATOMIC_HAVE_CAS64 1
79  #elif defined(__ECC) //IA-64 version
80  static inline int64_t
81  fenced_compare_exchange_strong(int64_t *ptr, int64_t expected, int64_t desired)
82  {
83  return _InterlockedCompareExchange64((void*)ptr, desired, expected);
84  }
85  #define BOOST_ATOMIC_HAVE_CAS64 1
86  #endif
87  }}}
88 
89 #elif (defined(__SUNPRO_CC) && defined(__sparc))
90  #include <sys/atomic.h>
91  namespace boost { namespace detail { namespace atomic {
92  static inline int32_t
93  fenced_compare_exchange_strong_32(int32_t *ptr, int32_t expected, int32_t desired)
94  {
95  return atomic_cas_32((volatile unsigned int*)ptr, expected, desired);
96  }
97  #define BOOST_ATOMIC_HAVE_CAS32 1
98 
99  /* FIXME: check for 64 bit mode */
100  static inline int64_t
101  fenced_compare_exchange_strong_64(int64_t *ptr, int64_t expected, int64_t desired)
102  {
103  return atomic_cas_64((volatile unsigned long long*)ptr, expected, desired);
104  }
105  #define BOOST_ATOMIC_HAVE_CAS64 1
106  }}}
107 #endif
108 
109 
110 namespace boost { namespace detail { namespace atomic {
111 
112 #ifdef BOOST_ATOMIC_HAVE_CAS32
113 template<typename T>
114 class atomic_generic_cas32 {
115 private:
116  typedef atomic_generic_cas32 this_type;
117 public:
118  explicit atomic_generic_cas32(T v) : i((int32_t)v) {}
119  atomic_generic_cas32() {}
120  T load(memory_order order=memory_order_seq_cst) const volatile
121  {
122  T expected=(T)i;
123  do { } while(!const_cast<this_type *>(this)->compare_exchange_weak(expected, expected, order, memory_order_relaxed));
124  return expected;
125  }
126  void store(T v, memory_order order=memory_order_seq_cst) volatile
127  {
128  exchange(v);
129  }
130  bool compare_exchange_strong(
131  T &expected,
132  T desired,
133  memory_order success_order,
134  memory_order failure_order) volatile
135  {
136  T found;
137  found=(T)fenced_compare_exchange_strong_32(&i, (int32_t)expected, (int32_t)desired);
138  bool success=(found==expected);
139  expected=found;
140  return success;
141  }
142  bool compare_exchange_weak(
143  T &expected,
144  T desired,
145  memory_order success_order,
146  memory_order failure_order) volatile
147  {
148  return compare_exchange_strong(expected, desired, success_order, failure_order);
149  }
150  T exchange(T r, memory_order order=memory_order_seq_cst) volatile
151  {
152  T expected=(T)i;
153  do { } while(!compare_exchange_weak(expected, r, order, memory_order_relaxed));
154  return expected;
155  }
156 
157  bool is_lock_free(void) const volatile {return true;}
158  typedef T integral_type;
159 private:
160  mutable int32_t i;
161 };
162 
163 template<typename T>
164 class platform_atomic_integral<T, 4> : public build_atomic_from_exchange<atomic_generic_cas32<T> > {
165 public:
166  typedef build_atomic_from_exchange<atomic_generic_cas32<T> > super;
167  explicit platform_atomic_integral(T v) : super(v) {}
168  platform_atomic_integral(void) {}
169 };
170 
171 template<typename T>
172 class platform_atomic_integral<T, 1>: public build_atomic_from_larger_type<atomic_generic_cas32<int32_t>, T> {
173 public:
174  typedef build_atomic_from_larger_type<atomic_generic_cas32<int32_t>, T> super;
175 
176  explicit platform_atomic_integral(T v) : super(v) {}
177  platform_atomic_integral(void) {}
178 };
179 
180 template<typename T>
181 class platform_atomic_integral<T, 2>: public build_atomic_from_larger_type<atomic_generic_cas32<int32_t>, T> {
182 public:
183  typedef build_atomic_from_larger_type<atomic_generic_cas32<int32_t>, T> super;
184 
185  explicit platform_atomic_integral(T v) : super(v) {}
186  platform_atomic_integral(void) {}
187 };
188 #endif
189 
190 } } }
191 
192 #endif
Definition: base.hpp:116


rosatomic
Author(s): Josh Faust
autogenerated on Mon Jun 10 2019 14:44:41