ATLAS Offline Software
|
Cached pointer with atomic update. More...
#include <CachedPointer.h>
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... | |
CachedPointer & | operator= (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_t * | ptr () const |
Return a pointer to the cached element. More... | |
Private Attributes | |
std::atomic< pointer_t > | m_a |
The cached element, both directly and as an atomic (recall that this is a union). More... | |
pointer_t | m_e |
Transient. More... | |
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.
typedef const T* CxxUtils::CachedPointer< T >::pointer_t |
The stored pointer type.
Definition at line 61 of file CachedPointer.h.
CxxUtils::CachedPointer< T >::CachedPointer | ( | ) |
Default constructor. Sets the element to null.
CxxUtils::CachedPointer< T >::CachedPointer | ( | pointer_t | elt | ) |
Constructor from an element.
|
noexcept |
Copy constructor.
|
noexcept |
Move constructor.
pointer_t CxxUtils::CachedPointer< T >::get | ( | ) | const |
Return the current value of the element.
CachedPointer& CxxUtils::CachedPointer< T >::operator= | ( | const CachedPointer< T > & | other | ) |
Assignment.
const pointer_t* CxxUtils::CachedPointer< T >::ptr | ( | ) | const |
Return a pointer to the cached element.
void CxxUtils::CachedPointer< T >::set | ( | pointer_t | elt | ) | const |
Set the element, assuming it is currently null.
void CxxUtils::CachedPointer< T >::store | ( | pointer_t | elt | ) |
Store a new value to the element.
|
mutableprivate |
The cached element, both directly and as an atomic (recall that this is a union).
Definition at line 101 of file CachedPointer.h.
|
private |
Transient.
Definition at line 102 of file CachedPointer.h.