DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
InterModuleCommunicator.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
5
6namespace DynExpInstr
7{
13
15 {
16 // Shut down derived instrument first.
17 ExitFuncImpl(dispatch_tag<ExitTask>(), Instance);
18 }
19
21 {
22 // Update derived instrument.
23 UpdateFuncImpl(dispatch_tag<UpdateTask>(), Instance);
24 }
25
30
32 : DynExp::InstrumentBase(OwnerThreadID, std::move(Params)), Core(GetParams()->GetCore())
33 {
34 }
35
37 {
38 auto& ModuleMgr = Core.GetModuleManager();
39 const auto UserIDs = GetUserIDs();
40
41 for (auto ID : UserIDs)
42 {
43 // Do not send the event back to the caller.
44 if (Caller.GetID() == ID)
45 continue;
46
47 auto Resource = ModuleMgr.GetResource(ID);
48
49 // Retry in case of Util::TimeoutException a couple of times to not lose events.
50 for (int NumTries = 0; true; NumTries++)
51 {
52 try
53 {
54 if (Resource->IsReady())
55 {
56 // Copies the event for each receiver.
57 Resource->EnqueueEvent(InterModuleEvent.Clone(GetID()));
58 }
59 }
60 catch ([[maybe_unused]] const Util::TimeoutException& e)
61 {
62 if (NumTries >= 100)
63 throw;
64 else
65 {
66 std::this_thread::yield();
67
68 continue;
69 }
70 }
71
72 break;
73 }
74 }
75 }
76
81}
Implementation of the inter-module communicator instrument to exchange events in between DynExp modul...
void ResetImpl(dispatch_tag< DynExp::InstrumentDataBase >) override final
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
void ExitFuncImpl(dispatch_tag< DynExp::ExitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Deinitializes the respective instrument within the instrument inheritance hierarchy....
void InitFuncImpl(dispatch_tag< DynExp::InitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Initializes the respective instrument within the instrument inheritance hierarchy....
void UpdateFuncImpl(dispatch_tag< DynExp::UpdateTaskBase >, DynExp::InstrumentInstance &Instance) override final
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
void PostEvent(const DynExp::ModuleBase &Caller, const DynExp::InterModuleEventBase &InterModuleEvent) const
Inserts the event passed to the function into the event queues of the modules making use of this Inte...
const DynExp::DynExpCore & Core
Reference to DynExp's core.
InterModuleCommunicator(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Constructs an instrument instance.
void ResetImpl(dispatch_tag< DynExp::InstrumentBase >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
auto & GetModuleManager() noexcept
Getter for the module manager.
Definition DynExpCore.h:293
Refer to DynExp::ParamsBase::dispatch_tag.
Refer to DynExp::ParamsBase::dispatch_tag.
Refer to ParamsBase::dispatch_tag.
Definition Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition Instrument.h:813
Common base class for all inter-module events.
Definition Module.h:1153
virtual InterModuleEventPtrType Clone(ItemIDType CommunicatorID) const =0
Creates a deep copy of this inter-module instance.
Base class for modules. Modules implement programs on their own (e.g. measurement protocols or server...
Definition Module.h:428
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.
Definition Object.h:2228
ItemIDType GetID() const noexcept
Returns the ID of this Object instance. Thread-safe since ID is const.
Definition Object.h:2143
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
Refer to DynExp::ParamsBase::dispatch_tag.
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition Exception.h:262
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
Accumulates include statements to provide a precompiled header.