-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimestampedSharedRingBuffer.hpp
350 lines (280 loc) · 8.51 KB
/
TimestampedSharedRingBuffer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef __TIMESTAMPED_SHARED_RING_BUFFER_H__
#define __TIMESTAMPED_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 TimestampedSharedRingBuffer: public SharedRingBuffer
{
public:
struct BlockMetadata;
struct BufferControl;
/*
* Metadata unit for each Datablock
*
* size: 16 bytes
*/
struct BlockMetadata {
/*
* Timestamp in ns for the first sample in block
* Type is ideally same 'long long' but written here out in exact byte-format.
*/
uint64_t timestamp;
/*
* Possible flags are:
* - SOAPY_SDR_HAS_TIME
* - SOAPY_SDR_END_BURST
* - SOAPY_SDR_MORE_FRAGMENTS
*
* 0xFFFF if not valid!
*/
uint32_t flags;
/*
* Number of valid bytes in the block
* Ideally this is always same as the length of the block but shorter blocks
* are also support in some cases. For example when transmitting a burst.
*/
uint32_t size;
};
/*
* 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; // Current position in the ring buffer
size_t tail;
enum BufferState state;
size_t n_channels; // Number of channels
size_t block_size; // Size of the timestamped blocks
size_t n_blocks; // Number of blocks
/*
* Metadata about the streamed data
*/
unsigned int version; // Revision number of these settings
char format[8]; // SoapySDR data format string
double center_frequency; // Center frequency
double sample_rate; // Sample rate of the stream
// Write access mutex
boost::interprocess::interprocess_mutex write_mutex;
// Header data mutex
boost::interprocess::interprocess_mutex header_mutex;
// New-data-has-arrived condition variable
boost::interprocess::interprocess_condition cond_new_data;
/*
* Array of BlockMetadata (1 per datablock)
*/
BlockMetadata meta[0];
};
typedef uint64_t Timestamp;
static const uint32_t Magic = 0x50A971;
static const uint32_t MagicOneToMany = 0x50A972;
static const uint32_t MagicManyToOne = 0x50A973;
/*
* Check doest shared memory buffer exist?
*/
static bool checkSHM(std::string name);
/*
* Create a new shared memory buffer
*/
static std::unique_ptr<TimestampedSharedRingBuffer> create(const std::string& name, boost::interprocess::mode_t mode, std::string format=std::string(), size_t n_blocks=0, size_t block_size=0, size_t n_channels=1);
/*
* Open a shared memory buffer
*/
static std::unique_ptr<TimestampedSharedRingBuffer> open(const std::string& name, boost::interprocess::mode_t mode);
/*
* Destructor!
*/
~TimestampedSharedRingBuffer();
/**/
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();
/**/
void reset();
/*
* Get number of available new samples till end pointer or end of the buffer
*/
size_t getSamplesAvailable();
/*
* Is the ring buffer empty?
*/
bool isEmpty() const;
/*
* Get number of samples that can be written to TX position
*/
size_t getSamplesLeft();
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 read/write position
*/
template<typename T> T* getWritePointer() {
//assert(sizeof(T) && sizeof(T) == datasize);
return static_cast<T*>(reinterpret_cast<size_t>(buffers[0]) + datasize * ctrl->head);
}
template<typename T> void getWritePointers(T* ptrs[]) {
//assert(sizeof(T) && sizeof(T) == datasize);
for (size_t ch = 0; ch < ctrl->n_channels; ch++)
ptrs[ch] = reinterpret_cast<T*>(reinterpret_cast<size_t>(buffers[ch]) + datasize * ctrl->head);
}
/*
* Return pointer to current read/write position
*/
template<typename T> T* getReadPointer() {
//assert(sizeof(T) && sizeof(T) == datasize);
return static_cast<T*>(reinterpret_cast<size_t>(buffers[0]) + datasize * tail);
}
template<typename T> void getReadPointers(T* ptrs[]) {
//assert(sizeof(T) && sizeof(T) == datasize);
for (size_t ch = 0; ch < ctrl->n_channels; ch++)
ptrs[ch] = reinterpret_cast<T*>(reinterpret_cast<size_t>(buffers[ch]) + datasize * tail);
}
#endif
/*
* Call getPointer() before calling this function!
*/
size_t read(size_t maxElems, long long& timestamp);
/*
* Get timestamp for current sample
*/
long long getTimestamp();
/*
* 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
*/
double getCenterFrequency() const {
assert(ctrl != NULL);
return ctrl->center_frequency;
}
/*
* Return center frequency
*/
void setSampleRate(double rate);
/*
* Return center frequency
*/
double getSampleRate() const {
assert(ctrl != NULL);
return ctrl->sample_rate;
}
/*
* Return the number of channels
*/
size_t getNumChannels() const {
assert(ctrl != NULL);
return 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
* Throws an `boost::interprocess::interprocess_exception` in case of failure.
*/
void releaseWriteLock();
/*
*/
bool ownsWriteLock();
/*
*
*/
boost::interprocess::interprocess_mutex& write_mutex() {
assert(ctrl != NULL);
return ctrl->write_mutex;
}
/*
* Wait for new data
*/
void wait_head(unsigned int timeoutUs);
void wait_head(const boost::posix_time::ptime& abs_timeout);
void wait_tail(unsigned int timeoutUs);
void wait_tail(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:
/*
*/
const BlockMetadata getMetadata(size_t block) const {
assert(ctrl != NULL);
assert(block <= ctrl->n_blocks);
return ctrl->meta[block];
}
/*
* This contructor is private!
* TimestampedSharedRingBuffer::open() and TimestampedSharedRingBuffer::create() should be used
*/
TimestampedSharedRingBuffer(std::string name);
void mapBuffer(size_t location, boost::interprocess::mode_t mode);
//TimestampedSharedRingBuffer(TimestampedSharedRingBuffer && moved) { }
//TimestampedSharedRingBuffer& operator=(TimestampedSharedRingBuffer && moved) { }
std::string name;
size_t datasize;
size_t buffer_size;
size_t block_size;
size_t n_blocks;
boost::interprocess::shared_memory_object shm;
boost::interprocess::mapped_region mapped_ctrl, mapped_data;
BufferControl* ctrl;
std::vector<void*> buffers;
size_t tail;
size_t version;
bool owner;
};
std::ostream& operator<<(std::ostream& stream, const TimestampedSharedRingBuffer& buf);
#endif /* __TIMESTAMPED_SHARED_RING_BUFFER_H__ */