1 | /* Copyright 2015 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_FRAMEWORK_READER_OP_KERNEL_H_ |
17 | #define TENSORFLOW_CORE_FRAMEWORK_READER_OP_KERNEL_H_ |
18 | |
19 | #include <functional> |
20 | #include <string> |
21 | |
22 | #include "tensorflow/core/framework/op_kernel.h" |
23 | #include "tensorflow/core/framework/reader_interface.h" |
24 | #include "tensorflow/core/framework/resource_mgr.h" |
25 | #include "tensorflow/core/framework/resource_op_kernel.h" |
26 | #include "tensorflow/core/platform/mutex.h" |
27 | #include "tensorflow/core/platform/types.h" |
28 | |
29 | namespace tensorflow { |
30 | |
31 | // NOTE: This is now a very thin layer over ResourceOpKernel. |
32 | // TODO(sjhwang): Remove dependencies to this class, then delete this. |
33 | |
34 | // Implementation for ops providing a Reader. |
35 | class ReaderOpKernel : public ResourceOpKernel<ReaderInterface> { |
36 | public: |
37 | using ResourceOpKernel::ResourceOpKernel; |
38 | |
39 | // Must be called by descendants before the first call to Compute() (typically |
40 | // called during construction). factory must return a ReaderInterface |
41 | // descendant allocated with new that ReaderOpKernel will take ownership of. |
42 | void SetReaderFactory(std::function<ReaderInterface*()> factory) |
43 | TF_LOCKS_EXCLUDED(mu_) { |
44 | DCHECK(get_resource() == nullptr); |
45 | mutex_lock l(mu_); |
46 | factory_ = factory; |
47 | } |
48 | |
49 | void Compute(OpKernelContext* context) override { |
50 | if (!IsCancellable()) { |
51 | ResourceOpKernel<ReaderInterface>::Compute(context); |
52 | } else { |
53 | // Install cancellation |
54 | CancellationManager* cm = context->cancellation_manager(); |
55 | CancellationToken token = cm->get_cancellation_token(); |
56 | bool already_cancelled = |
57 | !cm->RegisterCallback(token, [this]() { this->Cancel(); }); |
58 | |
59 | if (!already_cancelled) { |
60 | ResourceOpKernel<ReaderInterface>::Compute(context); |
61 | } else { |
62 | context->SetStatus(errors::Cancelled("read operation was cancelled")); |
63 | } |
64 | } |
65 | } |
66 | |
67 | private: |
68 | virtual bool IsCancellable() const { return false; } |
69 | virtual void Cancel() {} |
70 | |
71 | Status CreateResource(ReaderInterface** reader) |
72 | TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) override { |
73 | *reader = factory_(); |
74 | if (*reader == nullptr) { |
75 | return errors::ResourceExhausted("Failed to allocate reader"); |
76 | } |
77 | std::function<ReaderInterface*()> temp = nullptr; |
78 | factory_.swap(temp); |
79 | return OkStatus(); |
80 | } |
81 | |
82 | std::function<ReaderInterface*()> factory_ TF_GUARDED_BY(mu_); |
83 | }; |
84 | |
85 | } // namespace tensorflow |
86 | |
87 | #endif // TENSORFLOW_CORE_FRAMEWORK_READER_OP_KERNEL_H_ |
88 |