1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
---|---|
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://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, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | #include "tensorflow/core/distributed_runtime/cancellable_call.h" |
16 | |
17 | namespace tensorflow { |
18 | |
19 | void CancellableCall::Start(const StatusCallback& done) { |
20 | if (cancel_mgr_ == nullptr) { |
21 | IssueCall(done); |
22 | return; |
23 | } |
24 | CancellationToken token = cancel_mgr_->get_cancellation_token(); |
25 | const bool not_yet_cancelled = |
26 | cancel_mgr_->RegisterCallback(token, [this]() { Cancel(); }); |
27 | if (not_yet_cancelled) { |
28 | IssueCall([this, token, done](const Status& s) { |
29 | cancel_mgr_->DeregisterCallback(token); |
30 | done(s); |
31 | }); |
32 | } else { |
33 | done(errors::Cancelled("RPC Request was cancelled")); |
34 | } |
35 | } |
36 | |
37 | void CancellableCall::Cancel() { |
38 | { |
39 | mutex_lock l(mu_); |
40 | if (is_cancelled_) { |
41 | return; |
42 | } |
43 | is_cancelled_ = true; |
44 | } |
45 | opts_.StartCancel(); |
46 | } |
47 | |
48 | } // namespace tensorflow |
49 |