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 38 of file AtomicPtrCache.h.

Constructor & Destructor Documentation

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

Definition at line 81 of file AtomicPtrCache.h.

81 :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 84 of file AtomicPtrCache.h.

84 : 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 87 of file AtomicPtrCache.h.

87  :
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  }
std::atomic< T * > m_data
long double T
template<typename T >
edm::AtomicPtrCache< T >::~AtomicPtrCache ( )
inline

Definition at line 113 of file AtomicPtrCache.h.

113  {
114  delete m_data.load(std::memory_order_acquire);
115  }
std::atomic< T * > m_data

Member Function Documentation

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

Definition at line 124 of file AtomicPtrCache.h.

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

124 { 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 118 of file AtomicPtrCache.h.

118 { 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 55 of file AtomicPtrCache.h.

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

Definition at line 66 of file AtomicPtrCache.h.

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

Definition at line 54 of file AtomicPtrCache.h.

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

Definition at line 65 of file AtomicPtrCache.h.

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

Definition at line 96 of file AtomicPtrCache.h.

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

96  {
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  }
std::atomic< T * > m_data
long double T
template<typename T >
T * edm::AtomicPtrCache< T >::release ( )
inline

Definition at line 140 of file AtomicPtrCache.h.

References tmp.

Referenced by cuy.ValElement::__init__().

140  {
141  T* tmp = m_data.exchange(nullptr);
142  return tmp;
143  }
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 137 of file AtomicPtrCache.h.

Referenced by DTReadOutMapping::clear().

137 { 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 127 of file AtomicPtrCache.h.

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

127  {
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  }
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 78 of file AtomicPtrCache.h.

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