-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSharedRingBuffer.hpp
301 lines (241 loc) · 7.64 KB
/
SimpleSharedRingBuffer.hpp
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
#ifndef __SIMPLE_SHARED_RING_BUFFER_H__
#define __SIMPLE_SHARED_RING_BUFFER_H__
#include <string>
#include <memory>
#include <iostream>
#include <boost/thread/thread_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include "SharedRingBuffer.hpp"
class SimpleSharedRingBuffer: public SharedRingBuffer
{
public:
/*
* Control structure which lives in the beginning of the SHM
*/
struct BufferControl {
/*
* Magic number to identify the control structure
*/
uint32_t magic;
char label[32]; // Text label for the device
enum BufferMode mode;
/*
* For the ring buffer
*/
size_t head; // Ring buffer position on the stream
size_t tail; // Current
enum BufferState state;
size_t n_channels;
/*
* Metadata about the streamed data
*/
unsigned int version; // Revision number of these settings
char format[6]; // Data format string
double center_frequency; // Center frequency
double sample_rate; // Sample rate of the stream
boost::interprocess::interprocess_mutex write_mutex;
boost::interprocess::interprocess_mutex header_mutex;
boost::interprocess::interprocess_condition cond_head;
boost::interprocess::interprocess_condition cond_tail;
};
static const uint32_t Magic = 0x50B982;
static const uint32_t MagicOneToMany = 0x50B983;
static const uint32_t MagicManyToOne = 0x50B984;
/*
* Check doest shared memory buffer exist?
*/
static bool checkSHM(std::string name);
/*
* Create a new shared memory buffer
*/
static std::unique_ptr<SimpleSharedRingBuffer> create(const std::string &name, enum BufferMode mode, boost::interprocess::mode_t access_mode, std::string format = std::string(), size_t buffer_size = 0, size_t n_channels=1);
/*
* Open a shared memory buffer
*/
static std::unique_ptr<SimpleSharedRingBuffer> open(const std::string &name, enum BufferMode mode, boost::interprocess::mode_t access_mode);
/*
* Destructor!
*/
~SimpleSharedRingBuffer();
/**/
enum BufferState getState() const { return ctrl->state; }
void setState(enum BufferState state) { ctrl->state = state; }
/*
* Ignore history and move current pointer to end.
*/
void sync();
/*
* Reset the state of the ring buffer
*/
void reset();
/*
* Get number of available new samples for reading out.
*/
size_t getSamplesAvailable(); // TODO: getReadSamples(), getNewSamples()?
/*
* Is the ring buffer empty?
*/
bool isEmpty() const { return tail == ctrl->head; }
/*
* Get number of samples that can be written to TX position
*/
size_t getSamplesLeft(); // TODO: getWriteSamples(), getFreeSpace()?
void* getWritePointer() {
return reinterpret_cast<void *>(reinterpret_cast<size_t>(buffers[0]) + datasize * ctrl->head);
}
void getWritePointers(void* ptrs[]) {
for (size_t ch = 0; ch < ctrl->n_channels; ch++)
ptrs[ch] = reinterpret_cast<void *>(reinterpret_cast<size_t>(buffers[ch]) + datasize * ctrl->head);
}
void *getReadPointer() {
return reinterpret_cast<void *>(reinterpret_cast<size_t>(buffers[0]) + datasize * tail);
}
void getReadPointers(void* ptrs[]) {
for (size_t ch = 0; ch < ctrl->n_channels; ch++)
ptrs[ch] = reinterpret_cast<void *>(reinterpret_cast<size_t>(buffers[ch]) + datasize * tail);
}
#if 0
/*
* Return pointer to current write position
*/
template<typename T> T* getWritePointer() {
//assert(sizeof(T) && sizeof(T) == datasize);
return reinterpret_cast<T*>(reinterpret_cast<size_t>(buffer) + datasize * ctrl->head);
}
template<typename T> void getWritePointers(T* ptrs[]) {
//assert(sizeof(T) && sizeof(T) == datasize);
ptrs[0] = reinterpret_cast<T*>(reinterpret_cast<size_t>(buffer) + datasize * ctrl->head);
}
/*
* Return pointer to current read position
*/
template<typename T> T* getReadPointer() {
//assert(sizeof(T) && sizeof(T) == datasize);
return reinterpret_cast<T*>(reinterpret_cast<size_t>(buffer) + datasize * tail);
}
template<typename T> void getReadPointers(T* ptrs[]) {
//assert(sizeof(T) && sizeof(T) == datasize);
ptrs[0] = reinterpret_cast<void*>(reinterpret_cast<size_t>(buffer) + datasize * tail);
}
#endif
/*
* Get number of new samples in the ring buffer and move the reading
* location forward.
*
* params:
* maxElems: Maxmimum number of elements/items to be read
* timestamp: Sampling time of the next
* returns:
* Number of available samples in the buffer
* note:
* Call getPointer() before calling this function!
* takes
*/
size_t read(size_t maxElems, long long& timestamp);
/*
* Write items to buffer and move the end pointer torwards
*/
void write(size_t numItems, long long timestamp);
/*
* Get format string
*/
std::string getFormat() const;
/*
* Returns true if the settings have changed from previous call.
*/
bool settingsChanged();
/*
* Get datasize
*/
size_t getDatasize() const { return datasize; }
/*
* Set center frequency
*/
void setCenterFrequency(double frequency);
/*
* Return center frequency of the sample stream
*/
double getCenterFrequency() const {
assert(ctrl != NULL);
return ctrl->center_frequency;
}
/*
* Set stream sample rate
*/
void setSampleRate(double rate);
/*
* Return stream sample rate
*/
double getSampleRate() const {
assert(ctrl != NULL);
return ctrl->sample_rate;
}
/*
* Return number of channels
*/
size_t getNumChannels() const {
assert(ctrl != NULL);
return 1; //ctrl->n_channels;
}
/*
* Try to acquire the write lock for writing to the buffer.
* Throws an `boost::interprocess::interprocess_exception` in case of failure.
*/
void acquireWriteLock(unsigned int timeoutUs = 0);
/*
* Release the write lock.
* Function shall not be called if acquireWriteLock hasn't been succesfully called
* from this process.
* Throws an `boost::interprocess::interprocess_exception` in case of failure.
*/
void releaseWriteLock();
bool ownsWriteLock();
/*
* Returns the global write mutex for the buffer
*/
boost::interprocess::interprocess_mutex &write_mutex()
{
assert(ctrl != NULL);
return ctrl->write_mutex;
}
/*
* Wait for new data
*/
void wait_tail(unsigned int timeoutUs);
void wait_tail(const boost::posix_time::ptime& abs_timeout);
void wait_head(unsigned int timeoutUs);
void wait_head(const boost::posix_time::ptime &abs_timeout);
/*
* Stream opetator to print the buffer description
*/
void print(std::ostream& stream) const;
const BufferControl& getCtrl() const {
return *ctrl;
}
private:
/*
* This contructor is private!
* SimpleSharedRingBuffer::open() and SimpleSharedRingBuffer::create() should be used
*/
SimpleSharedRingBuffer(std::string name);
void mapBuffer(boost::interprocess::mode_t mode);
//SimpleSharedRingBuffer(SimpleSharedRingBuffer && moved) { }
//SimpleSharedRingBuffer& operator=(SimpleSharedRingBuffer && moved) { }
std::string name;
size_t datasize, buffer_size;
boost::interprocess::shared_memory_object shm;
boost::interprocess::mapped_region mapped_ctrl, mapped_data;
public:
BufferControl* ctrl;
std::vector<void *> buffers;
size_t tail;
size_t version;
bool owner;
bool owns_write_lock;
friend void *transmitter_thread(void *p);
};
#endif /* __SIMPLE_SHARED_RING_BUFFER_H__ */