ChimeraTK-DeviceAccess 03.29.00
Loading...
Searching...
No Matches
DeviceBackendImpl.cc
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
4#include "DeviceBackendImpl.h"
5
6#include "SharedAccessor.h"
7
8#include <unistd.h>
9
10namespace ChimeraTK {
11
12 /********************************************************************************************************************/
13
15 // busy-wait until all exceptions have been distributed to the AsyncAccessors
17 usleep(10000);
18 }
19
20 _versionOnOpen = ChimeraTK::VersionNumber{};
21 _opened = true;
22 _hasActiveException = false;
23 std::lock_guard<std::mutex> lk(_mx_activeExceptionMessage);
24 _activeExceptionMessage = "(exception cleared)";
25 }
26
27 /********************************************************************************************************************/
28
29 void DeviceBackendImpl::setException(const std::string& message) noexcept {
30 // set exception flag and atomically get the previous state
31 bool hadExceptionBefore = _hasActiveException.exchange(true);
32
33 // if previously backend was already in exception state, don't continue here
34 if(hadExceptionBefore) {
35 return;
36 }
37
38 // set exception message
39 {
40 std::lock_guard<std::mutex> lk(_mx_activeExceptionMessage);
41 _activeExceptionMessage = message;
42 }
43
44 // execute backend-specific code
45 setExceptionImpl();
46
47 // finally turn off all async accessors and distribute the exception to them
48 _asyncDomainsContainer.sendExceptions(message);
49 }
50
51 /********************************************************************************************************************/
52
54 std::lock_guard<std::mutex> lk(_mx_activeExceptionMessage);
55 return _activeExceptionMessage;
56 }
57
58 /********************************************************************************************************************/
59
60 std::set<DeviceBackend::BackendID> DeviceBackendImpl::getInvolvedBackendIDs() {
61 return {getBackendID()};
62 }
63
64 /********************************************************************************************************************/
65
67 return _versionOnOpen;
68 }
69
70 /********************************************************************************************************************/
71
72 boost::shared_ptr<detail::SharedAccessors> DeviceBackendImpl::getSharedAccessors() {
73 std::lock_guard<std::mutex> lk(_sharedAccessorsMutex);
74 if(!_sharedAccessors) {
75 if(!_sharedAccessors) {
76 _sharedAccessors = boost::make_shared<detail::SharedAccessors>();
77 }
78 }
79 return _sharedAccessors;
80 }
81
82 /********************************************************************************************************************/
83
84} // namespace ChimeraTK
BackendID getBackendID()
Get a unique ID for this backend instance.
async::DomainsContainer _asyncDomainsContainer
Container for async::Domains to support wait_for_new_data.
void setOpenedAndClearException() noexcept
Backends should call this function at the end of a (successful) open() call.
std::string getActiveExceptionMessage() noexcept
ChimeraTK::VersionNumber getVersionOnOpen() const override
Get the version number which was set during the open process.
void setException(const std::string &message) noexcept final
Set the backend into an exception state.
std::set< DeviceBackend::BackendID > getInvolvedBackendIDs() override
Get the backend IDs of all involved backends.
std::atomic< bool > _opened
flag if backend is opened
boost::shared_ptr< detail::SharedAccessors > getSharedAccessors() final
Get the SharedAccessors object of this backend instance.
Class for generating and holding version numbers without exposing a numeric representation.
bool isSendingExceptions()
Check whether an exception distribution is started and not completed yet.