Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/inotify.h> | 10 #include <sys/inotify.h> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 | 59 |
| 60 // Callback for InotifyReaderTask. | 60 // Callback for InotifyReaderTask. |
| 61 void OnInotifyEvent(const inotify_event* event); | 61 void OnInotifyEvent(const inotify_event* event); |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 friend struct DefaultLazyInstanceTraits<InotifyReader>; | 64 friend struct DefaultLazyInstanceTraits<InotifyReader>; |
| 65 | 65 |
| 66 typedef std::set<FilePathWatcherImpl*> WatcherSet; | 66 typedef std::set<FilePathWatcherImpl*> WatcherSet; |
| 67 | 67 |
| 68 InotifyReader(); | 68 InotifyReader(); |
| 69 ~InotifyReader(); | 69 // There is no destructor because |g_inotify_reader| is a |
| 70 // base::LazyInstace::Leaky object. Having a destructor causes build | |
| 71 // issues with GCC 6 (http://crbug.com/636346). | |
| 70 | 72 |
| 71 // We keep track of which delegates want to be notified on which watches. | 73 // We keep track of which delegates want to be notified on which watches. |
| 72 hash_map<Watch, WatcherSet> watchers_; | 74 hash_map<Watch, WatcherSet> watchers_; |
| 73 | 75 |
| 74 // Lock to protect watchers_. | 76 // Lock to protect watchers_. |
| 75 Lock lock_; | 77 Lock lock_; |
| 76 | 78 |
| 77 // Separate thread on which we run blocking read for inotify events. | 79 // Separate thread on which we run blocking read for inotify events. |
| 78 Thread thread_; | 80 Thread thread_; |
| 79 | 81 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 shutdown_pipe_[0] = -1; | 268 shutdown_pipe_[0] = -1; |
| 267 shutdown_pipe_[1] = -1; | 269 shutdown_pipe_[1] = -1; |
| 268 if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) { | 270 if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) { |
| 269 thread_.task_runner()->PostTask( | 271 thread_.task_runner()->PostTask( |
| 270 FROM_HERE, | 272 FROM_HERE, |
| 271 Bind(&InotifyReaderCallback, this, inotify_fd_, shutdown_pipe_[0])); | 273 Bind(&InotifyReaderCallback, this, inotify_fd_, shutdown_pipe_[0])); |
| 272 valid_ = true; | 274 valid_ = true; |
| 273 } | 275 } |
| 274 } | 276 } |
| 275 | 277 |
| 276 InotifyReader::~InotifyReader() { | |
| 277 if (valid_) { | |
| 278 // Write to the self-pipe so that the select call in InotifyReaderTask | |
| 279 // returns. | |
| 280 ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1)); | |
|
Mattias Nissler (ping if slow)
2016/08/10 14:59:30
Removing this means that the shutdown_pipe will no
| |
| 281 DPCHECK(ret > 0); | |
| 282 DCHECK_EQ(ret, 1); | |
| 283 thread_.Stop(); | |
| 284 } | |
| 285 if (inotify_fd_ >= 0) | |
| 286 close(inotify_fd_); | |
| 287 if (shutdown_pipe_[0] >= 0) | |
| 288 close(shutdown_pipe_[0]); | |
| 289 if (shutdown_pipe_[1] >= 0) | |
| 290 close(shutdown_pipe_[1]); | |
| 291 } | |
| 292 | |
| 293 InotifyReader::Watch InotifyReader::AddWatch( | 278 InotifyReader::Watch InotifyReader::AddWatch( |
| 294 const FilePath& path, FilePathWatcherImpl* watcher) { | 279 const FilePath& path, FilePathWatcherImpl* watcher) { |
| 295 if (!valid_) | 280 if (!valid_) |
| 296 return kInvalidWatch; | 281 return kInvalidWatch; |
| 297 | 282 |
| 298 AutoLock auto_lock(lock_); | 283 AutoLock auto_lock(lock_); |
| 299 | 284 |
| 300 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), | 285 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), |
| 301 IN_ATTRIB | IN_CREATE | IN_DELETE | | 286 IN_ATTRIB | IN_CREATE | IN_DELETE | |
| 302 IN_CLOSE_WRITE | IN_MOVE | | 287 IN_CLOSE_WRITE | IN_MOVE | |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 680 return watches_.back().subdir.empty(); | 665 return watches_.back().subdir.empty(); |
| 681 } | 666 } |
| 682 | 667 |
| 683 } // namespace | 668 } // namespace |
| 684 | 669 |
| 685 FilePathWatcher::FilePathWatcher() { | 670 FilePathWatcher::FilePathWatcher() { |
| 686 impl_ = new FilePathWatcherImpl(); | 671 impl_ = new FilePathWatcherImpl(); |
| 687 } | 672 } |
| 688 | 673 |
| 689 } // namespace base | 674 } // namespace base |
| OLD | NEW |