• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()
40   // (typically called during construction).  factory must return a
41   // ReaderInterface descendant allocated with new that ReaderOpKernel
42   // will take ownership of.
SetReaderFactory(std::function<ReaderInterface * ()> factory)43   void SetReaderFactory(std::function<ReaderInterface*()> factory)
44       LOCKS_EXCLUDED(mu_) {
45     mutex_lock l(mu_);
46     DCHECK(resource_ == nullptr);
47     factory_ = factory;
48   }
49 
Compute(OpKernelContext * context)50   void Compute(OpKernelContext* context) override {
51     if (!IsCancellable()) {
52       ResourceOpKernel<ReaderInterface>::Compute(context);
53     } else {
54       // Install cancellation
55       CancellationManager* cm = context->cancellation_manager();
56       CancellationToken token = cm->get_cancellation_token();
57       bool already_cancelled =
58           !cm->RegisterCallback(token, [this]() { this->Cancel(); });
59 
60       if (!already_cancelled) {
61         ResourceOpKernel<ReaderInterface>::Compute(context);
62       } else {
63         context->SetStatus(errors::Cancelled("read operation was cancelled"));
64       }
65     }
66   }
67 
68  private:
IsCancellable()69   virtual bool IsCancellable() const { return false; }
Cancel()70   virtual void Cancel() {}
71 
CreateResource(ReaderInterface ** reader)72   Status CreateResource(ReaderInterface** reader)
73       EXCLUSIVE_LOCKS_REQUIRED(mu_) override {
74     *reader = factory_();
75     if (*reader == nullptr) {
76       return errors::ResourceExhausted("Failed to allocate reader");
77     }
78     std::function<ReaderInterface*()> temp = nullptr;
79     factory_.swap(temp);
80     return Status::OK();
81   }
82 
83   std::function<ReaderInterface*()> factory_ GUARDED_BY(mu_);
84 };
85 
86 }  // namespace tensorflow
87 
88 #endif  // TENSORFLOW_CORE_FRAMEWORK_READER_OP_KERNEL_H_
89