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

Unified Diff: base/message_loop.h

Issue 6463013: Add support for base::Closure in the MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: rebased Created 9 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: base/message_loop.h
diff --git a/base/message_loop.h b/base/message_loop.h
index c1dce433572a948521eafeba465170b844c263dd..9a1fdcd389c83a3ba34e2693dc3460af6ec5ce78 100644
--- a/base/message_loop.h
+++ b/base/message_loop.h
@@ -10,11 +10,14 @@
#include <string>
#include "base/basictypes.h"
+#include "base/callback.h"
#include "base/message_pump.h"
#include "base/observer_list.h"
#include "base/ref_counted.h"
#include "base/synchronization/lock.h"
#include "base/task.h"
+#include "base/time.h"
+#include "base/tracked.h"
#if defined(OS_WIN)
// We need this to declare base::MessagePumpWin::Dispatcher, which we should
@@ -35,6 +38,12 @@ namespace base {
class Histogram;
}
+#if defined(TRACK_ALL_TASK_OBJECTS)
+namespace tracked_objects {
+class Births;
+}
+#endif // defined(TRACK_ALL_TASK_OBJECTS)
+
// A MessageLoop is used to process events for a particular thread. There is
// at most one MessageLoop instance per thread.
//
@@ -162,6 +171,22 @@ class MessageLoop : public base::MessagePump::Delegate {
void PostNonNestableDelayedTask(
const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
+ void PostClosure(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure);
+
+ void PostDelayedClosure(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure, int64 delay_ms);
+
+ void PostNonNestableClosure(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure);
+
+ void PostNonNestableDelayedClosure(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure, int64 delay_ms);
+
// A variant on PostTask that deletes the given object. This is useful
// if the object needs to live until the next run of the MessageLoop (for
// example, deleting a RenderProcessHost from within an IPC callback is not
@@ -279,28 +304,28 @@ class MessageLoop : public base::MessagePump::Delegate {
// Returns true if we are currently running a nested message loop.
bool IsNested();
- // A TaskObserver is an object that receives task notifications from the
+ // A ClosureObserver is an object that receives task notifications from the
// MessageLoop.
//
- // NOTE: A TaskObserver implementation should be extremely fast!
- class TaskObserver {
+ // NOTE: A ClosureObserver implementation should be extremely fast!
+ class ClosureObserver {
public:
- TaskObserver();
+ ClosureObserver();
// This method is called before processing a task.
- virtual void WillProcessTask(const Task* task) = 0;
+ virtual void WillProcessClosure(base::TimeTicks time_posted) = 0;
// This method is called after processing a task.
- virtual void DidProcessTask(const Task* task) = 0;
+ virtual void DidProcessClosure(base::TimeTicks time_posted) = 0;
protected:
- virtual ~TaskObserver();
+ virtual ~ClosureObserver();
};
// These functions can only be called on the same thread that |this| is
// running on.
- void AddTaskObserver(TaskObserver* task_observer);
- void RemoveTaskObserver(TaskObserver* task_observer);
+ void AddClosureObserver(ClosureObserver* closure_observer);
+ void RemoveClosureObserver(ClosureObserver* closure_observer);
// Returns true if the message loop has high resolution timers enabled.
// Provided for testing.
@@ -344,28 +369,44 @@ class MessageLoop : public base::MessagePump::Delegate {
};
// This structure is copied around by value.
- struct PendingTask {
- PendingTask(Task* task, bool nestable)
- : task(task), sequence_num(0), nestable(nestable) {
- }
+ struct PendingClosure {
+ PendingClosure(const base::Closure& closure,
+ const tracked_objects::Location& posted_from,
+ base::TimeTicks delayed_run_time,
+ bool nestable);
// Used to support sorting.
- bool operator<(const PendingTask& other) const;
+ bool operator<(const PendingClosure& other) const;
- Task* task; // The task to run.
- base::TimeTicks delayed_run_time; // The time when the task should be run.
- int sequence_num; // Secondary sort key for run time.
- bool nestable; // OK to dispatch from a nested loop.
+ // The Closure to run.
+ base::Closure closure;
+
+#if defined(TRACK_ALL_TASK_OBJECTS)
+ // Counter for location where the Closure was posted from.
+ tracked_objects::Births* post_births;
+#endif // defined(TRACK_ALL_TASK_OBJECTS)
+
+ // Time this PendingClosure was posted.
+ base::TimeTicks time_posted;
+
+ // The time when the task should be run.
+ base::TimeTicks delayed_run_time;
+
+ // Secondary sort key for run time.
+ int sequence_num;
+
+ // OK to dispatch from a nested loop.
+ bool nestable;
};
- class TaskQueue : public std::queue<PendingTask> {
+ class TaskQueue : public std::queue<PendingClosure> {
public:
void Swap(TaskQueue* queue) {
c.swap(queue->c); // Calls std::deque::swap
}
};
- typedef std::priority_queue<PendingTask> DelayedTaskQueue;
+ typedef std::priority_queue<PendingClosure> DelayedTaskQueue;
#if defined(OS_WIN)
base::MessagePumpWin* pump_win() {
@@ -395,15 +436,20 @@ class MessageLoop : public base::MessagePump::Delegate {
// Called to process any delayed non-nestable tasks.
bool ProcessNextDelayedNonNestableTask();
- // Runs the specified task and deletes it.
- void RunTask(Task* task);
+ // Runs the specified PendingClosure.
+ void RunClosure(const PendingClosure& closure);
- // Calls RunTask or queues the pending_task on the deferred task list if it
+ // Calls RunTask or queues the pending_closure 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 DeferOrRunPendingClosure(const PendingClosure& pending_closure);
// Adds the pending task to delayed_work_queue_.
- void AddToDelayedWorkQueue(const PendingTask& pending_task);
+ void AddToDelayedWorkQueue(const PendingClosure& pending_closure);
+
+ // Adds the pending task to our incoming_queue_.
+ // Caller retains ownership of |pending_closure|, but this function will
+ // reset the value of pending_closure->closure.
+ void AddToIncomingQueue(PendingClosure* pending_closure);
// 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
@@ -413,11 +459,10 @@ class MessageLoop : public base::MessagePump::Delegate {
// Delete tasks that haven't run yet without running them. Used in the
// destructor to make sure all the task's destructors get called. Returns
// true if some work was done.
- bool DeletePendingTasks();
+ bool DeletePendingClosures();
- // Post a task to our incomming queue.
- void PostTask_Helper(const tracked_objects::Location& from_here, Task* task,
- int64 delay_ms, bool nestable);
+ // Calcuates the time at which a PendingClosure 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.
@@ -466,14 +511,16 @@ class MessageLoop : public base::MessagePump::Delegate {
// A null terminated list which creates an incoming_queue of tasks that are
// 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.
+ // 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_;
// Protect access to incoming_queue_.
mutable base::Lock incoming_queue_lock_;
RunState* state_;
+ bool should_leak_tasks_;
+
#if defined(OS_WIN)
base::TimeTicks high_resolution_timer_expiration_;
#endif
@@ -481,7 +528,7 @@ class MessageLoop : public base::MessagePump::Delegate {
// The next sequence number to use for delayed tasks.
int next_sequence_num_;
- ObserverList<TaskObserver> task_observers_;
+ ObserverList<ClosureObserver> closure_observers_;
DISALLOW_COPY_AND_ASSIGN(MessageLoop);
};
« no previous file with comments | « base/bind_internal_win.h.pump ('k') | base/message_loop.cc » ('j') | base/message_loop.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698