SegmentUtilImplementations.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX
17 * @author Mirko Waechter( mirko.waechter at kit dot edu)
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 #pragma once
23 
24 #include <atomic>
25 #include <mutex>
26 #include <shared_mutex>
27 
29 
30 #include <MemoryX/interface/core/MemoryInterface.h>
31 
32 #define MEMORYX_TOKEN(lock) \
33  { \
34  { \
35  "token", lock->token \
36  } \
37  }
38 
39 namespace memoryx
40 {
41  class SegmentLock : public SegmentLockBase
42  {
43  protected:
44  ~SegmentLock() override;
45 
46  public:
47  SegmentLock(bool start_watchdog = false);
48 
49  // SegmentLockBase interface
50  void unlock(const Ice::Current& c = Ice::emptyCurrent) override;
51  void ice_postUnmarshal() override;
52  void startWatchdog();
53 
54  protected:
55  void keepAlive();
57  };
58 
59  class SegmentUtilImplementations : virtual public EntityMemorySegmentInterface
60  {
61  public:
63 
64  public:
65  using ScopedSharedLock = std::unique_lock<std::shared_mutex>;
66  using ScopedSharedLockPtr = std::unique_ptr<ScopedSharedLock>;
67  using ScopedUniqueLock = std::unique_lock<std::shared_mutex>;
68  using ScopedUniqueLockPtr = std::unique_ptr<ScopedSharedLock>;
69 
70  // EntityMemorySegmentInterface interface
71  ScopedSharedLockPtr getReadLock(const Ice::Current& c) const;
72  ScopedUniqueLockPtr getWriteLock(const Ice::Current& c) const;
73  std::string getJSONEntityById(const std::string& id, const Ice::Current&) const override;
74  EntityBaseList
75  getEntityWithChildrenById(const std::string& id,
76  bool includeMetaEntities,
77  const Ice::Current& c = Ice::emptyCurrent) const override;
78  EntityBaseList
79  getEntityWithChildrenByName(const std::string&,
80  bool includeMetaEntities,
81  const Ice::Current& c = Ice::emptyCurrent) const override;
82 
83  SegmentLockBasePtr lockSegment(const Ice::Current& c) override;
84  bool keepLockAlive(const std::string& token, const Ice::Current&) override;
85  bool unlockSegment(const SegmentLockBasePtr& lock, const Ice::Current& c) override;
86  bool unlockSegmentWithToken(const std::string& token, const Ice::Current& c) override;
87 
88  protected:
89  void keepAliveCheck();
90 
91  mutable std::mutex mutex;
92 
93  EntityMemorySegmentInterfacePrx selfProxy;
96  mutable std::shared_mutex segmentMutex;
99  mutable std::mutex tokenMutex;
100  std::string lockToken;
102  };
103 
104 } // namespace memoryx
memoryx::SegmentUtilImplementations::getJSONEntityById
std::string getJSONEntityById(const std::string &id, const Ice::Current &) const override
Definition: SegmentUtilImplementations.cpp:69
memoryx::SegmentUtilImplementations::ScopedSharedLock
std::unique_lock< std::shared_mutex > ScopedSharedLock
Definition: SegmentUtilImplementations.h:65
memoryx::SegmentLock::ice_postUnmarshal
void ice_postUnmarshal() override
Definition: SegmentUtilImplementations.cpp:311
memoryx::SegmentUtilImplementations::unlockSegment
bool unlockSegment(const SegmentLockBasePtr &lock, const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:209
memoryx::SegmentUtilImplementations::readLock
ScopedSharedLockPtr readLock
Definition: SegmentUtilImplementations.h:94
memoryx::SegmentUtilImplementations::getReadLock
ScopedSharedLockPtr getReadLock(const Ice::Current &c) const
Definition: SegmentUtilImplementations.cpp:45
memoryx::SegmentUtilImplementations::SegmentUtilImplementations
SegmentUtilImplementations()
Definition: SegmentUtilImplementations.cpp:40
memoryx::SegmentUtilImplementations::keepAliveTimestamp
IceUtil::Time keepAliveTimestamp
Definition: SegmentUtilImplementations.h:98
memoryx::SegmentUtilImplementations
Definition: SegmentUtilImplementations.h:59
memoryx::SegmentUtilImplementations::keepAliveCheckTask
armarx::PeriodicTask< SegmentUtilImplementations >::pointer_type keepAliveCheckTask
Definition: SegmentUtilImplementations.h:101
PeriodicTask.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
memoryx::SegmentUtilImplementations::segmentMutex
std::shared_mutex segmentMutex
Definition: SegmentUtilImplementations.h:96
memoryx::SegmentUtilImplementations::keepAliveCheck
void keepAliveCheck()
Definition: SegmentUtilImplementations.cpp:246
memoryx::SegmentUtilImplementations::mutex
std::mutex mutex
Definition: SegmentUtilImplementations.h:91
memoryx::SegmentUtilImplementations::keepAliveTimestampMutex
std::mutex keepAliveTimestampMutex
Definition: SegmentUtilImplementations.h:97
memoryx::SegmentUtilImplementations::lockToken
std::string lockToken
Definition: SegmentUtilImplementations.h:100
memoryx::SegmentUtilImplementations::ScopedUniqueLock
std::unique_lock< std::shared_mutex > ScopedUniqueLock
Definition: SegmentUtilImplementations.h:67
memoryx::SegmentUtilImplementations::lockSegment
SegmentLockBasePtr lockSegment(const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:156
memoryx::SegmentUtilImplementations::tokenMutex
std::mutex tokenMutex
Definition: SegmentUtilImplementations.h:99
memoryx::SegmentUtilImplementations::getWriteLock
ScopedUniqueLockPtr getWriteLock(const Ice::Current &c) const
Definition: SegmentUtilImplementations.cpp:57
memoryx::SegmentLock::keepAliveTask
armarx::PeriodicTask< SegmentLock >::pointer_type keepAliveTask
Definition: SegmentUtilImplementations.h:56
memoryx::SegmentUtilImplementations::getEntityWithChildrenByName
EntityBaseList getEntityWithChildrenByName(const std::string &, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SegmentUtilImplementations.cpp:140
memoryx::SegmentLock
Definition: SegmentUtilImplementations.h:41
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
memoryx::SegmentLock::~SegmentLock
~SegmentLock() override
Definition: SegmentUtilImplementations.cpp:268
memoryx::SegmentUtilImplementations::selfProxy
EntityMemorySegmentInterfacePrx selfProxy
Definition: SegmentUtilImplementations.h:93
memoryx::SegmentUtilImplementations::writeLock
ScopedUniqueLockPtr writeLock
Definition: SegmentUtilImplementations.h:95
memoryx::SegmentUtilImplementations::ScopedUniqueLockPtr
std::unique_ptr< ScopedSharedLock > ScopedUniqueLockPtr
Definition: SegmentUtilImplementations.h:68
IceUtil::Handle
Definition: forward_declarations.h:30
memoryx::SegmentLock::startWatchdog
void startWatchdog()
Definition: SegmentUtilImplementations.cpp:303
memoryx::SegmentUtilImplementations::getEntityWithChildrenById
EntityBaseList getEntityWithChildrenById(const std::string &id, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SegmentUtilImplementations.cpp:82
memoryx::SegmentUtilImplementations::ScopedSharedLockPtr
std::unique_ptr< ScopedSharedLock > ScopedSharedLockPtr
Definition: SegmentUtilImplementations.h:66
memoryx::SegmentLock::unlock
void unlock(const Ice::Current &c=Ice::emptyCurrent) override
Definition: SegmentUtilImplementations.cpp:286
memoryx::SegmentUtilImplementations::unlockSegmentWithToken
bool unlockSegmentWithToken(const std::string &token, const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:227
memoryx::SegmentLock::SegmentLock
SegmentLock(bool start_watchdog=false)
Definition: SegmentUtilImplementations.cpp:277
memoryx::SegmentUtilImplementations::keepLockAlive
bool keepLockAlive(const std::string &token, const Ice::Current &) override
Definition: SegmentUtilImplementations.cpp:188
memoryx::SegmentLock::keepAlive
void keepAlive()
Definition: SegmentUtilImplementations.cpp:317