PLCnext API Documentation 25.0.2.69
SharedData.hxx
1#pragma once
3#include "Arp/System/Core/TypeName.hxx"
4#include "Arp/System/Commons/Ipc/SharedMemory.hpp"
5#include "Arp/System/Commons/Threading/Thread.hpp"
6#include "boost/interprocess/allocators/allocator.hpp"
7#include "boost/interprocess/containers/string.hpp"
8#include "boost/interprocess/containers/vector.hpp"
9#include "boost/interprocess/containers/list.hpp"
10#include "boost/interprocess/containers/map.hpp"
11#include "boost/interprocess/containers/set.hpp"
12#include <algorithm> // using std::replace
13#include <new> // using std::new(p)
14
15#ifdef ARP_PLATFORM_LINUX
16 #include <pthread.h>
17#endif
18
19namespace Arp { namespace System { namespace Commons { namespace Ipc
20{
21
22// This class uses CRTP (Curiously recurring template pattern) to implement an own SharedData singleton
23// for each derived class.
24// For CRTP see: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
25template<class Derived>
26class SharedData : public SharedMemory
27{
28public: // usings
29 using Base = SharedMemory;
30 using DerivedType = Derived;
31 using SharedMemory = boost::interprocess::managed_shared_memory;
32 using SegmentManager = SharedMemory::segment_manager;
33 using SharedMemoryPermission = boost::interprocess::permissions;
34
35 // define pointer type
36 template <class T>
37 using Ptr = boost::interprocess::offset_ptr<T>;
38
39 // define const pointer type
40 template <class T>
41 using ConstPtr = boost::interprocess::offset_ptr<const T>;
42
43protected: // construction/destruction
44 SharedData(void); // opens an existing instance, throws if not exist
45 SharedData(size_t memorySize); // creates an non-existing instance, throws if yet exists
46
47public: // nested type Allocator
48 template <class T>
49 class Allocator : public boost::interprocess::allocator<T, SegmentManager>
50 {
51 public: // typedef
52 using AllocatorBase = boost::interprocess::allocator<T, SegmentManager>;
53 using segment_manager = SegmentManager;
54 using value_type = typename AllocatorBase::value_type;
55 using pointer = typename AllocatorBase::pointer;
56 using const_pointer = typename AllocatorBase::const_pointer;
57 using void_pointer = typename AllocatorBase::void_pointer;
58 using reference = typename AllocatorBase::reference;
59 using const_reference = typename AllocatorBase::const_reference;
60 using size_type = typename AllocatorBase::size_type;
61 using difference_type = typename AllocatorBase::difference_type;
62 using version = typename AllocatorBase::version;
63
64 public: // construction
65 Allocator(void) : AllocatorBase(DerivedType::GetInstance().sharedMemoryImpl.get_segment_manager()) {}
66
67 public: // meta-programming
68 template<class U>
69 struct rebind
70 {
71 typedef Allocator<U> other;
72 };
73 };
74 // declare Allocator as friend of this class
75 template<class T> friend class Allocator;
76
77public: // nested type Mutex
78#ifdef ARP_PLATFORM_LINUX
79 class Mutex
80 {
81 public: // construction
82 Mutex(void)
83 {
84 pthread_mutexattr_t attr;
85 pthread_mutexattr_init(&attr);
86
87 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
88 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
89 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
90
91 pthread_mutex_init(&this->mutex, &attr);
92
93 pthread_mutexattr_destroy(&attr);
94 }
95 Mutex(const Mutex&) = delete;
96 Mutex(Mutex&&) = delete;
97 Mutex& operator=(const Mutex&) = delete;
98 Mutex& operator=(Mutex&&) = delete;
99 ~Mutex(void)
100 {
101 pthread_mutex_destroy(&this->mutex);
102 }
103
104 public: // Operations
105 void Lock(void)
106 {
107 pthread_mutex_lock(&this->mutex);
108 }
109 bool TryLock(void)
110 {
111 return (pthread_mutex_trylock(&this->mutex) == 0);
112 }
113 void Unlock(void)
114 {
115 pthread_mutex_unlock(&this->mutex);
116 }
117
118 private: // fields
119 pthread_mutex_t mutex;
120 };
121#else
122 class Mutex
123 {
124 public: // construction
125 Mutex(void) = default;
126 ~Mutex(void) = default;
127
128 public: // Operations
129 void Lock(void)
130 {
131 this->mutex.lock();
132 }
133 bool TryLock(void)
134 {
135 return this->mutex.try_lock();
136 }
137 void Unlock(void)
138 {
139 this->mutex.unlock();
140 }
141
142 private: // fields
143 boost::interprocess::interprocess_mutex mutex;
144 };
145#endif
146
148 {
149 public: // construction
150 NullMutex(void) = default;
151 ~NullMutex(void) = default;
152
153 public: // Operations
154 void Lock(void) {}
155 void Unlock(void) {}
156 };
157
158public: // nested type ScopedLock
159 template <class TMutex>
161 {
162 public: // construction
163 ScopedLock(TMutex& mutex): pMutex(&mutex)
164 {
165 this->pMutex->Lock();
166 }
167 ScopedLock(const ScopedLock&) = delete;
168 ScopedLock(ScopedLock&&) = delete;
169 ScopedLock& operator=(const ScopedLock&) = delete;
170 ScopedLock& operator=(ScopedLock&&) = delete;
171
172 ~ScopedLock(void)
173 {
174 if (this->pMutex)
175 {
176 this->pMutex->Unlock();
177 }
178 }
179
180 private: // fields
181 TMutex* pMutex;
182 };
183
184public: // usings on nested types
185 // define shared string type
186 using String = boost::interprocess::basic_string<char, std::char_traits<char>, Allocator<char>>;
187 // define shared vector type
188 template <class T>
189 using vector = boost::interprocess::vector<T, Allocator<T>>;
190 // define shared list type
191 template <class T>
192 using list = boost::interprocess::list<T, Allocator<T>>;
193 // define shared map type
194 template <class TKey, class T, class TCompare = std::less<TKey>>
195 using map = boost::interprocess::map<TKey, T, TCompare, Allocator<std::pair<const TKey, T>>>;
196 // define shared set type
197 template <class T, class TCompare = std::less<T>>
198 using set = boost::interprocess::set<T, TCompare, Allocator<T>>;
199};
200
202// inline methods of class SharedData<Derived>
203template<class Derived>
205 : Base(TypeName<DerivedType>().GetSafeName())
206{
207}
208
209template<class Derived>
210inline SharedData<Derived>::SharedData(size_t memorySize)
211 : Base(TypeName<DerivedType>().GetSafeName(), memorySize)
212{
213}
214
216// inline methods of nested class SharedData<Derived>::Allocator<T>
217
218
219}}}} // end of namespace Arp::System::Commons::Ipc
This (meta programming) class provides the C++ type-name of the as template argument passed type.
Definition: TypeName.hxx:20
Definition: SharedData.hxx:123
Definition: SharedData.hxx:27
SharedMemoryImpl sharedMemoryImpl
Actual implementation of the shared memory functionality.
Definition: SharedMemory.hpp:74
Mutual exclusion object to prevent data from concurrent modifications.
Definition: Mutex.hpp:27
Root namespace for the PLCnext API