diff --git a/src/anbox/qemu/adb_message_processor.cpp b/src/anbox/qemu/adb_message_processor.cpp index ef56773..ad2e792 100644 --- a/src/anbox/qemu/adb_message_processor.cpp +++ b/src/anbox/qemu/adb_message_processor.cpp @@ -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 &rt, const std::shared_ptr &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(); diff --git a/src/anbox/qemu/adb_message_processor.h b/src/anbox/qemu/adb_message_processor.h index b98fa7a..a58925e 100644 --- a/src/anbox/qemu/adb_message_processor.h +++ b/src/anbox/qemu/adb_message_processor.h @@ -27,6 +27,8 @@ #include +#include + namespace anbox { namespace qemu { class AdbMessageProcessor : public network::MessageProcessor { @@ -66,6 +68,9 @@ class AdbMessageProcessor : public network::MessageProcessor { std::shared_ptr host_messenger_; std::array host_buffer_; boost::asio::deadline_timer host_notify_timer_; + std::unique_lock lock_; + + static std::mutex active_instance_; }; } // namespace graphics } // namespace anbox