tensorflow-core-framework-function_handle_cache.cc 2019-06-10 304 tensorflow-core-framework ```cpp #include "tensorflow/core/framework/function_handle_cache.h" #include "tensorflow/core/lib/gtl/map_util.h" #include "tensorflow/core/lib/random/random.h" #include "tensorflow/core/lib/strings/stringprintf.h" namespace tensorflow { namespace data { FunctionHandleCache::FunctionHandleCache(FunctionLibraryRuntime* lib) : lib_(lib), state_handle_(strings::Printf("%lld", random::New64())) {} FunctionHandleCache::~FunctionHandleCache() { Status s = Clear(); if (!s.ok()) { LOG(ERROR) << "Failed to clear function handle cache: " << s.ToString(); } } Status FunctionHandleCache::Instantiate( const string& function_name, AttrSlice attrs, FunctionLibraryRuntime::InstantiateOptions options, FunctionLibraryRuntime::Handle* handle) { string key = Canonicalize(function_name, attrs, options); FunctionLibraryRuntime::Handle h; { tf_shared_lock l(mu_); h = gtl::FindWithDefault(handles_, key, kInvalidHandle); } if (h == kInvalidHandle) { options.state_handle = state_handle_; TF_RETURN_IF_ERROR( lib_->Instantiate(function_name, attrs, options, handle)); mutex_lock l(mu_); handles_[key] = *handle; } else { *handle = h; } return Status::OK(); } Status FunctionHandleCache::Clear() { mutex_lock l(mu_); for (auto entry : handles_) { TF_RETURN_IF_ERROR(lib_->ReleaseHandle(entry.second)); } handles_.clear(); return Status::OK(); } } // namespace data } // namespace tensorflow ``` 本文链接: http://codeeyes.net/archives/tensorflow-core-framework-function_handle_cache_cc.html