1 | /* Copyright 2016 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 | |
16 | #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CALL_OPTIONS_H_ |
17 | #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CALL_OPTIONS_H_ |
18 | |
19 | #include <functional> |
20 | |
21 | #include "tensorflow/core/platform/macros.h" |
22 | #include "tensorflow/core/platform/mutex.h" |
23 | #include "tensorflow/core/platform/thread_annotations.h" |
24 | #include "tensorflow/core/platform/types.h" |
25 | |
26 | namespace tensorflow { |
27 | |
28 | // Options passed to interface calls. This class provides portable |
29 | // functionality across different RPC systems on top of |
30 | // platform-specific mechanisms (for client and server contexts, |
31 | // cancellation, etc.). |
32 | // |
33 | // TODO(zhifengc): Maybe change all RPC methods to take CallOptions. |
34 | class CallOptions { |
35 | public: |
36 | CallOptions(); |
37 | |
38 | // Cancellation. |
39 | // |
40 | // The caller may call StartCancel() anytime as long as this |
41 | // CallOptions object is alive. The callee may or may not receive |
42 | // the cancellation notification depending on the rpc layer |
43 | // implementation. |
44 | void StartCancel(); |
45 | |
46 | // The callee (the rpc layer implementation) must set a cancellation |
47 | // notifier before its blocking operation and clear the notifier |
48 | // before the call returns. |
49 | // |
50 | // "cancel_func" may be called zero, once or more time. Therefore, it |
51 | // should _not_ be responsible for memory management of any objects. |
52 | // |
53 | // "cancel_func" must be very light-weight. It should not block on |
54 | // IO or locking. Typically, it just calls the rpc implementation |
55 | // layer's specific cancellation mechanism and does nothing else. |
56 | // |
57 | // NOTE: "cancel_func" itself is pass-by-value. Therefore, we do not |
58 | // worry about its ownership here. |
59 | typedef std::function<void()> CancelFunction; |
60 | void SetCancelCallback(CancelFunction cancel_func); |
61 | void ClearCancelCallback(); |
62 | |
63 | // Get and set operation timeout. Timeout value is in milliseconds. |
64 | // |
65 | // Default: 0. indicating there is no timeout for this call. |
66 | int64_t GetTimeout(); |
67 | void SetTimeout(int64_t ms); |
68 | |
69 | private: |
70 | mutex mu_; |
71 | CancelFunction cancel_func_ TF_GUARDED_BY(mu_); |
72 | |
73 | // RPC operation timeout in milliseconds. |
74 | int64_t timeout_in_ms_ TF_GUARDED_BY(mu_) = 0; |
75 | |
76 | TF_DISALLOW_COPY_AND_ASSIGN(CallOptions); |
77 | }; |
78 | |
79 | } // namespace tensorflow |
80 | |
81 | #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CALL_OPTIONS_H_ |
82 | |