ATLAS Offline Software
|
Helper for recycling objects from event to event. More...
#include <RecyclableDataObject.h>
Public Types | |
typedef tbb::concurrent_queue< DOBJ * > | queue_t |
Underlying queue type holding these objects. More... | |
Public Member Functions | |
template<typename... ARGS> | |
RecyclableDataObject (std::shared_ptr< queue_t > queue, ARGS &&... args) | |
Constructor. More... | |
virtual unsigned long | release () override |
DataObject release method. More... | |
Private Attributes | |
std::shared_ptr< queue_t > | m_queue |
The queue on which this object will be placed when it is recycled. More... | |
Helper for recycling objects from event to event.
In some cases, one wants to reuse already-allocated objects from event to event, rather than deleting and recreating them each time. This has typically been done for complicated objects, such as classes deriving from IdentfiableContainer.
Some care is obviously needed for this to work in a MT job. Rather than holding a pointer to a specific object from a class member, we instead maintain a concurrent_queue of pointers to objects. When we want an object, we get one from the queue; if the queue is empty, then we allocate a new one.
The object used should derive from DataObject
. We bump the reference count by 1 when the object is allocated so that it won't get deleted by StoreGate. We also override release()
so that when the reference count goes back to 1 it is ‘recycled’. When this happens, we call the recycle()
method on the object (which should probably be protected) and put the object back on the queue to be allocated again.
Example use:
DOBJ
should derive from DataObject
and should provide a member recycle()
.
Implementation note: one needs to be careful about the order in which objects are deleted during finalization. In particular, it is possible for a Gaudi component that holds a RecyclableDataQueue
as a member to be deleted while there are still associated data objects registered in the event store. To be robust against this, we allocate the actual queue object separately from RecyclableDataQueue
and manage it with shared_ptr
; RecyclableDataObject
then references the queue with a shared_ptr
.
Definition at line 87 of file RecyclableDataObject.h.
typedef tbb::concurrent_queue<DOBJ*> Athena::RecyclableDataObject< DOBJ >::queue_t |
Underlying queue type holding these objects.
Definition at line 91 of file RecyclableDataObject.h.
Athena::RecyclableDataObject< DOBJ >::RecyclableDataObject | ( | std::shared_ptr< queue_t > | queue, |
ARGS &&... | args | ||
) |
Constructor.
queue | Queue on which this object will be entered when it is released. |
args... | Additional arguments to pass to the DOBJ constructor. |
|
overridevirtual |
DataObject release method.
This overrides the release()
method of DataObject
. Once the reference count falls back to 1 (meaning that the object is no longer referenced from StoreGate), we recycle the object back to the queue.
|
private |
The queue on which this object will be placed when it is recycled.
Definition at line 115 of file RecyclableDataObject.h.