1 | /* Copyright 2018 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 | #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CANCELLABLE_CALL_H_ |
16 | #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CANCELLABLE_CALL_H_ |
17 | |
18 | #include <string> |
19 | #include "tensorflow/core/distributed_runtime/call_options.h" |
20 | #include "tensorflow/core/distributed_runtime/worker_cache.h" |
21 | #include "tensorflow/core/framework/cancellation.h" |
22 | #include "tensorflow/core/platform/mutex.h" |
23 | |
24 | namespace tensorflow { |
25 | |
26 | // Supports client side cancellation of WorkerInterface calls via |
27 | // registration with a CancellationManager. |
28 | class CancellableCall { |
29 | public: |
30 | CancellableCall(CancellationManager* cancel_mgr, const string& remote_worker, |
31 | WorkerCacheInterface* wc) |
32 | : is_cancelled_(false), |
33 | cancel_mgr_(cancel_mgr), |
34 | remote_worker_(remote_worker), |
35 | wc_(wc), |
36 | wi_(wc_->GetOrCreateWorker(remote_worker_)) {} |
37 | |
38 | virtual ~CancellableCall() { wc_->ReleaseWorker(remote_worker_, wi_); } |
39 | |
40 | virtual void IssueCall(const StatusCallback& done) = 0; |
41 | |
42 | void Start(const StatusCallback& done); |
43 | |
44 | // Cancels the RPC if it's not cancelled yet. This must be called after |
45 | // Start(). This is normally used if there's a needed to cancel the RPC from a |
46 | // sideband. If appliable, pass a cancellation manager to the constructor |
47 | // instead of using this method. |
48 | void Cancel() TF_LOCKS_EXCLUDED(mu_); |
49 | |
50 | protected: |
51 | mutex mu_; |
52 | bool is_cancelled_; |
53 | CancellationManager* const cancel_mgr_; // Not owned |
54 | const string remote_worker_; |
55 | WorkerCacheInterface* const wc_; // Not owned |
56 | WorkerInterface* const wi_; // Owned by wc_, must be released. |
57 | CallOptions opts_; |
58 | }; |
59 | |
60 | } // namespace tensorflow |
61 | #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CANCELLABLE_CALL_H_ |
62 | |