Skip to content

Commit 3356bea

Browse files
committed
Use stdlib mutexes instead of boost mutexes where possible
1 parent 838e578 commit 3356bea

26 files changed

+119
-114
lines changed

src/api_config.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "api_config.h"
22
#include "common.h"
33
#include "inireader.h"
4-
#include <boost/thread/once.hpp>
4+
#include <algorithm>
55
#include <fstream>
6+
#include <mutex>
67

78
using namespace lsl;
89

@@ -231,16 +232,14 @@ void api_config::load_from_file(const std::string &filename) {
231232
}
232233
}
233234

235+
static std::once_flag api_config_once_flag;
236+
234237
const api_config *api_config::get_instance() {
235-
lslboost::call_once(&called_once, once_flag);
238+
std::call_once(api_config_once_flag, []() { api_config::get_instance_internal(); });
236239
return get_instance_internal();
237240
}
238241

239242
api_config *api_config::get_instance_internal() {
240243
static api_config cfg;
241244
return &cfg;
242245
}
243-
244-
void api_config::called_once() { get_instance_internal(); }
245-
246-
lslboost::once_flag api_config::once_flag = BOOST_ONCE_INIT;

src/api_config.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include <vector>
66

77
#include "common.h"
8-
namespace lslboost {
9-
struct once_flag;
10-
}
118

129
namespace lsl {
1310
/**
@@ -194,10 +191,8 @@ class api_config {
194191
api_config &operator=(const api_config &rhs) = delete;
195192

196193
private:
197-
// Thread-safe initialization logic (boilerplate).
198-
static lslboost::once_flag once_flag;
194+
/// Get the api_config singleton after thread-safe initialization if needed
199195
static api_config *get_instance_internal();
200-
static void called_once();
201196

202197
/**
203198
* Constructor.

src/cancellable_streambuf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class cancellable_streambuf : public std::streambuf,
6565
*/
6666
void cancel() {
6767
cancel_issued_ = true;
68-
lslboost::lock_guard<lslboost::recursive_mutex> lock(cancel_mut_);
68+
std::lock_guard<std::recursive_mutex> lock(cancel_mut_);
6969
cancel_started_ = false;
7070
this->get_service().get_io_context().post([this]() { close_if_open(); });
7171
}
@@ -80,7 +80,7 @@ class cancellable_streambuf : public std::streambuf,
8080
*/
8181
cancellable_streambuf *connect(const Protocol::endpoint &endpoint) {
8282
{
83-
lslboost::lock_guard<lslboost::recursive_mutex> lock(cancel_mut_);
83+
std::lock_guard<std::recursive_mutex> lock(cancel_mut_);
8484
if (cancel_issued_)
8585
throw std::runtime_error(
8686
"Attempt to connect() a cancellable_streambuf after it has been cancelled.");
@@ -130,7 +130,7 @@ class cancellable_streambuf : public std::streambuf,
130130
/// This function makes sure that a cancellation, if issued, is not being eaten by the
131131
/// io_context reset()
132132
void protected_reset() {
133-
lslboost::lock_guard<lslboost::recursive_mutex> lock(cancel_mut_);
133+
std::lock_guard<std::recursive_mutex> lock(cancel_mut_);
134134
// if the cancel() comes between completion of a run_one() and this call, close will be
135135
// issued right here at the next opportunity
136136
if (cancel_issued_) close_if_open();
@@ -224,7 +224,7 @@ class cancellable_streambuf : public std::streambuf,
224224
std::size_t bytes_transferred_;
225225
bool cancel_issued_;
226226
bool cancel_started_;
227-
lslboost::recursive_mutex cancel_mut_;
227+
std::recursive_mutex cancel_mut_;
228228
};
229229
} // namespace lsl
230230

src/cancellation.h

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

4-
#include <boost/thread/lock_guard.hpp>
5-
#include <boost/thread/recursive_mutex.hpp>
4+
#include <mutex>
65
#include <set>
76

87
namespace lsl {
@@ -30,22 +29,22 @@ class cancellable_registry {
3029

3130
/// Register a cancellable object.
3231
void register_cancellable(class cancellable_obj *o) {
33-
lslboost::lock_guard<lslboost::recursive_mutex> lock(state_mut_);
32+
std::lock_guard<std::recursive_mutex> lock(state_mut_);
3433
if (shutdown_issued_)
3534
throw shutdown_error(
3635
"The registry has begun to shut down; no new registrations possible.");
3736
cancellables_.insert(o);
3837
}
3938
/// Unregister a cancellable object.
4039
void unregister_cancellable(class cancellable_obj *o) {
41-
lslboost::lock_guard<lslboost::recursive_mutex> lock(state_mut_);
40+
std::lock_guard<std::recursive_mutex> lock(state_mut_);
4241
cancellables_.erase(o);
4342
}
4443

4544
bool shutdown_issued_{false}; // whether a shutdown has been issued
4645
std::set<cancellable_obj *>
4746
cancellables_; // a set of objects that we have to cancel upon re-resolves & disengage
48-
lslboost::recursive_mutex state_mut_; // mutex to protect the registry's state
47+
std::recursive_mutex state_mut_; // mutex to protect the registry's state
4948
};
5049

5150

@@ -77,15 +76,15 @@ class cancellable_obj {
7776

7877
/// Cancel all registered objects.
7978
inline void cancellable_registry::cancel_all_registered() {
80-
lslboost::lock_guard<lslboost::recursive_mutex> lock(state_mut_);
79+
std::lock_guard<std::recursive_mutex> lock(state_mut_);
8180
std::set<cancellable_obj *> copy(cancellables_);
8281
for (auto obj : copy)
8382
if (cancellables_.find(obj) != cancellables_.end()) obj->cancel();
8483
}
8584

8685
/// Cancel and prevent future object registrations.
8786
inline void cancellable_registry::cancel_and_shutdown() {
88-
lslboost::lock_guard<lslboost::recursive_mutex> lock(state_mut_);
87+
std::lock_guard<std::recursive_mutex> lock(state_mut_);
8988
shutdown_issued_ = true;
9089
cancel_all_registered();
9190
}

src/consumer_queue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "sample.h"
44
#include "send_buffer.h"
55
#include <boost/chrono/duration.hpp>
6+
#include <boost/thread/thread_only.hpp>
67

78
using namespace lsl;
89

src/data_receiver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ data_receiver::~data_receiver() {
4444

4545
void data_receiver::open_stream(double timeout) {
4646
closing_stream_ = false;
47-
lslboost::unique_lock<lslboost::mutex> lock(connected_mut_);
47+
std::unique_lock<std::mutex> lock(connected_mut_);
4848
auto connection_completed = [this]() { return connected_ || conn_.lost(); };
4949
if (!connection_completed()) {
5050
// start thread if not yet running
@@ -56,7 +56,7 @@ void data_receiver::open_stream(double timeout) {
5656
if (timeout >= FOREVER)
5757
connected_upd_.wait(lock, connection_completed);
5858
else if (!connected_upd_.wait_for(
59-
lock, lslboost::chrono::duration<double>(timeout), connection_completed))
59+
lock, std::chrono::duration<double>(timeout), connection_completed))
6060
throw timeout_error("The open_stream() operation timed out.");
6161
}
6262
if (conn_.lost())
@@ -296,7 +296,7 @@ void data_receiver::data_thread() {
296296
// been successful, so we're now connected (and remain to be even if we later
297297
// recover silently)
298298
{
299-
lslboost::lock_guard<lslboost::mutex> lock(connected_mut_);
299+
std::lock_guard<std::mutex> lock(connected_mut_);
300300
connected_ = true;
301301
}
302302
connected_upd_.notify_all();

src/data_receiver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "cancellation.h"
55
#include "consumer_queue.h"
66
#include "inlet_connection.h"
7-
#include <boost/thread/mutex.hpp>
87
#include <boost/thread/thread_only.hpp>
8+
#include <condition_variable>
9+
#include <mutex>
910

1011
using lslboost::asio::ip::tcp;
1112

@@ -90,9 +91,9 @@ class data_receiver : public cancellable_registry {
9091
/// queue of samples ready to be picked up (populated by the data thread)
9192
consumer_queue sample_queue_;
9293
/// mutex to protect the connected state
93-
lslboost::mutex connected_mut_;
94+
std::mutex connected_mut_;
9495
/// condition variable to indicate that an update for the connected state is available
95-
lslboost::condition_variable connected_upd_;
96+
std::condition_variable connected_upd_;
9697

9798
// internal data used by the reader thread
9899
/// the maximum number of samples to be buffered for this inlet

src/info_receiver.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ lsl::info_receiver::~info_receiver() {
1818
}
1919

2020
const lsl::stream_info_impl &lsl::info_receiver::info(double timeout) {
21-
lslboost::unique_lock<lslboost::mutex> lock(fullinfo_mut_);
21+
std::unique_lock<std::mutex> lock(fullinfo_mut_);
2222
auto info_ready = [this]() { return fullinfo_ || conn_.lost(); };
2323
if (!info_ready()) {
2424
// start thread if not yet running
@@ -27,8 +27,7 @@ const lsl::stream_info_impl &lsl::info_receiver::info(double timeout) {
2727
// wait until we are ready to return a result (or we time out)
2828
if (timeout >= FOREVER)
2929
fullinfo_upd_.wait(lock, info_ready);
30-
else if (!fullinfo_upd_.wait_for(
31-
lock, lslboost::chrono::duration<double>(timeout), info_ready))
30+
else if (!fullinfo_upd_.wait_for(lock, std::chrono::duration<double>(timeout), info_ready))
3231
throw timeout_error("The info() operation timed out.");
3332
}
3433
if (conn_.lost())
@@ -61,7 +60,7 @@ void lsl::info_receiver::info_thread() {
6160
if (!info.created_at()) continue;
6261
// store the result for pickup & return
6362
{
64-
lslboost::lock_guard<lslboost::mutex> lock(fullinfo_mut_);
63+
std::lock_guard<std::mutex> lock(fullinfo_mut_);
6564
fullinfo_ = std::make_shared<stream_info_impl>(info);
6665
}
6766
fullinfo_upd_.notify_all();

src/info_receiver.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include "common.h"
55
#include "forward.h"
6-
#include <boost/thread/condition_variable.hpp>
7-
#include <boost/thread/mutex.hpp>
86
#include <boost/thread/thread_only.hpp>
7+
#include <condition_variable>
8+
#include <mutex>
99

1010
namespace lsl {
1111
class inlet_connection;
@@ -49,9 +49,9 @@ class info_receiver {
4949
/// the full stream_info_impl object (retrieved by the info thread)
5050
stream_info_impl_p fullinfo_;
5151
/// mutex to protect the fullinfo
52-
lslboost::mutex fullinfo_mut_;
52+
std::mutex fullinfo_mut_;
5353
/// condition variable to indicate that an update for the fullinfo is available
54-
lslboost::condition_variable fullinfo_upd_;
54+
std::condition_variable fullinfo_upd_;
5555
};
5656

5757
} // namespace lsl

0 commit comments

Comments
 (0)