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_CORE_FRAMEWORK_FUNCTION_HANDLE_CACHE_H_ 16 #define TENSORFLOW_CORE_FRAMEWORK_FUNCTION_HANDLE_CACHE_H_ 17 18 #include <string> 19 20 #include "tensorflow/core/framework/function.h" 21 22 namespace tensorflow { 23 namespace data { 24 25 class FunctionHandleCache { 26 public: 27 explicit FunctionHandleCache(FunctionLibraryRuntime* lib); 28 29 ~FunctionHandleCache(); 30 31 // Looks up the function to be instantiated in the cache first. If present, 32 // returns handle from there. Otherwise, instantiates a new function 33 // and stores handle in the cache. 34 Status Instantiate(const string& function_name, AttrSlice attrs, 35 FunctionLibraryRuntime::InstantiateOptions options, 36 FunctionLibraryRuntime::Handle* handle); 37 38 // Releases all the handles in the cache, clearing out the state for all 39 // functions involved. 40 Status Clear(); 41 42 private: 43 mutex mu_; 44 FunctionLibraryRuntime* lib_ = nullptr; // not owned 45 const string state_handle_; 46 std::unordered_map<string, FunctionLibraryRuntime::Handle> handles_ 47 TF_GUARDED_BY(mu_); 48 }; 49 50 } // namespace data 51 } // namespace tensorflow 52 53 #endif // TENSORFLOW_CORE_FRAMEWORK_FUNCTION_HANDLE_CACHE_H_ 54