• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 // Thread-safe data structure for caching function instantiations.
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   //
35   // The cache retains the ownership of the handle. In particular, the caller
36   // should not invoke `ReleaseHandle`.
37   Status Instantiate(const string& function_name, AttrSlice attrs,
38                      FunctionLibraryRuntime::InstantiateOptions options,
39                      FunctionLibraryRuntime::Handle* handle);
40 
41   // Releases all the handles in the cache, clearing out the state for all
42   // functions involved.
43   Status Clear();
44 
45  private:
46   mutex mu_;
47   FunctionLibraryRuntime* lib_ = nullptr;  // not owned
48   const string state_handle_;
49   std::unordered_map<string, FunctionLibraryRuntime::Handle> handles_
50       TF_GUARDED_BY(mu_);
51 };
52 
53 }  // namespace tensorflow
54 
55 #endif  // TENSORFLOW_CORE_FRAMEWORK_FUNCTION_HANDLE_CACHE_H_
56