Use std::unique_lock for mutex operations
This commit is contained in:
parent
183c02d2d0
commit
62ce199dbc
2 changed files with 10 additions and 6 deletions
|
|
@ -36,13 +36,14 @@ const std::string start_command{"start"};
|
||||||
// user until we connect to the adb host instance after it appeared and not
|
// user until we connect to the adb host instance after it appeared and not
|
||||||
// too short to not put unnecessary burden on the CPU.
|
// too short to not put unnecessary burden on the CPU.
|
||||||
const boost::posix_time::seconds default_adb_wait_time{1};
|
const boost::posix_time::seconds default_adb_wait_time{1};
|
||||||
static std::mutex active_instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
namespace anbox {
|
namespace anbox {
|
||||||
namespace qemu {
|
namespace qemu {
|
||||||
|
std::mutex AdbMessageProcessor::active_instance_{};
|
||||||
|
|
||||||
AdbMessageProcessor::AdbMessageProcessor(
|
AdbMessageProcessor::AdbMessageProcessor(
|
||||||
const std::shared_ptr<Runtime> &rt,
|
const std::shared_ptr<Runtime> &rt,
|
||||||
const std::shared_ptr<network::SocketMessenger> &messenger)
|
const std::shared_ptr<network::SocketMessenger> &messenger)
|
||||||
|
|
@ -50,16 +51,14 @@ AdbMessageProcessor::AdbMessageProcessor(
|
||||||
state_(waiting_for_guest_accept_command),
|
state_(waiting_for_guest_accept_command),
|
||||||
expected_command_(accept_command),
|
expected_command_(accept_command),
|
||||||
messenger_(messenger),
|
messenger_(messenger),
|
||||||
host_notify_timer_(rt->service()) {}
|
host_notify_timer_(rt->service()),
|
||||||
|
lock_(active_instance_, std::defer_lock) {}
|
||||||
|
|
||||||
AdbMessageProcessor::~AdbMessageProcessor() {
|
AdbMessageProcessor::~AdbMessageProcessor() {
|
||||||
state_ = closed_by_host;
|
state_ = closed_by_host;
|
||||||
|
|
||||||
host_notify_timer_.cancel();
|
host_notify_timer_.cancel();
|
||||||
host_connector_.reset();
|
host_connector_.reset();
|
||||||
|
|
||||||
// Unlock our lock to bring down any waiting instance
|
|
||||||
active_instance.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdbMessageProcessor::advance_state() {
|
void AdbMessageProcessor::advance_state() {
|
||||||
|
|
@ -69,7 +68,7 @@ void AdbMessageProcessor::advance_state() {
|
||||||
// running we don't have to do anything here until that one is done.
|
// running we don't have to do anything here until that one is done.
|
||||||
// The container directly starts a second connection once the first
|
// The container directly starts a second connection once the first
|
||||||
// one is established but will not use it until the active one is closed.
|
// one is established but will not use it until the active one is closed.
|
||||||
active_instance.lock();
|
lock_.lock();
|
||||||
|
|
||||||
if (state_ == closed_by_host) {
|
if (state_ == closed_by_host) {
|
||||||
host_notify_timer_.cancel();
|
host_notify_timer_.cancel();
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace anbox {
|
namespace anbox {
|
||||||
namespace qemu {
|
namespace qemu {
|
||||||
class AdbMessageProcessor : public network::MessageProcessor {
|
class AdbMessageProcessor : public network::MessageProcessor {
|
||||||
|
|
@ -66,6 +68,9 @@ class AdbMessageProcessor : public network::MessageProcessor {
|
||||||
std::shared_ptr<network::TcpSocketMessenger> host_messenger_;
|
std::shared_ptr<network::TcpSocketMessenger> host_messenger_;
|
||||||
std::array<std::uint8_t, 8192> host_buffer_;
|
std::array<std::uint8_t, 8192> host_buffer_;
|
||||||
boost::asio::deadline_timer host_notify_timer_;
|
boost::asio::deadline_timer host_notify_timer_;
|
||||||
|
std::unique_lock<std::mutex> lock_;
|
||||||
|
|
||||||
|
static std::mutex active_instance_;
|
||||||
};
|
};
|
||||||
} // namespace graphics
|
} // namespace graphics
|
||||||
} // namespace anbox
|
} // namespace anbox
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue