pw_rpc: Guard the channels list with the RPC mutex

Change-Id: I99c5a888572fb14da03edf2595ddf656f5f4752b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/80000
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_rpc/call.cc b/pw_rpc/call.cc
index 044c6f5..a4d8f72 100644
--- a/pw_rpc/call.cc
+++ b/pw_rpc/call.cc
@@ -117,6 +117,9 @@
     Channel& c = channel();
     rpc_lock().unlock();
 
+    // TODO(pwbug/597): Ensure the call object is locked before releasing the
+    //     RPC lock.
+
     // Don't call AcquireBuffer with rpc_lock() held, as this may cause deadlock
     // if the channel is also protected by a mutex.
     Channel::OutputBuffer buffer = c.AcquireBuffer();
diff --git a/pw_rpc/public/pw_rpc/internal/endpoint.h b/pw_rpc/public/pw_rpc/internal/endpoint.h
index 145e0a2..e7ce69f 100644
--- a/pw_rpc/public/pw_rpc/internal/endpoint.h
+++ b/pw_rpc/public/pw_rpc/internal/endpoint.h
@@ -62,13 +62,15 @@
         packet.channel_id(), packet.service_id(), packet.method_id());
   }
 
-  // Finds an internal:::Channel with this ID or nullptr if none matches.
-  Channel* GetInternalChannel(uint32_t id) const;
+  // Finds an internal::Channel with this ID or nullptr if none matches.
+  Channel* GetInternalChannel(uint32_t id) const
+      PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
 
   // Creates a channel with the provided ID and ChannelOutput, if a channel slot
   // is available. Returns a pointer to the channel if one is created, nullptr
   // otherwise.
-  Channel* AssignChannel(uint32_t id, ChannelOutput& interface);
+  Channel* AssignChannel(uint32_t id, ChannelOutput& interface)
+      PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
 
  private:
   // Give Call access to the register/unregister functions.
@@ -103,7 +105,7 @@
                      uint32_t method_id)
       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
 
-  std::span<Channel> channels_;
+  std::span<Channel> channels_ PW_GUARDED_BY(rpc_lock());
   IntrusiveList<Call> calls_ PW_GUARDED_BY(rpc_lock());
 
   uint32_t next_call_id_;