summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-15 23:36:30 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-15 23:36:30 +0000
commitdd1f9fe143f20d4be760c44c974351e8c9aaca6d (patch)
tree2cc832d55557cb59e5e8c759033e246681019dee /base
parentf371ee77f051996ea398b0076afc8852d4243d04 (diff)
downloadchromium_src-dd1f9fe143f20d4be760c44c974351e8c9aaca6d.zip
chromium_src-dd1f9fe143f20d4be760c44c974351e8c9aaca6d.tar.gz
chromium_src-dd1f9fe143f20d4be760c44c974351e8c9aaca6d.tar.bz2
base: Refactor PendingTask out of MessageLoop.
Also removes copy/pasted instances of this class. BUG=none TEST=none R=willchan@chromium.org Review URL: http://codereview.chromium.org/8565024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110206 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gypi2
-rw-r--r--base/message_loop.cc51
-rw-r--r--base/message_loop.h59
-rw-r--r--base/pending_task.cc54
-rw-r--r--base/pending_task.h54
-rw-r--r--base/threading/worker_pool_posix.cc22
-rw-r--r--base/threading/worker_pool_posix.h22
-rw-r--r--base/threading/worker_pool_win.cc29
8 files changed, 147 insertions, 146 deletions
diff --git a/base/base.gypi b/base/base.gypi
index 9224fa9..435cd1e 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -205,6 +205,8 @@
'os_compat_android.h',
'path_service.cc',
'path_service.h',
+ 'pending_task.cc',
+ 'pending_task.h',
'pickle.cc',
'pickle.h',
'platform_file.cc',
diff --git a/base/message_loop.cc b/base/message_loop.cc
index f4db323..3ff2068 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -35,6 +35,7 @@
#include <gdk/gdkx.h>
#endif // defined(OS_POSIX) && !defined(OS_MACOSX)
+using base::PendingTask;
using base::TimeDelta;
using base::TimeTicks;
@@ -260,10 +261,10 @@ void MessageLoop::PostTask(
const tracked_objects::Location& from_here, Task* task) {
DCHECK(task);
PendingTask pending_task(
+ from_here,
base::Bind(
&base::subtle::TaskClosureAdapter::Run,
new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
- from_here,
CalculateDelayedRuntime(0), true);
AddToIncomingQueue(&pending_task);
}
@@ -272,10 +273,10 @@ void MessageLoop::PostDelayedTask(
const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
DCHECK(task);
PendingTask pending_task(
+ from_here,
base::Bind(
&base::subtle::TaskClosureAdapter::Run,
new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
- from_here,
CalculateDelayedRuntime(delay_ms), true);
AddToIncomingQueue(&pending_task);
}
@@ -284,10 +285,10 @@ void MessageLoop::PostNonNestableTask(
const tracked_objects::Location& from_here, Task* task) {
DCHECK(task);
PendingTask pending_task(
+ from_here,
base::Bind(
&base::subtle::TaskClosureAdapter::Run,
new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
- from_here,
CalculateDelayedRuntime(0), false);
AddToIncomingQueue(&pending_task);
}
@@ -296,10 +297,10 @@ void MessageLoop::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
DCHECK(task);
PendingTask pending_task(
+ from_here,
base::Bind(
&base::subtle::TaskClosureAdapter::Run,
new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
- from_here,
CalculateDelayedRuntime(delay_ms), false);
AddToIncomingQueue(&pending_task);
}
@@ -307,7 +308,7 @@ void MessageLoop::PostNonNestableDelayedTask(
void MessageLoop::PostTask(
const tracked_objects::Location& from_here, const base::Closure& task) {
DCHECK(!task.is_null()) << from_here.ToString();
- PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), true);
+ PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), true);
AddToIncomingQueue(&pending_task);
}
@@ -315,7 +316,7 @@ void MessageLoop::PostDelayedTask(
const tracked_objects::Location& from_here, const base::Closure& task,
int64 delay_ms) {
DCHECK(!task.is_null()) << from_here.ToString();
- PendingTask pending_task(task, from_here,
+ PendingTask pending_task(from_here, task,
CalculateDelayedRuntime(delay_ms), true);
AddToIncomingQueue(&pending_task);
}
@@ -323,7 +324,7 @@ void MessageLoop::PostDelayedTask(
void MessageLoop::PostNonNestableTask(
const tracked_objects::Location& from_here, const base::Closure& task) {
DCHECK(!task.is_null()) << from_here.ToString();
- PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), false);
+ PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), false);
AddToIncomingQueue(&pending_task);
}
@@ -331,7 +332,7 @@ void MessageLoop::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here, const base::Closure& task,
int64 delay_ms) {
DCHECK(!task.is_null()) << from_here.ToString();
- PendingTask pending_task(task, from_here,
+ PendingTask pending_task(from_here, task,
CalculateDelayedRuntime(delay_ms), false);
AddToIncomingQueue(&pending_task);
}
@@ -774,40 +775,6 @@ MessageLoop::AutoRunState::~AutoRunState() {
}
//------------------------------------------------------------------------------
-// MessageLoop::PendingTask
-
-MessageLoop::PendingTask::PendingTask(
- const base::Closure& task,
- const tracked_objects::Location& posted_from,
- TimeTicks delayed_run_time,
- bool nestable)
- : base::TrackingInfo(posted_from, delayed_run_time),
- task(task),
- posted_from(posted_from),
- sequence_num(0),
- nestable(nestable) {
-}
-
-MessageLoop::PendingTask::~PendingTask() {
-}
-
-bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
- // Since the top of a priority queue is defined as the "greatest" element, we
- // need to invert the comparison here. We want the smaller time to be at the
- // top of the heap.
-
- if (delayed_run_time < other.delayed_run_time)
- return false;
-
- if (delayed_run_time > other.delayed_run_time)
- return true;
-
- // If the times happen to match, then we use the sequence number to decide.
- // Compare the difference to support integer roll-over.
- return (sequence_num - other.sequence_num) > 0;
-}
-
-//------------------------------------------------------------------------------
// MessageLoopForUI
#if defined(OS_WIN)
diff --git a/base/message_loop.h b/base/message_loop.h
index c14b663..00d5ba7 100644
--- a/base/message_loop.h
+++ b/base/message_loop.h
@@ -17,6 +17,7 @@
#include "base/message_loop_proxy.h"
#include "base/message_pump.h"
#include "base/observer_list.h"
+#include "base/pending_task.h"
#include "base/synchronization/lock.h"
#include "base/task.h"
#include "base/tracking_info.h"
@@ -121,7 +122,7 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
static void InitMessagePumpForUIFactory(MessagePumpFactory* factory);
// A DestructionObserver is notified when the current MessageLoop is being
- // destroyed. These obsevers are notified prior to MessageLoop::current()
+ // destroyed. These observers are notified prior to MessageLoop::current()
// being changed to return NULL. This gives interested parties the chance to
// do final cleanup that depends on the MessageLoop.
//
@@ -377,6 +378,9 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
//----------------------------------------------------------------------------
protected:
+ // PendingTasks are sorted by their |delayed_run_time| property.
+ typedef std::priority_queue<base::PendingTask> DelayedTaskQueue;
+
struct RunState {
// Used to count how many Run() invocations are on the stack.
int run_depth;
@@ -407,39 +411,6 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
protected:
#endif
- // This structure is copied around by value.
- struct PendingTask : public base::TrackingInfo {
- PendingTask(const base::Closure& task,
- const tracked_objects::Location& posted_from,
- base::TimeTicks delayed_run_time,
- bool nestable);
- ~PendingTask();
-
- // Used to support sorting.
- bool operator<(const PendingTask& other) const;
-
- // The task to run.
- base::Closure task;
-
- // The site this PendingTask was posted from.
- tracked_objects::Location posted_from;
-
- // Secondary sort key for run time.
- int sequence_num;
-
- // OK to dispatch from a nested loop.
- bool nestable;
- };
-
- class TaskQueue : public std::queue<PendingTask> {
- public:
- void Swap(TaskQueue* queue) {
- c.swap(queue->c); // Calls std::deque::swap
- }
- };
-
- typedef std::priority_queue<PendingTask> DelayedTaskQueue;
-
#if defined(OS_WIN)
base::MessagePumpWin* pump_win() {
return static_cast<base::MessagePumpWin*>(pump_.get());
@@ -469,14 +440,14 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
bool ProcessNextDelayedNonNestableTask();
// Runs the specified PendingTask.
- void RunTask(const PendingTask& pending_task);
+ void RunTask(const base::PendingTask& pending_task);
// Calls RunTask or queues the pending_task on the deferred task list if it
// cannot be run right now. Returns true if the task was run.
- bool DeferOrRunPendingTask(const PendingTask& pending_task);
+ bool DeferOrRunPendingTask(const base::PendingTask& pending_task);
// Adds the pending task to delayed_work_queue_.
- void AddToDelayedWorkQueue(const PendingTask& pending_task);
+ void AddToDelayedWorkQueue(const base::PendingTask& pending_task);
// Adds the pending task to our incoming_queue_.
//
@@ -484,7 +455,7 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
// reset the value of pending_task->task. This is needed to ensure
// that the posting call stack does not retain pending_task->task
// beyond this function call.
- void AddToIncomingQueue(PendingTask* pending_task);
+ void AddToIncomingQueue(base::PendingTask* pending_task);
// Load tasks from the incoming_queue_ into work_queue_ if the latter is
// empty. The former requires a lock to access, while the latter is directly
@@ -496,14 +467,14 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
// true if some work was done.
bool DeletePendingTasks();
- // Calcuates the time at which a PendingTask should run.
+ // Calculates the time at which a PendingTask should run.
base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
// Start recording histogram info about events and action IF it was enabled
// and IF the statistics recorder can accept a registration of our histogram.
void StartHistogrammer();
- // Add occurence of event to our histogram, so that we can see what is being
+ // Add occurrence of event to our histogram, so that we can see what is being
// done in a specific MessageLoop instance (i.e., specific thread).
// If message_histogram_ is NULL, this is a no-op.
void HistogramEvent(int event);
@@ -517,7 +488,7 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
// A list of tasks that need to be processed by this instance. Note that
// this queue is only accessed (push/pop) by our current thread.
- TaskQueue work_queue_;
+ base::TaskQueue work_queue_;
// Contains delayed tasks, sorted by their 'delayed_run_time' property.
DelayedTaskQueue delayed_work_queue_;
@@ -528,13 +499,13 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
// A queue of non-nestable tasks that we had to defer because when it came
// time to execute them we were in a nested message loop. They will execute
// once we're out of nested message loops.
- TaskQueue deferred_non_nestable_work_queue_;
+ base::TaskQueue deferred_non_nestable_work_queue_;
scoped_refptr<base::MessagePump> pump_;
ObserverList<DestructionObserver> destruction_observers_;
- // A recursion block that prevents accidentally running additonal tasks when
+ // A recursion block that prevents accidentally running additional tasks when
// insider a (accidentally induced?) nested message pump.
bool nestable_tasks_allowed_;
@@ -548,7 +519,7 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
// acquired under a mutex for processing on this instance's thread. These
// tasks have not yet been sorted out into items for our work_queue_ vs items
// that will be handled by the TimerManager.
- TaskQueue incoming_queue_;
+ base::TaskQueue incoming_queue_;
// Protect access to incoming_queue_.
mutable base::Lock incoming_queue_lock_;
diff --git a/base/pending_task.cc b/base/pending_task.cc
new file mode 100644
index 0000000..5a128fe
--- /dev/null
+++ b/base/pending_task.cc
@@ -0,0 +1,54 @@
+// Copyright (c) 2011 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 "base/pending_task.h"
+
+#include "base/tracked_objects.h"
+
+namespace base {
+
+PendingTask::PendingTask(const tracked_objects::Location& posted_from,
+ const base::Closure& task)
+ : base::TrackingInfo(posted_from, TimeTicks()),
+ task(task),
+ posted_from(posted_from),
+ sequence_num(0),
+ nestable(false) {
+}
+
+PendingTask::PendingTask(const tracked_objects::Location& posted_from,
+ const base::Closure& task,
+ TimeTicks delayed_run_time,
+ bool nestable)
+ : base::TrackingInfo(posted_from, delayed_run_time),
+ task(task),
+ posted_from(posted_from),
+ sequence_num(0),
+ nestable(nestable) {
+}
+
+PendingTask::~PendingTask() {
+}
+
+bool PendingTask::operator<(const PendingTask& other) const {
+ // Since the top of a priority queue is defined as the "greatest" element, we
+ // need to invert the comparison here. We want the smaller time to be at the
+ // top of the heap.
+
+ if (delayed_run_time < other.delayed_run_time)
+ return false;
+
+ if (delayed_run_time > other.delayed_run_time)
+ return true;
+
+ // If the times happen to match, then we use the sequence number to decide.
+ // Compare the difference to support integer roll-over.
+ return (sequence_num - other.sequence_num) > 0;
+}
+
+void TaskQueue::Swap(TaskQueue* queue) {
+ c.swap(queue->c); // Calls std::deque::swap.
+}
+
+} // namespace base
diff --git a/base/pending_task.h b/base/pending_task.h
new file mode 100644
index 0000000..dcaea4c
--- /dev/null
+++ b/base/pending_task.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2011 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.
+
+#ifndef PENDING_TASK_H_
+#define PENDING_TASK_H_
+#pragma once
+
+#include <queue>
+
+#include "base/callback.h"
+#include "base/location.h"
+#include "base/time.h"
+#include "base/tracking_info.h"
+
+namespace base {
+
+// Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
+// for use by classes that queue and execute tasks.
+struct PendingTask : public TrackingInfo {
+ PendingTask(const tracked_objects::Location& posted_from,
+ const Closure& task);
+ PendingTask(const tracked_objects::Location& posted_from,
+ const Closure& task,
+ TimeTicks delayed_run_time,
+ bool nestable);
+ ~PendingTask();
+
+ // Used to support sorting.
+ bool operator<(const PendingTask& other) const;
+
+ // The task to run.
+ Closure task;
+
+ // The site this PendingTask was posted from.
+ tracked_objects::Location posted_from;
+
+ // Secondary sort key for run time.
+ int sequence_num;
+
+ // OK to dispatch from a nested loop.
+ bool nestable;
+};
+
+// Wrapper around std::queue specialized for PendingTask which adds a Swap
+// helper method.
+class TaskQueue : public std::queue<PendingTask> {
+ public:
+ void Swap(TaskQueue* queue);
+};
+
+} // namespace base
+
+#endif // PENDING_TASK_H_
diff --git a/base/threading/worker_pool_posix.cc b/base/threading/worker_pool_posix.cc
index cf45f24..da3a86b 100644
--- a/base/threading/worker_pool_posix.cc
+++ b/base/threading/worker_pool_posix.cc
@@ -15,6 +15,8 @@
#include "base/threading/worker_pool.h"
#include "base/tracked_objects.h"
+using tracked_objects::TrackedTime;
+
namespace base {
namespace {
@@ -83,20 +85,20 @@ void WorkerThread::ThreadMain() {
PlatformThread::SetName(name.c_str());
for (;;) {
- PosixDynamicThreadPool::PendingTask pending_task = pool_->WaitForTask();
+ PendingTask pending_task = pool_->WaitForTask();
if (pending_task.task.is_null())
break;
UNSHIPPED_TRACE_EVENT2("task", "WorkerThread::ThreadMain::Run",
"src_file", pending_task.posted_from.file_name(),
"src_func", pending_task.posted_from.function_name());
- tracked_objects::TrackedTime start_time =
+ TrackedTime start_time =
tracked_objects::ThreadData::NowForStartOfRun();
pending_task.task.Run();
tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking(
- pending_task.birth_tally, pending_task.time_posted,
+ pending_task.birth_tally, TrackedTime(pending_task.time_posted),
start_time, tracked_objects::ThreadData::NowForEndOfRun());
}
@@ -118,18 +120,6 @@ bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
return true;
}
-PosixDynamicThreadPool::PendingTask::PendingTask(
- const tracked_objects::Location& posted_from,
- const base::Closure& task)
- : posted_from(posted_from),
- task(task) {
- birth_tally = tracked_objects::ThreadData::TallyABirthIfActive(posted_from);
- time_posted = tracked_objects::ThreadData::Now();
-}
-
-PosixDynamicThreadPool::PendingTask::~PendingTask() {
-}
-
PosixDynamicThreadPool::PosixDynamicThreadPool(
const std::string& name_prefix,
int idle_seconds_before_exit)
@@ -198,7 +188,7 @@ void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) {
}
}
-PosixDynamicThreadPool::PendingTask PosixDynamicThreadPool::WaitForTask() {
+PendingTask PosixDynamicThreadPool::WaitForTask() {
AutoLock locked(lock_);
if (terminated_)
diff --git a/base/threading/worker_pool_posix.h b/base/threading/worker_pool_posix.h
index 2cc1150..1b5032b 100644
--- a/base/threading/worker_pool_posix.h
+++ b/base/threading/worker_pool_posix.h
@@ -34,6 +34,7 @@
#include "base/time.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
+#include "base/pending_task.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"
@@ -48,23 +49,6 @@ class BASE_EXPORT PosixDynamicThreadPool
public:
class PosixDynamicThreadPoolPeer;
- struct PendingTask {
- PendingTask(const tracked_objects::Location& posted_from,
- const base::Closure& task);
- ~PendingTask();
-
- // Counter for location where the Closure was posted from.
- tracked_objects::Births* birth_tally;
-
- // Time the task was posted.
- tracked_objects::TrackedTime time_posted;
-
- const tracked_objects::Location posted_from;
-
- // The task to run.
- base::Closure task;
- };
-
// All worker threads will share the same |name_prefix|. They will exit after
// |idle_seconds_before_exit|.
PosixDynamicThreadPool(const std::string& name_prefix,
@@ -84,7 +68,7 @@ class BASE_EXPORT PosixDynamicThreadPool
// Adds |task| to the thread pool.
void PostTask(const tracked_objects::Location& from_here,
- const base::Closure& task);
+ const Closure& task);
// Worker thread method to wait for up to |idle_seconds_before_exit| for more
// work from the thread pool. Returns NULL if no work is available.
@@ -107,7 +91,7 @@ class BASE_EXPORT PosixDynamicThreadPool
// is being deleted and they can exit.
ConditionVariable pending_tasks_available_cv_;
int num_idle_threads_;
- std::queue<PendingTask> pending_tasks_;
+ TaskQueue pending_tasks_;
bool terminated_;
// Only used for tests to ensure correct thread ordering. It will always be
// NULL in non-test code.
diff --git a/base/threading/worker_pool_win.cc b/base/threading/worker_pool_win.cc
index 9444476..722a57c 100644
--- a/base/threading/worker_pool_win.cc
+++ b/base/threading/worker_pool_win.cc
@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
+#include "base/pending_task.h"
#include "base/task.h"
#include "base/tracked_objects.h"
@@ -14,29 +15,6 @@ namespace base {
namespace {
-struct PendingTask {
- PendingTask(
- const tracked_objects::Location& posted_from,
- const base::Closure& task)
- : posted_from(posted_from),
- task(task) {
- birth_tally = tracked_objects::ThreadData::TallyABirthIfActive(posted_from);
- time_posted = tracked_objects::ThreadData::Now();
- }
-
- // Counter for location where the Closure was posted from.
- tracked_objects::Births* birth_tally;
-
- // Time the task was posted.
- tracked_objects::TrackedTime time_posted;
-
- // The site this PendingTask was posted from.
- tracked_objects::Location posted_from;
-
- // The task to run.
- base::Closure task;
-};
-
DWORD CALLBACK WorkItemCallback(void* param) {
PendingTask* pending_task = static_cast<PendingTask*>(param);
UNSHIPPED_TRACE_EVENT2("task", "WorkItemCallback::Run",
@@ -49,8 +27,9 @@ DWORD CALLBACK WorkItemCallback(void* param) {
pending_task->task.Run();
tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking(
- pending_task->birth_tally, pending_task->time_posted,
- start_time, tracked_objects::ThreadData::NowForEndOfRun());
+ pending_task->birth_tally,
+ tracked_objects::TrackedTime(pending_task->time_posted), start_time,
+ tracked_objects::ThreadData::NowForEndOfRun());
delete pending_task;
return 0;