xref: /aosp_15_r20/external/pigweed/pw_rpc/client_call.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_rpc/internal/client_call.h"
16 
17 namespace pw::rpc::internal {
18 
MoveClientCallFrom(ClientCall & other)19 void ClientCall::MoveClientCallFrom(ClientCall& other)
20     PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
21   WaitUntilReadyForMove(*this, other);
22   CloseClientCall();
23   MoveFrom(other);
24 }
25 
HandleCompleted(ConstByteSpan response,Status status)26 void UnaryResponseClientCall::HandleCompleted(
27     ConstByteSpan response, Status status) PW_NO_LOCK_SAFETY_ANALYSIS {
28   UnregisterAndMarkClosed();
29 
30   auto on_completed_local = std::move(on_completed_);
31   CallbackStarted();
32 
33   // The lock is only released when calling into user code. If the callback is
34   // wrapped, this on_completed is an internal function that expects the lock to
35   // be held, and releases it before invoking user code.
36   if (!hold_lock_while_invoking_callback_with_payload()) {
37     rpc_lock().unlock();
38   }
39 
40   if (on_completed_local) {
41     on_completed_local(response, status);
42   }
43 
44   // This mutex lock could be avoided by making callbacks_executing_ atomic.
45   RpcLockGuard lock;
46   CallbackFinished();
47 }
48 
HandleCompleted(Status status)49 void StreamResponseClientCall::HandleCompleted(Status status) {
50   UnregisterAndMarkClosed();
51   auto on_completed_local = std::move(on_completed_);
52   CallbackStarted();
53   rpc_lock().unlock();
54 
55   if (on_completed_local) {
56     on_completed_local(status);
57   }
58 
59   // This mutex lock could be avoided by making callbacks_executing_ atomic.
60   RpcLockGuard lock;
61   CallbackFinished();
62 }
63 
64 }  // namespace pw::rpc::internal
65