ChimeraTK-DeviceAccess 03.26.00
Loading...
Searching...
No Matches
SharedAccessor.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 "SharedAccessor.h"
5
6namespace ChimeraTK::detail {
7
8 /********************************************************************************************************************/
9
10 SharedAccessors& SharedAccessors::getInstance() {
11 static SharedAccessors instance;
12 return instance;
13 }
14
15 /********************************************************************************************************************/
16
17 void SharedAccessors::combineTransferSharedStates(TransferElementID oldId, TransferElementID newId) {
18 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
19 auto oldIter = _transferSharedStates.find(oldId);
20 assert(oldIter != _transferSharedStates.end());
21 auto newIter = _transferSharedStates.find(newId);
22 assert(newIter != _transferSharedStates.end());
23
24 // The only action at the moment: sum the instance counts of both states
25 newIter->second.instanceCount = newIter->second.instanceCount + oldIter->second.instanceCount;
26 _transferSharedStates.erase(oldIter);
27 }
28
29 /********************************************************************************************************************/
30
31 void SharedAccessors::addTransferElement(TransferElementID id) {
32 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
33 assert(_transferSharedStates.find(id) == _transferSharedStates.end()); // must not be in the map yet
34 _transferSharedStates[id].instanceCount = 1;
35 }
36
37 /********************************************************************************************************************/
38
39 void SharedAccessors::removeTransferElement(TransferElementID id) {
40 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
41 auto iter = _transferSharedStates.find(id);
42 assert(iter != _transferSharedStates.end()); // must be in the map
43 assert(iter->second.instanceCount >= 1);
44 --(iter->second.instanceCount);
45 if(iter->second.instanceCount == 0) {
46 _transferSharedStates.erase(iter);
47 }
48 }
49
50 /********************************************************************************************************************/
51
52 size_t SharedAccessors::instanceCount(TransferElementID id) {
53 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
54 auto iter = _transferSharedStates.find(id);
55 if(iter == _transferSharedStates.end()) {
56 return 0;
57 }
58 return iter->second.instanceCount;
59 }
60
61 /********************************************************************************************************************/
62
63} // namespace ChimeraTK::detail