CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AtomicPtrCache.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AtomicPtrCache_h
2 #define DataFormats_Common_AtomicPtrCache_h
3 // -*- C++ -*-
4 //
5 // Package: DataFormats/Common
6 // Class : AtomicPtrCache
7 //
24 //
25 // Original Author: Chris Jones
26 // Created: Thu, 07 Nov 2013 00:50:40 GMT
27 //
28 
29 // system include files
30 #include <atomic>
31 #include <memory>
32 // user include files
33 
34 // forward declarations
35 
36 namespace edm {
37  template <typename T>
39  {
40 
41  public:
43 
45  explicit AtomicPtrCache(T*);
46 
50 
52 
53  // ---------- const member functions ---------------------
54  T const* operator->() const { return load();}
55  T const& operator*() const {return *load(); }
56  T const* load() const;
57 
58  bool isSet() const;
61  bool set(std::unique_ptr<T> iNewValue) const;
62  // ---------- static member functions --------------------
63 
64  // ---------- member functions ---------------------------
65  T* operator->() { return load();}
66  T& operator*() {return *load();}
67 
68  T* load();
69 
71  void reset();
72 
73  T* release();
74 
75  private:
76 
77  // ---------- member data --------------------------------
78  mutable std::atomic<T*> m_data;
79  };
80  template<typename T>
81  inline AtomicPtrCache<T>::AtomicPtrCache():m_data{nullptr} {}
82 
83  template<typename T>
84  inline AtomicPtrCache<T>::AtomicPtrCache(T* iValue): m_data{iValue} {}
85 
86  template<typename T>
88  m_data{nullptr}
89  {
90  auto ptr = iOther.m_data.load(std::memory_order_acquire);
91  if(ptr != nullptr) {
92  m_data.store( new T{*ptr}, std::memory_order_release);
93  }
94  }
95  template<typename T>
97  auto ptr = iOther.m_data.load(std::memory_order_acquire);
98  if(ptr != nullptr) {
99  auto ourPtr =m_data.load(std::memory_order_acquire);
100  if( ourPtr !=nullptr) {
101  *ourPtr = *ptr;
102  } else {
103  m_data.store( new T{*ptr}, std::memory_order_release);
104  }
105  } else {
106  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
107  }
108  return *this;
109  }
110 
111 
112  template<typename T>
114  delete m_data.load(std::memory_order_acquire);
115  }
116 
117  template<typename T>
118  inline T* AtomicPtrCache<T>::load(){ return m_data.load(std::memory_order_acquire);}
119 
120  template<typename T>
121  inline T const* AtomicPtrCache<T>::load() const{ return m_data.load(std::memory_order_acquire);}
122 
123  template<typename T>
124  inline bool AtomicPtrCache<T>::isSet() const { return nullptr!=m_data.load(std::memory_order_acquire);}
125 
126  template<typename T>
127  inline bool AtomicPtrCache<T>::set(std::unique_ptr<T> iNewValue) const {
128  bool retValue;
129  T* expected = nullptr;
130  if( (retValue = m_data.compare_exchange_strong(expected,iNewValue.get(), std::memory_order_acq_rel)) ) {
131  iNewValue.release();
132  }
133  return retValue;
134  }
135 
136  template<typename T>
137  inline void AtomicPtrCache<T>::reset() { delete m_data.exchange(nullptr,std::memory_order_acq_rel);}
138 
139  template<typename T>
141  T* tmp = m_data.exchange(nullptr);
142  return tmp;
143  }
144 }
145 #endif
T const & operator*() const
void reset()
unsets the value and deletes the memory
bool isSet() const
AtomicPtrCache & operator=(const AtomicPtrCache< T > &)
bool set(std::unique_ptr< T > iNewValue) const
T const * operator->() const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::atomic< T * > m_data
long double T
T const * load() const