ATLAS Offline Software
Loading...
Searching...
No Matches
G4RunToolEventQueue Class Reference

Thread-safe event handoff between Athena and the Geant4 worker threads. More...

#include <G4RunToolEventQueue.h>

Collaboration diagram for G4RunToolEventQueue:

Public Types

using UPEvent = std::unique_ptr<AtlasG4SyncEventUserInfo>

Public Member Functions

std::size_t Size () const
void PushEvent (UPEvent event)
 Submit a non-null event. Throws std::invalid_argument for null events.
UPEvent GetEvent ()
void Close () noexcept
 Stop accepting events and wake all Geant4 workers. Idempotent.
void CompleteOutstandingEvents () noexcept
 Release abandoned events and report run termination to their waiters.

Private Types

using WeakSyncInterface

Private Attributes

std::queue< UPEventm_events
std::vector< WeakSyncInterfacem_outstandingEvents
bool m_closed {false}
std::mutex m_mutex
std::condition_variable m_cv

Detailed Description

Thread-safe event handoff between Athena and the Geant4 worker threads.

Events submitted while the queue is open are tracked until their Athena waiters have been notified. Closing the queue wakes every blocked Geant4 worker; subsequent GetEvent() calls return nullptr so that SyncEventAction aborts the run. Once the workers have stopped, CompleteOutstandingEvents() releases any abandoned events and wakes their Athena waiters.

Definition at line 24 of file G4RunToolEventQueue.h.

Member Typedef Documentation

◆ UPEvent

Definition at line 27 of file G4RunToolEventQueue.h.

◆ WeakSyncInterface

Initial value:
std::weak_ptr<G4EventSynchronizationInterface>

Definition at line 42 of file G4RunToolEventQueue.h.

Member Function Documentation

◆ Close()

void G4RunToolEventQueue::Close ( )
noexcept

Stop accepting events and wake all Geant4 workers. Idempotent.

Definition at line 69 of file G4RunToolEventQueue.cxx.

70{
71 {
72 std::scoped_lock lock(m_mutex);
73 m_closed = true;
74 }
75 m_cv.notify_all();
76}
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
std::condition_variable m_cv

◆ CompleteOutstandingEvents()

void G4RunToolEventQueue::CompleteOutstandingEvents ( )
noexcept

Release abandoned events and report run termination to their waiters.

Must be called only after all Geant4 workers have stopped. Idempotent.

Definition at line 78 of file G4RunToolEventQueue.cxx.

79{
80 std::vector<WeakSyncInterface> outstandingEvents;
81 std::queue<UPEvent> abandonedEvents;
82 {
83 std::scoped_lock lock(m_mutex);
84 outstandingEvents.swap(m_outstandingEvents);
85 abandonedEvents.swap(m_events);
86 }
87
88 // Destroy queued event information before waking Athena. In-flight event
89 // information has already been destroyed as part of worker termination.
90 while (!abandonedEvents.empty()) {
91 abandonedEvents.pop();
92 }
93
94 for (const WeakSyncInterface& weakInterface : outstandingEvents) {
95 if (auto syncInterface = weakInterface.lock()) {
96 syncInterface->Complete(
98 }
99 }
100}
std::queue< UPEvent > m_events
std::weak_ptr< G4EventSynchronizationInterface > WeakSyncInterface
std::vector< WeakSyncInterface > m_outstandingEvents

◆ GetEvent()

auto G4RunToolEventQueue::GetEvent ( )

Definition at line 56 of file G4RunToolEventQueue.cxx.

57{
58 std::unique_lock lock(m_mutex);
59 m_cv.wait(lock, [this] { return m_closed || !m_events.empty(); });
60 if (m_closed) {
61 return nullptr;
62 }
63
64 UPEvent event = std::move(m_events.front());
65 m_events.pop();
66 return event;
67}
std::unique_ptr< AtlasG4SyncEventUserInfo > UPEvent

◆ PushEvent()

void G4RunToolEventQueue::PushEvent ( UPEvent event)

Submit a non-null event. Throws std::invalid_argument for null events.

Definition at line 17 of file G4RunToolEventQueue.cxx.

18{
19 if (!event) {
20 throw std::invalid_argument(
21 "G4RunToolEventQueue::PushEvent requires a non-null event");
22 }
23
24 auto syncInterface = event->SyncInterface();
25 bool eventQueued = false;
26 {
27 std::scoped_lock lock(m_mutex);
28 if (!m_closed) {
32 [](const WeakSyncInterface& interface) {
33 return interface.expired();
34 }),
36 if (syncInterface) {
37 m_outstandingEvents.emplace_back(syncInterface);
38 }
39 m_events.push(std::move(event));
40 eventQueued = true;
41 }
42 }
43
44 if (eventQueued) {
45 m_cv.notify_one();
46 }
47 else if (syncInterface) {
48 // The event was never exposed to Geant4, so its owned state can be safely
49 // released before the Athena thread is notified.
50 event.reset();
51 syncInterface->Complete(
53 }
54}
DataModel_detail::iterator< DVL > remove_if(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, Predicate pred)
Specialization of remove_if for DataVector/List.

◆ Size()

std::size_t G4RunToolEventQueue::Size ( ) const

Definition at line 11 of file G4RunToolEventQueue.cxx.

12{
13 std::scoped_lock lock(m_mutex);
14 return m_events.size();
15}

Member Data Documentation

◆ m_closed

bool G4RunToolEventQueue::m_closed {false}
private

Definition at line 47 of file G4RunToolEventQueue.h.

47{false};

◆ m_cv

std::condition_variable G4RunToolEventQueue::m_cv
private

Definition at line 49 of file G4RunToolEventQueue.h.

◆ m_events

std::queue<UPEvent> G4RunToolEventQueue::m_events
private

Definition at line 45 of file G4RunToolEventQueue.h.

◆ m_mutex

std::mutex G4RunToolEventQueue::m_mutex
mutableprivate

Definition at line 48 of file G4RunToolEventQueue.h.

◆ m_outstandingEvents

std::vector<WeakSyncInterface> G4RunToolEventQueue::m_outstandingEvents
private

Definition at line 46 of file G4RunToolEventQueue.h.


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