RemoteReferenceCount.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Raphael Grimm( raphael dor grimm at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <set>
27 #include <queue>
28 #include <map>
29 
30 #include <atomic>
31 #include <functional>
32 #include <string>
33 #include <mutex>
34 #include <thread>
35 
36 #include <IceUtil/UUID.h>
37 
38 #include <ArmarXCore/interface/core/RemoteReferenceCount.h>
39 
40 #include "logging/Logging.h"
41 
42 //internal base impl
43 namespace armarx
44 {
45  class ArmarXManager;
46  class RemoteReferenceCountControlBlockManager;
47 }
48 
49 namespace armarx::detail
50 {
51  class RemoteReferenceCountControlBlockManagementInterface : virtual public Ice::Object
52  {
53  public:
56  {
57  countingActivated_ = true;
58  }
59  bool isCountingActivated() const
60  {
61  return countingActivated_;
62  }
63  bool hasCountReachedZero() const
64  {
65  return countReachedZero_;
66  }
67  Ice::ObjectPrx getProxy() const
68  {
69  return selfProxy;
70  }
71 
72  RemoteReferenceCountControlBlockManagementInterface(const ArmarXManagerPtr& manager, const std::string& id);
73 
75 
76  protected:
77  friend class ::armarx::RemoteReferenceCountControlBlockManager;
78  /**
79  * @brief Returns the next timepoint when this ControlBlock should be checked.
80  * If the returned time is in the past and counting was activated, this Block
81  * is removed from counting and onCountReachedZero is called.
82  * @return
83  */
84  virtual IceUtil::Time nextCheckTimePoint() = 0;
85  virtual void onCountReachedZero() = 0;
86 
87  void countReachedZero();
88 
90  Ice::ObjectPrx selfProxy;
91 
92  std::mutex mtx;
94 
95  const std::string id;
96 
97  private:
98  std::atomic_bool countingActivated_ {false};
99  std::atomic_bool countReachedZero_ {false};
100  };
102 }
103 
104 //internal base impl AbstractRemoteReferenceCountControlBlock
105 namespace armarx::detail
106 {
108  : virtual public RemoteReferenceCountControlBlockInterface,
110  {
111  public:
113  RemoteReferenceCounterBasePtr getReferenceCounter();
114 
115  void heartbeat(const std::string& counterId, const Ice::Current& = Ice::emptyCurrent) final override;
116  void addCounter(const std::string& counterId, const Ice::Current& = Ice::emptyCurrent) final override;
117  void removeCounter(const std::string& counterId, const Ice::Current& = Ice::emptyCurrent) final override;
118 
119  protected:
120  AbstractRemoteReferenceCountControlBlock(const ArmarXManagerPtr& manager, const std::string& id,
121  IceUtil::Time deletionDelay, IceUtil::Time orphantDeletionDelay, long heartBeatMs);
122 
123  private:
124  IceUtil::Time nextCheckTimePoint() final override;
125 
126  std::map<std::string, IceUtil::Time> counterIds;
127  const IceUtil::Time deletionDelay;
128  const IceUtil::Time orphantDeletionDelay;
129  const long heartBeatMs;
130  };
131 }
132 
133 //internal base impl AbstractSimpleRemoteReferenceCountControlBlock
134 namespace armarx::detail
135 {
137  : virtual public SimpleRemoteReferenceCountControlBlockInterface,
139  {
140  public:
142  SimpleRemoteReferenceCounterBasePtr getReferenceCounter();
143 
144  void addCounter(const std::string& counterId, const Ice::Current&) final override;
145  void removeCounter(const std::string& counterId, const Ice::Current&) final override;
146 
147  protected:
148  AbstractSimpleRemoteReferenceCountControlBlock(const ArmarXManagerPtr& manager, const std::string& id, IceUtil::Time deletionDelay);
149 
150  private:
151  IceUtil::Time nextCheckTimePoint() final override;
152 
153  std::set<std::string> counterIds;
154  const IceUtil::Time deletionDelay;
155  };
156 }
157 
158 //impl RemoteReferenceCountControlBlock
159 namespace armarx
160 {
161  template<class FunctionType = std::function<void(void)>, class DataType = void>
163  {
165  const ArmarXManagerPtr& manager, const std::string& id, FunctionType f, DataType d,
166  IceUtil::Time deletionDelay, IceUtil::Time orphantDeletionDelay, long heartBeatMs
167  )
169  detail::AbstractRemoteReferenceCountControlBlock(manager, id, deletionDelay, orphantDeletionDelay, heartBeatMs),
170  data {std::move(d)},
171  function {std::move(f)}
172  {}
173 
174  DataType data;
175  FunctionType function;
176  void onCountReachedZero() final override
177  {
178  function(data);
179  }
180  friend class ArmarXManager;
182  };
183  template<class FunctionType>
185  {
187  const ArmarXManagerPtr& manager, const std::string& id, FunctionType f,
188  IceUtil::Time deletionDelay, IceUtil::Time orphantDeletionDelay, long heartBeatMs
189  )
191  detail::AbstractRemoteReferenceCountControlBlock(manager, id, deletionDelay, orphantDeletionDelay, heartBeatMs),
192  function {std::move(f)}
193  {}
194  FunctionType function;
195  void onCountReachedZero() final override
196  {
197  function();
198  }
199  friend class ArmarXManager;
201  };
202 
203  template<class FunctionType, class DataType = void>
205 }
206 //impl SimpleRemoteReferenceCountControlBlock
207 namespace armarx
208 {
209  template<class FunctionType = std::function<void(void)>, class DataType = void>
211  {
213  const ArmarXManagerPtr& manager, const std::string& id, FunctionType f, DataType d, IceUtil::Time deletionDelay)
215  detail::AbstractSimpleRemoteReferenceCountControlBlock(manager, id, deletionDelay),
216  data {std::move(d)}, function {std::move(f)}
217  {}
218  DataType data;
219  FunctionType function;
220  void onCountReachedZero() final override
221  {
222  function(data);
223  }
224  friend class ArmarXManager;
226  };
227  template<class FunctionType>
229  {
231  const ArmarXManagerPtr& manager, const std::string& id, FunctionType f, IceUtil::Time deletionDelay)
233  detail::AbstractSimpleRemoteReferenceCountControlBlock(manager, id, deletionDelay), function {std::move(f)}
234  {}
235  FunctionType function;
236  void onCountReachedZero() final override
237  {
238  function();
239  }
240  friend class ArmarXManager;
242  };
243 
244  template<class FunctionType, class DataType = void>
246 }
247 //impl manager
248 namespace armarx
249 {
251  {
252  public:
255  {
256  stop();
257  }
260 
262  void stop();
263 
264  private:
265  void manage();
266 
267  const IceUtil::Time period;
268  using PqEntry = std::pair<IceUtil::Time, detail::RemoteReferenceCountControlBlockManagementInterfacePtr>;
269  struct PqEntryCompare
270  {
271  bool operator()(const PqEntry& lhs, const PqEntry& rhs) const
272  {
273  return std::make_pair(lhs.first, lhs.second.get()) > std::make_pair(rhs.first, rhs.second.get());
274  }
275  };
276  struct RRCBMIPtr
277  {
278  bool operator()(
281  {
282  return lhs.get() < rhs.get();
283  }
284  };
285  std::mutex stateMutex;
286  std::set<detail::RemoteReferenceCountControlBlockManagementInterfacePtr, RRCBMIPtr> pendingForActivation;
287  std::priority_queue<PqEntry, std::vector<PqEntry>, PqEntryCompare> rrccbs;
288  std::thread thread;
289  std::atomic_bool shutdown {false};
290  };
291 }
292 
armarx::detail::AbstractRemoteReferenceCountControlBlock::AbstractRemoteReferenceCountControlBlock
AbstractRemoteReferenceCountControlBlock(const ArmarXManagerPtr &manager, const std::string &id, IceUtil::Time deletionDelay, IceUtil::Time orphantDeletionDelay, long heartBeatMs)
Definition: RemoteReferenceCount.cpp:419
armarx::detail::AbstractRemoteReferenceCountControlBlock::removeCounter
void removeCounter(const std::string &counterId, const Ice::Current &=Ice::emptyCurrent) final override
Definition: RemoteReferenceCount.cpp:400
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::activateCounting
void activateCounting()
Definition: RemoteReferenceCount.h:55
armarx::RemoteReferenceCountControlBlockManager::DefaultOrphantDeletionDelayMs
static const Ice::Long DefaultOrphantDeletionDelayMs
Definition: RemoteReferenceCount.h:259
armarx::RemoteReferenceCountControlBlockManager::DefaultDeletionDelayMs
static const Ice::Long DefaultDeletionDelayMs
Definition: RemoteReferenceCount.h:258
armarx::detail::AbstractSimpleRemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:136
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::getProxy
Ice::ObjectPrx getProxy() const
Definition: RemoteReferenceCount.h:67
detail
Definition: OpenCVUtil.cpp:127
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::isCountingActivated
bool isCountingActivated() const
Definition: RemoteReferenceCount.h:59
IceUtil
Definition: Instance.h:21
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::onCountReachedZero
virtual void onCountReachedZero()=0
armarx::detail::AbstractRemoteReferenceCountControlBlock::addCounter
void addCounter(const std::string &counterId, const Ice::Current &=Ice::emptyCurrent) final override
Definition: RemoteReferenceCount.cpp:376
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::nextCheckTimePoint
virtual IceUtil::Time nextCheckTimePoint()=0
Returns the next timepoint when this ControlBlock should be checked.
armarx::detail::RemoteReferenceCountControlBlockManagementInterfacePtr
IceUtil::Handle< detail::RemoteReferenceCountControlBlockManagementInterface > RemoteReferenceCountControlBlockManagementInterfacePtr
Definition: RemoteReferenceCount.h:101
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::SimpleRemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:210
armarx::RemoteReferenceCountControlBlockManager
Definition: RemoteReferenceCount.h:250
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::countReachedZero
void countReachedZero()
Definition: RemoteReferenceCount.cpp:368
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::RemoteReferenceCountControlBlockManagementInterface
RemoteReferenceCountControlBlockManagementInterface(const ArmarXManagerPtr &manager, const std::string &id)
Definition: RemoteReferenceCount.cpp:338
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::detail::AbstractRemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:107
armarx::detail
Definition: ApplicationNetworkStats.cpp:34
armarx::detail::RemoteReferenceCountControlBlockManagementInterface
Definition: RemoteReferenceCount.h:51
armarx::RemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:162
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::id
const std::string id
Definition: RemoteReferenceCount.h:95
armarx::detail::AbstractRemoteReferenceCountControlBlock::getReferenceCounter
RemoteReferenceCounterBasePtr getReferenceCounter()
Definition: RemoteReferenceCount.cpp:445
armarx::detail::AbstractRemoteReferenceCountControlBlock::heartbeat
void heartbeat(const std::string &counterId, const Ice::Current &=Ice::emptyCurrent) final override
Definition: RemoteReferenceCount.cpp:390
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::hasCountReachedZero
bool hasCountReachedZero() const
Definition: RemoteReferenceCount.h:63
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::lastTimeReachedZero
IceUtil::Time lastTimeReachedZero
Definition: RemoteReferenceCount.h:93
std
Definition: Application.h:66
set
set(LIBS ArmarXCoreInterfaces ${CMAKE_THREAD_LIBS_INIT} ${dl_LIBRARIES} ${rt_LIBRARIES} ${QT_LIBRARIES} ${Boost_LIBRARIES} BoostAssertionHandler ArmarXCPPUtility SimoxUtility) set(LIB_FILES ArmarXManager.cpp ArmarXMultipleObjectsScheduler.cpp ArmarXObjectScheduler.cpp ManagedIceObject.cpp ManagedIceObjectPlugin.cpp Component.cpp ComponentPlugin.cpp IceGridAdmin.cpp ArmarXObjectObserver.cpp IceManager.cpp PackagePath.cpp RemoteReferenceCount.cpp logging/LoggingUtil.cpp logging/Logging.cpp logging/LogSender.cpp logging/ArmarXLogBuf.cpp system/ArmarXDataPath.cpp system/DynamicLibrary.cpp system/ProcessWatcher.cpp system/FactoryCollectionBase.cpp system/cmake/CMakePackageFinder.cpp system/cmake/CMakePackageFinderCache.cpp system/cmake/ArmarXPackageToolInterface.cpp system/RemoteObjectNode.cpp services/sharedmemory/HardwareId.cpp services/tasks/RunningTask.cpp services/tasks/ThreadList.cpp services/tasks/ThreadPool.cpp services/profiler/Profiler.cpp services/profiler/FileLoggingStrategy.cpp services/profiler/IceLoggingStrategy.cpp application/Application.cpp application/ApplicationOptions.cpp application/ApplicationProcessFacet.cpp application/ApplicationNetworkStats.cpp application/properties/PropertyUser.cpp application/properties/Property.cpp application/properties/PropertyDefinition.cpp application/properties/PropertyDefinitionContainer.cpp application/properties/PropertyDefinitionHelpFormatter.cpp application/properties/PropertyDefinitionConfigFormatter.cpp application/properties/PropertyDefinitionBriefHelpFormatter.cpp application/properties/PropertyDefinitionXmlFormatter.cpp application/properties/PropertyDefinitionDoxygenFormatter.cpp application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.cpp application/properties/PropertyDefinitionContainerBriefHelpFormatter.cpp application/properties/IceProperties.cpp exceptions/Exception.cpp exceptions/local/UnexpectedEnumValueException.cpp util/FileSystemPathBuilder.cpp util/StringHelpers.cpp util/IceReportSkipper.cpp util/Throttler.cpp util/distributed/AMDCallbackCollection.cpp util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.cpp util/distributed/RemoteHandle/RemoteHandle.cpp util/distributed/RemoteHandle/RemoteHandleControlBlock.cpp time/ice_conversions.cpp time/json_conversions.cpp time/CallbackWaitLock.cpp time/Clock.cpp time/ClockType.cpp time/ClockTypeNames.cpp time/CycleUtil.cpp time/DateTime.cpp time/Duration.cpp time/Frequency.cpp time/LocalTimeServer.cpp time/Metronome.cpp time/ScopedStopWatch.cpp time/StopWatch.cpp time/Timer.cpp time/TimeKeeper.cpp time/TimeUtil.cpp csv/CsvWriter.cpp csv/CsvReader.cpp eigen/conversions.cpp eigen/ice_conversions.cpp) set(LIB_HEADERS ArmarXManager.h ArmarXDummyManager.h ArmarXMultipleObjectsScheduler.h ArmarXObjectObserver.h ArmarXObjectScheduler.h ArmarXFwd.h Component.h ComponentPlugin.h ComponentFactories.h CoreObjectFactories.h IceGridAdmin.h IceManager.h IceManagerImpl.h json_conversions.h ManagedIceObject.h ManagedIceObjectPlugin.h ManagedIceObjectImpl.h ManagedIceObjectDependency.h ManagedIceObjectRegistryInterface.h PackagePath.h RemoteReferenceCount.h system/ImportExport.h system/ImportExportComponent.h system/AbstractFactoryMethod.h system/FactoryCollectionBase.h system/Synchronization.h system/ArmarXDataPath.h system/DynamicLibrary.h system/ProcessWatcher.h system/ConditionSynchronization.h system/cmake/CMakePackageFinder.h system/cmake/CMakePackageFinderCache.h system/cmake/FindPackageX.cmake system/cmake/ArmarXPackageToolInterface.h system/RemoteObjectNode.h logging/LoggingUtil.h logging/LogSender.h logging/Logging.h logging/ArmarXLogBuf.h logging/SpamFilterData.h services/tasks/RunningTask.h services/tasks/PeriodicTask.h services/tasks/ThreadList.h services/tasks/TaskUtil.h services/tasks/ThreadPool.h services/sharedmemory/SharedMemoryProvider.h services/sharedmemory/SharedMemoryConsumer.h services/sharedmemory/IceSharedMemoryProvider.h services/sharedmemory/IceSharedMemoryConsumer.h services/sharedmemory/HardwareIdentifierProvider.h services/sharedmemory/HardwareId.h services/sharedmemory/exceptions/SharedMemoryExceptions.h services/profiler/Profiler.h services/profiler/LoggingStrategy.h services/profiler/FileLoggingStrategy.h services/profiler/IceLoggingStrategy.h application/Application.h application/ApplicationOptions.h application/ApplicationProcessFacet.h application/ApplicationNetworkStats.h application/properties/forward_declarations.h application/properties/Properties.h application/properties/Property.h application/properties/PluginEigen.h application/properties/PluginEnumNames.h application/properties/PluginCfgStruct.h application/properties/PluginAll.h application/properties/PropertyUser.h application/properties/PropertyDefinition.h application/properties/PropertyDefinition.hpp application/properties/PropertyDefinitionInterface.h application/properties/PropertyDefinitionContainer.h application/properties/PropertyDefinitionFormatter.h application/properties/PropertyDefinitionContainerFormatter.h application/properties/PropertyDefinitionConfigFormatter.h application/properties/PropertyDefinitionHelpFormatter.h application/properties/PropertyDefinitionBriefHelpFormatter.h application/properties/PropertyDefinitionXmlFormatter.h application/properties/PropertyDefinitionDoxygenFormatter.h application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.h application/properties/PropertyDefinitionContainerBriefHelpFormatter.h application/properties/ProxyPropertyDefinition.h application/properties/IceProperties.h exceptions/Exception.h exceptions/LocalException.h exceptions/local/DynamicLibraryException.h exceptions/local/ExpressionException.h exceptions/local/FileIOException.h exceptions/local/InvalidPropertyValueException.h exceptions/local/MissingRequiredPropertyException.h exceptions/local/PropertyInheritanceCycleException.h exceptions/local/ProxyNotInitializedException.h exceptions/local/UnexpectedEnumValueException.h exceptions/local/UnmappedValueException.h exceptions/local/ValueRangeExceededException.h exceptions/user/NotImplementedYetException.h rapidxml/rapidxml.hpp rapidxml/rapidxml_print.hpp rapidxml/rapidxml_iterators.hpp rapidxml/rapidxml_utils.hpp rapidxml/wrapper/RapidXmlReader.h rapidxml/wrapper/RapidXmlWriter.h rapidxml/wrapper/DefaultRapidXmlReader.h rapidxml/wrapper/MultiNodeRapidXMLReader.h util/IceBlobToObject.h util/ObjectToIceBlob.h util/FileSystemPathBuilder.h util/FiniteStateMachine.h util/StringHelpers.h util/StringHelperTemplates.h util/algorithm.h util/OnScopeExit.h util/Predicates.h util/Preprocessor.h util/PropagateConst.h util/Registrar.h util/TemplateMetaProgramming.h util/TripleBuffer.h util/IceReportSkipper.h util/Throttler.h util/distributed/AMDCallbackCollection.h util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.h util/distributed/RemoteHandle/RemoteHandle.h util/distributed/RemoteHandle/RemoteHandleControlBlock.h util/SimpleStatemachine.h time.h time_minimal.h time/forward_declarations.h time/ice_conversions.h time/json_conversions.h time/CallbackWaitLock.h time/Clock.h time/ClockType.h time/ClockTypeNames.h time/CycleUtil.h time/DateTime.h time/Duration.h time/Frequency.h time/LocalTimeServer.h time/Metronome.h time/ScopedStopWatch.h time/StopWatch.h time/Timer.h time/TimeUtil.h time/TimeKeeper.h csv/CsvWriter.h csv/CsvReader.h eigen/conversions.h eigen/ice_conversions.h ice_conversions.h ice_conversions/ice_conversions_boost_templates.h ice_conversions/ice_conversions_templates.h ice_conversions/ice_conversions_templates.tpp $
Definition: CMakeLists.txt:12
IceUtil::Handle< ArmarXManager >
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::selfProxy
Ice::ObjectPrx selfProxy
Definition: RemoteReferenceCount.h:90
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::armarXManager
ArmarXManagerPtr armarXManager
Definition: RemoteReferenceCount.h:89
Logging.h
armarx::RemoteReferenceCountControlBlockManager::~RemoteReferenceCountControlBlockManager
~RemoteReferenceCountControlBlockManager()
Definition: RemoteReferenceCount.h:254
armarx::RemoteReferenceCountControlBlockManager::RemoteReferenceCountControlBlockManager
RemoteReferenceCountControlBlockManager(IceUtil::Time period)
Definition: RemoteReferenceCount.h:253
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::mtx
std::mutex mtx
Definition: RemoteReferenceCount.h:92
armarx::ArmarXManager
Main class of an ArmarX process.
Definition: ArmarXManager.h:97
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::detail::RemoteReferenceCountControlBlockManagementInterface::~RemoteReferenceCountControlBlockManagementInterface
~RemoteReferenceCountControlBlockManagementInterface() override
Definition: RemoteReferenceCount.cpp:348