Skip to content

Commit

Permalink
pw_rpc: Hide internal function in Server and Client
Browse files Browse the repository at this point in the history
Make the internal Endpoint::ClaimLocked() function private in the Server
and Client classes.

Change-Id: I435392eed03f1301349d66fa23bcd500f8b1e7c4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/126679
Commit-Queue: Wyatt Hepler <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Reviewed-by: Alexei Frolov <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Jan 19, 2023
1 parent b7b0a19 commit a81d369
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions pw_rpc/public/pw_rpc/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Client : public internal::Endpoint {
private:
// Remove these internal::Endpoint functions from the public interface.
using Endpoint::active_call_count;
using Endpoint::ClaimLocked;
using Endpoint::GetInternalChannel;
};

Expand Down
26 changes: 16 additions & 10 deletions pw_rpc/public/pw_rpc/internal/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ class Endpoint {
public:
~Endpoint();

// Claims that `rpc_lock()` is held, returning a wrapped endpoint.
//
// This function should only be called in contexts in which it is clear that
// `rpc_lock()` is held. When calling this function from a constructor, the
// lock annotation will not result in errors, so care should be taken to
// ensure that `rpc_lock()` is held.
LockedEndpoint& ClaimLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
// Public functions

// Creates a channel with the provided ID and ChannelOutput, if a channel slot
// is available or can be allocated (if PW_RPC_DYNAMIC_ALLOCATION is enabled).
Expand All @@ -68,14 +62,23 @@ class Endpoint {
// called with the ABORTED status.
Status CloseChannel(uint32_t channel_id) PW_LOCKS_EXCLUDED(rpc_lock());

// For internal use only: returns the number calls in the RPC calls list.
// Internal functions, hidden by the Client and Server classes

// Returns the number calls in the RPC calls list.
size_t active_call_count() const PW_LOCKS_EXCLUDED(rpc_lock()) {
LockGuard lock(rpc_lock());
return calls_.size();
}

// For internal use only: finds an internal::Channel with this ID or nullptr
// if none matches.
// Claims that `rpc_lock()` is held, returning a wrapped endpoint.
//
// This function should only be called in contexts in which it is clear that
// `rpc_lock()` is held. When calling this function from a constructor, the
// lock annotation will not result in errors, so care should be taken to
// ensure that `rpc_lock()` is held.
LockedEndpoint& ClaimLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());

// Finds an internal::Channel with this ID or nullptr if none matches.
Channel* GetInternalChannel(uint32_t channel_id)
PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
return channels_.Get(channel_id);
Expand Down Expand Up @@ -152,6 +155,9 @@ class Endpoint {
uint32_t call_id) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());

ChannelList channels_ PW_GUARDED_BY(rpc_lock());

// List of all active calls associated with this endpoint. Calls are added to
// this list when they start and removed from it when they finish.
IntrusiveList<Call> calls_ PW_GUARDED_BY(rpc_lock());

uint32_t next_call_id_ PW_GUARDED_BY(rpc_lock()) = 0;
Expand Down
1 change: 1 addition & 0 deletions pw_rpc/public/pw_rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Server : public internal::Endpoint {

// Remove these internal::Endpoint functions from the public interface.
using Endpoint::active_call_count;
using Endpoint::ClaimLocked;
using Endpoint::GetInternalChannel;

IntrusiveList<Service> services_ PW_GUARDED_BY(internal::rpc_lock());
Expand Down
2 changes: 1 addition & 1 deletion pw_rpc/raw/client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace {
template <auto kMethod, typename Call, typename Context>
Call MakeCall(Context& context)
PW_EXCLUSIVE_LOCKS_REQUIRED(internal::rpc_lock()) {
return Call(context.client().ClaimLocked(),
return Call(static_cast<internal::Endpoint&>(context.client()).ClaimLocked(),
context.channel().id(),
internal::MethodInfo<kMethod>::kServiceId,
internal::MethodInfo<kMethod>::kMethodId,
Expand Down

0 comments on commit a81d369

Please sign in to comment.