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 > &)
 
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 88 of file AtomicPtrCache.h.

88 :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 91 of file AtomicPtrCache.h.

91 : 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 94 of file AtomicPtrCache.h.

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

Definition at line 120 of file AtomicPtrCache.h.

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

Member Function Documentation

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

Definition at line 128 of file AtomicPtrCache.h.

Referenced by edm::AtomicPtrCache< reco::TrackRefVector >::operator*(), and edm::AtomicPtrCache< reco::TrackRefVector >::operator->().

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

Definition at line 125 of file AtomicPtrCache.h.

125 { 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 103 of file AtomicPtrCache.h.

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

103  {
104  auto ptr = iOther.m_data.load(std::memory_order_acquire);
105  if(ptr != nullptr) {
106  auto ourPtr =m_data.load(std::memory_order_acquire);
107  if( ourPtr !=nullptr) {
108  *ourPtr = *ptr;
109  } else {
110  m_data.store( new T{*ptr}, std::memory_order_release);
111  }
112  } else {
113  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
114  }
115  return *this;
116  }
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 144 of file AtomicPtrCache.h.

Referenced by pat::Jet::setPFCandidates().

144 { 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 134 of file AtomicPtrCache.h.

Referenced by pat::Jet::cachePFCandidates(), betterConfigParser.BetterConfigParser::getGeneral(), pat::Tau::isolationPFCands(), pat::Tau::isolationPFChargedHadrCands(), pat::Tau::isolationPFGammaCands(), pat::Tau::isolationPFNeutrHadrCands(), pat::Tau::isolationTracks(), pat::Tau::signalPFCands(), pat::Tau::signalPFChargedHadrCands(), pat::Tau::signalPFGammaCands(), pat::Tau::signalPFNeutrHadrCands(), pat::Tau::signalTracks(), pat::Photon::superCluster(), and pat::Electron::superCluster().

134  {
135  bool retValue;
136  T* expected = nullptr;
137  if( (retValue = m_data.compare_exchange_strong(expected,iNewValue.get(), std::memory_order_acq_rel)) ) {
138  iNewValue.release();
139  }
140  return retValue;
141  }
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 81 of file AtomicPtrCache.h.

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