Skip to content

Commit

Permalink
Move CancelableTaskTracker to //base/task/CancelableTaskTracker.
Browse files Browse the repository at this point in the history
CancelableTaskTracker is used by //chrome code that is due to componentized.
This CL moves the class to //base/task and wraps it in the ::base::task namespace.

BUG=335135
TBR=thakis

Review URL: https://codereview.chromium.org/137263007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249397 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
blundell@chromium.org committed Feb 6, 2014
1 parent 70c4ebd commit e95b717
Show file tree
Hide file tree
Showing 82 changed files with 452 additions and 447 deletions.
1 change: 1 addition & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
'synchronization/waitable_event_watcher_unittest.cc',
'sys_info_unittest.cc',
'system_monitor/system_monitor_unittest.cc',
'task/cancelable_task_tracker_unittest.cc',
'task_runner_util_unittest.cc',
'template_util_unittest.cc',
'test/expectations/expectation_unittest.cc',
Expand Down
2 changes: 2 additions & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@
'sys_info_openbsd.cc',
'sys_info_posix.cc',
'sys_info_win.cc',
'task/cancelable_task_tracker.cc',
'task/cancelable_task_tracker.h',
'task_runner.cc',
'task_runner.h',
'task_runner_util.h',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/cancelable_task_tracker.h"
#include "base/task/cancelable_task_tracker.h"

#include <utility>

Expand Down Expand Up @@ -54,12 +54,13 @@ void RunOrPostToTaskRunner(TaskRunner* task_runner, const Closure& closure) {

} // namespace

namespace base {

// static
const CancelableTaskTracker::TaskId CancelableTaskTracker::kBadTaskId = 0;

CancelableTaskTracker::CancelableTaskTracker()
: weak_factory_(this),
next_id_(1) {}
: weak_factory_(this), next_id_(1) {}

CancelableTaskTracker::~CancelableTaskTracker() {
DCHECK(thread_checker_.CalledOnValidThread());
Expand Down Expand Up @@ -92,13 +93,15 @@ CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply(
TaskId id = next_id_;
next_id_++; // int64 is big enough that we ignore the potential overflow.

const Closure& untrack_closure = Bind(
&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id);
bool success = task_runner->PostTaskAndReply(
from_here,
Bind(&RunIfNotCanceled, flag, task),
Bind(&RunIfNotCanceledThenUntrack,
base::Owned(flag), reply, untrack_closure));
const Closure& untrack_closure =
Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id);
bool success =
task_runner->PostTaskAndReply(from_here,
Bind(&RunIfNotCanceled, flag, task),
Bind(&RunIfNotCanceledThenUntrack,
base::Owned(flag),
reply,
untrack_closure));

if (!success)
return kBadTaskId;
Expand All @@ -125,13 +128,12 @@ CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId(

// Will always run |untrack_and_delete_flag| on current MessageLoop.
base::ScopedClosureRunner* untrack_and_delete_flag_runner =
new base::ScopedClosureRunner(
Bind(&RunOrPostToTaskRunner,
base::MessageLoopProxy::current(),
untrack_and_delete_flag));
new base::ScopedClosureRunner(Bind(&RunOrPostToTaskRunner,
base::MessageLoopProxy::current(),
untrack_and_delete_flag));

*is_canceled_cb = Bind(
&IsCanceled, flag, base::Owned(untrack_and_delete_flag_runner));
*is_canceled_cb =
Bind(&IsCanceled, flag, base::Owned(untrack_and_delete_flag_runner));

Track(id, flag);
return id;
Expand Down Expand Up @@ -181,3 +183,5 @@ void CancelableTaskTracker::Untrack(TaskId id) {
size_t num = task_flags_.erase(id);
DCHECK_EQ(1u, num);
}

} // namespace base
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// CancelableTaskTracker posts tasks (in the form of a Closure) to a TaskRunner,
// and is able to cancel the task later if it's not needed anymore. On
// destruction, CancelableTaskTracker will cancel all tracked tasks.
// CancelableTaskTracker posts tasks (in the form of a Closure) to a
// TaskRunner, and is able to cancel the task later if it's not needed
// anymore. On destruction, CancelableTaskTracker will cancel all
// tracked tasks.
//
// Each cancelable task can be associated with a reply (also a Closure). After
// the task is run on the TaskRunner, |reply| will be posted back to originating
// TaskRunner.
// the task is run on the TaskRunner, |reply| will be posted back to
// originating TaskRunner.
//
// NOTE:
//
Expand All @@ -17,39 +18,41 @@
// cancelation from another thread. This is sometimes a performance critical
// requirement. E.g. We need to cancel database lookup task on DB thread when
// user changes inputed text. If it is performance critical to do a best effort
// cancelation of a task, then CancelableTaskTracker is appropriate, otherwise
// use one of the other mechanisms.
// cancelation of a task, then CancelableTaskTracker is appropriate,
// otherwise use one of the other mechanisms.
//
// THREAD-SAFETY:
//
// 1. CancelableTaskTracker objects are not thread safe. They must be created,
// used, and destroyed on the originating thread that posts the task. It's safe
// to destroy a CancelableTaskTracker while there are outstanding tasks. This is
// commonly used to cancel all outstanding tasks.
// 1. CancelableTaskTracker objects are not thread safe. They must
// be created, used, and destroyed on the originating thread that posts the
// task. It's safe to destroy a CancelableTaskTracker while there
// are outstanding tasks. This is commonly used to cancel all outstanding
// tasks.
//
// 2. Both task and reply are deleted on the originating thread.
//
// 3. IsCanceledCallback is thread safe and can be run or deleted on any thread.

#ifndef CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
#define CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
// 3. IsCanceledCallback is thread safe and can be run or deleted on any
// thread.
#ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_
#define BASE_TASK_CANCELABLE_TASK_TRACKER_H_

#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"

namespace base {
class CancellationFlag;
class TaskRunner;
} // namespace base

namespace tracked_objects {
class Location;
} // namespace tracked_objects

class CancelableTaskTracker {
namespace base {

class CancellationFlag;
class TaskRunner;

class BASE_EXPORT CancelableTaskTracker {
public:
// All values except kBadTaskId are valid.
typedef int64 TaskId;
Expand Down Expand Up @@ -112,4 +115,6 @@ class CancelableTaskTracker {
DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker);
};

#endif // CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
} // namespace base

#endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_
Loading

0 comments on commit e95b717

Please sign in to comment.