Skip to content

Commit 21b415a

Browse files
committed
Add function lsl_inlet_flush() to drop all outstanding samples from an inlet to avoid the pull_ overhead in realtime applications
1 parent 1576a76 commit 21b415a

File tree

7 files changed

+28
-0
lines changed

7 files changed

+28
-0
lines changed

include/lsl/inlet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ extern LIBLSL_C_API unsigned long lsl_pull_chunk_buf(lsl_inlet in, char **data_b
273273
*/
274274
extern LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in);
275275

276+
/// Drop all queued not-yet pulled samples, return the nr of dropped samples
277+
extern LIBLSL_C_API uint32_t lsl_inlet_flush(lsl_inlet in);
278+
276279
/**
277280
* Query whether the clock was potentially reset since the last call to lsl_was_clock_reset().
278281
*

include/lsl_cpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,9 @@ class stream_inlet {
15751575
*/
15761576
std::size_t samples_available() { return lsl_samples_available(obj); }
15771577

1578+
/// Drop all queued not-yet pulled samples, return the nr of dropped samples
1579+
uint32_t flush() noexcept { return lsl_inlet_flush(obj); }
1580+
15781581
/**
15791582
* Query whether the clock was potentially reset since the last call to was_clock_reset().
15801583
*

src/consumer_queue.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ sample_p consumer_queue::pop_sample(double timeout) {
4646
return result;
4747
}
4848

49+
uint32_t consumer_queue::flush() noexcept {
50+
uint32_t n = 0;
51+
while (buffer_.pop()) n++;
52+
return n;
53+
}
54+
4955
bool consumer_queue::empty() { return buffer_.empty(); }

src/consumer_queue.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class consumer_queue : private lslboost::noncopyable {
3737
*/
3838
sample_p pop_sample(double timeout = FOREVER);
3939

40+
/// Number of available samples
41+
std::size_t read_available() const { return buffer_.read_available(); }
42+
43+
/// Flush the queue, return the number of dropped samples
44+
uint32_t flush() noexcept;
45+
4046
/// Check whether the buffer is empty.
4147
bool empty();
4248

src/data_receiver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class data_receiver : public cancellable_registry {
7070
/// Check whether the underlying buffer is empty. This value may be inaccurate.
7171
bool empty() { return sample_queue_.empty(); }
7272

73+
/// Flush the queue, return the number of dropped samples
74+
uint32_t flush() noexcept { return sample_queue_.flush(); }
75+
7376
private:
7477
/// The data reader thread.
7578
void data_thread();

src/lsl_inlet_c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in) {
357357
} catch (std::exception &) { return 0; }
358358
}
359359

360+
LIBLSL_C_API uint32_t lsl_inlet_flush(lsl_inlet in) {
361+
return in->flush();
362+
}
363+
360364
LIBLSL_C_API uint32_t lsl_was_clock_reset(lsl_inlet in) {
361365
try {
362366
return (uint32_t)in->was_clock_reset();

src/stream_inlet_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ class stream_inlet_impl {
304304
*/
305305
std::size_t samples_available() { return (std::size_t)(!data_receiver_.empty()); }
306306

307+
/// Flush the queue, return the number of dropped samples
308+
uint32_t flush() { return !data_receiver_.flush(); }
309+
307310
/** Query whether the clock was potentially reset since the last call to was_clock_reset().
308311
*
309312
* This is only interesting for applications that combine multiple time_correction values to

0 commit comments

Comments
 (0)