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
39namespace 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;
98 IceUtil::Time keepAliveTimestamp;
99 mutable std::mutex tokenMutex;
100 std::string lockToken;
102 };
103
104} // namespace memoryx
constexpr T c
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
armarx::PeriodicTask< SegmentLock >::pointer_type keepAliveTask
void unlock(const Ice::Current &c=Ice::emptyCurrent) override
SegmentLock(bool start_watchdog=false)
std::unique_lock< std::shared_mutex > ScopedUniqueLock
std::string getJSONEntityById(const std::string &id, const Ice::Current &) const override
bool unlockSegmentWithToken(const std::string &token, const Ice::Current &c) override
std::unique_ptr< ScopedSharedLock > ScopedSharedLockPtr
bool unlockSegment(const SegmentLockBasePtr &lock, const Ice::Current &c) override
EntityBaseList getEntityWithChildrenByName(const std::string &, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
EntityMemorySegmentInterfacePrx selfProxy
ScopedSharedLockPtr getReadLock(const Ice::Current &c) const
ScopedUniqueLockPtr getWriteLock(const Ice::Current &c) const
std::unique_lock< std::shared_mutex > ScopedSharedLock
bool keepLockAlive(const std::string &token, const Ice::Current &) override
armarx::PeriodicTask< SegmentUtilImplementations >::pointer_type keepAliveCheckTask
std::unique_ptr< ScopedSharedLock > ScopedUniqueLockPtr
EntityBaseList getEntityWithChildrenById(const std::string &id, bool includeMetaEntities, const Ice::Current &c=Ice::emptyCurrent) const override
SegmentLockBasePtr lockSegment(const Ice::Current &c) override
VirtualRobot headers.