Use std::unique_lock for mutex operations

This commit is contained in:
Simon Fels 2017-05-14 12:53:48 +02:00
commit 62ce199dbc
2 changed files with 10 additions and 6 deletions

View file

@ -36,13 +36,14 @@ const std::string start_command{"start"};
// user until we connect to the adb host instance after it appeared and not
// too short to not put unnecessary burden on the CPU.
const boost::posix_time::seconds default_adb_wait_time{1};
static std::mutex active_instance;
}
using namespace std::placeholders;
namespace anbox {
namespace qemu {
std::mutex AdbMessageProcessor::active_instance_{};
AdbMessageProcessor::AdbMessageProcessor(
const std::shared_ptr<Runtime> &rt,
const std::shared_ptr<network::SocketMessenger> &messenger)
@ -50,16 +51,14 @@ AdbMessageProcessor::AdbMessageProcessor(
state_(waiting_for_guest_accept_command),
expected_command_(accept_command),
messenger_(messenger),
host_notify_timer_(rt->service()) {}
host_notify_timer_(rt->service()),
lock_(active_instance_, std::defer_lock) {}
AdbMessageProcessor::~AdbMessageProcessor() {
state_ = closed_by_host;
host_notify_timer_.cancel();
host_connector_.reset();
// Unlock our lock to bring down any waiting instance
active_instance.unlock();
}
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.
// The container directly starts a second connection once the first
// one is established but will not use it until the active one is closed.
active_instance.lock();
lock_.lock();
if (state_ == closed_by_host) {
host_notify_timer_.cancel();

View file

@ -27,6 +27,8 @@
#include <boost/asio.hpp>
#include <mutex>
namespace anbox {
namespace qemu {
class AdbMessageProcessor : public network::MessageProcessor {
@ -66,6 +68,9 @@ class AdbMessageProcessor : public network::MessageProcessor {
std::shared_ptr<network::TcpSocketMessenger> host_messenger_;
std::array<std::uint8_t, 8192> host_buffer_;
boost::asio::deadline_timer host_notify_timer_;
std::unique_lock<std::mutex> lock_;
static std::mutex active_instance_;
};
} // namespace graphics
} // namespace anbox