CMS 3D CMS Logo

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 (const AtomicPtrCache< T > &)
 Uses T's copy constructor to make a copy. More...
 
 AtomicPtrCache (T *)
 Takes exclusive ownership of the value. More...
 
bool isSet () const
 
Tload ()
 
T const * load () const
 
Toperator* ()
 
T const & operator* () const
 
Toperator-> ()
 
T const * operator-> () const
 
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

◆ AtomicPtrCache() [1/3]

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

Definition at line 78 of file AtomicPtrCache.h.

78 : m_data{nullptr} {}

◆ AtomicPtrCache() [2/3]

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} {}

◆ AtomicPtrCache() [3/3]

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  }

◆ ~AtomicPtrCache()

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  }

Member Function Documentation

◆ isSet()

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

Definition at line 122 of file AtomicPtrCache.h.

122  {
123  return nullptr != m_data.load(std::memory_order_acquire);
124  }

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

◆ load() [1/2]

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  }

◆ load() [2/2]

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

◆ operator*() [1/2]

template<typename T>
T& edm::AtomicPtrCache< T >::operator* ( )
inline

Definition at line 64 of file AtomicPtrCache.h.

64 { return *load(); }

◆ operator*() [2/2]

template<typename T>
T const& edm::AtomicPtrCache< T >::operator* ( ) const
inline

Definition at line 53 of file AtomicPtrCache.h.

53 { return *load(); }

◆ operator->() [1/2]

template<typename T>
T* edm::AtomicPtrCache< T >::operator-> ( )
inline

Definition at line 63 of file AtomicPtrCache.h.

63 { return load(); }

◆ operator->() [2/2]

template<typename T>
T const* edm::AtomicPtrCache< T >::operator-> ( ) const
inline

Definition at line 52 of file AtomicPtrCache.h.

52 { return load(); }

◆ operator=()

template<typename T>
AtomicPtrCache< T > & edm::AtomicPtrCache< T >::operator= ( const AtomicPtrCache< T > &  iOther)
inline

Definition at line 91 of file AtomicPtrCache.h.

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  }

◆ release()

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

Definition at line 142 of file AtomicPtrCache.h.

142  {
143  T* tmp = m_data.exchange(nullptr);
144  return tmp;
145  }

Referenced by edm::AtomicPtrCache< std::vector< reco::PFRecoTauChargedHadron > >::set().

◆ reset()

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.

137  {
138  delete m_data.exchange(nullptr, std::memory_order_acq_rel);
139  }

Referenced by DTReadOutMapping::clear().

◆ set()

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.

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  }

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

Member Data Documentation

◆ m_data

template<typename T>
std::atomic<T*> edm::AtomicPtrCache< T >::m_data
mutableprivate
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
edm::AtomicPtrCache::load
T const * load() const
Definition: AtomicPtrCache.h:117
edm::AtomicPtrCache::m_data
std::atomic< T * > m_data
Definition: AtomicPtrCache.h:75
T
long double T
Definition: Basic3DVectorLD.h:48