1 /* Copyright 2021 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_TFRT_UTILS_THREAD_POOL_H_ 16 #define TENSORFLOW_CORE_TFRT_UTILS_THREAD_POOL_H_ 17 18 #include "tensorflow/core/platform/env.h" 19 #include "tensorflow/core/platform/threadpool.h" 20 #include "tensorflow/core/platform/threadpool_interface.h" 21 22 namespace tensorflow { 23 namespace tfrt_stub { 24 25 class TfThreadPool : public thread::ThreadPoolInterface { 26 public: TfThreadPool(const std::string & name,int num_threads)27 explicit TfThreadPool(const std::string& name, int num_threads) 28 : underlying_threadpool_(tensorflow::Env::Default(), name, num_threads) {} 29 Schedule(std::function<void ()> fn)30 void Schedule(std::function<void()> fn) override { 31 underlying_threadpool_.Schedule(std::move(fn)); 32 } 33 ScheduleWithHint(std::function<void ()> fn,int start,int end)34 void ScheduleWithHint(std::function<void()> fn, int start, int end) override { 35 underlying_threadpool_.ScheduleWithHint(std::move(fn), start, end); 36 } 37 Cancel()38 void Cancel() override { 39 underlying_threadpool_.AsEigenThreadPool()->Cancel(); 40 } 41 NumThreads()42 int NumThreads() const override { 43 return underlying_threadpool_.NumThreads(); 44 } 45 CurrentThreadId()46 int CurrentThreadId() const override { 47 return underlying_threadpool_.CurrentThreadId(); 48 } 49 50 private: 51 tensorflow::thread::ThreadPool underlying_threadpool_; 52 }; 53 54 } // namespace tfrt_stub 55 } // namespace tensorflow 56 57 #endif // TENSORFLOW_CORE_TFRT_UTILS_THREAD_POOL_H_ 58