SharedMemory.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 ...
17 * @author Andre Meixner ( andre dot meixner at kit dot edu )
18 * @date 2024
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <boost/interprocess/creation_tags.hpp>
26#include <boost/interprocess/detail/os_file_functions.hpp>
27#include <boost/interprocess/interprocess_fwd.hpp>
28#include <boost/interprocess/mapped_region.hpp>
29#include <boost/interprocess/shared_memory_object.hpp>
30#include <boost/interprocess/sync/named_semaphore.hpp>
31
33
35{
36 inline boost::interprocess::shared_memory_object
37 InitSHM(const std::string& sharedMemoryName)
38 {
39 boost::interprocess::shared_memory_object::remove(sharedMemoryName.c_str());
40 boost::interprocess::permissions permissions;
41 //permissions.set_unrestricted(); // potential way to activate permissions for other users
42 return boost::interprocess::shared_memory_object(boost::interprocess::create_only,
43 sharedMemoryName.c_str(),
44 boost::interprocess::read_write,
45 permissions);
46 }
47
48 inline boost::interprocess::named_semaphore
49 InitSem(const std::string& semName)
50 {
51 boost::interprocess::named_semaphore::remove(semName.c_str());
52 return boost::interprocess::named_semaphore(
53 boost::interprocess::create_only, semName.c_str(), 0);
54 }
55
56 template <typename T>
58 {
59 public:
60 SharedMemoryWriter(const std::string& sharedMemoryName, const std::string& semName) :
61 sem(InitSem(semName)), shm(InitSHM(sharedMemoryName))
62 {
63 ARMARX_INFO << "Initializing SharedMemoryWriter";
64
65 shm.truncate(T::size());
66 region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
67
68 ARMARX_INFO << "Finished creation of SharedMemoryWriter of size " << T::size();
69 }
70
71 inline void
72 updateNonLocking(const T& data) const
73 {
74 std::memcpy(region.get_address(), &data, sizeof(T));
75 }
76
77 boost::interprocess::named_semaphore sem;
78
79 private:
80 boost::interprocess::shared_memory_object shm;
81 boost::interprocess::mapped_region region;
82 };
83
84 template <typename T>
86 {
87 public:
88 SharedMemoryReader(const std::string& sharedMemoryName, const std::string& semName) :
89 sem(InitSem(semName)), shm(InitSHM(sharedMemoryName))
90 {
91 ARMARX_INFO << "Initializing SharedMemoryReader";
92
93 shm.truncate(T::size());
94 region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
95
96 ARMARX_INFO << "Finished creation of SharedMemoryReader of size" << T::size();
97 }
98
99 inline void
101 {
102 std::memcpy(&data, region.get_address(), T::size());
103 }
104
105 boost::interprocess::named_semaphore sem;
106
107 private:
108 boost::interprocess::shared_memory_object shm;
109 boost::interprocess::mapped_region region;
110 };
111
112} // namespace armarx::control::njoint_controller::task_space
SharedMemoryReader(const std::string &sharedMemoryName, const std::string &semName)
SharedMemoryWriter(const std::string &sharedMemoryName, const std::string &semName)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
boost::interprocess::named_semaphore InitSem(const std::string &semName)
boost::interprocess::shared_memory_object InitSHM(const std::string &sharedMemoryName)