ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Simulation
G4Utilities
G4RunManagement
src
G4RunToolEventQueue.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#include "
G4RunToolEventQueue.h
"
6
7
#include <algorithm>
8
#include <stdexcept>
9
#include <utility>
10
11
std::size_t
G4RunToolEventQueue::Size
()
const
12
{
13
std::scoped_lock
lock
(
m_mutex
);
14
return
m_events
.size();
15
}
16
17
void
G4RunToolEventQueue::PushEvent
(
UPEvent
event)
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
) {
29
m_outstandingEvents
.erase(
30
std::remove_if
(
m_outstandingEvents
.begin(),
31
m_outstandingEvents
.end(),
32
[](
const
WeakSyncInterface
& interface) {
33
return interface.expired();
34
}),
35
m_outstandingEvents
.end());
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(
52
G4EventSynchronizationInterface::EventOutcome::RunTerminated
);
53
}
54
}
55
56
auto
G4RunToolEventQueue::GetEvent
() ->
UPEvent
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
}
68
69
void
G4RunToolEventQueue::Close
() noexcept
70
{
71
{
72
std::scoped_lock
lock
(
m_mutex
);
73
m_closed
=
true
;
74
}
75
m_cv
.notify_all();
76
}
77
78
void
G4RunToolEventQueue::CompleteOutstandingEvents
() noexcept
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(
97
G4EventSynchronizationInterface::EventOutcome::RunTerminated
);
98
}
99
}
100
}
G4RunToolEventQueue.h
lock
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
G4EventSynchronizationInterface::EventOutcome::RunTerminated
@ RunTerminated
Definition
AtlasG4SyncEventUserInfo.h:39
G4RunToolEventQueue::m_cv
std::condition_variable m_cv
Definition
G4RunToolEventQueue.h:49
G4RunToolEventQueue::PushEvent
void PushEvent(UPEvent event)
Submit a non-null event. Throws std::invalid_argument for null events.
Definition
G4RunToolEventQueue.cxx:17
G4RunToolEventQueue::m_events
std::queue< UPEvent > m_events
Definition
G4RunToolEventQueue.h:45
G4RunToolEventQueue::WeakSyncInterface
std::weak_ptr< G4EventSynchronizationInterface > WeakSyncInterface
Definition
G4RunToolEventQueue.h:42
G4RunToolEventQueue::CompleteOutstandingEvents
void CompleteOutstandingEvents() noexcept
Release abandoned events and report run termination to their waiters.
Definition
G4RunToolEventQueue.cxx:78
G4RunToolEventQueue::Size
std::size_t Size() const
Definition
G4RunToolEventQueue.cxx:11
G4RunToolEventQueue::m_closed
bool m_closed
Definition
G4RunToolEventQueue.h:47
G4RunToolEventQueue::UPEvent
std::unique_ptr< AtlasG4SyncEventUserInfo > UPEvent
Definition
G4RunToolEventQueue.h:27
G4RunToolEventQueue::m_mutex
std::mutex m_mutex
Definition
G4RunToolEventQueue.h:48
G4RunToolEventQueue::m_outstandingEvents
std::vector< WeakSyncInterface > m_outstandingEvents
Definition
G4RunToolEventQueue.h:46
G4RunToolEventQueue::Close
void Close() noexcept
Stop accepting events and wake all Geant4 workers. Idempotent.
Definition
G4RunToolEventQueue.cxx:69
G4RunToolEventQueue::GetEvent
UPEvent GetEvent()
Definition
G4RunToolEventQueue.cxx:56
std::remove_if
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.
Definition
DVL_algorithms.h:70
Generated on
for ATLAS Offline Software by
1.17.0