Host will wait on HOST_CONNECTED queues until queue_state changes.

Bug: 80486122
Bug: 80104636
Change-Id: If447364be757feb40053e85e30c7969f84b7d25a
Merged-In: If447364be757feb40053e85e30c7969f84b7d25a
Test: local boot and hammer with adb connect commands
(cherry picked from commit 0bf3b641ce6bbb328ad755442bcea3dfd43cb844)
diff --git a/common/vsoc/lib/socket_forward_region_view.cpp b/common/vsoc/lib/socket_forward_region_view.cpp
index 5c9c281..c8a153b 100644
--- a/common/vsoc/lib/socket_forward_region_view.cpp
+++ b/common/vsoc/lib/socket_forward_region_view.cpp
@@ -24,7 +24,7 @@
 
 using vsoc::layout::socket_forward::Queue;
 using vsoc::layout::socket_forward::QueuePair;
-using vsoc::layout::socket_forward::QueueState;
+namespace QueueState = vsoc::layout::socket_forward::QueueState;
 // store the read and write direction as variables to keep the ifdefs and macros
 // in later code to a minimum
 constexpr auto ReadDirection = &QueuePair::
@@ -143,7 +143,7 @@
   auto end_packet = Packet::MakeEnd();
   end_packet.set_generation(current_generation);
   for (auto&& queue_pair : data()->queues_) {
-    QueueState state{};
+    std::uint32_t state{};
     {
       auto guard = make_lock_guard(&queue_pair.queue_state_lock_);
       state = (queue_pair.*WriteDirection).queue_state_;
@@ -179,13 +179,10 @@
 #ifdef CUTTLEFISH_HOST
   // if the host has connected but the guest hasn't seen it yet, wait for the
   // guest to connect so the protocol can follow the normal state transition.
-  while (true) {
-    auto guard = make_lock_guard(&queue_pair.queue_state_lock_);
-    if (queue.queue_state_ != QueueState::HOST_CONNECTED) {
-      break;
-    }
-    LOG(WARNING) << "closing queue in HOST_CONNECTED state. waiting";
-    sleep(1);
+  while (queue.queue_state_ == QueueState::HOST_CONNECTED) {
+    LOG(WARNING) << "closing queue[" << connection_id
+                 << "] in HOST_CONNECTED state. waiting";
+    WaitForSignal(&queue.queue_state_, QueueState::HOST_CONNECTED);
   }
 #endif
 
@@ -261,6 +258,8 @@
       LOG(DEBUG) << "found waiting connection at index " << id;
       queue_pair.host_to_guest.queue_state_ = QueueState::BOTH_CONNECTED;
       queue_pair.guest_to_host.queue_state_ = QueueState::BOTH_CONNECTED;
+      SendSignal(layout::Sides::Peer, &queue_pair.host_to_guest.queue_state_);
+      SendSignal(layout::Sides::Peer, &queue_pair.guest_to_host.queue_state_);
       return id;
     }
     ++id;
diff --git a/common/vsoc/shm/socket_forward_layout.h b/common/vsoc/shm/socket_forward_layout.h
index 5c78f21..02523af 100644
--- a/common/vsoc/shm/socket_forward_layout.h
+++ b/common/vsoc/shm/socket_forward_layout.h
@@ -27,17 +27,15 @@
 constexpr std::size_t kMaxPacketSize = 8192;
 constexpr std::size_t kNumQueues = 16;
 
-enum class QueueState : std::uint32_t {
-  INACTIVE = 0,
-  HOST_CONNECTED = 1,
-  BOTH_CONNECTED = 2,
-  HOST_CLOSED = 3,
-  GUEST_CLOSED = 4,
-  // If both are closed then the queue goes back to INACTIVE
-  // BOTH_CLOSED = 0,
-};
-static_assert(ShmTypeValidator<QueueState, 4>::valid,
-              "Compilation error. Please fix above errors and retry.");
+namespace QueueState {
+constexpr std::uint32_t INACTIVE = 0;
+constexpr std::uint32_t HOST_CONNECTED = 1;
+constexpr std::uint32_t BOTH_CONNECTED = 2;
+constexpr std::uint32_t HOST_CLOSED = 3;
+constexpr std::uint32_t GUEST_CLOSED = 4;
+// If both are closed then the queue goes back to INACTIVE
+// BOTH_CLOSED = 0,
+}  // namespace QueueState
 
 struct Queue {
   static constexpr size_t layout_size =
@@ -45,11 +43,9 @@
 
   CircularPacketQueue<16, kMaxPacketSize> queue;
 
-  QueueState queue_state_;
+  std::atomic_uint32_t queue_state_;
 
-  bool Recover() {
-    return queue.Recover();
-  }
+  bool Recover() { return queue.Recover(); }
 };
 ASSERT_SHM_COMPATIBLE(Queue);
 
@@ -65,11 +61,10 @@
 
   SpinLock queue_state_lock_;
 
-
   bool Recover() {
     // TODO: Put queue_state_ and port_ recovery here, probably after grabbing
     bool recovered = false;
-    recovered = recovered ||  host_to_guest.Recover();
+    recovered = recovered || host_to_guest.Recover();
     recovered = recovered || guest_to_host.Recover();
     recovered = recovered || queue_state_lock_.Recover();
     return recovered;
@@ -86,13 +81,14 @@
       bool rval = i.Recover();
       recovered = recovered || rval;
     }
-    //TODO: consider handling the sequence number here
+    // TODO: consider handling the sequence number here
     return recovered;
   }
 
   QueuePair queues_[kNumQueues];
-  std::atomic_uint32_t seq_num; // incremented for every new connection
-  std::atomic_uint32_t generation_num; // incremented for every new socket forward process
+  std::atomic_uint32_t seq_num;  // incremented for every new connection
+  std::atomic_uint32_t
+      generation_num;  // incremented for every new socket forward process
   static const char* region_name;
 };