SHM
Shared-memory based Handy-communication Manager
shm_base.hpp
Go to the documentation of this file.
1 
10 #ifndef __SHM_BASE_LIB_H__
11 #define __SHM_BASE_LIB_H__
12 
13 #include <iostream>
14 #include <limits>
15 #include <string>
16 #include <regex>
17 #include <stdexcept>
18 #include <mutex>
19 #include <atomic>
20 extern "C" {
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/time.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/ipc.h>
29 #include <sys/shm.h>
30 }
31 
32 namespace irlab
33 {
34 
35 namespace shm
36 {
41  enum PERM : mode_t
42  {
43  PERM_USER_READ = S_IRUSR,
47  PERM_USER_WRITE = S_IWUSR,
51  PERM_GROUP_READ = S_IRGRP,
55  PERM_GROUP_WRITE = S_IWGRP,
59  PERM_OTHER_READ = S_IROTH,
63  PERM_OTHER_WRITE = S_IWOTH,
67  };
68  const PERM DEFAULT_PERM = static_cast<PERM>(PERM_USER_READ | PERM_USER_WRITE | PERM_GROUP_READ | PERM_GROUP_WRITE | PERM_OTHER_READ | PERM_OTHER_WRITE);
69 
70 
71 // ****************************************************************************
72 // Function Declarations
73 // 関数宣言
74 // ****************************************************************************
75 
76 int disconnectMemory(std::string name);
77 int disconnectMemory(int id);
78 
79 // ****************************************************************************
84 // ****************************************************************************
86 {
87 public:
88  SharedMemory(int oflag, PERM perm);
89  virtual ~SharedMemory() noexcept = default;
90 
91  virtual bool connect(size_t size = 0) = 0;
92  virtual int disconnect() = 0;
93  size_t getSize() const;
94  unsigned char* getPtr();
95 
96  virtual bool isDisconnected() const = 0;
97 
98 protected:
99  int shm_fd;
100  int shm_oflag;
101  PERM shm_perm;
102  size_t shm_size;
103  unsigned char *shm_ptr;
104 };
105 
106 
107 // ****************************************************************************
112 // ****************************************************************************
114 {
115 public:
116  SharedMemoryPosix(std::string name, int oflag, PERM perm);
118 
119  virtual bool connect(size_t size = 0);
120  virtual int disconnect();
121 
122  virtual bool isDisconnected() const;
123 
124 protected:
125  std::string shm_name;
126 };
127 
128 
129 
130 // ****************************************************************************
135 // ****************************************************************************
137 {
138 public:
139  static size_t getSize(size_t element_size, int buffer_num);
140 
141  RingBuffer(unsigned char* first_ptr, size_t size = 0, int buffer_num = 0);
142  ~RingBuffer();
143 
144  const uint64_t getTimestamp_us() const;
145  void setTimestamp_us(uint64_t input_time_us, int buffer_num);
146  int getNewestBufferNum();
147  int getOldestBufferNum();
148  bool allocateBuffer(int buffer_num);
149  size_t getElementSize() const;
150  unsigned char* getDataList();
151  void signal();
152  bool waitFor(uint64_t timeout_usec);
153  bool isUpdated() const;
154  void setDataExpiryTime_us(uint64_t time_us);
155 
156 private:
157  void initializeExclusiveAccess();
158 
159  unsigned char *memory_ptr;
160 
161  pthread_mutex_t *mutex;
162  pthread_cond_t *condition;
163  size_t *element_size;
164  size_t *buf_num;
165  std::atomic<uint64_t> *timestamp_list;
166  unsigned char *data_list;
167 
168  uint64_t timestamp_us;
169  uint64_t data_expiry_time_us;
170 };
171 
172 }
173 
174 }
175 
176 #endif /* __SHM_BASE_LIB_H__ */
Class that is described ring-buffer used for shared memory.
Definition: shm_base.hpp:137
const uint64_t getTimestamp_us() const
タイムスタンプ取得
Definition: ring_buffer.cpp:92
bool isUpdated() const
共有メモリの更新確認
RingBuffer(unsigned char *first_ptr, size_t size=0, int buffer_num=0)
コンストラクタ
Definition: ring_buffer.cpp:21
void setTimestamp_us(uint64_t input_time_us, int buffer_num)
タイムスタンプ取得
bool waitFor(uint64_t timeout_usec)
トピックの更新待ち
Class that abstracts the method of accessing shared memory.
Definition: shm_base.hpp:86
Class that is described the method of accessing POSIX shared memory.
Definition: shm_base.hpp:114
@ PERM_OTHER_WRITE
Definition: shm_base.hpp:63
@ PERM_USER_WRITE
Definition: shm_base.hpp:47
@ PERM_GROUP_READ
Definition: shm_base.hpp:51
@ PERM_OTHER_READ
Definition: shm_base.hpp:59
@ PERM_USER_READ
Definition: shm_base.hpp:43
@ PERM_GROUP_WRITE
Definition: shm_base.hpp:55