ATLAS Offline Software
Public Types | Public Member Functions | Private Attributes | List of all members
CxxUtils::CachedPointer< T > Union Template Reference

Cached pointer with atomic update. More...

#include <CachedPointer.h>

Collaboration diagram for CxxUtils::CachedPointer< T >:

Public Types

typedef const T * pointer_t
 The stored pointer type. More...
 

Public Member Functions

 CachedPointer ()
 Default constructor. Sets the element to null. More...
 
 CachedPointer (pointer_t elt)
 Constructor from an element. More...
 
 CachedPointer (const CachedPointer &other) noexcept
 Copy constructor. More...
 
 CachedPointer (CachedPointer &&other) noexcept
 Move constructor. More...
 
CachedPointeroperator= (const CachedPointer &other)
 Assignment. More...
 
void set (pointer_t elt) const
 Set the element, assuming it is currently null. More...
 
void store (pointer_t elt)
 Store a new value to the element. More...
 
pointer_t get () const
 Return the current value of the element. More...
 
const pointer_tptr () const
 Return a pointer to the cached element. More...
 

Private Attributes

std::atomic< pointer_tm_a
 The cached element, both directly and as an atomic (recall that this is a union). More...
 
pointer_t m_e
 Transient. More...
 

Detailed Description

template<class T>
union CxxUtils::CachedPointer< T >

Cached pointer with atomic update.

The pointer may be set atomically through a const method. This can happen in multiple threads, but the value set should be the same in all threads. (The value can also be set using the non-const store() method; however, this is not atomic and should not be done while other threads may be accessing the value.)

In principle, we need this to be atomic, since this cached value can be changed from a const method. However, because any access should always fill this with the same value, we don't care about ordering, only that the access itself is atomic in the sense of reading/writing the entire object at once. Hence, we can use a relaxed memory ordering for accesses to the value. A further wrinkle is that the use case in AthLinks requires that we return a pointer to the value here; there doesn't seem to be a way around that without either changing how links are used or storing additional data here. It should, however, be ok from a data race point of view because we ensure that we don't return a pointer until after it's set, and also that after it's set it isn't written again (from a const method). There is however, no portable way of getting a pointer to the underlying value of a std::atomic, so here we cheat and use a union. As an added check, we require that the compiler say that atomic pointers are always lock-free.

Definition at line 57 of file CachedPointer.h.

Member Typedef Documentation

◆ pointer_t

template<class T >
typedef const T* CxxUtils::CachedPointer< T >::pointer_t

The stored pointer type.

Definition at line 61 of file CachedPointer.h.

Constructor & Destructor Documentation

◆ CachedPointer() [1/4]

template<class T >
CxxUtils::CachedPointer< T >::CachedPointer ( )

Default constructor. Sets the element to null.

◆ CachedPointer() [2/4]

template<class T >
CxxUtils::CachedPointer< T >::CachedPointer ( pointer_t  elt)

Constructor from an element.

◆ CachedPointer() [3/4]

template<class T >
CxxUtils::CachedPointer< T >::CachedPointer ( const CachedPointer< T > &  other)
noexcept

Copy constructor.

◆ CachedPointer() [4/4]

template<class T >
CxxUtils::CachedPointer< T >::CachedPointer ( CachedPointer< T > &&  other)
noexcept

Move constructor.

Member Function Documentation

◆ get()

template<class T >
pointer_t CxxUtils::CachedPointer< T >::get ( ) const

Return the current value of the element.

◆ operator=()

template<class T >
CachedPointer& CxxUtils::CachedPointer< T >::operator= ( const CachedPointer< T > &  other)

Assignment.

◆ ptr()

template<class T >
const pointer_t* CxxUtils::CachedPointer< T >::ptr ( ) const

Return a pointer to the cached element.

◆ set()

template<class T >
void CxxUtils::CachedPointer< T >::set ( pointer_t  elt) const

Set the element, assuming it is currently null.

◆ store()

template<class T >
void CxxUtils::CachedPointer< T >::store ( pointer_t  elt)

Store a new value to the element.

Member Data Documentation

◆ m_a

template<class T >
std::atomic<pointer_t> CxxUtils::CachedPointer< T >::m_a
mutableprivate

The cached element, both directly and as an atomic (recall that this is a union).

Definition at line 101 of file CachedPointer.h.

◆ m_e

template<class T >
pointer_t CxxUtils::CachedPointer< T >::m_e
private

Transient.

Definition at line 102 of file CachedPointer.h.


The documentation for this union was generated from the following file: