Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: base/files/file_path_watcher_kqueue.cc

Issue 2598193002: Allow FilePathWatcher to be used from sequenced tasks. (Closed)
Patch Set: thread -> sequence in comment Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_path_watcher_kqueue.h ('k') | base/files/file_path_watcher_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_kqueue.h" 5 #include "base/files/file_path_watcher_kqueue.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/param.h> 9 #include <sys/param.h>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/sequenced_task_runner_handle.h"
16 16
17 // On some platforms these are not defined. 17 // On some platforms these are not defined.
18 #if !defined(EV_RECEIPT) 18 #if !defined(EV_RECEIPT)
19 #define EV_RECEIPT 0 19 #define EV_RECEIPT 0
20 #endif 20 #endif
21 #if !defined(O_EVTONLY) 21 #if !defined(O_EVTONLY)
22 #define O_EVTONLY O_RDONLY 22 #define O_EVTONLY O_RDONLY
23 #endif 23 #endif
24 24
25 namespace base { 25 namespace base {
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 const FilePathWatcher::Callback& callback) { 234 const FilePathWatcher::Callback& callback) {
235 DCHECK(target_.value().empty()); // Can only watch one path. 235 DCHECK(target_.value().empty()); // Can only watch one path.
236 DCHECK(!callback.is_null()); 236 DCHECK(!callback.is_null());
237 DCHECK_EQ(kqueue_, -1); 237 DCHECK_EQ(kqueue_, -1);
238 // Recursive watch is not supported using kqueue. 238 // Recursive watch is not supported using kqueue.
239 DCHECK(!recursive); 239 DCHECK(!recursive);
240 240
241 callback_ = callback; 241 callback_ = callback;
242 target_ = path; 242 target_ = path;
243 243
244 set_task_runner(ThreadTaskRunnerHandle::Get()); 244 set_task_runner(SequencedTaskRunnerHandle::Get());
245 245
246 kqueue_ = kqueue(); 246 kqueue_ = kqueue();
247 if (kqueue_ == -1) { 247 if (kqueue_ == -1) {
248 DPLOG(ERROR) << "kqueue"; 248 DPLOG(ERROR) << "kqueue";
249 return false; 249 return false;
250 } 250 }
251 251
252 int last_entry = EventsForPath(target_, &events_); 252 int last_entry = EventsForPath(target_, &events_);
253 DCHECK_NE(last_entry, 0); 253 DCHECK_NE(last_entry, 0);
254 254
(...skipping 17 matching lines...) Expand all
272 kqueue_, Bind(&FilePathWatcherKQueue::OnKQueueReadable, this)); 272 kqueue_, Bind(&FilePathWatcherKQueue::OnKQueueReadable, this));
273 return true; 273 return true;
274 } 274 }
275 275
276 void FilePathWatcherKQueue::Cancel() { 276 void FilePathWatcherKQueue::Cancel() {
277 if (!task_runner()) { 277 if (!task_runner()) {
278 set_cancelled(); 278 set_cancelled();
279 return; 279 return;
280 } 280 }
281 281
282 DCHECK(task_runner()->BelongsToCurrentThread()); 282 DCHECK(task_runner()->RunsTasksOnCurrentThread());
283 if (!is_cancelled()) { 283 if (!is_cancelled()) {
284 set_cancelled(); 284 set_cancelled();
285 kqueue_watch_controller_.reset(); 285 kqueue_watch_controller_.reset();
286 if (IGNORE_EINTR(close(kqueue_)) != 0) { 286 if (IGNORE_EINTR(close(kqueue_)) != 0) {
287 DPLOG(ERROR) << "close kqueue"; 287 DPLOG(ERROR) << "close kqueue";
288 } 288 }
289 kqueue_ = -1; 289 kqueue_ = -1;
290 std::for_each(events_.begin(), events_.end(), ReleaseEvent); 290 std::for_each(events_.begin(), events_.end(), ReleaseEvent);
291 events_.clear(); 291 events_.clear();
292 callback_.Reset(); 292 callback_.Reset();
293 } 293 }
294 } 294 }
295 295
296 void FilePathWatcherKQueue::OnKQueueReadable() { 296 void FilePathWatcherKQueue::OnKQueueReadable() {
297 DCHECK(task_runner()->BelongsToCurrentThread()); 297 DCHECK(task_runner()->RunsTasksOnCurrentThread());
298 DCHECK(events_.size()); 298 DCHECK(events_.size());
299 299
300 // Request the file system update notifications that have occurred and return 300 // Request the file system update notifications that have occurred and return
301 // them in |updates|. |count| will contain the number of updates that have 301 // them in |updates|. |count| will contain the number of updates that have
302 // occurred. 302 // occurred.
303 EventVector updates(events_.size()); 303 EventVector updates(events_.size());
304 struct timespec timeout = {0, 0}; 304 struct timespec timeout = {0, 0};
305 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), 305 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(),
306 &timeout)); 306 &timeout));
307 307
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 Cancel(); 359 Cancel();
360 } 360 }
361 } 361 }
362 362
363 if (send_notification) { 363 if (send_notification) {
364 callback_.Run(target_, false); 364 callback_.Run(target_, false);
365 } 365 }
366 } 366 }
367 367
368 } // namespace base 368 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_path_watcher_kqueue.h ('k') | base/files/file_path_watcher_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698