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_VARIABLE_OPS_H_
     17 #define TENSORFLOW_KERNELS_VARIABLE_OPS_H_
     18 
     19 #include "tensorflow/core/framework/allocator.h"
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 #include "tensorflow/core/framework/register_types.h"
     22 #include "tensorflow/core/framework/resource_mgr.h"
     23 #include "tensorflow/core/lib/core/errors.h"
     24 #include "tensorflow/core/platform/macros.h"
     25 #include "tensorflow/core/platform/mutex.h"
     26 #include "tensorflow/core/platform/types.h"
     27 
     28 namespace tensorflow {
     29 
     30 // Resource stored by variables in the resource manager
     31 // (new, resource-style version).
     32 class Var : public ResourceBase {
     33  public:
     34   explicit Var(DataType dtype) : tensor_(dtype) {}
     35   // Not copyable or movable.
     36   Var(const Var&) = delete;
     37   Var& operator=(const Var&) = delete;
     38 
     39   // TODO(ebrevdo): Use LockSet instead of exposing mu.
     40   mutex* mu() { return &mu_; }
     41   Tensor* tensor() { return &tensor_; }
     42 
     43   string DebugString() override {
     44     return strings::StrCat(DataTypeString(tensor_.dtype()), "/",
     45                            tensor_.shape().DebugString());
     46   }
     47 
     48  private:
     49   mutex mu_;
     50   Tensor tensor_;
     51 
     52   ~Var() override {}
     53 };
     54 
     55 class VariableOp : public OpKernel {
     56  public:
     57   explicit VariableOp(OpKernelConstruction* context);
     58   void Compute(OpKernelContext* ctx) override;
     59 
     60  private:
     61   DataType dtype_;
     62   TensorShape shape_;
     63 
     64   mutex init_mu_;
     65   ContainerInfo cinfo_ GUARDED_BY(init_mu_);
     66   bool initialized_ GUARDED_BY(init_mu_){false};
     67 
     68   TF_DISALLOW_COPY_AND_ASSIGN(VariableOp);
     69 };
     70 
     71 }  // namespace tensorflow
     72 
     73 #endif  // TENSORFLOW_KERNELS_VARIABLE_OPS_H_
     74