ChimeraTK-DeviceAccess 03.29.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 void SharedAccessors::combineTransferSharedStates(TransferElementID oldId, TransferElementID newId) {
11 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
12 auto oldIter = _transferSharedStates.find(oldId);
13 assert(oldIter != _transferSharedStates.end());
14 auto newIter = _transferSharedStates.find(newId);
15 assert(newIter != _transferSharedStates.end());
16
17 // Shift instanceCount from old to new. When old is no longer used after the shifting,
18 // drop the old entry
19 assert(oldIter->second.instanceCount >= 1);
20 --(oldIter->second.instanceCount);
21 ++(newIter->second.instanceCount);
22 if(oldIter->second.instanceCount == 0) {
23 _transferSharedStates.erase(oldIter);
24 }
25 }
26
27 /********************************************************************************************************************/
28
29 void SharedAccessors::addTransferElement(TransferElementID id) {
30 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
31 assert(_transferSharedStates.find(id) == _transferSharedStates.end()); // must not be in the map yet
32 _transferSharedStates[id].instanceCount = 1;
33 }
34
35 /********************************************************************************************************************/
36
37 void SharedAccessors::removeTransferElement(TransferElementID id) {
38 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
39 auto iter = _transferSharedStates.find(id);
40 assert(iter != _transferSharedStates.end()); // must be in the map
41 assert(iter->second.instanceCount >= 1);
42 --(iter->second.instanceCount);
43 if(iter->second.instanceCount == 0) {
44 _transferSharedStates.erase(iter);
45 }
46 }
47
48 /********************************************************************************************************************/
49
50 size_t SharedAccessors::instanceCount(TransferElementID id) {
51 std::lock_guard<std::mutex> l(_mapMutex); // protect against concurrent map insertion via the [] operator
52 auto iter = _transferSharedStates.find(id);
53 if(iter == _transferSharedStates.end()) {
54 return 0;
55 }
56 return iter->second.instanceCount;
57 }
58
59 /********************************************************************************************************************/
60
61} // namespace ChimeraTK::detail