OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_descriptor_watcher_posix.h" | 5 #include "base/files/file_descriptor_watcher_posix.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/sequenced_task_runner_handle.h" | 13 #include "base/threading/sequenced_task_runner_handle.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/threading/thread_local.h" | 15 #include "base/threading/thread_local.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // MessageLoopForIO used to watch file descriptors for which callbacks are | 21 // MessageLoopForIO used to watch file descriptors for which callbacks are |
22 // registered from a given thread. | 22 // registered from a given thread. |
23 LazyInstance<ThreadLocalPointer<MessageLoopForIO>>::Leaky | 23 LazyInstance<ThreadLocalPointer<MessageLoopForIO>>::Leaky |
24 tls_message_loop_for_io = LAZY_INSTANCE_INITIALIZER; | 24 tls_message_loop_for_io = LAZY_INSTANCE_INITIALIZER; |
25 | 25 |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 FileDescriptorWatcher::Controller::~Controller() { | 28 FileDescriptorWatcher::Controller::~Controller() { |
29 DCHECK(sequence_checker_.CalledOnValidSequence()); | 29 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 30 |
| 31 // Delete |watcher_| on the MessageLoopForIO. |
| 32 // |
| 33 // If the MessageLoopForIO is deleted before Watcher::StartWatching() runs, |
| 34 // |watcher_| is leaked. If the MessageLoopForIO is deleted after |
| 35 // Watcher::StartWatching() runs but before the DeleteSoon task runs, |
| 36 // |watcher_| is deleted from Watcher::WillDestroyCurrentMessageLoop(). |
30 message_loop_for_io_task_runner_->DeleteSoon(FROM_HERE, watcher_.release()); | 37 message_loop_for_io_task_runner_->DeleteSoon(FROM_HERE, watcher_.release()); |
| 38 |
31 // Since WeakPtrs are invalidated by the destructor, RunCallback() won't be | 39 // Since WeakPtrs are invalidated by the destructor, RunCallback() won't be |
32 // invoked after this returns. | 40 // invoked after this returns. |
33 } | 41 } |
34 | 42 |
35 class FileDescriptorWatcher::Controller::Watcher | 43 class FileDescriptorWatcher::Controller::Watcher |
36 : public MessageLoopForIO::Watcher, | 44 : public MessageLoopForIO::Watcher, |
37 public MessageLoop::DestructionObserver { | 45 public MessageLoop::DestructionObserver { |
38 public: | 46 public: |
39 Watcher(WeakPtr<Controller> controller, MessageLoopForIO::Mode mode, int fd); | 47 Watcher(WeakPtr<Controller> controller, MessageLoopForIO::Mode mode, int fd); |
40 ~Watcher() override; | 48 ~Watcher() override; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 return WrapUnique(new Controller(MessageLoopForIO::WATCH_READ, fd, callback)); | 198 return WrapUnique(new Controller(MessageLoopForIO::WATCH_READ, fd, callback)); |
191 } | 199 } |
192 | 200 |
193 std::unique_ptr<FileDescriptorWatcher::Controller> | 201 std::unique_ptr<FileDescriptorWatcher::Controller> |
194 FileDescriptorWatcher::WatchWritable(int fd, const Closure& callback) { | 202 FileDescriptorWatcher::WatchWritable(int fd, const Closure& callback) { |
195 return WrapUnique( | 203 return WrapUnique( |
196 new Controller(MessageLoopForIO::WATCH_WRITE, fd, callback)); | 204 new Controller(MessageLoopForIO::WATCH_WRITE, fd, callback)); |
197 } | 205 } |
198 | 206 |
199 } // namespace base | 207 } // namespace base |
OLD | NEW |