OLD | NEW |
(Empty) | |
| 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" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/sequenced_task_runner_handle.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "base/threading/thread_local.h" |
| 16 |
| 17 namespace base { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // MessageLoopForIO used to watch file descriptors for which callbacks are |
| 22 // registered from a given thread. |
| 23 LazyInstance<ThreadLocalPointer<MessageLoopForIO>>::Leaky |
| 24 tls_message_loop_for_io = LAZY_INSTANCE_INITIALIZER; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 FileDescriptorWatcher::Controller::~Controller() { |
| 29 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 30 message_loop_for_io_task_runner_->DeleteSoon(FROM_HERE, watcher_.release()); |
| 31 // Since WeakPtrs are invalidated by the destructor, RunCallback() won't be |
| 32 // invoked after this returns. |
| 33 } |
| 34 |
| 35 class FileDescriptorWatcher::Controller::Watcher |
| 36 : public MessageLoopForIO::Watcher, |
| 37 public MessageLoop::DestructionObserver { |
| 38 public: |
| 39 Watcher(WeakPtr<Controller> controller, MessageLoopForIO::Mode mode, int fd); |
| 40 ~Watcher() override; |
| 41 |
| 42 void StartWatching(); |
| 43 |
| 44 private: |
| 45 friend class FileDescriptorWatcher; |
| 46 |
| 47 // MessageLoopForIO::Watcher: |
| 48 void OnFileCanReadWithoutBlocking(int fd) override; |
| 49 void OnFileCanWriteWithoutBlocking(int fd) override; |
| 50 |
| 51 // MessageLoop::DestructionObserver: |
| 52 void WillDestroyCurrentMessageLoop() override; |
| 53 |
| 54 // Used to instruct the MessageLoopForIO to stop watching the file descriptor. |
| 55 MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_; |
| 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 |
| 62 // The Controller that created this Watcher. |
| 63 WeakPtr<Controller> controller_; |
| 64 |
| 65 // Whether this Watcher is notified when |fd_| becomes readable or writable |
| 66 // without blocking. |
| 67 const MessageLoopForIO::Mode mode_; |
| 68 |
| 69 // The watched file descriptor. |
| 70 const int fd_; |
| 71 |
| 72 // Except for the constructor, every method of this class must run on the same |
| 73 // MessageLoopForIO thread. |
| 74 ThreadChecker thread_checker_; |
| 75 |
| 76 // Whether this Watcher was registered as a DestructionObserver on the |
| 77 // MessageLoopForIO thread. |
| 78 bool registered_as_destruction_observer_ = false; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(Watcher); |
| 81 }; |
| 82 |
| 83 FileDescriptorWatcher::Controller::Watcher::Watcher( |
| 84 WeakPtr<Controller> controller, |
| 85 MessageLoopForIO::Mode mode, |
| 86 int fd) |
| 87 : controller_(controller), mode_(mode), fd_(fd) { |
| 88 DCHECK(callback_task_runner_); |
| 89 thread_checker_.DetachFromThread(); |
| 90 } |
| 91 |
| 92 FileDescriptorWatcher::Controller::Watcher::~Watcher() { |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 MessageLoopForIO::current()->RemoveDestructionObserver(this); |
| 95 } |
| 96 |
| 97 void FileDescriptorWatcher::Controller::Watcher::StartWatching() { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 |
| 100 MessageLoopForIO::current()->WatchFileDescriptor( |
| 101 fd_, false, mode_, &file_descriptor_watcher_, this); |
| 102 |
| 103 if (!registered_as_destruction_observer_) { |
| 104 MessageLoopForIO::current()->AddDestructionObserver(this); |
| 105 registered_as_destruction_observer_ = true; |
| 106 } |
| 107 } |
| 108 |
| 109 void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking( |
| 110 int fd) { |
| 111 DCHECK_EQ(fd_, fd); |
| 112 DCHECK_EQ(MessageLoopForIO::WATCH_READ, mode_); |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 |
| 115 // Run the callback on the sequence on which the watch was initiated. |
| 116 callback_task_runner_->PostTask(FROM_HERE, |
| 117 Bind(&Controller::RunCallback, controller_)); |
| 118 } |
| 119 |
| 120 void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking( |
| 121 int fd) { |
| 122 DCHECK_EQ(fd_, fd); |
| 123 DCHECK_EQ(MessageLoopForIO::WATCH_WRITE, mode_); |
| 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 |
| 126 // Run the callback on the sequence on which the watch was initiated. |
| 127 callback_task_runner_->PostTask(FROM_HERE, |
| 128 Bind(&Controller::RunCallback, controller_)); |
| 129 } |
| 130 |
| 131 void FileDescriptorWatcher::Controller::Watcher:: |
| 132 WillDestroyCurrentMessageLoop() { |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 134 |
| 135 // A Watcher is owned by a Controller. When the Controller is deleted, it |
| 136 // transfers ownership of the Watcher to a delete task posted to the |
| 137 // MessageLoopForIO. If the MessageLoopForIO is deleted before the delete task |
| 138 // runs, the following line takes care of deleting the Watcher. |
| 139 delete this; |
| 140 } |
| 141 |
| 142 FileDescriptorWatcher::Controller::Controller(MessageLoopForIO::Mode mode, |
| 143 int fd, |
| 144 const Closure& callback) |
| 145 : callback_(callback), |
| 146 message_loop_for_io_task_runner_( |
| 147 tls_message_loop_for_io.Get().Get()->task_runner()), |
| 148 weak_factory_(this) { |
| 149 DCHECK(!callback_.is_null()); |
| 150 DCHECK(message_loop_for_io_task_runner_); |
| 151 watcher_ = MakeUnique<Watcher>(weak_factory_.GetWeakPtr(), mode, fd); |
| 152 StartWatching(); |
| 153 } |
| 154 |
| 155 void FileDescriptorWatcher::Controller::StartWatching() { |
| 156 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 157 // It is safe to use Unretained() below because |watcher_| can only be deleted |
| 158 // by a delete task posted to |message_loop_for_io_task_runner_| by this |
| 159 // Controller's destructor. Since this delete task hasn't been posted yet, it |
| 160 // can't run before the task posted below. |
| 161 message_loop_for_io_task_runner_->PostTask( |
| 162 FROM_HERE, Bind(&Watcher::StartWatching, Unretained(watcher_.get()))); |
| 163 } |
| 164 |
| 165 void FileDescriptorWatcher::Controller::RunCallback() { |
| 166 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 167 |
| 168 WeakPtr<Controller> weak_this = weak_factory_.GetWeakPtr(); |
| 169 |
| 170 callback_.Run(); |
| 171 |
| 172 // If |this| wasn't deleted, re-enable the watch. |
| 173 if (weak_this) |
| 174 StartWatching(); |
| 175 } |
| 176 |
| 177 FileDescriptorWatcher::FileDescriptorWatcher( |
| 178 MessageLoopForIO* message_loop_for_io) { |
| 179 DCHECK(message_loop_for_io); |
| 180 DCHECK(!tls_message_loop_for_io.Get().Get()); |
| 181 tls_message_loop_for_io.Get().Set(message_loop_for_io); |
| 182 } |
| 183 |
| 184 FileDescriptorWatcher::~FileDescriptorWatcher() { |
| 185 tls_message_loop_for_io.Get().Set(nullptr); |
| 186 } |
| 187 |
| 188 std::unique_ptr<FileDescriptorWatcher::Controller> |
| 189 FileDescriptorWatcher::WatchReadable(int fd, const Closure& callback) { |
| 190 return WrapUnique(new Controller(MessageLoopForIO::WATCH_READ, fd, callback)); |
| 191 } |
| 192 |
| 193 std::unique_ptr<FileDescriptorWatcher::Controller> |
| 194 FileDescriptorWatcher::WatchWritable(int fd, const Closure& callback) { |
| 195 return WrapUnique( |
| 196 new Controller(MessageLoopForIO::WATCH_WRITE, fd, callback)); |
| 197 } |
| 198 |
| 199 } // namespace base |
OLD | NEW |