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