xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/batching_util/bounded_executor.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #include "tensorflow/core/kernels/batching_util/bounded_executor.h"
17 
18 #include <algorithm>
19 #include <atomic>
20 
21 #include "absl/functional/bind_front.h"
22 #include "tensorflow/core/platform/errors.h"
23 #include "tensorflow/core/platform/statusor.h"
24 #include "tensorflow/core/platform/threadpool.h"
25 
26 namespace tensorflow {
27 namespace serving {
Create(const Options & options)28 StatusOr<std::unique_ptr<BoundedExecutor>> BoundedExecutor::Create(
29     const Options& options) {
30   if (options.env == nullptr) {
31     return errors::InvalidArgument("options.env must not be nullptr");
32   }
33   if (options.num_threads <= 0) {
34     return errors::InvalidArgument("options.num_threads must be positive");
35   }
36   return absl::WrapUnique(new BoundedExecutor(options));
37 }
38 
BoundedExecutor(const Options & options)39 BoundedExecutor::BoundedExecutor(const Options& options) : options_(options) {
40   InitWorker();
41 }
42 
InitWorker()43 void BoundedExecutor::InitWorker() {
44   for (int i = 0; i < options_.num_threads; i++) {
45     std::unique_ptr<Thread> thread = absl::WrapUnique(
46         options_.env->StartThread(options_.thread_options, options_.thread_name,
47                                   [this]() { this->Run(); }));
48     threads_.push_back(std::move(thread));
49   }
50 }
51 
~BoundedExecutor()52 BoundedExecutor::~BoundedExecutor() {
53   {
54     mutex_lock l(work_queue_mu_);
55     // Enqueue an empty task (nullptr) to signal exit.
56     // This way, each thread blocks on waiting a task, and exit run-loop
57     // if task is nullptr.
58     for (int i = 0; i < NumThreads(); i++) {
59       work_queue_.push_back(nullptr);
60       work_queue_cv_.notify_one();
61     }
62   }
63   // Each thread will be joined in its destructor.
64   threads_.clear();
65 }
66 
Schedule(std::function<void ()> func)67 void BoundedExecutor::Schedule(std::function<void()> func) {
68   // use DCHECK so as not to introduce CHECK in prod code.
69   DCHECK(func != nullptr) << "func is nullptr";
70   mutex_lock l(work_queue_mu_);
71 
72   work_queue_.push_back(std::move(func));
73 
74   work_queue_cv_.notify_one();
75 }
76 
NumThreads() const77 int BoundedExecutor::NumThreads() const { return options_.num_threads; }
78 
CurrentThreadId() const79 int BoundedExecutor::CurrentThreadId() const { return -1; }
80 
Run()81 void BoundedExecutor::Run() {
82   while (true) {
83     std::function<void()> func = nullptr;
84     {
85       mutex_lock l(work_queue_mu_);
86 
87       while (work_queue_.empty()) {
88         work_queue_cv_.wait(l);
89       }
90 
91       func = std::move(work_queue_.front());
92       work_queue_.pop_front();
93     }
94 
95     // Exit run-loop when func is nullptr.
96     if (func != nullptr) {
97       func();
98     } else {
99       break;
100     }
101   }
102 }
103 }  // namespace serving
104 }  // namespace tensorflow
105