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

Thread-safe lifecycle state shared by the Athena and Geant4 main threads. More...

#include <G4RunToolStateSynchronization.h>

Collaboration diagram for G4RunToolStateSynchronization:

Public Types

enum class  Status {
  Starting , Running , FinalizeRequested , Failed ,
  Stopped
}

Public Member Functions

void NotifyBeginRun ()
 Transition Starting to Running. Ignored in every other state.
void RequestFinalize ()
 Transition Starting or Running to FinalizeRequested.
void Fail (std::string message)
 Transition any non-terminal state to Failed and wake startup waiters.
void NotifyThreadExit ()
 Record normal exit after finalization, or failure after an unexpected exit.
bool WaitBeginRun (std::string &failureMessage)
 Wait until startup succeeds or the lifecycle reaches a terminal condition.
bool StopRequested () const
 Return whether the Geant4 run loop must stop.

Private Attributes

Status m_status {Status::Starting}
std::string m_failureMessage
std::mutex m_mutex
std::condition_variable m_cv

Detailed Description

Thread-safe lifecycle state shared by the Athena and Geant4 main threads.

The normal lifecycle is:

Starting –NotifyBeginRun()--> Running Starting/Running –RequestFinalize()--> FinalizeRequested FinalizeRequested –NotifyThreadExit()--> Stopped

A failure before normal thread exit instead transitions Starting, Running or FinalizeRequested to Failed. An unexpected thread exit while Starting or Running is also treated as a failure. Failed and Stopped are terminal states; late BeginOfRun, finalize and failure notifications cannot leave them.

Definition at line 24 of file G4RunToolStateSynchronization.h.

Member Enumeration Documentation

◆ Status

Enumerator
Starting 

Waiting for the first Geant4 BeginOfRun notification.

Running 

BeginOfRun was observed; Athena may submit events.

FinalizeRequested 

Athena requested termination of the Geant4 run loop.

Failed 

The Geant4 main thread failed; terminal state.

Stopped 

The Geant4 main thread exited normally; terminal state.

Definition at line 27 of file G4RunToolStateSynchronization.h.

27 {
28 Starting,
29 Running,
30 FinalizeRequested,
31 Failed,
32 Stopped
33 };

Member Function Documentation

◆ Fail()

void G4RunToolStateSynchronization::Fail ( std::string message)

Transition any non-terminal state to Failed and wake startup waiters.

Definition at line 31 of file G4RunToolStateSynchronization.cxx.

32{
33 {
34 std::scoped_lock lk(m_mutex);
35 // Preserve the first terminal outcome and its diagnostic message.
37 m_failureMessage = std::move(message);
39 }
40 }
41 m_cv.notify_all();
42}
@ Stopped
The Geant4 main thread exited normally; terminal state.
@ Failed
The Geant4 main thread failed; terminal state.

◆ NotifyBeginRun()

void G4RunToolStateSynchronization::NotifyBeginRun ( )

Transition Starting to Running. Ignored in every other state.

Definition at line 9 of file G4RunToolStateSynchronization.cxx.

10{
11 {
12 std::scoped_lock lk(m_mutex);
15 }
16 }
17 m_cv.notify_all();
18}
@ Running
BeginOfRun was observed; Athena may submit events.
@ Starting
Waiting for the first Geant4 BeginOfRun notification.

◆ NotifyThreadExit()

void G4RunToolStateSynchronization::NotifyThreadExit ( )

Record normal exit after finalization, or failure after an unexpected exit.

Definition at line 44 of file G4RunToolStateSynchronization.cxx.

45{
46 {
47 std::scoped_lock lk(m_mutex);
49 if (m_failureMessage.empty()) {
51 "Geant4 main thread exited without completing its lifecycle";
52 }
54 }
57 }
58 }
59 m_cv.notify_all();
60}
@ FinalizeRequested
Athena requested termination of the Geant4 run loop.

◆ RequestFinalize()

void G4RunToolStateSynchronization::RequestFinalize ( )

Transition Starting or Running to FinalizeRequested.

Definition at line 20 of file G4RunToolStateSynchronization.cxx.

21{
22 {
23 std::scoped_lock lk(m_mutex);
26 }
27 }
28 m_cv.notify_all();
29}

◆ StopRequested()

bool G4RunToolStateSynchronization::StopRequested ( ) const

Return whether the Geant4 run loop must stop.

Definition at line 78 of file G4RunToolStateSynchronization.cxx.

79{
80 std::scoped_lock lk(m_mutex);
84}

◆ WaitBeginRun()

bool G4RunToolStateSynchronization::WaitBeginRun ( std::string & failureMessage)

Wait until startup succeeds or the lifecycle reaches a terminal condition.

Definition at line 62 of file G4RunToolStateSynchronization.cxx.

64{
65 std::unique_lock lk(m_mutex);
66 m_cv.wait(lk, [this]{ return m_status != Status::Starting; });
68 return true;
69 }
70
71 failureMessage = m_failureMessage;
72 if (failureMessage.empty()) {
73 failureMessage = "Geant4 stopped before beginning a run";
74 }
75 return false;
76}

Member Data Documentation

◆ m_cv

std::condition_variable G4RunToolStateSynchronization::m_cv
private

Definition at line 52 of file G4RunToolStateSynchronization.h.

◆ m_failureMessage

std::string G4RunToolStateSynchronization::m_failureMessage
private

Definition at line 50 of file G4RunToolStateSynchronization.h.

◆ m_mutex

std::mutex G4RunToolStateSynchronization::m_mutex
mutableprivate

Definition at line 51 of file G4RunToolStateSynchronization.h.

◆ m_status

Status G4RunToolStateSynchronization::m_status {Status::Starting}
private

Definition at line 49 of file G4RunToolStateSynchronization.h.


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