Skip to content

Commit dab6647

Browse files
committed
Convert trivial usages of Boost.Bind to lambda expressions
1 parent dfb1df2 commit dab6647

14 files changed

+76
-121
lines changed

src/data_receiver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ data_receiver::~data_receiver() {
6363
void data_receiver::open_stream(double timeout) {
6464
closing_stream_ = false;
6565
lslboost::unique_lock<lslboost::mutex> lock(connected_mut_);
66+
auto connection_completed = [this]() { return connected_ || conn_.lost(); };
6667
if (!connection_completed()) {
6768
// start thread if not yet running
6869
if (check_thread_start_ && !data_thread_.joinable()) {
@@ -71,10 +72,10 @@ void data_receiver::open_stream(double timeout) {
7172
}
7273
// wait until the connection attempt completes (or we time out)
7374
if (timeout >= FOREVER)
74-
connected_upd_.wait(lock, lslboost::bind(&data_receiver::connection_completed,this));
75-
else
76-
if (!connected_upd_.wait_for(lock, lslboost::chrono::duration<double>(timeout), lslboost::bind(&data_receiver::connection_completed,this)))
77-
throw timeout_error("The open_stream() operation timed out.");
75+
connected_upd_.wait(lock, connection_completed);
76+
else if (!connected_upd_.wait_for(
77+
lock, lslboost::chrono::duration<double>(timeout), connection_completed))
78+
throw timeout_error("The open_stream() operation timed out.");
7879
}
7980
if (conn_.lost())
8081
throw lost_error("The stream read by this inlet has been lost. To recover, you need to re-resolve the source and re-create the inlet.");

src/data_receiver.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ namespace lsl {
6161
/// The data reader thread.
6262
void data_thread();
6363

64-
/// Function that is polled by the condition variable
65-
bool connection_completed() { return connected_ || conn_.lost(); }
66-
6764
// the underlying connection
6865
inlet_connection &conn_;
6966

src/info_receiver.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ info_receiver::~info_receiver() {
3030
/// Retrieve the complete information of the given stream, including the extended description.
3131
const stream_info_impl &info_receiver::info(double timeout) {
3232
lslboost::unique_lock<lslboost::mutex> lock(fullinfo_mut_);
33+
auto info_ready = [this](){ return fullinfo_ || conn_.lost(); };
3334
if (!info_ready()) {
3435
// start thread if not yet running
3536
if (!info_thread_.joinable())
3637
info_thread_ = lslboost::thread(&info_receiver::info_thread,this);
3738
// wait until we are ready to return a result (or we time out)
3839
if (timeout >= FOREVER)
39-
fullinfo_upd_.wait(lock, lslboost::bind(&info_receiver::info_ready,this));
40-
else
41-
if (!fullinfo_upd_.wait_for(lock, lslboost::chrono::duration<double>(timeout), lslboost::bind(&info_receiver::info_ready,this)))
42-
throw timeout_error("The info() operation timed out.");
40+
fullinfo_upd_.wait(lock, info_ready);
41+
else if (!fullinfo_upd_.wait_for(
42+
lock, lslboost::chrono::duration<double>(timeout), info_ready))
43+
throw timeout_error("The info() operation timed out.");
4344
}
4445
if (conn_.lost())
4546
throw lost_error("The stream read by this inlet has been lost. To recover, you need to re-resolve the source and re-create the inlet.");
@@ -91,5 +92,3 @@ void info_receiver::info_thread() {
9192
conn_.release_watchdog();
9293
}
9394

94-
bool info_receiver::info_ready() { return fullinfo_ || conn_.lost(); }
95-

src/info_receiver.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ namespace lsl {
3333
/// The info reader thread.
3434
void info_thread();
3535

36-
/// function polled by the condition variable
37-
bool info_ready();
38-
39-
4036
// the underlying connection
4137
inlet_connection &conn_; // reference to our connection
4238

src/inlet_connection.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include <boost/bind.hpp>
2-
#include <functional>
31
#include "inlet_connection.h"
42
#include "api_config.h"
3+
#include <functional>
54

65

76
// === implementation of the inlet_connection class ===
@@ -246,7 +245,10 @@ void inlet_connection::watchdog_thread() {
246245
// so that the watchdog can be cancelled conveniently
247246
{
248247
lslboost::unique_lock<lslboost::mutex> lock(shutdown_mut_);
249-
shutdown_cond_.wait_for(lock,lslboost::chrono::duration<double>(api_config::get_instance()->watchdog_check_interval()), lslboost::bind(&inlet_connection::shutdown,this));
248+
shutdown_cond_.wait_for(lock,
249+
lslboost::chrono::duration<double>(
250+
api_config::get_instance()->watchdog_check_interval()),
251+
[this]() { return shutdown(); });
250252
}
251253
} catch(std::exception &e) {
252254
LOG_F(ERROR, "Unexpected hiccup in the watchdog thread: %s", e.what());

src/resolver_impl.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
using namespace lsl;
1515
using namespace lslboost::asio;
16+
using err_t = const lslboost::system::error_code&;
1617

1718
/**
1819
* Instantiate a new resolver and configure timing parameters.
@@ -83,7 +84,9 @@ std::vector<stream_info_impl> resolver_impl::resolve_oneshot(const std::string &
8384
// start a timer that cancels all outstanding IO operations and wave schedules after the timeout has expired
8485
if (timeout != FOREVER) {
8586
resolve_timeout_expired_.expires_after(timeout_sec(timeout));
86-
resolve_timeout_expired_.async_wait(lslboost::bind(&resolver_impl::resolve_timeout_expired,this,placeholders::error));
87+
resolve_timeout_expired_.async_wait([this](err_t err) {
88+
if (err != error::operation_aborted) cancel_ongoing_resolve();
89+
});
8790
}
8891

8992
// start the first wave of resolve packets
@@ -148,17 +151,20 @@ void resolver_impl::next_resolve_wave() {
148151
} else {
149152
// start a new multicast wave
150153
udp_multicast_burst();
154+
155+
auto wave_timer_timeout =
156+
(fast_mode_ ? 0 : cfg_->continuous_resolve_interval()) + cfg_->multicast_min_rtt();
151157
if (!ucast_endpoints_.empty()) {
152-
// we have known peer addresses: we spawn a unicast wave and shortly thereafter the next wave
158+
// we have known peer addresses: we spawn a unicast wave
153159
unicast_timer_.expires_after(timeout_sec(cfg_->multicast_min_rtt()));
154-
unicast_timer_.async_wait(lslboost::bind(&resolver_impl::udp_unicast_burst,this,placeholders::error));
155-
wave_timer_.expires_after(timeout_sec((fast_mode_?0:cfg_->continuous_resolve_interval())+(cfg_->multicast_min_rtt()+cfg_->unicast_min_rtt())));
156-
wave_timer_.async_wait(lslboost::bind(&resolver_impl::wave_timeout_expired,this,placeholders::error));
157-
} else {
158-
// there are no known peer addresses; just set up the next wave
159-
wave_timer_.expires_after(timeout_sec((fast_mode_?0:cfg_->continuous_resolve_interval())+cfg_->multicast_min_rtt()));
160-
wave_timer_.async_wait(lslboost::bind(&resolver_impl::wave_timeout_expired,this,placeholders::error));
160+
unicast_timer_.async_wait([this](err_t ec) { this->udp_unicast_burst(ec); });
161+
// delay the next multicast wave
162+
wave_timer_timeout += cfg_->unicast_min_rtt();
161163
}
164+
wave_timer_.expires_after(timeout_sec(wave_timer_timeout));
165+
wave_timer_.async_wait([this](err_t err) {
166+
if (err != error::operation_aborted) next_resolve_wave();
167+
});
162168
}
163169
}
164170

@@ -199,19 +205,6 @@ void resolver_impl::udp_unicast_burst(error_code err) {
199205
}
200206
}
201207

202-
/// This handler is called when the overall resolve timeout (if any) expires.
203-
void resolver_impl::resolve_timeout_expired(error_code err) {
204-
if (err != error::operation_aborted)
205-
cancel_ongoing_resolve();
206-
}
207-
208-
/// This handler is called when the wave timeout expires.
209-
void resolver_impl::wave_timeout_expired(error_code err) {
210-
if (err != error::operation_aborted)
211-
next_resolve_wave();
212-
}
213-
214-
215208
// === cancellation and teardown ===
216209

217210
/// Cancel any ongoing operations and render the resolver unusable.
@@ -226,10 +219,10 @@ void resolver_impl::cancel_ongoing_resolve() {
226219
// make sure that ongoing handler loops terminate
227220
expired_ = true;
228221
// timer fires: cancel the next wave schedule
229-
post(*io_, lslboost::bind(&steady_timer::cancel, &wave_timer_));
230-
post(*io_, lslboost::bind(&steady_timer::cancel, &unicast_timer_));
222+
post(*io_, [this]() { wave_timer_.cancel(); });
223+
post(*io_, [this]() { unicast_timer_.cancel(); });
231224
// and cancel the timeout, too
232-
post(*io_, lslboost::bind(&steady_timer::cancel, &resolve_timeout_expired_));
225+
post(*io_, [this]() { resolve_timeout_expired_.cancel(); });
233226
// cancel all currently active resolve attempts
234227
cancel_all_registered();
235228
}

src/resolver_impl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ namespace lsl {
9191
/// Start a new resolver attempt on the known peers.
9292
void udp_unicast_burst(error_code err);
9393

94-
/// This handler is called when the overall timeout (if any) expires.
95-
void resolve_timeout_expired(error_code err);
96-
97-
/// This handler is called when the wave timeout (if any) expires.
98-
void wave_timeout_expired(error_code err);
99-
10094
/// Cancel the currently ongoing resolve, if any.
10195
void cancel_ongoing_resolve();
10296

src/send_buffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "send_buffer.h"
22
#include "consumer_queue.h"
3-
#include <boost/bind.hpp>
43
#include <memory>
54

65

@@ -63,6 +62,7 @@ bool send_buffer::have_consumers() {
6362
/// Wait until some consumers are present.
6463
bool send_buffer::wait_for_consumers(double timeout) {
6564
lslboost::unique_lock<lslboost::mutex> lock(consumers_mut_);
66-
return some_registered_.wait_for(lock, lslboost::chrono::duration<double>(timeout), lslboost::bind(&send_buffer::some_registered,this));
65+
return some_registered_.wait_for(
66+
lock, lslboost::chrono::duration<double>(timeout), [this]() { return some_registered(); });
6767
}
6868

src/stream_inlet_impl.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef STREAM_INLET_IMPL_H
22
#define STREAM_INLET_IMPL_H
33

4-
#include <boost/bind.hpp>
54
#include "data_receiver.h"
65
#include "time_receiver.h"
76
#include "common.h"
@@ -32,11 +31,13 @@ namespace lsl {
3231
* In all other cases (recover is false or the stream is not recoverable) a lost_error is thrown where
3332
* indicated if the stream's source is lost (e.g., due to an app or computer crash).
3433
*/
35-
stream_inlet_impl(const stream_info_impl &info, int32_t max_buflen=360, int32_t max_chunklen=0, bool recover=true): conn_(info,recover), info_receiver_(conn_), time_receiver_(conn_), data_receiver_(conn_,max_buflen,max_chunklen),
36-
postprocessor_(lslboost::bind(&time_receiver::time_correction,&time_receiver_,5),
37-
lslboost::bind(&inlet_connection::current_srate,&conn_),
38-
lslboost::bind(&time_receiver::was_reset,&time_receiver_))
39-
{
34+
stream_inlet_impl(const stream_info_impl &info, int32_t max_buflen = 360,
35+
int32_t max_chunklen = 0, bool recover = true)
36+
: conn_(info, recover), info_receiver_(conn_), time_receiver_(conn_),
37+
data_receiver_(conn_, max_buflen, max_chunklen),
38+
postprocessor_([this]() { return time_receiver_.time_correction(5); },
39+
[this]() { return conn_.current_srate(); },
40+
[this]() { return time_receiver_.was_reset(); }) {
4041
ensure_lsl_initialized();
4142
conn_.engage();
4243
}

src/stream_outlet_impl.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "stream_outlet_impl.h"
22
#include "tcp_server.h"
33
#include "udp_server.h"
4-
#include <boost/bind.hpp>
54
#include <boost/thread/thread_only.hpp>
65
#include <memory>
76

@@ -46,9 +45,19 @@ stream_outlet_impl::stream_outlet_impl(const stream_info_impl &info, int chunk_s
4645
for (auto &responder : responders_) responder->begin_serving();
4746

4847
// and start the IO threads to handle them
48+
const std::string name{"IO_" + this->info().name().substr(0, 11)};
4949
for (const auto &io : ios_)
50-
io_threads_.push_back(std::make_shared<lslboost::thread>(
51-
lslboost::bind(&stream_outlet_impl::run_io, this, io)));
50+
io_threads_.emplace_back(std::make_shared<lslboost::thread>([io, name]() {
51+
loguru::set_thread_name(name.c_str());
52+
while (true) {
53+
try {
54+
io->run();
55+
return;
56+
} catch (std::exception &e) {
57+
LOG_F(ERROR, "Error during io_context processing: %s", e.what());
58+
}
59+
}
60+
}));
5261
}
5362

5463
/**
@@ -111,19 +120,6 @@ stream_outlet_impl::~stream_outlet_impl() {
111120
} catch (...) { LOG_F(ERROR, "Severe error during stream outlet shutdown."); }
112121
}
113122

114-
// Run an IO service.
115-
void stream_outlet_impl::run_io(io_context_p &ios) {
116-
loguru::set_thread_name((std::string("IO_") += info().name().substr(0, 11)).c_str());
117-
while (true) {
118-
try {
119-
ios->run();
120-
return;
121-
} catch(std::exception &e) {
122-
LOG_F(ERROR, "Error during io_context processing: %s", e.what());
123-
}
124-
}
125-
}
126-
127123
/**
128124
* Retrieve the stream info associated with this outlet.
129125
* This is the constant meta-data header that was used to create the stream.

0 commit comments

Comments
 (0)