ChimeraTK-DeviceAccess  03.18.00
TriggeredPollDistributor.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "../TransferGroup.h"
6 #include "AsyncAccessorManager.h"
8 
9 #include <memory>
10 
11 namespace ChimeraTK::async {
18  public:
19  TriggeredPollDistributor(boost::shared_ptr<DeviceBackend> backend,
20  boost::shared_ptr<SubDomain<std::nullptr_t>> parent, boost::shared_ptr<Domain> asyncDomain);
21 
23  bool prepareIntermediateBuffers() override;
24 
25  template<typename UserType>
26  std::unique_ptr<AsyncVariable> createAsyncVariable(AccessorInstanceDescriptor const& descriptor);
27 
28  protected:
30  boost::shared_ptr<SubDomain<std::nullptr_t>> _parent;
31  };
32 
33  /********************************************************************************************************************/
34 
37  template<typename UserType>
38  struct PolledAsyncVariable : public AsyncVariableImpl<UserType> {
39  void fillSendBuffer() final;
40 
43  explicit PolledAsyncVariable(boost::shared_ptr<NDRegisterAccessor<UserType>> syncAccessor_, VersionNumber& v);
44 
45  boost::shared_ptr<NDRegisterAccessor<UserType>> syncAccessor;
47 
48  unsigned int getNumberOfChannels() override { return syncAccessor->getNumberOfChannels(); }
49  unsigned int getNumberOfSamples() override { return syncAccessor->getNumberOfSamples(); }
50  const std::string& getUnit() override { return syncAccessor->getUnit(); }
51  const std::string& getDescription() override { return syncAccessor->getDescription(); }
52  };
53 
54  /********************************************************************************************************************/
55  // Implementations
56  /********************************************************************************************************************/
57 
58  template<typename UserType>
59  std::unique_ptr<AsyncVariable> TriggeredPollDistributor::createAsyncVariable(
60  AccessorInstanceDescriptor const& descriptor) {
61  auto synchronousFlags = descriptor.flags;
62  synchronousFlags.remove(AccessMode::wait_for_new_data);
63  // Don't call backend->getSyncRegisterAccessor() here. It might skip the overriding of a backend.
64  auto syncAccessor = _backend->getRegisterAccessor<UserType>(
65  descriptor.name, descriptor.numberOfWords, descriptor.wordOffsetInRegister, synchronousFlags);
66  // read the initial value before adding it to the transfer group
67  if(_asyncDomain->unsafeGetIsActive()) {
68  try {
69  syncAccessor->read();
70  }
71  catch(ChimeraTK::runtime_error&) {
72  // Nothing to do here. The backend's setException() has already been called by the syncAccessor.
73  }
74  }
75 
76  _transferGroup.addAccessor(syncAccessor);
77  return std::make_unique<PolledAsyncVariable<UserType>>(syncAccessor, _version);
78  }
79 
80  /********************************************************************************************************************/
81  template<typename UserType>
83  this->_sendBuffer.versionNumber = _version;
84  this->_sendBuffer.dataValidity = syncAccessor->dataValidity();
85  this->_sendBuffer.value.swap(syncAccessor->accessChannels());
86  }
87 
88  /********************************************************************************************************************/
89  template<typename UserType>
91  boost::shared_ptr<NDRegisterAccessor<UserType>> syncAccessor_, VersionNumber& v)
92  : AsyncVariableImpl<UserType>(syncAccessor_->getNumberOfChannels(), syncAccessor_->getNumberOfSamples()),
93  syncAccessor(syncAccessor_), _version(v) {}
94 
95 } // namespace ChimeraTK::async
ChimeraTK::async::TriggeredPollDistributor::TriggeredPollDistributor
TriggeredPollDistributor(boost::shared_ptr< DeviceBackend > backend, boost::shared_ptr< SubDomain< std::nullptr_t >> parent, boost::shared_ptr< Domain > asyncDomain)
Definition: TriggeredPollDistributor.cc:8
ChimeraTK::TransferGroup::addAccessor
void addAccessor(TransferElementAbstractor &accessor)
Add a register accessor to the group.
Definition: TransferGroup.cc:305
ChimeraTK::async::TriggeredPollDistributor::_transferGroup
TransferGroup _transferGroup
Definition: TriggeredPollDistributor.h:29
ChimeraTK::async::SubDomain
Send backend-specific asynchronous data to different distributors:
Definition: MuxedInterruptDistributor.h:23
ChimeraTK::async
Definition: design_AsyncNDRegisterAcessor_and_NumericAddressedBackend.dox:1
MuxedInterruptDistributor.h
ChimeraTK::async::AccessorInstanceDescriptor::numberOfWords
size_t numberOfWords
Definition: AsyncAccessorManager.h:50
ChimeraTK::AccessModeFlags::remove
void remove(AccessMode flag)
Remove the given flag from the set.
Definition: AccessMode.cc:56
ChimeraTK::async::PolledAsyncVariable::getNumberOfChannels
unsigned int getNumberOfChannels() override
Helper functions for the creation of an AsyncNDRegisterAccessor.
Definition: TriggeredPollDistributor.h:48
ChimeraTK::async::AccessorInstanceDescriptor::wordOffsetInRegister
size_t wordOffsetInRegister
Definition: AsyncAccessorManager.h:51
ChimeraTK::async::AsyncVariableImpl
AsyncVariableImpl contains a weak pointer to an AsyncNDRegisterAccessor<UserType> and a send buffer N...
Definition: AsyncAccessorManager.h:166
ChimeraTK::async::SourceTypedAsyncAccessorManager
Definition: AsyncAccessorManager.h:135
ChimeraTK::runtime_error
Exception thrown when a runtime error has occured.
Definition: Exception.h:18
ChimeraTK::TransferGroup
Group multiple data accessors to efficiently trigger data transfers on the whole group.
Definition: TransferGroup.h:26
ChimeraTK::async::AccessorInstanceDescriptor::flags
AccessModeFlags flags
Definition: AsyncAccessorManager.h:52
ChimeraTK::async::TriggeredPollDistributor
The TriggeredPollDistributor has std::nullptr_t source data type and is polling the data for the Asyn...
Definition: TriggeredPollDistributor.h:17
ChimeraTK::async::AccessorInstanceDescriptor::name
RegisterPath name
Definition: AsyncAccessorManager.h:48
ChimeraTK::async::SourceTypedAsyncAccessorManager< std::nullptr_t >::_version
VersionNumber _version
Definition: AsyncAccessorManager.h:151
ChimeraTK::async::AccessorInstanceDescriptor
Helper class to have a complete descriton to create an Accessor.
Definition: AsyncAccessorManager.h:47
ChimeraTK::async::PolledAsyncVariable::fillSendBuffer
void fillSendBuffer() final
Fill the send buffer with data and version number.
Definition: TriggeredPollDistributor.h:82
ChimeraTK::AccessMode::wait_for_new_data
@ wait_for_new_data
Make any read blocking until new data has arrived since the last read.
ChimeraTK::async::PolledAsyncVariable
Implementation of the PolledAsyncVariable for the concrete UserType.
Definition: TriggeredPollDistributor.h:38
ChimeraTK::async::AsyncAccessorManager::_backend
boost::shared_ptr< DeviceBackend > _backend
Definition: AsyncAccessorManager.h:105
ChimeraTK::async::AsyncAccessorManager::_asyncDomain
boost::shared_ptr< Domain > _asyncDomain
Definition: AsyncAccessorManager.h:106
ChimeraTK::async::TriggeredPollDistributor::_parent
boost::shared_ptr< SubDomain< std::nullptr_t > > _parent
Definition: TriggeredPollDistributor.h:30
ChimeraTK::async::PolledAsyncVariable::PolledAsyncVariable
PolledAsyncVariable(boost::shared_ptr< NDRegisterAccessor< UserType >> syncAccessor_, VersionNumber &v)
The constructor takes an already created synchronous accessor and a reference to the version variable...
Definition: TriggeredPollDistributor.h:90
AsyncAccessorManager.h
ChimeraTK::async::TriggeredPollDistributor::createAsyncVariable
std::unique_ptr< AsyncVariable > createAsyncVariable(AccessorInstanceDescriptor const &descriptor)
Definition: TriggeredPollDistributor.h:59
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::async::PolledAsyncVariable::getUnit
const std::string & getUnit() override
Definition: TriggeredPollDistributor.h:50
ChimeraTK::async::PolledAsyncVariable::getDescription
const std::string & getDescription() override
Definition: TriggeredPollDistributor.h:51
ChimeraTK::async::PolledAsyncVariable::_version
VersionNumber & _version
Definition: TriggeredPollDistributor.h:46
ChimeraTK::async::TriggeredPollDistributor::prepareIntermediateBuffers
bool prepareIntermediateBuffers() override
Poll all sync variables.
Definition: TriggeredPollDistributor.cc:17
ChimeraTK::async::PolledAsyncVariable::getNumberOfSamples
unsigned int getNumberOfSamples() override
Definition: TriggeredPollDistributor.h:49
ChimeraTK::NDRegisterAccessor
N-dimensional register accessor.
Definition: ForwardDeclarations.h:17
ChimeraTK::async::PolledAsyncVariable::syncAccessor
boost::shared_ptr< NDRegisterAccessor< UserType > > syncAccessor
Definition: TriggeredPollDistributor.h:45