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" |
| 8 | #include "base/lazy_instance.h" |
| 9 | #include "base/logging.h" |
| 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" |
| 15 | #include "base/threading/sequenced_task_runner_handle.h" |
| 16 | #include "base/threading/thread_checker.h" |
| 17 | #include "base/threading/thread_local.h" |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // MessageLoopForIO used to watch file descriptors for which callbacks are |
| 24 | // registered from a given thread. |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 25 | LazyInstance<ThreadLocalPointer<FileDescriptorWatcher>>::Leaky tls_fd_watcher = |
| 26 | LAZY_INSTANCE_INITIALIZER; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | FileDescriptorWatcher::Controller::~Controller() { |
| 31 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
fdoray | 20e8000 | 2016-10-01 15:37:29 | [diff] [blame] | 32 | |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 33 | // Delete |watcher_| on the IO thread task runner. |
fdoray | 20e8000 | 2016-10-01 15:37:29 | [diff] [blame] | 34 | // |
| 35 | // If the MessageLoopForIO is deleted before Watcher::StartWatching() runs, |
| 36 | // |watcher_| is leaked. If the MessageLoopForIO is deleted after |
| 37 | // Watcher::StartWatching() runs but before the DeleteSoon task runs, |
| 38 | // |watcher_| is deleted from Watcher::WillDestroyCurrentMessageLoop(). |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 39 | io_thread_task_runner_->DeleteSoon(FROM_HERE, watcher_.release()); |
fdoray | 20e8000 | 2016-10-01 15:37:29 | [diff] [blame] | 40 | |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 41 | // Since WeakPtrs are invalidated by the destructor, RunCallback() won't be |
| 42 | // invoked after this returns. |
| 43 | } |
| 44 | |
| 45 | class FileDescriptorWatcher::Controller::Watcher |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 46 | : public MessagePumpForIO::FdWatcher, |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 47 | public MessageLoopCurrent::DestructionObserver { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 48 | public: |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 49 | Watcher(WeakPtr<Controller> controller, MessagePumpForIO::Mode mode, int fd); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 50 | ~Watcher() override; |
| 51 | |
| 52 | void StartWatching(); |
| 53 | |
| 54 | private: |
| 55 | friend class FileDescriptorWatcher; |
| 56 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 57 | // MessagePumpForIO::FdWatcher: |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 58 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 59 | void OnFileCanWriteWithoutBlocking(int fd) override; |
| 60 | |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 61 | // MessageLoopCurrent::DestructionObserver: |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 62 | void WillDestroyCurrentMessageLoop() override; |
| 63 | |
Gabriel Charette | 889b87c | 2018-05-11 22:23:36 | [diff] [blame] | 64 | // The MessageLoopForIO's watch handle (stops the watch when destroyed). |
| 65 | MessagePumpForIO::FdWatchController fd_watch_controller_; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 66 | |
| 67 | // Runs tasks on the sequence on which this was instantiated (i.e. the |
| 68 | // sequence on which the callback must run). |
| 69 | const scoped_refptr<SequencedTaskRunner> callback_task_runner_ = |
| 70 | SequencedTaskRunnerHandle::Get(); |
| 71 | |
| 72 | // The Controller that created this Watcher. |
| 73 | WeakPtr<Controller> controller_; |
| 74 | |
| 75 | // Whether this Watcher is notified when |fd_| becomes readable or writable |
| 76 | // without blocking. |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 77 | const MessagePumpForIO::Mode mode_; |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 78 | |
| 79 | // The watched file descriptor. |
| 80 | const int fd_; |
| 81 | |
| 82 | // Except for the constructor, every method of this class must run on the same |
| 83 | // MessageLoopForIO thread. |
| 84 | ThreadChecker thread_checker_; |
| 85 | |
| 86 | // Whether this Watcher was registered as a DestructionObserver on the |
| 87 | // MessageLoopForIO thread. |
| 88 | bool registered_as_destruction_observer_ = false; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(Watcher); |
| 91 | }; |
| 92 | |
| 93 | FileDescriptorWatcher::Controller::Watcher::Watcher( |
| 94 | WeakPtr<Controller> controller, |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 95 | MessagePumpForIO::Mode mode, |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 96 | int fd) |
Gabriel Charette | 889b87c | 2018-05-11 22:23:36 | [diff] [blame] | 97 | : fd_watch_controller_(FROM_HERE), |
ssid | 5dd4fb3 | 2017-02-16 23:57:17 | [diff] [blame] | 98 | controller_(controller), |
| 99 | mode_(mode), |
| 100 | fd_(fd) { |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 101 | DCHECK(callback_task_runner_); |
| 102 | thread_checker_.DetachFromThread(); |
| 103 | } |
| 104 | |
| 105 | FileDescriptorWatcher::Controller::Watcher::~Watcher() { |
| 106 | DCHECK(thread_checker_.CalledOnValidThread()); |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 107 | MessageLoopCurrentForIO::Get()->RemoveDestructionObserver(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void FileDescriptorWatcher::Controller::Watcher::StartWatching() { |
| 111 | DCHECK(thread_checker_.CalledOnValidThread()); |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 112 | DCHECK(MessageLoopCurrentForIO::IsSet()); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 113 | |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 114 | if (!MessageLoopCurrentForIO::Get()->WatchFileDescriptor( |
Gabriel Charette | 889b87c | 2018-05-11 22:23:36 | [diff] [blame] | 115 | fd_, false, mode_, &fd_watch_controller_, this)) { |
Wez | 1be89cc | 2017-07-12 06:44:49 | [diff] [blame] | 116 | // TODO(wez): Ideally we would [D]CHECK here, or propagate the failure back |
| 117 | // to the caller, but there is no guarantee that they haven't already |
| 118 | // closed |fd_| on another thread, so the best we can do is Debug-log. |
| 119 | DLOG(ERROR) << "Failed to watch fd=" << fd_; |
| 120 | } |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 121 | |
| 122 | if (!registered_as_destruction_observer_) { |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 123 | MessageLoopCurrentForIO::Get()->AddDestructionObserver(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 124 | registered_as_destruction_observer_ = true; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking( |
| 129 | int fd) { |
| 130 | DCHECK_EQ(fd_, fd); |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 131 | DCHECK_EQ(MessagePumpForIO::WATCH_READ, mode_); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 132 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 133 | |
| 134 | // Run the callback on the sequence on which the watch was initiated. |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 135 | callback_task_runner_->PostTask( |
| 136 | FROM_HERE, BindOnce(&Controller::RunCallback, controller_)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking( |
| 140 | int fd) { |
| 141 | DCHECK_EQ(fd_, fd); |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 142 | DCHECK_EQ(MessagePumpForIO::WATCH_WRITE, mode_); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 143 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 | |
| 145 | // Run the callback on the sequence on which the watch was initiated. |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 146 | callback_task_runner_->PostTask( |
| 147 | FROM_HERE, BindOnce(&Controller::RunCallback, controller_)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void FileDescriptorWatcher::Controller::Watcher:: |
| 151 | WillDestroyCurrentMessageLoop() { |
| 152 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 | |
| 154 | // A Watcher is owned by a Controller. When the Controller is deleted, it |
| 155 | // transfers ownership of the Watcher to a delete task posted to the |
| 156 | // MessageLoopForIO. If the MessageLoopForIO is deleted before the delete task |
| 157 | // runs, the following line takes care of deleting the Watcher. |
| 158 | delete this; |
| 159 | } |
| 160 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 161 | FileDescriptorWatcher::Controller::Controller(MessagePumpForIO::Mode mode, |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 162 | int fd, |
| 163 | const Closure& callback) |
| 164 | : callback_(callback), |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 165 | io_thread_task_runner_( |
| 166 | tls_fd_watcher.Get().Get()->io_thread_task_runner()), |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 167 | weak_factory_(this) { |
| 168 | DCHECK(!callback_.is_null()); |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 169 | DCHECK(io_thread_task_runner_); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 170 | watcher_ = std::make_unique<Watcher>(weak_factory_.GetWeakPtr(), mode, fd); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 171 | StartWatching(); |
| 172 | } |
| 173 | |
| 174 | void FileDescriptorWatcher::Controller::StartWatching() { |
| 175 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 176 | // It is safe to use Unretained() below because |watcher_| can only be deleted |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 177 | // by a delete task posted to |io_thread_task_runner_| by this |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 178 | // Controller's destructor. Since this delete task hasn't been posted yet, it |
| 179 | // can't run before the task posted below. |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 180 | io_thread_task_runner_->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 181 | FROM_HERE, BindOnce(&Watcher::StartWatching, Unretained(watcher_.get()))); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void FileDescriptorWatcher::Controller::RunCallback() { |
| 185 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 186 | |
| 187 | WeakPtr<Controller> weak_this = weak_factory_.GetWeakPtr(); |
| 188 | |
| 189 | callback_.Run(); |
| 190 | |
| 191 | // If |this| wasn't deleted, re-enable the watch. |
| 192 | if (weak_this) |
| 193 | StartWatching(); |
| 194 | } |
| 195 | |
| 196 | FileDescriptorWatcher::FileDescriptorWatcher( |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 197 | scoped_refptr<SingleThreadTaskRunner> io_thread_task_runner) |
| 198 | : io_thread_task_runner_(std::move(io_thread_task_runner)) { |
| 199 | DCHECK(!tls_fd_watcher.Get().Get()); |
| 200 | tls_fd_watcher.Get().Set(this); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | FileDescriptorWatcher::~FileDescriptorWatcher() { |
Alexander Timin | 6247f6a91a | 2018-10-27 15:40:10 | [diff] [blame^] | 204 | tls_fd_watcher.Get().Set(nullptr); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | std::unique_ptr<FileDescriptorWatcher::Controller> |
| 208 | FileDescriptorWatcher::WatchReadable(int fd, const Closure& callback) { |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 209 | return WrapUnique(new Controller(MessagePumpForIO::WATCH_READ, fd, callback)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | std::unique_ptr<FileDescriptorWatcher::Controller> |
| 213 | FileDescriptorWatcher::WatchWritable(int fd, const Closure& callback) { |
| 214 | return WrapUnique( |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 215 | new Controller(MessagePumpForIO::WATCH_WRITE, fd, callback)); |
fdoray | f9f6c1e9 | 2016-09-15 13:23:24 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace base |