CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Attributes
edm::AtomicPtrCache< T > Class Template Reference

#include "DataFormats/Common/interface/AtomicPtrCache.h"

Public Member Functions

 AtomicPtrCache ()
 
 AtomicPtrCache (T *)
 Takes exclusive ownership of the value. More...
 
 AtomicPtrCache (const AtomicPtrCache< T > &)
 Uses T's copy constructor to make a copy. More...
 
bool isSet () const
 
T const * load () const
 
Tload ()
 
T const & operator* () const
 
Toperator* ()
 
T const * operator-> () const
 
Toperator-> ()
 
AtomicPtrCacheoperator= (const AtomicPtrCache< T > &)
 
Trelease ()
 
void reset ()
 unsets the value and deletes the memory More...
 
bool set (std::unique_ptr< T > iNewValue) const
 
 ~AtomicPtrCache ()
 

Private Attributes

std::atomic< T * > m_data
 

Detailed Description

template<typename T>
class edm::AtomicPtrCache< T >

Description: A thread safe cache managed by a pointer

Usage: Data products which need to cache results into non-trivial structures (e.g. an std::vector) can use this class to manage the cache in a thread-safe way. The thread-safety guarantee is only the standard C++, if calls are made to const functions simultaneously then everything is thread safe. Calling a non-const function while calling any other functions is not thread-safe.

This class also hides the std::atomic from ROOT so this class can safely be used in a stored class.

WARNING: member data which uses this class must be made transient in the classes_def.xml file!

Definition at line 40 of file AtomicPtrCache.h.

Constructor & Destructor Documentation

template<typename T >
edm::AtomicPtrCache< T >::AtomicPtrCache ( )
inline

Definition at line 90 of file AtomicPtrCache.h.

90 :m_data{nullptr} {}
std::atomic< T * > m_data
template<typename T>
edm::AtomicPtrCache< T >::AtomicPtrCache ( T iValue)
inlineexplicit

Takes exclusive ownership of the value.

Definition at line 93 of file AtomicPtrCache.h.

93 : m_data{iValue} {}
std::atomic< T * > m_data
template<typename T>
edm::AtomicPtrCache< T >::AtomicPtrCache ( const AtomicPtrCache< T > &  iOther)
inline

Uses T's copy constructor to make a copy.

Definition at line 96 of file AtomicPtrCache.h.

96  :
97  m_data{nullptr}
98  {
99  auto ptr = iOther.m_data.load(std::memory_order_acquire);
100  if(ptr != nullptr) {
101  m_data.store( new T{*ptr}, std::memory_order_release);
102  }
103  }
std::atomic< T * > m_data
long double T
template<typename T >
edm::AtomicPtrCache< T >::~AtomicPtrCache ( )
inline

Definition at line 122 of file AtomicPtrCache.h.

122  {
123  delete m_data.load(std::memory_order_acquire);
124  }
std::atomic< T * > m_data

Member Function Documentation

template<typename T >
bool edm::AtomicPtrCache< T >::isSet ( ) const
inline

Definition at line 133 of file AtomicPtrCache.h.

Referenced by HcalGeometry::fillDetIds(), and HcalGeometry::getValidDetIds().

133 { return nullptr!=m_data.load(std::memory_order_acquire);}
std::atomic< T * > m_data
template<typename T >
T const * edm::AtomicPtrCache< T >::load ( ) const
inline
template<typename T >
T * edm::AtomicPtrCache< T >::load ( )
inline

Definition at line 127 of file AtomicPtrCache.h.

127 { return m_data.load(std::memory_order_acquire);}
std::atomic< T * > m_data
template<typename T>
T const& edm::AtomicPtrCache< T >::operator* ( ) const
inline

Definition at line 57 of file AtomicPtrCache.h.

57 {return *load(); }
T const * load() const
template<typename T>
T& edm::AtomicPtrCache< T >::operator* ( )
inline

Definition at line 70 of file AtomicPtrCache.h.

70 {return *load();}
T const * load() const
template<typename T>
T const* edm::AtomicPtrCache< T >::operator-> ( ) const
inline

Definition at line 56 of file AtomicPtrCache.h.

56 { return load();}
T const * load() const
template<typename T>
T* edm::AtomicPtrCache< T >::operator-> ( )
inline

Definition at line 69 of file AtomicPtrCache.h.

69 { return load();}
T const * load() const
template<typename T>
AtomicPtrCache< T > & edm::AtomicPtrCache< T >::operator= ( const AtomicPtrCache< T > &  iOther)
inline

Definition at line 105 of file AtomicPtrCache.h.

References edm::AtomicPtrCache< T >::m_data.

105  {
106  auto ptr = iOther.m_data.load(std::memory_order_acquire);
107  if(ptr != nullptr) {
108  auto ourPtr =m_data.load(std::memory_order_acquire);
109  if( ourPtr !=nullptr) {
110  *ourPtr = *ptr;
111  } else {
112  m_data.store( new T{*ptr}, std::memory_order_release);
113  }
114  } else {
115  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
116  }
117  return *this;
118  }
std::atomic< T * > m_data
long double T
template<typename T >
T * edm::AtomicPtrCache< T >::release ( )
inline

Definition at line 149 of file AtomicPtrCache.h.

References tmp.

Referenced by cuy.ValElement::__init__().

149  {
150  T* tmp = m_data.exchange(nullptr);
151  return tmp;
152  }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::atomic< T * > m_data
long double T
template<typename T >
void edm::AtomicPtrCache< T >::reset ( void  )
inline

unsets the value and deletes the memory

Definition at line 146 of file AtomicPtrCache.h.

Referenced by DTReadOutMapping::clear().

146 { delete m_data.exchange(nullptr,std::memory_order_acq_rel);}
std::atomic< T * > m_data
template<typename T>
bool edm::AtomicPtrCache< T >::set ( std::unique_ptr< T iNewValue) const
inline

returns true if actually was set. Will delete value held by iNewValue if not the first time set

Definition at line 136 of file AtomicPtrCache.h.

Referenced by DTReadOutMapping::cacheMap(), HcalGeometry::fillDetIds(), and betterConfigParser.BetterConfigParser::getGeneral().

136  {
137  bool retValue;
138  T* expected = nullptr;
139  if( (retValue = m_data.compare_exchange_strong(expected,iNewValue.get(), std::memory_order_acq_rel)) ) {
140  iNewValue.release();
141  }
142  return retValue;
143  }
std::atomic< T * > m_data
long double T

Member Data Documentation

template<typename T>
std::atomic<T*> edm::AtomicPtrCache< T >::m_data
mutableprivate

Definition at line 83 of file AtomicPtrCache.h.

Referenced by edm::AtomicPtrCache< T >::operator=().