Celeritas 0.7.0-3+develop.835656ac1
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
celeritas::StateDependent Class Referencefinal

Receive notifications when the Geant4 application state changes. More...

#include <StateDependent.hh>

Inheritance diagram for celeritas::StateDependent:
Inheritance graph
[legend]

Public Types

enum class  Mode { raw , lifecycle }
 How to dispatch Geant4 state changes.
 
enum class  LifecycleRole { global , local }
 Lifecycle cleanup scope owned by this monitor.
 

Public Member Functions

 StateDependent (LocalGeantStateChangeFunc cb, Mode mode, LifecycleRole lifecycle_role)
 Construct with a stream ID and state-change callback.
 
 StateDependent (LocalGeantStateChangeFunc cb)
 Construct locally in raw mode with a state-change callback.
 
 StateDependent (StateDependent const &)=delete
 
StateDependent & operator= (StateDependent const &)=delete
 
 StateDependent (StateDependent &&)=delete
 
StateDependent & operator= (StateDependent &&)=delete
 
G4bool Notify (G4ApplicationState state) final
 Handle a Geant4 state transition notification.
 
StreamId local_stream () const
 Stream that created this state dependent.
 

Detailed Description

Receive notifications when the Geant4 application state changes.

This thread-local wrapper observes Geant4 application state transitions and calls a shared user-provided function for the creating stream. Raw mode dispatches every encoded state transition; lifecycle mode dispatches the filtered run lifecycle used by automatic Celeritas setup.

Warning
The Geant4 memory semantics for this class are unusual.
  • The thread-local G4StateManager singleton keeps a pointer (via the G4VStateDependent base constructor) to this local instance and calls delete on it when it's deleted as the run manager shuts down. Therefore this class must be deleted before G4StateManager ends.
  • The base class destructor calls the thread-local G4StateManager without checking for validity, so the destructor of this class after deleting the run manager will also crash the code.
  • The thread-local pointer mapping means that this class must be deallocated on the thread in which it's created.

To bypass the first failure path, we use Notify to deregister ourselves when we see the run manager is about to exit or abort.

The owner must keep the instance on the creating thread and callbacks must not delete the active state-dependent object. In lifecycle mode this object should be owned explicitly, similarly to Geant4 UI messenger objects.

In Mode::lifecycle this class filters raw Geant4 state changes into the lifecycle transitions needed by Celeritas automatic setup: begin_run, end_run, and end_program.

Constructor & Destructor Documentation

◆ StateDependent()

celeritas::StateDependent::StateDependent ( LocalGeantStateChangeFunc  cb,
Mode  mode,
LifecycleRole  lifecycle_role 
)

Construct with a stream ID and state-change callback.

Note
The base class performs the actual registration.
We also store a pointer to the thread-local manager that registered this object so terminal notifications can deregister from the same manager.

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