fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/files/file_descriptor_watcher_posix.h" |
| 6 | |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 9 | #include "base/bind.h" |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 10 | #include "base/callback_helpers.h" |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 12 | #include "base/message_loop/message_pump_for_io.h" |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 13 | #include "base/no_destructor.h" |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 14 | #include "base/sequenced_task_runner.h" |
| 15 | #include "base/single_thread_task_runner.h" |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 16 | #include "base/synchronization/waitable_event.h" |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 17 | #include "base/task/current_thread.h" |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 18 | #include "base/threading/sequenced_task_runner_handle.h" |
| 19 | #include "base/threading/thread_checker.h" |
| 20 | #include "base/threading/thread_local.h" |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 21 | #include "base/threading/thread_restrictions.h" |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | |
| 25 | namespace { |
| 26 | |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 27 | // Per-thread FileDescriptorWatcher registration. |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 28 | ThreadLocalPointer<FileDescriptorWatcher>& GetTlsFdWatcher() { |
| 29 | static NoDestructor<ThreadLocalPointer<FileDescriptorWatcher>> tls_fd_watcher; |
| 30 | return *tls_fd_watcher; |
| 31 | } |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 32 | |
| 33 | } // namespace |
| 34 | |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 35 | class FileDescriptorWatcher::Controller::Watcher |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 36 | : public MessagePumpForIO::FdWatcher, |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 37 | public CurrentThread::DestructionObserver { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 38 | public: |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 39 | Watcher(WeakPtr<Controller> controller, MessagePumpForIO::Mode mode, int fd); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 40 | ~Watcher() override; |
| 41 | |
| 42 | void StartWatching(); |
| 43 | |
| 44 | private: |
| 45 | friend class FileDescriptorWatcher; |
| 46 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 47 | // MessagePumpForIO::FdWatcher: |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 48 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 49 | void OnFileCanWriteWithoutBlocking(int fd) override; |
| 50 | |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 51 | // CurrentThread::DestructionObserver: |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 52 | void WillDestroyCurrentMessageLoop() override; |
| 53 | |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 54 | // The MessagePumpForIO's watch handle (stops the watch when destroyed). |
Gabriel Charette | 889b87c | 2018-05-11 22:23:36 | [diff] [blame] | 55 | MessagePumpForIO::FdWatchController fd_watch_controller_; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 56 | |
| 57 | // Runs tasks on the sequence on which this was instantiated (i.e. the |
| 58 | // sequence on which the callback must run). |
| 59 | const scoped_refptr<SequencedTaskRunner> callback_task_runner_ = |
| 60 | SequencedTaskRunnerHandle::Get(); |
| 61 | |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 62 | // The Controller that created this Watcher. This WeakPtr is bound to the |
| 63 | // |controller_| thread and can only be used by this Watcher to post back to |
| 64 | // |callback_task_runner_|. |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 65 | WeakPtr<Controller> controller_; |
| 66 | |
| 67 | // Whether this Watcher is notified when |fd_| becomes readable or writable |
| 68 | // without blocking. |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 69 | const MessagePumpForIO::Mode mode_; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 70 | |
| 71 | // The watched file descriptor. |
| 72 | const int fd_; |
| 73 | |
| 74 | // Except for the constructor, every method of this class must run on the same |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 75 | // MessagePumpForIO thread. |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 76 | ThreadChecker thread_checker_; |
| 77 | |
| 78 | // Whether this Watcher was registered as a DestructionObserver on the |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 79 | // MessagePumpForIO thread. |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 80 | bool registered_as_destruction_observer_ = false; |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(Watcher); |
| 83 | }; |
| 84 | |
| 85 | FileDescriptorWatcher::Controller::Watcher::Watcher( |
| 86 | WeakPtr<Controller> controller, |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 87 | MessagePumpForIO::Mode mode, |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 88 | int fd) |
Gabriel Charette | 889b87c | 2018-05-11 22:23:36 | [diff] [blame] | 89 | : fd_watch_controller_(FROM_HERE), |
ssid | 5dd4fb3 | 2017-02-16 23:57:17 | [diff] [blame] | 90 | controller_(controller), |
| 91 | mode_(mode), |
| 92 | fd_(fd) { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 93 | DCHECK(callback_task_runner_); |
| 94 | thread_checker_.DetachFromThread(); |
| 95 | } |
| 96 | |
| 97 | FileDescriptorWatcher::Controller::Watcher::~Watcher() { |
| 98 | DCHECK(thread_checker_.CalledOnValidThread()); |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 99 | CurrentIOThread::Get()->RemoveDestructionObserver(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void FileDescriptorWatcher::Controller::Watcher::StartWatching() { |
| 103 | DCHECK(thread_checker_.CalledOnValidThread()); |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 104 | DCHECK(CurrentIOThread::IsSet()); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 105 | |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 106 | const bool watch_success = CurrentIOThread::Get()->WatchFileDescriptor( |
| 107 | fd_, false, mode_, &fd_watch_controller_, this); |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 108 | DCHECK(watch_success) << "Failed to watch fd=" << fd_; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 109 | |
| 110 | if (!registered_as_destruction_observer_) { |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 111 | CurrentIOThread::Get()->AddDestructionObserver(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 112 | registered_as_destruction_observer_ = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking( |
| 117 | int fd) { |
| 118 | DCHECK_EQ(fd_, fd); |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 119 | DCHECK_EQ(MessagePumpForIO::WATCH_READ, mode_); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 120 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 | |
| 122 | // Run the callback on the sequence on which the watch was initiated. |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 123 | callback_task_runner_->PostTask( |
| 124 | FROM_HERE, BindOnce(&Controller::RunCallback, controller_)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking( |
| 128 | int fd) { |
| 129 | DCHECK_EQ(fd_, fd); |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 130 | DCHECK_EQ(MessagePumpForIO::WATCH_WRITE, mode_); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 131 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | |
| 133 | // Run the callback on the sequence on which the watch was initiated. |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 134 | callback_task_runner_->PostTask( |
| 135 | FROM_HERE, BindOnce(&Controller::RunCallback, controller_)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void FileDescriptorWatcher::Controller::Watcher:: |
| 139 | WillDestroyCurrentMessageLoop() { |
| 140 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 141 | |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 142 | if (callback_task_runner_->RunsTasksInCurrentSequence()) { |
| 143 | // |controller_| can be accessed directly when Watcher runs on the same |
| 144 | // thread. |
| 145 | controller_->watcher_.reset(); |
| 146 | } else { |
| 147 | // If the Watcher and the Controller live on different threads, delete |
| 148 | // |this| synchronously. Pending tasks bound to an unretained Watcher* will |
| 149 | // not run since this loop is dead. The associated Controller still |
| 150 | // technically owns this via unique_ptr but it never uses it directly and |
| 151 | // will ultimately send it to this thread for deletion (and that also won't |
| 152 | // run since the loop being dead). |
| 153 | delete this; |
| 154 | } |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 155 | } |
| 156 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 157 | FileDescriptorWatcher::Controller::Controller(MessagePumpForIO::Mode mode, |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 158 | int fd, |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 159 | const RepeatingClosure& callback) |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 160 | : callback_(callback), |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 161 | io_thread_task_runner_(GetTlsFdWatcher().Get()->io_thread_task_runner()) { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 162 | DCHECK(!callback_.is_null()); |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame] | 163 | DCHECK(io_thread_task_runner_); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 164 | watcher_ = std::make_unique<Watcher>(weak_factory_.GetWeakPtr(), mode, fd); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 165 | StartWatching(); |
| 166 | } |
| 167 | |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 168 | FileDescriptorWatcher::Controller::~Controller() { |
| 169 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 170 | |
| 171 | if (io_thread_task_runner_->BelongsToCurrentThread()) { |
| 172 | // If the MessagePumpForIO and the Controller live on the same thread. |
| 173 | watcher_.reset(); |
| 174 | } else { |
| 175 | // Synchronously wait until |watcher_| is deleted on the MessagePumpForIO |
| 176 | // thread. This ensures that the file descriptor is never accessed after |
| 177 | // this destructor returns. |
| 178 | // |
Gabriel Charette | 30cdf0e5 | 2019-07-16 19:28:21 | [diff] [blame] | 179 | // Use a ScopedClosureRunner to ensure that |done| is signaled even if the |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 180 | // thread doesn't run any more tasks (if PostTask returns true, it means |
| 181 | // that the task was queued, but it doesn't mean that a RunLoop will run the |
| 182 | // task before the queue is deleted). |
| 183 | // |
| 184 | // We considered associating "generations" to file descriptors to avoid the |
| 185 | // synchronous wait. For example, if the IO thread gets a "cancel" for fd=6, |
| 186 | // generation=1 after getting a "start watching" for fd=6, generation=2, it |
| 187 | // can ignore the "Cancel". However, "generations" didn't solve this race: |
| 188 | // |
| 189 | // T1 (client) Start watching fd = 6 with WatchReadable() |
| 190 | // Stop watching fd = 6 |
| 191 | // Close fd = 6 |
| 192 | // Open a new file, fd = 6 gets reused. |
| 193 | // T2 (io) Watcher::StartWatching() |
| 194 | // Incorrectly starts watching fd = 6 which now refers to a |
| 195 | // different file than when WatchReadable() was called. |
| 196 | WaitableEvent done; |
| 197 | io_thread_task_runner_->PostTask( |
| 198 | FROM_HERE, BindOnce( |
| 199 | [](Watcher* watcher, ScopedClosureRunner closure) { |
| 200 | // Since |watcher| is a raw pointer, it isn't deleted |
| 201 | // if this callback is deleted before it gets to run. |
| 202 | delete watcher; |
| 203 | // |closure| runs at the end of this scope. |
| 204 | }, |
| 205 | Unretained(watcher_.release()), |
| 206 | ScopedClosureRunner(BindOnce(&WaitableEvent::Signal, |
| 207 | Unretained(&done))))); |
| 208 | ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow; |
| 209 | done.Wait(); |
| 210 | } |
| 211 | |
| 212 | // Since WeakPtrs are invalidated by the destructor, any pending RunCallback() |
| 213 | // won't be invoked after this returns. |
| 214 | } |
| 215 | |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 216 | void FileDescriptorWatcher::Controller::StartWatching() { |
| 217 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
François Doray | 1e392d2 | 2019-01-29 16:00:19 | [diff] [blame] | 218 | if (io_thread_task_runner_->BelongsToCurrentThread()) { |
| 219 | // If the MessagePumpForIO and the Controller live on the same thread. |
| 220 | watcher_->StartWatching(); |
| 221 | } else { |
| 222 | // It is safe to use Unretained() below because |watcher_| can only be |
| 223 | // deleted by a delete task posted to |io_thread_task_runner_| by this |
| 224 | // Controller's destructor. Since this delete task hasn't been posted yet, |
| 225 | // it can't run before the task posted below. |
| 226 | io_thread_task_runner_->PostTask( |
| 227 | FROM_HERE, |
| 228 | BindOnce(&Watcher::StartWatching, Unretained(watcher_.get()))); |
| 229 | } |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void FileDescriptorWatcher::Controller::RunCallback() { |
| 233 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 234 | |
| 235 | WeakPtr<Controller> weak_this = weak_factory_.GetWeakPtr(); |
| 236 | |
| 237 | callback_.Run(); |
| 238 | |
| 239 | // If |this| wasn't deleted, re-enable the watch. |
| 240 | if (weak_this) |
| 241 | StartWatching(); |
| 242 | } |
| 243 | |
| 244 | FileDescriptorWatcher::FileDescriptorWatcher( |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame] | 245 | scoped_refptr<SingleThreadTaskRunner> io_thread_task_runner) |
| 246 | : io_thread_task_runner_(std::move(io_thread_task_runner)) { |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 247 | DCHECK(!GetTlsFdWatcher().Get()); |
| 248 | GetTlsFdWatcher().Set(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | FileDescriptorWatcher::~FileDescriptorWatcher() { |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 252 | GetTlsFdWatcher().Set(nullptr); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | std::unique_ptr<FileDescriptorWatcher::Controller> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 256 | FileDescriptorWatcher::WatchReadable(int fd, const RepeatingClosure& callback) { |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 257 | return WrapUnique(new Controller(MessagePumpForIO::WATCH_READ, fd, callback)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | std::unique_ptr<FileDescriptorWatcher::Controller> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 261 | FileDescriptorWatcher::WatchWritable(int fd, const RepeatingClosure& callback) { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 262 | return WrapUnique( |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 263 | new Controller(MessagePumpForIO::WATCH_WRITE, fd, callback)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 264 | } |
| 265 | |
Francois Doray | c186829 | 2018-12-06 02:29:43 | [diff] [blame] | 266 | #if DCHECK_IS_ON() |
| 267 | void FileDescriptorWatcher::AssertAllowed() { |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 268 | DCHECK(GetTlsFdWatcher().Get()); |
Francois Doray | c186829 | 2018-12-06 02:29:43 | [diff] [blame] | 269 | } |
| 270 | #endif |
| 271 | |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 272 | } // namespace base |