Celeritas 0.7+960a30e
Loading...
Searching...
No Matches
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 Member Functions

 StateDependent (LocalGeantStateChangeFunc cb)
 Construct with a stream ID and state-change callback.
 
 StateDependent (StateDependent const &)=delete
 
StateDependentoperator= (StateDependent const &)=delete
 
 StateDependent (StateDependent &&)=delete
 
StateDependentoperator= (StateDependent &&)=delete
 
G4bool Notify (G4ApplicationState state) final
 Dispatch a state transition notification to the user callback.
 
StreamId local_stream () const
 Stream that created this state dependent.
 

Detailed Description

Receive notifications when the Geant4 application state changes.

This thread-local wrapper calls a shared user-provided function for the given worker stream whenever the simulation transitions between Geant4 application states.

Warning
The Geant4 memory semantics for this class are bonkers.
  • 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 only truly safe way to manage memory for this class is probably to leak it.

Constructor & Destructor Documentation

◆ StateDependent()

celeritas::StateDependent::StateDependent ( LocalGeantStateChangeFunc  cb)
explicit

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 this is registered with, in case we want to deregister in a thread other than the one we were created with. (This might be dangerous... but so is assuming we're destroyed on the same thread we're constructed in.)

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