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

Unified Diff: base/test/test_simple_task_runner.h

Issue 2296923003: Make TestSimpleTaskRunner thread-safe. (Closed)
Patch Set: fix asan error Created 4 years, 3 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
« no previous file with comments | « no previous file | base/test/test_simple_task_runner.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/test_simple_task_runner.h
diff --git a/base/test/test_simple_task_runner.h b/base/test/test_simple_task_runner.h
index 338c634c8d9d5533ecf94ef8364ef19d36620d35..eb600f40c3a68c9396614537993f1468b138b0bb 100644
--- a/base/test/test_simple_task_runner.h
+++ b/base/test/test_simple_task_runner.h
@@ -10,8 +10,9 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
+#include "base/synchronization/lock.h"
#include "base/test/test_pending_task.h"
-#include "base/threading/thread_checker.h"
+#include "base/threading/platform_thread.h"
namespace base {
@@ -25,8 +26,6 @@ class TimeDelta;
//
// TestSimpleTaskRunner has the following properties which make it simple:
//
-// - It is non-thread safe; all member functions must be called on
-// the same thread.
// - Tasks are simply stored in a queue in FIFO order, ignoring delay
// and nestability.
// - Tasks aren't guaranteed to be destroyed immediately after
@@ -36,10 +35,6 @@ class TimeDelta;
// handles the running of tasks that in turn call back into itself
// (e.g., to post more tasks).
//
-// If you need more complicated properties, consider using this class
-// as a template for writing a test TaskRunner implementation using
-// TestPendingTask.
-//
// Note that, like any TaskRunner, TestSimpleTaskRunner is
// ref-counted.
class TestSimpleTaskRunner : public SingleThreadTaskRunner {
@@ -56,27 +51,35 @@ class TestSimpleTaskRunner : public SingleThreadTaskRunner {
bool RunsTasksOnCurrentThread() const override;
- const std::deque<TestPendingTask>& GetPendingTasks() const;
+ std::deque<TestPendingTask> GetPendingTasks() const;
+ size_t NumPendingTasks() const;
bool HasPendingTask() const;
base::TimeDelta NextPendingTaskDelay() const;
// Clears the queue of pending tasks without running them.
void ClearPendingTasks();
- // Runs each current pending task in order and clears the queue.
- // Any tasks posted by the tasks are not run.
- virtual void RunPendingTasks();
+ // Runs each current pending task in order and clears the queue. Tasks posted
+ // by the tasks that run within this call do not run within this call. Can
+ // only be called on the thread that created this TestSimpleTaskRunner.
+ void RunPendingTasks();
- // Runs pending tasks until the queue is empty.
+ // Runs pending tasks until the queue is empty. Can only be called on the
+ // thread that created this TestSimpleTaskRunner.
void RunUntilIdle();
protected:
~TestSimpleTaskRunner() override;
danakj 2016/09/01 21:22:18 nit: idk why this is protected.. maybe make this p
fdoray 2016/09/02 17:39:16 TestSimpleTaskRunner is ref-counted. Having a prot
danakj 2016/09/02 20:00:07 Oh I missed that since it's inheriting that. Thank
+ private:
+ // Thread on which this was instantiated.
+ const PlatformThreadRef thread_ref_ = PlatformThread::CurrentRef();
+
+ // Synchronizes access to |pending_tasks_|.
+ mutable Lock lock_;
+
std::deque<TestPendingTask> pending_tasks_;
- ThreadChecker thread_checker_;
- private:
DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner);
};
« no previous file with comments | « no previous file | base/test/test_simple_task_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698