forked from sccn/liblsl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer_queue.cpp
More file actions
70 lines (63 loc) · 1.81 KB
/
consumer_queue.cpp
File metadata and controls
70 lines (63 loc) · 1.81 KB
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
#include "consumer_queue.h"
#include "sample.h"
#include "send_buffer.h"
#include "common.h"
#include <iostream>
#include <boost/chrono/duration.hpp>
// === implementation of the consumer_queue class ===
using namespace lsl;
/**
* Create a new queue with a given capacity.
* @param max_capacity The maximum number of samples that can be held by the queue. Beyond that, the oldest samples are dropped.
* @param registry Optionally a pointer to a registration facility, to dispatch samples to all consumers.
*/
consumer_queue::consumer_queue(std::size_t max_capacity, send_buffer_p registry): registry_(registry), buffer_(max_capacity) {
if (registry_)
registry_->register_consumer(this);
}
/**
* Destructor.
* Unregisters from the send buffer, if any.
*/
consumer_queue::~consumer_queue() {
try {
if (registry_)
registry_->unregister_consumer(this);
} catch(std::exception &e) {
std::cerr << "Unexpected error while trying to unregister a consumer queue from its registry:" << e.what() << std::endl;
}
}
/**
* Push a new sample onto the queue.
*/
void consumer_queue::push_sample(const sample_p &sample) {
while (!buffer_.push(sample)) {
sample_p dummy;
buffer_.pop(dummy);
}
}
/**
* Pop a sample from the queue.
* Blocks if empty.
* @param timeout Timeout for the blocking, in seconds. If expired, an empty sample is returned.
*/
sample_p consumer_queue::pop_sample(double timeout) {
sample_p result;
if (timeout <= 0.0) {
buffer_.pop(result);
} else {
if (!buffer_.pop(result)) {
// turn timeout into the point in time at which we give up
timeout += lsl::lsl_clock();
do {
if (lsl::lsl_clock() >= timeout)
break;
lslboost::this_thread::sleep_for(lslboost::chrono::milliseconds(1));
} while (!buffer_.pop(result));
}
}
return result;
}
bool consumer_queue::empty() {
return buffer_.empty();
}