1 /* Copyright 2018 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 #ifndef TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_ 16 #define TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_ 17 18 #include <memory> 19 20 #include "tensorflow/core/platform/mutex.h" 21 #include "tensorflow/core/public/session.h" 22 23 namespace tensorflow { 24 25 class SessionLogger; 26 27 // A `SessionRef` manages the lifetime of a wrapped `Session` pointer. 28 // 29 // SessionRef blocks the return of Close() until all pending operations have 30 // been completed or cancelled and underlying session has been freed. Any 31 // subsequent operations on the SessionRef object will return errors::Cancelled. 32 class SessionRef : public Session { 33 public: 34 explicit SessionRef(Session* session); 35 ~SessionRef() override; 36 37 Status Create(const GraphDef& graph) override; 38 Status Extend(const GraphDef& graph) override; 39 Status Create(const RunOptions& run_options, const GraphDef& graph) override; 40 Status Extend(const RunOptions& run_options, const GraphDef& graph) override; 41 Status Run(const std::vector<std::pair<string, Tensor> >& inputs, 42 const std::vector<string>& output_tensor_names, 43 const std::vector<string>& target_node_names, 44 std::vector<Tensor>* outputs) override; 45 46 Status ListDevices(std::vector<DeviceAttributes>* response) override; 47 48 Status Close() override; 49 Status Close(const RunOptions& run_options) override; 50 51 Status Run(const RunOptions& run_options, 52 const std::vector<std::pair<string, Tensor> >& inputs, 53 const std::vector<string>& output_tensor_names, 54 const std::vector<string>& target_node_names, 55 std::vector<Tensor>* outputs, RunMetadata* run_metadata) override; 56 57 Status PRunSetup(const std::vector<string>& input_names, 58 const std::vector<string>& output_names, 59 const std::vector<string>& target_nodes, 60 string* handle) override; 61 62 Status PRun(const string& handle, 63 const std::vector<std::pair<string, Tensor> >& inputs, 64 const std::vector<string>& output_names, 65 std::vector<Tensor>* outputs) override; 66 67 Status MakeCallable(const CallableOptions& callable_options, 68 CallableHandle* out_handle) override; 69 70 Status RunCallable(CallableHandle handle, 71 const std::vector<Tensor>& feed_tensors, 72 std::vector<Tensor>* fetch_tensors, 73 RunMetadata* run_metadata) override; 74 75 Status ReleaseCallable(CallableHandle handle) override; 76 77 private: 78 mutex run_lock_; 79 condition_variable run_finished_; 80 uint64 run_count_ TF_GUARDED_BY(run_lock_) = {0}; 81 std::shared_ptr<Session> session_; 82 83 // Borrowed reference to global session logger. 84 SessionLogger* logger_; 85 86 Status CheckNotClosed(); 87 }; 88 89 } // namespace tensorflow 90 91 #endif // TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_ 92