00001 #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_FALLBACK_HPP
00002 #define BOOST_ATOMIC_DETAIL_ATOMIC_FALLBACK_HPP
00003
00004
00005
00006
00007
00008
00009
00010 #include <string.h>
00011
00012 #include <boost/version.hpp>
00013
00014 #if BOOST_VERSION > 103700
00015 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
00016 #else
00017 #include <boost/detail/spinlock_pool.hpp>
00018 #endif
00019
00020 namespace boost_atomic {
00021 namespace detail {
00022 namespace atomic {
00023
00024 template<typename T>
00025 class fallback_atomic {
00026 public:
00027 fallback_atomic(void) {}
00028 explicit fallback_atomic(const T &t) {memcpy(&i, &t, sizeof(T));}
00029
00030 void store(const T &t, memory_order2 order=memory_order2_seq_cst) volatile
00031 {
00032 boost::detail::spinlock_pool<0>::scoped_lock guard(const_cast<T*>(&i));
00033 memcpy((void*)&i, &t, sizeof(T));
00034 }
00035 T load(memory_order2 =memory_order2_seq_cst) volatile const
00036 {
00037 boost::detail::spinlock_pool<0>::scoped_lock guard(const_cast<T*>(&i));
00038 T tmp;
00039 memcpy(&tmp, (T*)&i, sizeof(T));
00040 return tmp;
00041 }
00042 bool compare_exchange_strong(
00043 T &expected,
00044 T desired,
00045 memory_order2 ,
00046 memory_order2 ) volatile
00047 {
00048 boost::detail::spinlock_pool<0>::scoped_lock guard(const_cast<T*>(&i));
00049 if (memcmp((void*)&i, &expected, sizeof(T))==0) {
00050 memcpy((void*)&i, &desired, sizeof(T));
00051 return true;
00052 } else {
00053 memcpy(&expected, (void*)&i, sizeof(T));
00054 return false;
00055 }
00056 }
00057 bool compare_exchange_weak(
00058 T &expected,
00059 T desired,
00060 memory_order2 success_order,
00061 memory_order2 failure_order) volatile
00062 {
00063 return compare_exchange_strong(expected, desired, success_order, failure_order);
00064 }
00065 T exchange(T replacement, memory_order2 =memory_order2_seq_cst) volatile
00066 {
00067 boost::detail::spinlock_pool<0>::scoped_lock guard(const_cast<T*>(&i));
00068 T tmp;
00069 memcpy(&tmp, (void*)&i, sizeof(T));
00070 memcpy((void*)&i, &replacement, sizeof(T));
00071 return tmp;
00072 }
00073 bool is_lock_free(void) const volatile {return false;}
00074 protected:
00075 T i;
00076 typedef T integral_type;
00077 };
00078
00079 }
00080 }
00081 }
00082
00083 #endif