00001 #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_INTEGRAL_CASTS_HPP
00002 #define BOOST_ATOMIC_DETAIL_ATOMIC_INTEGRAL_CASTS_HPP
00003
00004
00005
00006
00007
00008
00009
00010 #include <string.h>
00011
00012 namespace boost_atomic { namespace detail { namespace atomic {
00013 using namespace boost;
00014
00015 template<typename T>
00016 class platform_atomic<T, 1> : private platform_atomic_integral<uint8_t> {
00017 public:
00018 typedef platform_atomic_integral<uint8_t> super;
00019 typedef union { T e; uint8_t i;} conv;
00020
00021 platform_atomic() {}
00022 explicit platform_atomic(T t) : super(to_integral(t))
00023 {
00024 }
00025
00026 void store(T t, memory_order2 order=memory_order2_seq_cst) volatile
00027 {
00028 super::store(to_integral(t), order);
00029 }
00030 T load(memory_order2 order=memory_order2_seq_cst) volatile const
00031 {
00032 return from_integral(super::load(order));
00033 }
00034 bool compare_exchange_strong(
00035 T &expected,
00036 T desired,
00037 memory_order2 success_order,
00038 memory_order2 failure_order) volatile
00039 {
00040 uint8_t _expected, _desired;
00041 _expected=to_integral(expected);
00042 _desired=to_integral(desired);
00043 bool success=super::compare_exchange_strong(_expected, _desired, success_order, failure_order);
00044 expected=from_integral(_expected);
00045 return success;
00046 }
00047 bool compare_exchange_weak(
00048 T &expected,
00049 T desired,
00050 memory_order2 success_order,
00051 memory_order2 failure_order) volatile
00052 {
00053 uint8_t _expected, _desired;
00054 _expected=to_integral(expected);
00055 _desired=to_integral(desired);
00056 bool success=super::compare_exchange_weak(_expected, _desired, success_order, failure_order);
00057 expected=from_integral(_expected);
00058 return success;
00059 }
00060
00061 T exchange(T replacement, memory_order2 order=memory_order2_seq_cst) volatile
00062 {
00063 return from_integral(super::exchange(to_integral(replacement), order));
00064 }
00065
00066 operator T(void) const volatile {return load();}
00067 T operator=(T v) volatile {store(v); return v;}
00068
00069 using super::is_lock_free;
00070 protected:
00071 static inline uint8_t to_integral(T &t)
00072 {
00073 uint8_t tmp;
00074 memcpy(&tmp, &t, sizeof(t));
00075 return tmp;
00076 }
00077 static inline T from_integral(uint8_t t)
00078 {
00079 T tmp;
00080 memcpy(&tmp, &t, sizeof(t));
00081 return tmp;
00082 }
00083 };
00084
00085 template<typename T>
00086 class platform_atomic<T, 2> : private platform_atomic_integral<uint16_t> {
00087 public:
00088 typedef platform_atomic_integral<uint16_t> super;
00089 typedef union { T e; uint16_t i;} conv;
00090
00091 platform_atomic() {}
00092 explicit platform_atomic(T t) : super(to_integral(t))
00093 {
00094 }
00095
00096 void store(T t, memory_order2 order=memory_order2_seq_cst) volatile
00097 {
00098 super::store(to_integral(t), order);
00099 }
00100 T load(memory_order2 order=memory_order2_seq_cst) volatile const
00101 {
00102 return from_integral(super::load(order));
00103 }
00104 bool compare_exchange_strong(
00105 T &expected,
00106 T desired,
00107 memory_order2 success_order,
00108 memory_order2 failure_order) volatile
00109 {
00110 uint16_t _expected, _desired;
00111 _expected=to_integral(expected);
00112 _desired=to_integral(desired);
00113 bool success=super::compare_exchange_strong(_expected, _desired, success_order, failure_order);
00114 expected=from_integral(_expected);
00115 return success;
00116 }
00117 bool compare_exchange_weak(
00118 T &expected,
00119 T desired,
00120 memory_order2 success_order,
00121 memory_order2 failure_order) volatile
00122 {
00123 uint16_t _expected, _desired;
00124 _expected=to_integral(expected);
00125 _desired=to_integral(desired);
00126 bool success=super::compare_exchange_weak(_expected, _desired, success_order, failure_order);
00127 expected=from_integral(_expected);
00128 return success;
00129 }
00130
00131 T exchange(T replacement, memory_order2 order=memory_order2_seq_cst) volatile
00132 {
00133 return from_integral(super::exchange(to_integral(replacement), order));
00134 }
00135
00136 operator T(void) const volatile {return load();}
00137 T operator=(T v) volatile {store(v); return v;}
00138
00139 using super::is_lock_free;
00140 protected:
00141 static inline uint16_t to_integral(T &t)
00142 {
00143 uint16_t tmp;
00144 memcpy(&tmp, &t, sizeof(t));
00145 return tmp;
00146 }
00147 static inline T from_integral(uint16_t t)
00148 {
00149 T tmp;
00150 memcpy(&tmp, &t, sizeof(t));
00151 return tmp;
00152 }
00153 };
00154
00155 template<typename T>
00156 class platform_atomic<T, 4> : private platform_atomic_integral<uint32_t> {
00157 public:
00158 typedef platform_atomic_integral<uint32_t> super;
00159 typedef union { T e; uint32_t i;} conv;
00160
00161 platform_atomic() {}
00162 explicit platform_atomic(T t) : super(to_integral(t))
00163 {
00164 }
00165
00166 void store(T t, memory_order2 order=memory_order2_seq_cst) volatile
00167 {
00168 super::store(to_integral(t), order);
00169 }
00170 T load(memory_order2 order=memory_order2_seq_cst) volatile const
00171 {
00172 return from_integral(super::load(order));
00173 }
00174 bool compare_exchange_strong(
00175 T &expected,
00176 T desired,
00177 memory_order2 success_order,
00178 memory_order2 failure_order) volatile
00179 {
00180 uint32_t _expected, _desired;
00181 _expected=to_integral(expected);
00182 _desired=to_integral(desired);
00183 bool success=super::compare_exchange_strong(_expected, _desired, success_order, failure_order);
00184 expected=from_integral(_expected);
00185 return success;
00186 }
00187 bool compare_exchange_weak(
00188 T &expected,
00189 T desired,
00190 memory_order2 success_order,
00191 memory_order2 failure_order) volatile
00192 {
00193 uint32_t _expected, _desired;
00194 _expected=to_integral(expected);
00195 _desired=to_integral(desired);
00196 bool success=super::compare_exchange_weak(_expected, _desired, success_order, failure_order);
00197 expected=from_integral(_expected);
00198 return success;
00199 }
00200
00201 T exchange(T replacement, memory_order2 order=memory_order2_seq_cst) volatile
00202 {
00203 return from_integral(super::exchange(to_integral(replacement), order));
00204 }
00205
00206 operator T(void) const volatile {return load();}
00207 T operator=(T v) volatile {store(v); return v;}
00208
00209 using super::is_lock_free;
00210 protected:
00211 static inline uint32_t to_integral(T &t)
00212 {
00213 uint32_t tmp;
00214 memcpy(&tmp, &t, sizeof(t));
00215 return tmp;
00216 }
00217 static inline T from_integral(uint32_t t)
00218 {
00219 T tmp;
00220 memcpy(&tmp, &t, sizeof(t));
00221 return tmp;
00222 }
00223 };
00224
00225 template<typename T>
00226 class platform_atomic<T, 8> : private platform_atomic_integral<uint64_t> {
00227 public:
00228 typedef platform_atomic_integral<uint64_t> super;
00229 typedef union { T e; uint64_t i;} conv;
00230
00231 platform_atomic() {}
00232 explicit platform_atomic(T t) : super(to_integral(t))
00233 {
00234 }
00235
00236 void store(T t, memory_order2 order=memory_order2_seq_cst) volatile
00237 {
00238 super::store(to_integral(t), order);
00239 }
00240 T load(memory_order2 order=memory_order2_seq_cst) volatile const
00241 {
00242 return from_integral(super::load(order));
00243 }
00244 bool compare_exchange_strong(
00245 T &expected,
00246 T desired,
00247 memory_order2 success_order,
00248 memory_order2 failure_order) volatile
00249 {
00250 uint64_t _expected, _desired;
00251 _expected=to_integral(expected);
00252 _desired=to_integral(desired);
00253 bool success=super::compare_exchange_strong(_expected, _desired, success_order, failure_order);
00254 expected=from_integral(_expected);
00255 return success;
00256 }
00257 bool compare_exchange_weak(
00258 T &expected,
00259 T desired,
00260 memory_order2 success_order,
00261 memory_order2 failure_order) volatile
00262 {
00263 uint64_t _expected, _desired;
00264 _expected=to_integral(expected);
00265 _desired=to_integral(desired);
00266 bool success=super::compare_exchange_weak(_expected, _desired, success_order, failure_order);
00267 expected=from_integral(_expected);
00268 return success;
00269 }
00270
00271 T exchange(T replacement, memory_order2 order=memory_order2_seq_cst) volatile
00272 {
00273 return from_integral(super::exchange(to_integral(replacement), order));
00274 }
00275
00276 operator T(void) const volatile {return load();}
00277 T operator=(T v) volatile {store(v); return v;}
00278
00279 using super::is_lock_free;
00280 protected:
00281 static inline uint64_t to_integral(T &t)
00282 {
00283 uint64_t tmp;
00284 memcpy(&tmp, &t, sizeof(t));
00285 return tmp;
00286 }
00287 static inline T from_integral(uint64_t t)
00288 {
00289 T tmp;
00290 memcpy(&tmp, &t, sizeof(t));
00291 return tmp;
00292 }
00293 };
00294
00295 } } }
00296
00297 #endif