Home | History | Annotate | Download | only in kernels
      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_KERNELS_FIFO_QUEUE_H_
     17 #define TENSORFLOW_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/typed_queue.h"
     27 #include "tensorflow/core/platform/macros.h"
     28 #include "tensorflow/core/platform/mutex.h"
     29 #include "tensorflow/core/platform/types.h"
     30 
     31 namespace tensorflow {
     32 
     33 class FIFOQueue : public TypedQueue<std::deque<PersistentTensor> > {
     34  public:
     35   FIFOQueue(int32 capacity, const DataTypeVector& component_dtypes,
     36             const std::vector<TensorShape>& component_shapes,
     37             const string& name);
     38 
     39   // Implementations of QueueInterface methods --------------------------------
     40 
     41   void TryEnqueue(const Tuple& tuple, OpKernelContext* ctx,
     42                   DoneCallback callback) override;
     43   void TryEnqueueMany(const Tuple& tuple, OpKernelContext* ctx,
     44                       DoneCallback callback) override;
     45   void TryDequeue(OpKernelContext* ctx, CallbackWithTuple callback) override;
     46   void TryDequeueMany(int num_elements, OpKernelContext* ctx,
     47                       bool allow_small_batch,
     48                       CallbackWithTuple callback) override;
     49   Status MatchesNodeDef(const NodeDef& node_def) override;
     50 
     51   int32 size() override {
     52     mutex_lock lock(mu_);
     53     return queues_[0].size();
     54   }
     55 
     56  protected:
     57   ~FIFOQueue() override {}
     58 
     59   // Helper for dequeuing a single element from queues_.
     60   void DequeueLocked(OpKernelContext* ctx, Tuple* tuple)
     61       EXCLUSIVE_LOCKS_REQUIRED(mu_);
     62 
     63   static Status GetElementComponentFromBatch(const Tuple& tuple, int64 index,
     64                                              int component,
     65                                              OpKernelContext* ctx,
     66                                              PersistentTensor* out_element);
     67 
     68  private:
     69   TF_DISALLOW_COPY_AND_ASSIGN(FIFOQueue);
     70 };
     71 
     72 }  // namespace tensorflow
     73 
     74 #endif  // TENSORFLOW_KERNELS_FIFO_QUEUE_H_
     75