CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 78 of file AtomicPtrCache.h.

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

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

84  : m_data{nullptr} {
85  auto ptr = iOther.m_data.load(std::memory_order_acquire);
86  if (ptr != nullptr) {
87  m_data.store(new T{*ptr}, std::memory_order_release);
88  }
89  }
std::atomic< T * > m_data
long double T
template<typename T >
edm::AtomicPtrCache< T >::~AtomicPtrCache ( )
inline

Definition at line 107 of file AtomicPtrCache.h.

107  {
108  delete m_data.load(std::memory_order_acquire);
109  }
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
template<typename T >
T * edm::AtomicPtrCache< T >::load ( )
inline

Definition at line 112 of file AtomicPtrCache.h.

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

Definition at line 53 of file AtomicPtrCache.h.

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

Definition at line 64 of file AtomicPtrCache.h.

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

Definition at line 52 of file AtomicPtrCache.h.

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

Definition at line 63 of file AtomicPtrCache.h.

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

Definition at line 91 of file AtomicPtrCache.h.

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

91  {
92  auto ptr = iOther.m_data.load(std::memory_order_acquire);
93  if (ptr != nullptr) {
94  auto ourPtr = m_data.load(std::memory_order_acquire);
95  if (ourPtr != nullptr) {
96  *ourPtr = *ptr;
97  } else {
98  m_data.store(new T{*ptr}, std::memory_order_release);
99  }
100  } else {
101  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
102  }
103  return *this;
104  }
std::atomic< T * > m_data
long double T
template<typename T >
T * edm::AtomicPtrCache< T >::release ( )
inline

Definition at line 142 of file AtomicPtrCache.h.

References createJobs::tmp.

142  {
143  T* tmp = m_data.exchange(nullptr);
144  return tmp;
145  }
std::atomic< T * > m_data
tmp
align.sh
Definition: createJobs.py:716
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(), and pat::Jet::clearDaughters().

137  {
138  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
139  }
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(), reco::PFTau::isolationPFCands(), reco::PFTau::isolationPFChargedHadrCands(), reco::PFTau::isolationPFGammaCands(), reco::PFTau::isolationPFNeutrHadrCands(), reco::PFTau::signalPFCands(), reco::PFTau::signalPFChargedHadrCands(), reco::PFTau::signalPFGammaCands(), and reco::PFTau::signalPFNeutrHadrCands().

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

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