forked from bo-yang/shm_ring_buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshmringbuffer.hh
323 lines (278 loc) · 8.25 KB
/
shmringbuffer.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#ifndef __SHMRINGBUFFER_HH__
#define __SHMRINGBUFFER_HH__
#include <string>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <signal.h>
#include <fcntl.h> /* For O_CREAT, O_RDWR */
#include <sys/mman.h> /* shared memory and mmap() */
#include <sys/stat.h> /* S_IRWXU */
using std::string;
//
// Shared-memory based Ring buffer.
//
// T must be POD type
#define EVENT_BUFFER_SHM "/shm_ring_buffer"
template <typename T>
class ShmRingBuffer {
public:
ShmRingBuffer(size_t cap = 100, bool master = false, const char * path = EVENT_BUFFER_SHM):
_hdr(NULL),_lock(NULL),_v(NULL),_shm_path(path),_shm_size(0),_master(master) {
init(cap, master, path);
}
~ShmRingBuffer() {
if (_hdr)
munmap((void *)_hdr, _shm_size);
_hdr = NULL;
_lock = NULL;
_v = NULL;
//if (_master)
// shm_unlink(_shm_path);
}
size_t capacity() const;
size_t begin() const;
size_t end() const;
void clear(); // clear buffer
void push_back(const T&); // insert new event
T dump_front();
string unparse() const; // dump contents in the buffer to a string
private:
// Mutex, Condition and ReadWriteLock must be POD type to use shared memory
class Mutex {
public:
pthread_mutex_t _mutex;
pthread_mutexattr_t _attr;
void init(bool pshared = false) {
pthread_mutexattr_init(&_attr);
if (pshared)
pthread_mutexattr_setpshared(&_attr, PTHREAD_PROCESS_SHARED);
else
pthread_mutexattr_setpshared(&_attr, PTHREAD_PROCESS_PRIVATE);
pthread_mutex_init(&_mutex, &_attr);
}
int lock() {
return pthread_mutex_lock(&_mutex);
}
int trylock() {
return pthread_mutex_trylock(&_mutex);
}
int unlock() {
return pthread_mutex_unlock(&_mutex);
}
};
class Condition {
public:
pthread_cond_t _cond;
pthread_condattr_t _attr;
void init(bool pshared = false) {
pthread_condattr_init(&_attr);
if (pshared)
pthread_condattr_setpshared(&_attr, PTHREAD_PROCESS_SHARED);
else
pthread_condattr_setpshared(&_attr, PTHREAD_PROCESS_PRIVATE);
pthread_cond_init(&_cond, &_attr);
}
int wait(Mutex &m) {
return pthread_cond_wait(&_cond, &m._mutex);
}
int timedwait(const struct timespec &ts, Mutex &m) {
return pthread_cond_timedwait(&_cond, &m._mutex, &ts);
}
int signal() {
return pthread_cond_signal(&_cond);
}
int broadcast() {
return pthread_cond_broadcast(&_cond);
}
};
// Multiple-writer, multiple-reader lock (write-preferring)
class ReadWriteLock {
public:
void init(bool pshared = false) {
_nread = _nread_waiters = 0;
_nwrite = _nwrite_waiters = 0;
_mtx.init(pshared);
_rcond.init(pshared);
_wcond.init(pshared);
}
void read_lock() {
_mtx.lock();
if (_nwrite || _nwrite_waiters) {
_nread_waiters++;
do _rcond.wait(_mtx);
while (_nwrite || _nwrite_waiters);
_nread_waiters--;
}
_nread++;
_mtx.unlock();
}
void read_unlock() {
_mtx.lock();
_nread--;
if (_nwrite_waiters)
_wcond.broadcast();
_mtx.unlock();
}
void write_lock() {
_mtx.lock();
if (_nread || _nwrite) {
_nwrite_waiters++;
do _wcond.wait(_mtx);
while (_nread || _nwrite);
_nwrite_waiters--;
}
_nwrite++;
_mtx.unlock();
}
void write_unlock() {
_mtx.lock();
_nwrite--;
if (_nwrite_waiters)
_wcond.broadcast();
else if (_nread_waiters)
_rcond.broadcast();
_mtx.unlock();
}
private:
Mutex _mtx;
Condition _rcond;
Condition _wcond;
uint32_t _nread, _nread_waiters;
uint32_t _nwrite, _nwrite_waiters;
};
typedef struct _ShmHeader {
size_t _capacity; // max number of logs
int _begin; // start index of the circular buffer
int _end; // end index of the circular buffer
} ShmHeader;
ShmHeader * _hdr;
ReadWriteLock * _lock;
T * _v; // pointer to the head of event buffer
string _shm_path;
size_t _shm_size; // size(bytes) of shared memory
bool _master;
// template <class In> ...
ShmRingBuffer(const ShmRingBuffer<T> &);
ShmRingBuffer<T>& operator=(const ShmRingBuffer<T>&);
bool init(size_t cap, bool master, const char * path);
};
template <typename T> inline size_t
ShmRingBuffer<T>::capacity() const
{
assert(_hdr != NULL);
size_t cap = 0;
_lock->read_lock();
cap = _hdr->_capacity;
_lock->read_unlock();
return cap;
}
template <typename T> inline size_t
ShmRingBuffer<T>::begin() const
{
assert(_hdr != NULL);
size_t idx = 0;
_lock->read_lock();
idx = _hdr->_begin;
_lock->read_unlock();
return idx;
}
template <typename T> inline size_t
ShmRingBuffer<T>::end() const
{
assert(_hdr != NULL);
size_t idx = 0;
_lock->read_lock();
idx = _hdr->_end;
_lock->read_unlock();
return idx;
}
template <typename T> inline bool
ShmRingBuffer<T>::init(size_t cap, bool master, const char * path)
{
assert(path != NULL);
int shm_fd = shm_open(path, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG); // TODO: O_TRUNC?
if (shm_fd < 0) {
perror("shm_open failed");
return false;
}
_shm_size = sizeof(ShmHeader) + sizeof(ReadWriteLock) + cap * sizeof(T);
if (master && (ftruncate(shm_fd, _shm_size) < 0)) {
perror("ftruncate failed");
//shm_unlink(path);
//return false;
}
void *pbuf = NULL; /* shared memory adddress */
pbuf = mmap(NULL, _shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (pbuf == (void *) -1) {
perror("mmap failed");
return false;
}
_hdr = reinterpret_cast<ShmHeader *>(pbuf);
assert(_hdr != NULL);
_lock = reinterpret_cast<ReadWriteLock *>((char*)_hdr + sizeof(ShmHeader));
assert(_lock != NULL);
_v = reinterpret_cast<T *>((char*)_lock + sizeof(ReadWriteLock));
assert(_v != NULL);
if (master) {
_hdr->_capacity = cap;
_hdr->_begin = _hdr->_end = 0;
_lock->init(true);
}
return true;
}
template <typename T> inline void
ShmRingBuffer<T>::clear()
{
if (!_hdr || !_lock)
return;
_lock->write_lock();
_hdr->_begin = _hdr->_end = 0;
// TODO: memset the shared memory?
_lock->write_unlock();
}
template <typename T> inline void
ShmRingBuffer<T>::push_back(const T& e)
{
assert(_hdr != NULL);
assert(_v != NULL);
_lock->write_lock();
memcpy(_v + _hdr->_end, &e, sizeof(e));
_hdr->_end = (_hdr->_end + 1) % _hdr->_capacity; // make sure index is in range [0..._capacity)
if (_hdr->_end == _hdr->_begin) // buffer is full, advance begin index, too
_hdr->_begin = (_hdr->_begin + 1) % _hdr->_capacity;
_lock->write_unlock();
}
template <typename T> inline T
ShmRingBuffer<T>::dump_front()
{
assert(_hdr != NULL);
assert(_v != NULL);
T ret;
_lock->write_lock();
if (_hdr->_begin != _hdr->_end) {
ret = *(_v + _hdr->_begin);
_hdr->_begin = (_hdr->_begin + 1) % _hdr->_capacity;
}
_lock->write_unlock();
return ret;
}
template <typename T> inline string
ShmRingBuffer<T>::unparse() const {
assert(_hdr != NULL);
assert(_v != NULL);
string ret;
_lock->read_lock();
if (_hdr->_begin == _hdr->_end) {
_lock->read_unlock();
return string();
}
for (int i = _hdr->_begin; i != _hdr->_end; i = (i+1) % _hdr->_capacity) {
ret += string((_v + i)->unparse()) + "\n"; // Suppose T has a unparse() member function
}
_lock->read_unlock();
return ret;
}
#endif