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_KERNELS_FIFO_QUEUE_H_ 17 #define TENSORFLOW_CORE_KERNELS_FIFO_QUEUE_H_ 18 19 #include <deque> 20 #include <vector> 21 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/framework/tensor_shape.h" 25 #include "tensorflow/core/framework/types.h" 26 #include "tensorflow/core/kernels/queue_op.h" 27 #include "tensorflow/core/kernels/typed_queue.h" 28 #include "tensorflow/core/platform/macros.h" 29 #include "tensorflow/core/platform/mutex.h" 30 #include "tensorflow/core/platform/types.h" 31 32 namespace tensorflow { 33 34 class FIFOQueue : public TypedQueue<std::deque<PersistentTensor> > { 35 public: 36 FIFOQueue(int32 capacity, const DataTypeVector& component_dtypes, 37 const std::vector<TensorShape>& component_shapes, 38 const string& name); 39 40 // Implementations of QueueInterface methods -------------------------------- 41 42 void TryEnqueue(const Tuple& tuple, OpKernelContext* ctx, 43 DoneCallback callback) override; 44 void TryEnqueueMany(const Tuple& tuple, OpKernelContext* ctx, 45 DoneCallback callback) override; 46 void TryDequeue(OpKernelContext* ctx, CallbackWithTuple callback) override; 47 void TryDequeueMany(int num_elements, OpKernelContext* ctx, 48 bool allow_small_batch, 49 CallbackWithTuple callback) override; 50 Status MatchesNodeDef(const NodeDef& node_def) override; 51 size()52 int32 size() const override { 53 mutex_lock lock(mu_); 54 return queues_[0].size(); 55 } 56 57 protected: ~FIFOQueue()58 ~FIFOQueue() override {} 59 60 // Helper for dequeuing a single element from queues_. 61 void DequeueLocked(OpKernelContext* ctx, Tuple* tuple) 62 EXCLUSIVE_LOCKS_REQUIRED(mu_); 63 64 static Status GetElementComponentFromBatch(const Tuple& tuple, int64 index, 65 int component, 66 OpKernelContext* ctx, 67 PersistentTensor* out_element); 68 69 private: 70 TF_DISALLOW_COPY_AND_ASSIGN(FIFOQueue); 71 }; 72 73 // Defines a FIFOQueueOp, which produces a Queue (specifically, one 74 // backed by FIFOQueue) that persists across different graph 75 // executions, and sessions. Running this op produces a single-element 76 // tensor of handles to Queues in the corresponding device. 77 class FIFOQueueOp : public TypedQueueOp { 78 public: 79 explicit FIFOQueueOp(OpKernelConstruction* context); 80 81 private: 82 Status CreateResource(QueueInterface** ret) override 83 EXCLUSIVE_LOCKS_REQUIRED(mu_); 84 85 std::vector<TensorShape> component_shapes_; 86 TF_DISALLOW_COPY_AND_ASSIGN(FIFOQueueOp); 87 }; 88 89 } // namespace tensorflow 90 91 #endif // TENSORFLOW_CORE_KERNELS_FIFO_QUEUE_H_ 92