1 | /* Copyright 2019 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/data/unbounded_thread_pool.h" |
17 | |
18 | #include "absl/memory/memory.h" |
19 | #include "tensorflow/core/framework/dataset.h" |
20 | #include "tensorflow/core/lib/core/notification.h" |
21 | #include "tensorflow/core/platform/env.h" |
22 | #include "tensorflow/core/platform/resource.h" |
23 | #include "tensorflow/core/platform/unbounded_work_queue.h" |
24 | |
25 | namespace tensorflow { |
26 | namespace data { |
27 | |
28 | // A logical implementation of the `tensorflow::Thread` interface that uses |
29 | // physical threads in an `UnboundedThreadPool` to perform the work. |
30 | // |
31 | // NOTE: This object represents a logical thread of control that may be mapped |
32 | // onto the same physical thread as other work items that are submitted to the |
33 | // same `UnboundedThreadPool`. |
34 | class UnboundedThreadPool::LogicalThreadWrapper : public Thread { |
35 | public: |
36 | explicit LogicalThreadWrapper(std::shared_ptr<Notification> done) |
37 | : done_(std::move(done)) {} |
38 | |
39 | ~LogicalThreadWrapper() override { |
40 | // NOTE: The `Thread` destructor is expected to "join" the created thread, |
41 | // but the physical thread may continue to execute after the work for this |
42 | // thread is complete. We simulate this by waiting on a notification that |
43 | // the thread's work function will notify when it is complete. |
44 | done_->WaitForNotification(); |
45 | } |
46 | |
47 | private: |
48 | std::shared_ptr<Notification> done_; |
49 | }; |
50 | |
51 | // A lightweight wrapper for creating logical threads in a `UnboundedThreadPool` |
52 | // that can be shared (e.g.) in an `IteratorContext`. |
53 | class UnboundedThreadPool::LogicalThreadFactory : public ThreadFactory { |
54 | public: |
55 | explicit LogicalThreadFactory(UnboundedThreadPool* pool) : pool_(pool) {} |
56 | |
57 | std::unique_ptr<Thread> StartThread(const string& name, |
58 | std::function<void()> fn) override { |
59 | auto done = std::make_shared<Notification>(); |
60 | pool_->ScheduleOnWorkQueue(std::move(fn), done); |
61 | return std::make_unique<LogicalThreadWrapper>(std::move(done)); |
62 | } |
63 | |
64 | private: |
65 | UnboundedThreadPool* const pool_; // Not owned. |
66 | }; |
67 | |
68 | std::shared_ptr<ThreadFactory> UnboundedThreadPool::get_thread_factory() { |
69 | return std::make_shared<LogicalThreadFactory>(this); |
70 | } |
71 | |
72 | void UnboundedThreadPool::Schedule(std::function<void()> fn) { |
73 | auto tagged_fn = [fn = std::move(fn)]() { |
74 | tensorflow::ResourceTagger tag(kTFDataResourceTag, "ThreadPool" ); |
75 | fn(); |
76 | }; |
77 | ScheduleOnWorkQueue(std::move(tagged_fn), /*done=*/nullptr); |
78 | } |
79 | |
80 | int UnboundedThreadPool::NumThreads() const { return -1; } |
81 | |
82 | int UnboundedThreadPool::CurrentThreadId() const { return -1; } |
83 | |
84 | namespace { |
85 | void WorkQueueFunc(const std::function<void()>& fn, |
86 | std::shared_ptr<Notification> done) { |
87 | fn(); |
88 | if (done) { |
89 | done->Notify(); |
90 | } |
91 | } |
92 | } // namespace |
93 | |
94 | void UnboundedThreadPool::ScheduleOnWorkQueue( |
95 | std::function<void()> fn, std::shared_ptr<Notification> done) { |
96 | unbounded_work_queue_.Schedule( |
97 | std::bind(&WorkQueueFunc, std::move(fn), std::move(done))); |
98 | } |
99 | |
100 | } // namespace data |
101 | } // namespace tensorflow |
102 | |