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