ATLAS Offline Software
ThreadActionHolder.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef G4ATLASTOOLS__G4UA_THREADACTIONHOLDER_H
6 #define G4ATLASTOOLS__G4UA_THREADACTIONHOLDER_H
7 
8 // System includes
9 #include <thread>
10 #include <memory>
11 #include <utility>
12 
13 // Other includes
14 #include "tbb/concurrent_unordered_map.h"
15 
16 namespace G4UA
17 {
18 
29  template<class ActionType>
31  {
32 
33  public:
34 
36  using ThreadMapVal_t = ActionType*;
37  //using ThreadMapVal_t = std::unique_ptr<ActionType>; // not supported
38  using ThreadMapHash_t = std::hash<ThreadMapKey_t>;
39 
40  using ThreadMap_t = tbb::concurrent_unordered_map
42 
43  using const_iterator = typename ThreadMap_t::const_iterator;
44 
48  for(auto mapPair : m_threadMap){
49  delete mapPair.second;
50  }
51  m_threadMap.clear();
52  }
53 
55  ActionType* get() {
56  auto mapPair = m_threadMap.find( std::this_thread::get_id() );
57  if(mapPair == m_threadMap.end()) return nullptr;
58  return mapPair->second;
59  }
60 
63  void set(std::unique_ptr<ActionType> action) {
64  const auto tid = std::this_thread::get_id();
65  m_threadMap.insert( std::make_pair(tid, action.release()) );
66  }
67 
70  return m_threadMap.begin();
71  }
72 
74  const_iterator end() const {
75  return m_threadMap.end();
76  }
77 
78  private:
79 
82 
83  }; // class ThreadActionHolder
84 
85 }
86 
87 #endif
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::ThreadActionHolder::begin
const_iterator begin() const
Constant-access iteration over the action map.
Definition: ThreadActionHolder.h:69
G4UA::ThreadActionHolder< G4UA::G4AtlasEventAction >::ThreadMap_t
tbb::concurrent_unordered_map< ThreadMapKey_t, ThreadMapVal_t, ThreadMapHash_t > ThreadMap_t
Definition: ThreadActionHolder.h:41
G4UA::ThreadActionHolder::~ThreadActionHolder
~ThreadActionHolder()
Destructor will clean up the thread-local storage.
Definition: ThreadActionHolder.h:47
G4UA::ThreadActionHolder::set
void set(std::unique_ptr< ActionType > action)
Assign the object of the current thread.
Definition: ThreadActionHolder.h:63
G4UA::ThreadActionHolder< G4UA::G4AtlasEventAction >::const_iterator
typename ThreadMap_t::const_iterator const_iterator
Definition: ThreadActionHolder.h:43
G4UA::ThreadActionHolder< G4UA::G4AtlasEventAction >::ThreadMapKey_t
std::thread::id ThreadMapKey_t
Definition: ThreadActionHolder.h:35
G4UA::ThreadActionHolder::end
const_iterator end() const
Constant-access iteration over the action map.
Definition: ThreadActionHolder.h:74
G4UA::ThreadActionHolder< G4UA::G4AtlasEventAction >::ThreadMapHash_t
std::hash< ThreadMapKey_t > ThreadMapHash_t
Definition: ThreadActionHolder.h:38
G4UA::ThreadActionHolder::ThreadMapVal_t
ActionType * ThreadMapVal_t
Definition: ThreadActionHolder.h:36
G4UA::ThreadActionHolder
A thread-local storage wrapper for the user actions.
Definition: ThreadActionHolder.h:31
G4UA::G4AtlasEventAction
ATLAS subclass of the G4 event action.
Definition: G4AtlasEventAction.h:29
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
G4UA::ThreadActionHolder::get
ActionType * get()
Get the object of the current thread.
Definition: ThreadActionHolder.h:55
G4UA::ThreadActionHolder::m_threadMap
ThreadMap_t m_threadMap
The wrapped thread-local storage container.
Definition: ThreadActionHolder.h:81
python.CaloScaleNoiseConfig.action
action
Definition: CaloScaleNoiseConfig.py:77