3 #include "Arp/System/Core/TypeName.hxx"
4 #include "Arp/System/Core/AppDomainSingleton.hxx"
5 #include "Arp/System/Commons/Ipc/SharedMemory.hpp"
6 #include "Arp/System/Commons/Threading/Thread.hpp"
7 #include "boost/interprocess/allocators/allocator.hpp"
8 #include "boost/interprocess/containers/string.hpp"
9 #include "boost/interprocess/containers/vector.hpp"
10 #include "boost/interprocess/containers/list.hpp"
11 #include "boost/interprocess/containers/map.hpp"
12 #include "boost/interprocess/containers/set.hpp"
16 #ifdef ARP_PLATFORM_LINUX
20 namespace Arp {
namespace System {
namespace Commons {
namespace Ipc
26 template<
class Derived>
30 using Base = SharedMemory;
31 using DerivedType = Derived;
33 using SharedMemory = boost::interprocess::managed_shared_memory;
34 using SegmentManager = SharedMemory::segment_manager;
35 using SharedMemoryPermission = boost::interprocess::permissions;
39 using Ptr = boost::interprocess::offset_ptr<T>;
42 using ConstPtr = boost::interprocess::offset_ptr<const T>;
51 static void CreateInstance(
size_t memorySize);
52 static void OpenInstance(
void);
53 static void CloseInstance(
bool removeMemory =
false);
54 static void Remove(
void);
58 static void WaitInstance(
const char* waitMessage,
uint32 timeout = -1);
66 class Allocator :
public boost::interprocess::allocator<T, SegmentManager>
69 using AllocatorBase = boost::interprocess::allocator<T, SegmentManager>;
70 using segment_manager = SegmentManager;
71 using value_type =
typename AllocatorBase::value_type;
72 using pointer =
typename AllocatorBase::pointer;
73 using const_pointer =
typename AllocatorBase::const_pointer;
74 using void_pointer =
typename AllocatorBase::void_pointer;
75 using reference =
typename AllocatorBase::reference;
76 using const_reference =
typename AllocatorBase::const_reference;
77 using size_type =
typename AllocatorBase::size_type;
78 using difference_type =
typename AllocatorBase::difference_type;
79 using version =
typename AllocatorBase::version;
95 #ifdef ARP_PLATFORM_LINUX
101 pthread_mutexattr_t attr;
102 pthread_mutexattr_init(&attr);
104 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
105 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
106 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
108 pthread_mutex_init(&this->mutex, &attr);
110 pthread_mutexattr_destroy(&attr);
118 pthread_mutex_destroy(&this->mutex);
124 pthread_mutex_lock(&this->mutex);
128 pthread_mutex_unlock(&this->mutex);
132 pthread_mutex_t mutex;
138 Mutex(
void) =
default;
139 ~
Mutex(
void) =
default;
148 this->mutex.unlock();
152 boost::interprocess::interprocess_mutex mutex;
168 template <
class TMutex>
174 this->pMutex->Lock();
185 this->pMutex->Unlock();
195 using String = boost::interprocess::basic_string<char, std::char_traits<char>,
Allocator<char>>;
198 using vector = boost::interprocess::vector<T, Allocator<T>>;
201 using list = boost::interprocess::list<T, Allocator<T>>;
203 template <
class TKey,
class T,
class TCompare = std::less<TKey>>
204 using map = boost::interprocess::map<TKey, T, TCompare, Allocator<std::pair<const TKey, T>>>;
206 template <
class T,
class TCompare = std::less<T>>
207 using set = boost::interprocess::set<T, TCompare, Allocator<T>>;
212 template<
class Derived>
214 : Base(
TypeName<DerivedType>().GetSafeName())
218 template<
class Derived>
219 inline SharedData<Derived>::SharedData(
size_t memorySize)
220 : Base(
TypeName<DerivedType>().GetSafeName(), memorySize)
224 template<
class Derived>
225 void SharedData<Derived>::CreateInstance(
size_t memorySize)
231 template<
class Derived>
232 void SharedData<Derived>::OpenInstance()
238 template<
class Derived>
239 void SharedData<Derived>::WaitInstance(
const char* waitMessage,
uint32 )
246 bool instanceExists =
false;
252 instanceExists =
true;
256 Log::Debug(waitMessage);
260 }
while (!instanceExists);
263 template<
class Derived>
264 void SharedData<Derived>::CloseInstance(
bool removeMemory)
278 template<
class Derived>
279 void SharedData<Derived>::Remove()
281 (void)boost::interprocess::shared_memory_object::remove(TypeName<DerivedType>().GetSafeName());
This class implements the singleton pattern for singletons with process wide scope.
Definition: AppDomainSingleton.hxx:25
static Derived & GetInstance(void)
Gets a reference of the singleton instance.
Definition: AppDomainSingleton.hxx:102
static bool IsCreated(void)
Determines if this singleton instance is created yet.
Definition: AppDomainSingleton.hxx:91
static Derived & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: AppDomainSingleton.hxx:74
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: AppDomainSingleton.hxx:119
Definition: SharedData.hxx:67
Definition: SharedData.hxx:136
Definition: SharedData.hxx:157
Definition: SharedData.hxx:170
Definition: SharedData.hxx:28
SharedMemoryImpl sharedMemoryImpl
Actual implementation of the shared memory functionality.
Definition: SharedMemory.hpp:141
const Arp::String & GetName(void) const
Returns the name of the memory object.
Definition: SharedMemory.hpp:156
Mutual exclusion object to prevent data from concurrent modifications.
Definition: Mutex.hpp:26
static void Sleep(size_t milliseconds)
Suspends the execution of the calling thread.
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:56
std::uint32_t uint32
The Arp unsigned integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:35
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API
Definition: SharedData.hxx:87