ATLAS Offline Software
Loading...
Searching...
No Matches
G4UA::ThreadSpecificUserAction< ActionType > Class Template Reference

A thread-local storage wrapper for the user actions. More...

#include <ThreadSpecificUserAction.h>

Collaboration diagram for G4UA::ThreadSpecificUserAction< ActionType >:

Public Types

using ThreadMapKey_t = std::thread::id
using ThreadMapVal_t = ActionType*
using ThreadMapHash_t = std::hash<ThreadMapKey_t>
using ThreadMap_t
using const_iterator = typename ThreadMap_t::const_iterator

Public Member Functions

 ~ThreadSpecificUserAction ()
 Destructor will clean up the thread-local storage.
ActionType * get ()
 Get the object of the current thread.
void set (std::unique_ptr< ActionType > action)
 Assign the object of the current thread.
const_iterator begin () const
 Constant-access iteration over the action map.
const_iterator end () const
 Constant-access iteration over the action map.
template<class ResultType, class Mapper, class Reducer>
void accumulate (ResultType &result, Mapper mapOp, Reducer reduceOp)
 Accumulate results across user actions with specified operations.

Private Attributes

ThreadMap_t m_threadMap
 The wrapped thread-local storage container.

Detailed Description

template<class ActionType>
class G4UA::ThreadSpecificUserAction< ActionType >

A thread-local storage wrapper for the user actions.

This container is implemented as a wrapper for a concurrent map keyed by std thread ID. It is thus fully thread-safe (in theory).

The thread-local storage is cleared in the destructor for now, at least until TBB's concurrent containers support move semantics and unique_ptr.

Author
Steve Farrell Steve.nosp@m.n.Fa.nosp@m.rrell.nosp@m.@cer.nosp@m.n.ch

Definition at line 30 of file ThreadSpecificUserAction.h.

Member Typedef Documentation

◆ const_iterator

template<class ActionType>
using G4UA::ThreadSpecificUserAction< ActionType >::const_iterator = typename ThreadMap_t::const_iterator

Definition at line 41 of file ThreadSpecificUserAction.h.

◆ ThreadMap_t

template<class ActionType>
using G4UA::ThreadSpecificUserAction< ActionType >::ThreadMap_t
Initial value:
tbb::concurrent_unordered_map
std::hash< ThreadMapKey_t > ThreadMapHash_t

Definition at line 39 of file ThreadSpecificUserAction.h.

◆ ThreadMapHash_t

template<class ActionType>
using G4UA::ThreadSpecificUserAction< ActionType >::ThreadMapHash_t = std::hash<ThreadMapKey_t>

Definition at line 38 of file ThreadSpecificUserAction.h.

◆ ThreadMapKey_t

template<class ActionType>
using G4UA::ThreadSpecificUserAction< ActionType >::ThreadMapKey_t = std::thread::id

Definition at line 35 of file ThreadSpecificUserAction.h.

◆ ThreadMapVal_t

template<class ActionType>
using G4UA::ThreadSpecificUserAction< ActionType >::ThreadMapVal_t = ActionType*

Definition at line 36 of file ThreadSpecificUserAction.h.

Constructor & Destructor Documentation

◆ ~ThreadSpecificUserAction()

template<class ActionType>
G4UA::ThreadSpecificUserAction< ActionType >::~ThreadSpecificUserAction ( )
inline

Destructor will clean up the thread-local storage.

Would prefer to do this automatically with unique_ptr.

Definition at line 45 of file ThreadSpecificUserAction.h.

45 {
46 for(auto& mapPair : m_threadMap){
47 delete mapPair.second;
48 }
49 m_threadMap.clear();
50 }
A thread-local storage wrapper for the user actions.
ThreadMap_t m_threadMap
The wrapped thread-local storage container.

Member Function Documentation

◆ accumulate()

template<class ActionType>
template<class ResultType, class Mapper, class Reducer>
void G4UA::ThreadSpecificUserAction< ActionType >::accumulate ( ResultType & result,
Mapper mapOp,
Reducer reduceOp )
inline

Accumulate results across user actions with specified operations.

Takes an initial result object to accumulate into and two function-like objects:

  • a mapper gets a result type from each action
  • a reducer merges results together

The operations get wrapped in std::function which allows for some constrained flexibility while avoiding difficulties in template parameter determination in the arguments.

Definition at line 88 of file ThreadSpecificUserAction.h.

89 {
90 // Wrapping the ops in std::function allows for some constrained
91 // flexibility and avoids difficult template determination in the args.
94 // Loop over user actions and apply the functions
95 for(const auto& keyVal : m_threadMap) {
96 reducer( result, mapper(*keyVal.second) );
97 }
98 }

◆ begin()

template<class ActionType>
const_iterator G4UA::ThreadSpecificUserAction< ActionType >::begin ( ) const
inline

Constant-access iteration over the action map.

Definition at line 67 of file ThreadSpecificUserAction.h.

67 {
68 return m_threadMap.begin();
69 }

◆ end()

template<class ActionType>
const_iterator G4UA::ThreadSpecificUserAction< ActionType >::end ( ) const
inline

Constant-access iteration over the action map.

Definition at line 72 of file ThreadSpecificUserAction.h.

72 {
73 return m_threadMap.end();
74 }

◆ get()

template<class ActionType>
ActionType * G4UA::ThreadSpecificUserAction< ActionType >::get ( )
inline

Get the object of the current thread.

Definition at line 53 of file ThreadSpecificUserAction.h.

53 {
55 if(mapItr == m_threadMap.end()) return nullptr;
56 return mapItr->second;
57 }

◆ set()

template<class ActionType>
void G4UA::ThreadSpecificUserAction< ActionType >::set ( std::unique_ptr< ActionType > action)
inline

Assign the object of the current thread.

Memory management must be handled manually for now.

Definition at line 61 of file ThreadSpecificUserAction.h.

61 {
62 const auto tid = std::this_thread::get_id();
63 m_threadMap.insert( std::make_pair(tid, action.release()) );
64 }

Member Data Documentation

◆ m_threadMap

template<class ActionType>
ThreadMap_t G4UA::ThreadSpecificUserAction< ActionType >::m_threadMap
private

The wrapped thread-local storage container.

Definition at line 103 of file ThreadSpecificUserAction.h.


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