ATLAS Offline Software
TrigCostDataStore.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "AthenaBaseComps/AthCheckMacros.h"
6 
7 /////////////////////////////////////////////////////////////////////////////
8 
9 template<typename PAYLOAD>
10 StatusCode TrigCostDataStore<PAYLOAD>::initialize(size_t nSlots) {
11  m_store.resize( nSlots, tbb::concurrent_hash_map< AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>() );
12  return StatusCode::SUCCESS;
13 }
14 
15 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
16 
17 template<typename PAYLOAD>
18 StatusCode TrigCostDataStore<PAYLOAD>::insert(const AlgorithmIdentifier& ai, const PAYLOAD& payload,
19  MsgStream& msg) {
20  ATH_CHECK( checkSlot(ai.m_slotToSaveInto, msg) );
21  tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>& mapReference = m_store.at( ai.m_slotToSaveInto );
22  typename tbb::concurrent_hash_map< AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>::accessor acc;
23  if (mapReference.insert(acc, ai)) {
24  // Obtains lock on the key value 'name' until 'acc' goes out of scope or calls release()
25  acc->second = payload;
26  } else if (msg.level() <= MSG::DEBUG) {
27  msg << MSG::DEBUG << "Key caller:'" << ai.m_caller << "' store:'" << ai.m_store
28  << "' slot:" << ai.m_realSlot <<" already in the TrigCostDataStore" << endmsg;
29  }
30  return StatusCode::SUCCESS;
31 }
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
34 
35 template<typename PAYLOAD>
36 template<typename ENTRY>
37 StatusCode TrigCostDataStore<PAYLOAD>::push_back(const AlgorithmIdentifier& ai, ENTRY&& entry,
38  MsgStream& msg) {
39  ATH_CHECK( checkSlot(ai.m_slotToSaveInto, msg) );
40  tbb::concurrent_hash_map<AlgorithmIdentifier, std::vector<ENTRY>, AlgorithmIdentifierHashCompare>& mapReference = m_store.at( ai.m_slotToSaveInto );
41  typename tbb::concurrent_hash_map< AlgorithmIdentifier, std::vector<ENTRY>, AlgorithmIdentifierHashCompare>::accessor acc;
42 
43  // Adds new value to store entries. If the AI is already there function will return false
44  // and duplicate will not be added. Otherwise new payload will be created
45  // Also obtains write lock on the key value 'name' until 'acc' goes out of scope
46  mapReference.insert(acc, ai);
47  acc->second.push_back(std::move(entry));
48 
49  return StatusCode::SUCCESS;
50 }
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
53 
54 template<typename PAYLOAD>
55 StatusCode TrigCostDataStore<PAYLOAD>::retrieve(const AlgorithmIdentifier& ai,
56  typename tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>::const_accessor& payload,
57  MsgStream& msg) const
58 {
59  ATH_CHECK( checkSlot(ai.m_slotToSaveInto, msg) );
60  const tbb::concurrent_hash_map< AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>& mapReference = m_store.at( ai.m_slotToSaveInto );
61  if (!mapReference.find(payload, ai)) {
62  if (msg.level() <= MSG::DEBUG) {
63  msg << MSG::DEBUG << "Cannot access key caller:'" << ai.m_caller << "' store:'" << ai.m_store
64  << "' slot:" << ai.m_realSlot << " from the the TrigCostDataStore" << endmsg;
65  }
66  return StatusCode::FAILURE;
67  }
68  return StatusCode::SUCCESS;
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
73 
74 template<typename PAYLOAD>
75 StatusCode TrigCostDataStore<PAYLOAD>::clear(const EventContext& context, MsgStream& msg) {
76  ATH_CHECK( checkSlot(context.slot(), msg) );
77  tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>& mapReference = m_store.at( context.slot() );
78  mapReference.clear();
79  if (msg.level() <= MSG::DEBUG) msg << MSG::DEBUG << "Clearing slot " << context.slot() << endmsg;
80  return StatusCode::SUCCESS;
81 }
82 
83 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
84 
85 template<typename PAYLOAD>
86 StatusCode TrigCostDataStore<PAYLOAD>::getIterators(const EventContext& context, MsgStream& msg,
87  typename tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>::const_iterator& begin,
88  typename tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>::const_iterator& end) {
89  ATH_CHECK( checkSlot(context.slot(), msg) );
90  const tbb::concurrent_hash_map<AlgorithmIdentifier, PAYLOAD, AlgorithmIdentifierHashCompare>& mapReference = m_store.at( context.slot() );
91  begin = mapReference.begin();
92  end = mapReference.end();
93  return StatusCode::SUCCESS;
94 }
95 
96 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
97 
98 template<typename PAYLOAD>
99 StatusCode TrigCostDataStore<PAYLOAD>::checkSlot(const size_t slot, MsgStream& msg) const {
100  if (m_store.size() <= slot) {
101  msg << MSG::FATAL << "Requesting slot " << slot << " but we only reserved for " << m_store.size() << " slots. Make sure setSlots() was called." << endmsg;
102  return StatusCode::FAILURE;
103  }
104  return StatusCode::SUCCESS;
105 }
106