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 <MemoryX/interface/core/MemoryInterface.h>
26 
27 #include <atomic>
28 #include <shared_mutex>
29 #include <mutex>
30 
31 #define MEMORYX_TOKEN(lock) {{"token", lock->token}}
32 
33 namespace memoryx
34 {
35  class SegmentLock :
36  public SegmentLockBase
37  {
38  protected:
39  ~SegmentLock() override;
40  public:
41  SegmentLock(bool start_watchdog = false);
42 
43  // SegmentLockBase interface
44  void unlock(const Ice::Current& c = Ice::emptyCurrent) override;
45  void ice_postUnmarshal() override;
46  void startWatchdog();
47 
48  protected:
49  void keepAlive();
51  };
52 
53 
55  virtual public EntityMemorySegmentInterface
56  {
57  public:
59 
60  public:
61  using ScopedSharedLock = std::unique_lock<std::shared_mutex>;
62  using ScopedSharedLockPtr = std::unique_ptr<ScopedSharedLock>;
63  using ScopedUniqueLock = std::unique_lock<std::shared_mutex>;
64  using ScopedUniqueLockPtr = std::unique_ptr<ScopedSharedLock>;
65 
66  // EntityMemorySegmentInterface interface
67  ScopedSharedLockPtr getReadLock(const Ice::Current& c) const;
68  ScopedUniqueLockPtr getWriteLock(const Ice::Current& c) const;
69  std::string getJSONEntityById(const std::string& id, const Ice::Current&) const override;
70  EntityBaseList getEntityWithChildrenById(const std::string& id, bool includeMetaEntities, const Ice::Current& c = Ice::emptyCurrent) const override;
71  EntityBaseList getEntityWithChildrenByName(const std::string&, bool includeMetaEntities, const Ice::Current& c = Ice::emptyCurrent) const override;
72 
73  SegmentLockBasePtr lockSegment(const Ice::Current& c) override;
74  bool keepLockAlive(const std::string& token, const Ice::Current&) override;
75  bool unlockSegment(const SegmentLockBasePtr& lock, const Ice::Current& c) override;
76  bool unlockSegmentWithToken(const std::string& token, const Ice::Current& c) override;
77 
78  protected:
79  void keepAliveCheck();
80 
81  mutable std::mutex mutex;
82 
83  EntityMemorySegmentInterfacePrx selfProxy;
86  mutable std::shared_mutex segmentMutex;
89  mutable std::mutex tokenMutex;
90  std::string lockToken;
92  };
93 
94 } // namespace memoryx
95 
memoryx::SegmentUtilImplementations::getJSONEntityById
std::string getJSONEntityById(const std::string &id, const Ice::Current &) const override
Definition: SegmentUtilImplementations.cpp:67
memoryx::SegmentUtilImplementations::ScopedSharedLock
std::unique_lock< std::shared_mutex > ScopedSharedLock
Definition: SegmentUtilImplementations.h:61
memoryx::SegmentLock::ice_postUnmarshal
void ice_postUnmarshal() override
Definition: SegmentUtilImplementations.cpp:287
memoryx::SegmentUtilImplementations::unlockSegment
bool unlockSegment(const SegmentLockBasePtr &lock, const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:193
memoryx::SegmentUtilImplementations::readLock
ScopedSharedLockPtr readLock
Definition: SegmentUtilImplementations.h:84
memoryx::SegmentUtilImplementations::getReadLock
ScopedSharedLockPtr getReadLock(const Ice::Current &c) const
Definition: SegmentUtilImplementations.cpp:45
memoryx::SegmentUtilImplementations::SegmentUtilImplementations
SegmentUtilImplementations()
Definition: SegmentUtilImplementations.cpp:41
memoryx::SegmentUtilImplementations::keepAliveTimestamp
IceUtil::Time keepAliveTimestamp
Definition: SegmentUtilImplementations.h:88
memoryx::SegmentUtilImplementations
Definition: SegmentUtilImplementations.h:54
memoryx::SegmentUtilImplementations::keepAliveCheckTask
armarx::PeriodicTask< SegmentUtilImplementations >::pointer_type keepAliveCheckTask
Definition: SegmentUtilImplementations.h:91
PeriodicTask.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::SegmentUtilImplementations::segmentMutex
std::shared_mutex segmentMutex
Definition: SegmentUtilImplementations.h:86
memoryx::SegmentUtilImplementations::keepAliveCheck
void keepAliveCheck()
Definition: SegmentUtilImplementations.cpp:229
memoryx::SegmentUtilImplementations::mutex
std::mutex mutex
Definition: SegmentUtilImplementations.h:81
memoryx::SegmentUtilImplementations::keepAliveTimestampMutex
std::mutex keepAliveTimestampMutex
Definition: SegmentUtilImplementations.h:87
memoryx::SegmentUtilImplementations::lockToken
std::string lockToken
Definition: SegmentUtilImplementations.h:90
memoryx::SegmentUtilImplementations::ScopedUniqueLock
std::unique_lock< std::shared_mutex > ScopedUniqueLock
Definition: SegmentUtilImplementations.h:63
memoryx::SegmentUtilImplementations::lockSegment
SegmentLockBasePtr lockSegment(const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:149
memoryx::SegmentUtilImplementations::tokenMutex
std::mutex tokenMutex
Definition: SegmentUtilImplementations.h:89
memoryx::SegmentUtilImplementations::getWriteLock
ScopedUniqueLockPtr getWriteLock(const Ice::Current &c) const
Definition: SegmentUtilImplementations.cpp:56
memoryx::SegmentLock::keepAliveTask
armarx::PeriodicTask< SegmentLock >::pointer_type keepAliveTask
Definition: SegmentUtilImplementations.h:50
memoryx::SegmentUtilImplementations::getEntityWithChildrenByName
EntityBaseList getEntityWithChildrenByName(const std::string &, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SegmentUtilImplementations.cpp:136
memoryx::SegmentLock
Definition: SegmentUtilImplementations.h:35
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
memoryx::SegmentLock::~SegmentLock
~SegmentLock() override
Definition: SegmentUtilImplementations.cpp:247
memoryx::SegmentUtilImplementations::selfProxy
EntityMemorySegmentInterfacePrx selfProxy
Definition: SegmentUtilImplementations.h:83
memoryx::SegmentUtilImplementations::writeLock
ScopedUniqueLockPtr writeLock
Definition: SegmentUtilImplementations.h:85
memoryx::SegmentUtilImplementations::ScopedUniqueLockPtr
std::unique_ptr< ScopedSharedLock > ScopedUniqueLockPtr
Definition: SegmentUtilImplementations.h:64
IceUtil::Handle
Definition: forward_declarations.h:29
memoryx::SegmentLock::startWatchdog
void startWatchdog()
Definition: SegmentUtilImplementations.cpp:281
memoryx::SegmentUtilImplementations::getEntityWithChildrenById
EntityBaseList getEntityWithChildrenById(const std::string &id, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SegmentUtilImplementations.cpp:79
memoryx::SegmentUtilImplementations::ScopedSharedLockPtr
std::unique_ptr< ScopedSharedLock > ScopedSharedLockPtr
Definition: SegmentUtilImplementations.h:62
memoryx::SegmentLock::unlock
void unlock(const Ice::Current &c=Ice::emptyCurrent) override
Definition: SegmentUtilImplementations.cpp:264
memoryx::SegmentUtilImplementations::unlockSegmentWithToken
bool unlockSegmentWithToken(const std::string &token, const Ice::Current &c) override
Definition: SegmentUtilImplementations.cpp:212
memoryx::SegmentLock::SegmentLock
SegmentLock(bool start_watchdog=false)
Definition: SegmentUtilImplementations.cpp:256
memoryx::SegmentUtilImplementations::keepLockAlive
bool keepLockAlive(const std::string &token, const Ice::Current &) override
Definition: SegmentUtilImplementations.cpp:173
memoryx::SegmentLock::keepAlive
void keepAlive()
Definition: SegmentUtilImplementations.cpp:292