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
CloseClientCall()19 void ClientCall::CloseClientCall() {
20 if (client_stream_open()) {
21 CloseClientStreamLocked().IgnoreError();
22 }
23 UnregisterAndMarkClosed();
24 }
25
MoveClientCallFrom(ClientCall & other)26 void ClientCall::MoveClientCallFrom(ClientCall& other)
27 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
28 WaitUntilReadyForMove(*this, other);
29 CloseClientCall();
30 MoveFrom(other);
31 }
32
HandleCompleted(ConstByteSpan response,Status status)33 void UnaryResponseClientCall::HandleCompleted(
34 ConstByteSpan response, Status status) PW_NO_LOCK_SAFETY_ANALYSIS {
35 UnregisterAndMarkClosed();
36
37 auto on_completed_local = std::move(on_completed_);
38 CallbackStarted();
39
40 // The lock is only released when calling into user code. If the callback is
41 // wrapped, this on_completed is an internal function that expects the lock to
42 // be held, and releases it before invoking user code.
43 if (!hold_lock_while_invoking_callback_with_payload()) {
44 rpc_lock().unlock();
45 }
46
47 if (on_completed_local) {
48 on_completed_local(response, status);
49 }
50
51 // This mutex lock could be avoided by making callbacks_executing_ atomic.
52 RpcLockGuard lock;
53 CallbackFinished();
54 }
55
HandleCompleted(Status status)56 void StreamResponseClientCall::HandleCompleted(Status status) {
57 UnregisterAndMarkClosed();
58 auto on_completed_local = std::move(on_completed_);
59 CallbackStarted();
60 rpc_lock().unlock();
61
62 if (on_completed_local) {
63 on_completed_local(status);
64 }
65
66 // This mutex lock could be avoided by making callbacks_executing_ atomic.
67 RpcLockGuard lock;
68 CallbackFinished();
69 }
70
71 } // namespace pw::rpc::internal
72