libcluon  0.0.148
SharedMemory.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_SHAREDMEMORY_HPP
10 #define CLUON_SHAREDMEMORY_HPP
11 
12 #include "cluon/cluon.hpp"
13 #include "cluon/cluonDataStructures.hpp"
14 
15 // clang-format off
16 #ifdef WIN32
17  #include <Windows.h>
18 #else
19  #include <pthread.h>
20  #include <sys/ipc.h>
21 #endif
22 // clang-format on
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <atomic>
27 #include <string>
28 #include <utility>
29 
30 namespace cluon {
31 
33  private:
34  SharedMemory(const SharedMemory &) = delete;
35  SharedMemory(SharedMemory &&) = delete;
36  SharedMemory &operator=(const SharedMemory &) = delete;
37  SharedMemory &operator=(SharedMemory &&) = delete;
38 
39  public:
48  SharedMemory(const std::string &name, uint32_t size = 0) noexcept;
49  ~SharedMemory() noexcept;
50 
54  bool isLocked() const noexcept;
55 
59  void lock() noexcept;
60 
64  void unlock() noexcept;
65 
69  void wait() noexcept;
70 
74  void notifyAll() noexcept;
75 
86  bool setTimeStamp(const cluon::data::TimeStamp &ts) noexcept;
87 
95  std::pair<bool, cluon::data::TimeStamp> getTimeStamp() noexcept;
96 
97  public:
101  bool valid() noexcept;
102 
106  char *data() noexcept;
107 
111  uint32_t size() const noexcept;
112 
116  const std::string name() const noexcept;
117 
118 #ifdef WIN32
119  private:
120  void initWIN32() noexcept;
121  void deinitWIN32() noexcept;
122  void lockWIN32() noexcept;
123  void unlockWIN32() noexcept;
124  void waitWIN32() noexcept;
125  void notifyAllWIN32() noexcept;
126 #else
127  private:
128  void initPOSIX() noexcept;
129  void deinitPOSIX() noexcept;
130  void lockPOSIX() noexcept;
131  void unlockPOSIX() noexcept;
132  void waitPOSIX() noexcept;
133  void notifyAllPOSIX() noexcept;
134  bool validPOSIX() noexcept;
135 
136  void initSysV() noexcept;
137  void deinitSysV() noexcept;
138  void lockSysV() noexcept;
139  void unlockSysV() noexcept;
140  void waitSysV() noexcept;
141  void notifyAllSysV() noexcept;
142  bool validSysV() noexcept;
143 #endif
144 
145  private:
146  std::string m_name{""};
147  std::string m_nameForTimeStamping{""};
148  uint32_t m_size{0};
149  char *m_sharedMemory{nullptr};
150  char *m_userAccessibleSharedMemory{nullptr};
151  bool m_hasOnlyAttachedToSharedMemory{false};
152 
153  std::atomic<bool> m_broken{false};
154  std::atomic<bool> m_isLocked{false};
155 
156 #ifdef WIN32
157  HANDLE __conditionEvent{nullptr};
158  HANDLE __mutex{nullptr};
159  HANDLE __sharedMemory{nullptr};
160 #else
161  int32_t m_fdForTimeStamping{-1};
162 
163  bool m_usePOSIX{true};
164 
165  // Member fields for POSIX-based shared memory.
166 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
167  int32_t m_fd{-1};
168  struct SharedMemoryHeader {
169  uint32_t __size;
170  pthread_mutex_t __mutex;
171  pthread_cond_t __condition;
172  };
173  SharedMemoryHeader *m_sharedMemoryHeader{nullptr};
174 #endif
175 
176  // Member fields for SysV-based shared memory.
177  key_t m_shmKeySysV{0};
178  key_t m_mutexKeySysV{0};
179  key_t m_conditionKeySysV{0};
180 
181  int m_sharedMemoryIDSysV{-1};
182  int m_mutexIDSysV{-1};
183  int m_conditionIDSysV{-1};
184 #endif
185 };
186 } // namespace cluon
187 
188 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: SharedMemory.hpp:32