DynExp
Highly flexible laboratory automation for dynamically changing experiments.
|
Defines an Object
which possesses a thread it runs in. The RunnableObject
can be started and stopped as well as paused and resumed.
More...
Classes | |
class | NotUnusedException |
Exception type thrown by TerminateImpl() if the RunnableObject cannot be terminated for being used by another Object instance. More... | |
class | RunnableInstanceOnlyType |
Allow exclusive access to some of RunnableObject's private methods to class RunnableInstance . More... | |
Public Types | |
using | ParamsType = RunnableObjectParams |
Type of the parameter class belonging to this Object type. Declare this alias in every derived class with the respective parameter class accompanying the derived Object . More... | |
using | ConfigType = RunnableObjectConfigurator |
Type of the configurator class belonging to this Object type. Declare this alias in every derived class with the respective configurator class accompanying the derived Object . More... | |
Public Types inherited from DynExp::Object | |
using | ParamsType = ParamsBase |
Type of the parameter class belonging to this Object type. Declare this alias in every derived class with the respective parameter class accompanying the derived Object . More... | |
using | ParamsTypeSyncPtrType = Util::SynchronizedPointer< ParamsType > |
Alias for the return type of Object::GetParams(). Parameters wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way. More... | |
using | ParamsConstTypeSyncPtrType = Util::SynchronizedPointer< const ParamsType > |
Alias for the return type of Object::GetParams() const. Parameters wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way. More... | |
using | ConfigType = ConfiguratorBase |
Type of the configurator class belonging to this Object type. Declare this alias in every derived class with the respective configurator class accompanying the derived Object . More... | |
using | ParamsGetterType = Util::CallableMemberWrapper< Object, decltype(&Object::GetParams)> |
Invoking an instance of this alias is supposed to call Object::GetParams() of the instance the Util::CallableMemberWrapper has been constructed with. More... | |
Public Member Functions | |
RunnableObject (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params) | |
Constructs an Object instance. More... | |
virtual | ~RunnableObject ()=0 |
void | SetPaused (bool Pause, std::string Description="") |
Pauses or resumes a RunnableObject instance. Its thread stays running, but the instance does not perform any action while it is paused. More... | |
bool | IsRunning () const noexcept |
Returns Running. More... | |
bool | IsPaused () const noexcept |
Returns Paused. More... | |
bool | IsExiting () const noexcept |
Returns ShouldExit. More... | |
auto | GetStartupType () const noexcept |
Returns Startup. More... | |
auto | GetReasonWhyPaused () const |
Returns ReasonWhyPaused. More... | |
Not thread-safe | |
These functions must be called by the thread owning this | |
bool | Run (QWidget *ParentWidget=nullptr) |
Starts the RunnableObject instance's thread and ensures that all Object instances linked to this instance via ObjectLink parameters are in a ready state. Calls RunChild() which is to be overridden by derived classes to start a thread and to pass this thread back by calling StoreThread() . More... | |
bool | RunIfRunAutomatic () |
Calls Run() if RunnableObjectParams::Startup is set to RunnableObjectParams::Automatic. More... | |
bool | RunIfRunOnCreation () |
Calls Run() if RunnableObjectParams::Startup is set to RunnableObjectParams::OnCreation. More... | |
void | Terminate (bool Force=false, const std::chrono::milliseconds Timeout=TerminateTimeoutDefault) |
Notifies the RunnableObject instance's thread to terminate and waits until it has ended. Calls TerminateImpl(). More... | |
Public Member Functions inherited from Util::INonCopyable | |
INonCopyable (const INonCopyable &)=delete | |
INonCopyable & | operator= (const INonCopyable &)=delete |
Public Member Functions inherited from DynExp::Object | |
ItemIDType | GetID () const noexcept |
Returns the ID of this Object instance. Thread-safe since ID is const. More... | |
std::string | GetCategoryAndName () const |
Builds a string from an Object's category and name to allow the user to identify an Object's type. More... | |
void | EnsureReadyState (bool IsAutomaticStartup) |
Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices. More... | |
void | CheckLinkedObjectStates () const |
Checks whether Object instances this instance uses are in a ready state. Override CheckLinkedObjectStatesChild() to implement this behavior. More... | |
void | Reset () |
Resets this Object instance (including all its derived classes) by calling ResetImpl() . A reset implies that the derived classes reload all their parameters from Params and that all LinkedObjectWrapper instances owned by this Object instance become invalidated. More... | |
void | BlockIfUnused (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) |
Blocks this Object instance setting Object::IsBlocked to true. Refer to Object::IsBlocked for the consequences. More... | |
ParamsConstTypeSyncPtrType | GetParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const |
Locks the mutex of the parameter class instance Params assigned to this Object instance and returns a pointer to the locked Params. More... | |
ParamsTypeSyncPtrType | GetParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) |
Locks the mutex of the parameter class instance Params assigned to this Object instance and returns a pointer to the locked Params. More... | |
auto | GetObjectName (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const |
Returns the name of this Object instance. More... | |
bool | IsSharedUsageEnabled (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const |
Returns whether shared usage has been enabled for this Object instance. Refer to ParamsBase::UsageType. More... | |
void | SetWarning (std::string Description, int ErrorCode) const |
Setter for Object::Warning. Sets the warning by a description and an error code. More... | |
void | SetWarning (const Util::Exception &e) const |
Setter for Object::Warning. Sets the warning by retrieving the warning data from an exception e . More... | |
void | ClearWarning () const |
Resets Object::Warning. More... | |
auto | GetWarning () const |
Returns Object::Warning in a thread-safe way by copying its internal data. More... | |
std::exception_ptr | GetException (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const |
Returns a pointer to the exception which has caused this Object instance to fail. More... | |
bool | IsReady () const |
Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) and not blocked (refer to Object::IsBlocked). More... | |
auto | GetUseCount (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const |
Counts the registered useres in a thread-safe way. More... | |
bool | IsUnused (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const |
Returns whether this Object instance is used by other instances. More... | |
auto | GetUserIDs (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const |
Returns a list of the IDs of the registered users in a thread-safe way. More... | |
auto | GetUserNamesString (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const |
Builds a string describing which users are registered containing their object names, categories and type names in a thread-safe way. More... | |
virtual std::string | GetName () const =0 |
Returns the name of this Object type. More... | |
virtual std::string | GetCategory () const =0 |
Returns the category of this Object type. More... | |
Public Attributes | |
RunnableInstanceOnlyType | RunnableInstanceOnly |
Allow exclusive access to some of RunnableObject's private methods to class RunnableInstance . More... | |
Public Attributes inherited from DynExp::Object | |
LinkedObjectWrapperOnlyType | LinkedObjectWrapperOnly |
Allow exclusive access to some of Object's private methods to any LinkedObjectWrapper<T>. More... | |
Static Public Attributes | |
static constexpr auto | ShortTimeoutDefault = std::chrono::milliseconds(50) |
Default timeout e.g. used as a default for calls to InstrumentBase::GetInstrumentData or ModuleBase::GetModuleData. More... | |
static constexpr auto | TerminateTimeoutDefault = std::chrono::milliseconds(3000) |
Default timeout used as a default for calls to RunnableObject::Terminate. More... | |
Static Public Attributes inherited from DynExp::Object | |
static constexpr std::chrono::milliseconds | GetParamsTimeoutDefault = std::chrono::milliseconds(100) |
Default timeout used by Object::GetParams() to lock the mutex of the parameter instance assigned to this Object instance. More... | |
Protected Member Functions | |
void | Init () |
Initializes member variables in case of a reset. More... | |
std::promise< void > | MakeThreadExitedPromise () |
Helper function to be used by overridden RunChild() functions in derived classes to (re)initialize the ThreadExitedSignal future and to provide the derived class a promise to signal the RunnableObject instance that its thread has terminated. More... | |
void | StoreThread (std::thread &&Thread) noexcept |
Stores a thread constructed by a derived class overriding RunChild() in Thread taking ownership of the thread. Only call this function within RunChild()! More... | |
bool | IsCallFromRunnableThread () const |
Checks whether Thread's id matches the id of the calling thread. This is thread-safe if the function is called by the RunnableObject instance's thread since Terminate() joins the threads before changing the Thread member. It is also thread-safe if the function is called by the thread owning the the RunnableObject instance since Run() and Terminate() can only be called by this thread. Only Run() and Terminate() (indirectly) modify Thread . More... | |
void | EnsureCallFromRunnableThread () const |
Asserts that the call to this function is performed from the RunnableObject instance's thread by calling IsCallFromRunnableThread(). More... | |
void | SetReasonWhyPaused (std::string Description) |
Sets the reason why this RunnableObject instance has been paused. More... | |
void | SetReasonWhyPaused (const Util::Exception &e) |
Sets the reason why this RunnableObject instance has been paused. More... | |
void | ClearReasonWhyPaused () |
Removes the reason why this RunnableObject instance has been paused (since it is resumed). More... | |
Protected Member Functions inherited from Util::INonCopyable | |
constexpr | INonCopyable ()=default |
~INonCopyable ()=default | |
Protected Member Functions inherited from DynExp::Object | |
Object (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params) | |
Constructs an Object instance. More... | |
virtual | ~Object ()=0 |
void | EnsureCallFromOwningThread () const |
Asserts that the call to this function is performed from the thread which constructed this Object instance (the thread with the id stored in Object::OwnerThreadID). More... | |
ParamsTypeSyncPtrType | GetNonConstParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const |
Allows derived Objects to edit their own parameters - even in const task functions (for instruments) or event functions (for modules). More... | |
auto | LockUserList (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) |
Locks the user list for thread-safe manipulation. More... | |
void | DeregisterAllUnsafe () |
Deregisters all users and notifies them that they need to check the states of their used linked objects. More... | |
auto | GetUseCountUnsafe () |
auto | GetUserNamesStringUnsafe () const |
bool | IsUnusedUnsafe () |
Returns whether this Object instance is used by other instances (not thread-safe). More... | |
Private Member Functions | |
void | ResetImpl (dispatch_tag< Object >) override final |
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy. More... | |
virtual void | ResetImpl (dispatch_tag< RunnableObject >)=0 |
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy. More... | |
void | EnsureReadyStateChild (bool IsAutomaticStartup) override final |
Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices. More... | |
void | CheckLinkedObjectStatesChild () const override final |
Override to implement a check whether linked objects are in a ready state. More... | |
void | TerminateImpl (bool Force, const std::chrono::milliseconds Timeout=TerminateTimeoutDefault) |
Notifies the RunnableObject instance's thread to terminate and waits until it has ended. Calls TerminateImpl(). More... | |
void | TerminateUnsafe (bool Force, const std::chrono::milliseconds Timeout=TerminateTimeoutDefault) |
Notifies the RunnableObject instance's thread to terminate and waits until it has ended. Calls TerminateImpl(). More... | |
void | OnThreadHasExited () noexcept |
This function is called when the RunnableObject instance's thread terminates. The thread receives a RunnableInstance instance as a parameter. When it exits, this RunnableInstance instance is destroyed. So, RunnableInstance's destructor is invoked. It calls RunnableInstance::SetThreadExited(), which in turn calls this function, and signals the thread termination to ThreadExitedSignal. More... | |
Override | |
Override by derived classes. | |
virtual void | RunChild ()=0 |
Refer to Run() . More... | |
virtual void | NotifyChild () |
Notify derived classes that some state has changed (e.g. the termination of Thread is requested) and that the child's event/task queue should run now. More... | |
virtual void | TerminateChild (const std::chrono::milliseconds Timeout) |
Signals derived classes that terminating the RunnableObject instance's thread is about to be requested. Derived classes might now enqueue respective exit tasks/events into their task/event queues. Refer to TerminateUnsafe() . More... | |
virtual std::unique_ptr< BusyDialog > | MakeStartupBusyDialogChild (QWidget *ParentWidget) const |
Override to make this function return a pointer to a BusyDialog instance. Refer to Run() . More... | |
Private Attributes | |
std::atomic< RunnableObjectParams::StartupType > | Startup = RunnableObjectParams::StartupType::Automatic |
Reflects the value of RunnableObjectParams::Startup . This variable is only updated when Run() is called. It is atomic to allow the UI thread reading it safely. More... | |
std::thread | Thread |
The RunnableObject instance's thread. More... | |
std::future< void > | ThreadExitedSignal |
Future which signals that Thread has terminated. Refer to OnThreadHasExited() . More... | |
std::atomic< bool > | Running |
Indicates whether the RunnableObject instance is running. More... | |
std::atomic< bool > | Paused |
Indicates whether the RunnableObject instance is paused. More... | |
Util::Warning | ReasonWhyPaused |
Holds information about why the RunnableObject instance is paused. More... | |
std::atomic< bool > | LinkedObjStateCheckRequested = false |
Indicates whether the RunnableInstance instance belonging to this RunnableObject instance's thread should recheck the states of all Object instances this RunnableObject uses. More... | |
Inter-thread communication | |
These variables are for communication from the thread owning this | |
std::atomic< bool > | ShouldExit |
Indicates whether this RunnableObject instance's thread should terminate. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from DynExp::Object | |
static std::string | CategoryAndNameToStr (const std::string &Category, const std::string &Name) |
Builds a string from an Object's category and name to allow the user to identify an Object's type. More... | |
Defines an Object
which possesses a thread it runs in. The RunnableObject
can be started and stopped as well as paused and resumed.
DynExp::RunnableObject::RunnableObject | ( | const std::thread::id | OwnerThreadID, |
ParamsBasePtrType && | Params | ||
) |
Constructs an Object
instance.
OwnerThreadID | Thread id of the thread owning the Object instance to be constructed. |
Params | Parameter class instance to be assigned to the Object instance to be constructed. |
Util::InvalidArgException | is thrown if OwnerThreadID is an invalid thread id or if Params is nullptr. |
Definition at line 518 of file Object.cpp.
|
pure virtual |
Definition at line 524 of file Object.cpp.
|
inlinefinaloverrideprivatevirtual |
Override to implement a check whether linked objects are in a ready state.
Reimplemented from DynExp::Object.
|
inlineprotected |
Removes the reason why this RunnableObject
instance has been paused (since it is resumed).
|
protected |
Asserts that the call to this function is performed from the RunnableObject
instance's thread by calling IsCallFromRunnableThread().
Util::InvalidCallException | is thrown if the assertion fails. |
Definition at line 661 of file Object.cpp.
|
finaloverrideprivatevirtual |
Ensures that this Object
instance is ready by possibly starting its worker thread or by opening connections to hardware devices.
IsAutomaticStartup | Pass true if ensuring other Object instances to be ready have caused a call to this function. Pass false if an action performed by the user has caused the call to this function. |
Implements DynExp::Object.
Definition at line 676 of file Object.cpp.
|
inline |
Returns ReasonWhyPaused.
|
inlinenoexcept |
|
protected |
Initializes member variables in case of a reset.
Definition at line 634 of file Object.cpp.
|
protected |
Checks whether Thread's
id matches the id of the calling thread. This is thread-safe if the function is called by the RunnableObject
instance's thread since Terminate()
joins the threads before changing the Thread
member. It is also thread-safe if the function is called by the thread owning the the RunnableObject
instance since Run()
and Terminate()
can only be called by this thread. Only Run()
and Terminate()
(indirectly) modify Thread
.
Definition at line 656 of file Object.cpp.
|
inlinenoexcept |
Returns ShouldExit.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlineprivatevirtual |
Override to make this function return a pointer to a BusyDialog
instance. Refer to Run()
.
ParentWidget | Qt QWidget acting as a parent of the modal busy dialog |
BusyDialog
instance Reimplemented in DynExp::ModuleBase.
|
protected |
Helper function to be used by overridden RunChild()
functions in derived classes to (re)initialize the ThreadExitedSignal future and to provide the derived class a promise to signal the RunnableObject
instance that its thread has terminated.
Definition at line 643 of file Object.cpp.
|
inlineprivatevirtual |
Notify derived classes that some state has changed (e.g. the termination of Thread
is requested) and that the child's event/task queue should run now.
Reimplemented in DynExp::ModuleBase, and DynExp::InstrumentBase.
|
privatenoexcept |
This function is called when the RunnableObject
instance's thread terminates. The thread receives a RunnableInstance
instance as a parameter. When it exits, this RunnableInstance
instance is destroyed. So, RunnableInstance's
destructor is invoked. It calls RunnableInstance::SetThreadExited(), which in turn calls this function, and signals the thread termination to ThreadExitedSignal.
Definition at line 713 of file Object.cpp.
|
finaloverrideprivatevirtual |
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()
of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy.
Implements DynExp::Object.
Definition at line 668 of file Object.cpp.
|
privatepure virtual |
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()
of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy.
Implemented in DynExp::ModuleBase, and DynExp::InstrumentBase.
bool DynExp::RunnableObject::Run | ( | QWidget * | ParentWidget = nullptr | ) |
Starts the RunnableObject
instance's thread and ensures that all Object
instances linked to this instance via ObjectLink
parameters are in a ready state. Calls RunChild()
which is to be overridden by derived classes to start a thread and to pass this thread back by calling StoreThread()
.
ParentWidget | Qt QWidget acting as a parent of the modal busy dialog (BusyDialog ) to be displayed while starting the linked Object instances. |
RunnableObject
instance has been started now, false otherwise (e.g. if it is already running or if the startup policy (RunnableObjectParams::Startup
) prohibits so). Util::InvalidStateException | is thrown if the RunnableObject instance is in an error state. Call Object::Reset() to resolve this. |
Definition at line 550 of file Object.cpp.
|
privatepure virtual |
Refer to Run()
.
Implemented in DynExp::ModuleBase, and DynExp::InstrumentBase.
bool DynExp::RunnableObject::RunIfRunAutomatic | ( | ) |
Calls Run()
if RunnableObjectParams::Startup is set to RunnableObjectParams::Automatic.
RunnableObject
instance has been started now, false otherwise (e.g. if it is already running or if the startup policy (RunnableObjectParams::Startup
) prohibits so). Util::NotAvailableException | is thrown if the RunnableObject instance is not already running and if RunnableObjectParams::Startup is not set to RunnableObjectParams::Automatic. |
Definition at line 594 of file Object.cpp.
bool DynExp::RunnableObject::RunIfRunOnCreation | ( | ) |
Calls Run()
if RunnableObjectParams::Startup is set to RunnableObjectParams::OnCreation.
RunnableObject
instance has been started now, false otherwise (e.g. if it is already running or if the startup policy (RunnableObjectParams::Startup
) prohibits so). Definition at line 607 of file Object.cpp.
void DynExp::RunnableObject::SetPaused | ( | bool | Pause, |
std::string | Description = "" |
||
) |
Pauses or resumes a RunnableObject
instance. Its thread stays running, but the instance does not perform any action while it is paused.
Pause | Pass true to pause the RunnableObject instance or false to resume it. If Pause is false, ClearReasonWhyPaused() is called. |
Description | If Pause is true, Description is passed to SetReasonWhyPaused() . |
Definition at line 624 of file Object.cpp.
|
inlineprotected |
Sets the reason why this RunnableObject
instance has been paused.
e | Exception containing the reason (including a human-readable description) |
|
inlineprotected |
Sets the reason why this RunnableObject
instance has been paused.
Description | Human-readable string describing the reason |
|
protectednoexcept |
Stores a thread constructed by a derived class overriding RunChild()
in Thread
taking ownership of the thread. Only call this function within RunChild()!
Thread | Thread to store. |
Definition at line 651 of file Object.cpp.
void DynExp::RunnableObject::Terminate | ( | bool | Force = false , |
const std::chrono::milliseconds | Timeout = TerminateTimeoutDefault |
||
) |
Notifies the RunnableObject
instance's thread to terminate and waits until it has ended. Calls TerminateImpl().
Force | If true, the RunnableObject is terminated even if it is still used. |
Timeout | Time to wait until the RunnableObject's thread has ended. |
Definition at line 617 of file Object.cpp.
|
inlineprivatevirtual |
Signals derived classes that terminating the RunnableObject instance's thread is about to be requested. Derived classes might now enqueue respective exit tasks/events into their task/event queues. Refer to TerminateUnsafe()
.
Timeout | Time to wait until the RunnableObject's thread has ended. |
Reimplemented in DynExp::ModuleBase, and DynExp::InstrumentBase.
|
private |
Notifies the RunnableObject
instance's thread to terminate and waits until it has ended. Calls TerminateImpl().
Force | If true, the RunnableObject is terminated even if it is still used. |
Timeout | Time to wait until the RunnableObject's thread has ended. |
Bypasses the assertion check provided by Object::EnsureCallFromOwningThread(). Ensures thread-safety and calls TerminateUnsafe()
.
NotUnusedException | is thrown in case Force is false and if this RunnableObject instance is used by other Object instances. |
Definition at line 681 of file Object.cpp.
|
private |
Notifies the RunnableObject
instance's thread to terminate and waits until it has ended. Calls TerminateImpl().
Force | If true, the RunnableObject is terminated even if it is still used. |
Timeout | Time to wait until the RunnableObject's thread has ended. |
Joins this RunnableObject
instance's thread. Does nothing if Thread
is not joinable. Not thread-safe. Called by TerminateImpl()
. Calls TerminateChild()
before any other action.
Util::ThreadDidNotRespondException | is thrown if the thread does not terminate within the TerminateTimeoutDefault timeout after setting ShouldExit to true and calling NotifyChild() . |
Definition at line 695 of file Object.cpp.
|
mutableprivate |
Indicates whether the RunnableInstance
instance belonging to this RunnableObject
instance's thread should recheck the states of all Object
instances this RunnableObject
uses.
|
private |
Indicates whether the RunnableObject
instance is paused.
|
private |
Holds information about why the RunnableObject
instance is paused.
RunnableInstanceOnlyType DynExp::RunnableObject::RunnableInstanceOnly |
Allow exclusive access to some of RunnableObject's
private methods to class RunnableInstance
.
|
private |
Indicates whether the RunnableObject
instance is running.
|
staticconstexpr |
Default timeout e.g. used as a default for calls to InstrumentBase::GetInstrumentData or ModuleBase::GetModuleData.
|
private |
Indicates whether this RunnableObject
instance's thread should terminate.
|
private |
Reflects the value of RunnableObjectParams::Startup
. This variable is only updated when Run()
is called. It is atomic to allow the UI thread reading it safely.
|
staticconstexpr |
Default timeout used as a default for calls to RunnableObject::Terminate.
|
private |
The RunnableObject
instance's thread.
|
private |
Future which signals that Thread
has terminated. Refer to OnThreadHasExited()
.