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

Unified Diff: net/base/test_completion_callback_unittest.cc

Issue 2606213003: Remove usage of WorkerPool from test_completion_callback_unittest.cc. (Closed)
Patch Set: fix build error 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/test_completion_callback_unittest.cc
diff --git a/net/base/test_completion_callback_unittest.cc b/net/base/test_completion_callback_unittest.cc
index a2bc49092ddd4b41004da6a4e38db924bfff8c90..db62f7e6ae32559dd12aa54e9cc8c753834a945e 100644
--- a/net/base/test_completion_callback_unittest.cc
+++ b/net/base/test_completion_callback_unittest.cc
@@ -2,15 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// Illustrates how to use worker threads that issue completion callbacks
+// Illustrates how to use net::TestCompletionCallback.
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
-#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
-#include "base/threading/worker_pool.h"
+#include "base/threading/thread_task_runner_handle.h"
#include "net/base/completion_callback.h"
#include "net/base/test_completion_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -38,9 +37,8 @@ class ExampleEmployer {
ExampleEmployer();
~ExampleEmployer();
- // Do some imaginary work on a worker thread;
- // when done, worker posts callback on the original thread.
- // Returns true on success
+ // Posts to the current thread a task which itself posts |callback| to the
+ // current thread. Returns true on success
bool DoSomething(const CompletionCallback& callback);
private:
@@ -50,14 +48,12 @@ class ExampleEmployer {
DISALLOW_COPY_AND_ASSIGN(ExampleEmployer);
};
-// Helper class; this is how ExampleEmployer puts work on a different thread
+// Helper class; this is how ExampleEmployer schedules work.
class ExampleEmployer::ExampleWorker
: public base::RefCountedThreadSafe<ExampleWorker> {
public:
ExampleWorker(ExampleEmployer* employer, const CompletionCallback& callback)
- : employer_(employer),
- callback_(callback),
- origin_loop_(base::MessageLoop::current()) {}
+ : employer_(employer), callback_(callback) {}
void DoWork();
void DoCallback();
private:
@@ -69,23 +65,15 @@ class ExampleEmployer::ExampleWorker
ExampleEmployer* employer_;
CompletionCallback callback_;
// Used to post ourselves onto the origin thread.
- base::Lock origin_loop_lock_;
- base::MessageLoop* origin_loop_;
+ const scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_ =
+ base::ThreadTaskRunnerHandle::Get();
};
void ExampleEmployer::ExampleWorker::DoWork() {
- // Running on the worker thread
// In a real worker thread, some work would be done here.
// Pretend it is, and send the completion callback.
-
- // The origin loop could go away while we are trying to post to it, so we
- // need to call its PostTask method inside a lock. See ~ExampleEmployer.
- {
- base::AutoLock locked(origin_loop_lock_);
- if (origin_loop_)
- origin_loop_->task_runner()->PostTask(
- FROM_HERE, base::Bind(&ExampleWorker::DoCallback, this));
- }
+ origin_task_runner_->PostTask(FROM_HERE,
+ base::Bind(&ExampleWorker::DoCallback, this));
}
void ExampleEmployer::ExampleWorker::DoCallback() {
@@ -110,9 +98,8 @@ bool ExampleEmployer::DoSomething(const CompletionCallback& callback) {
request_ = new ExampleWorker(this, callback);
- // Dispatch to worker thread...
- if (!base::WorkerPool::PostTask(
- FROM_HERE, base::Bind(&ExampleWorker::DoWork, request_), true)) {
+ if (!base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(&ExampleWorker::DoWork, request_))) {
NOTREACHED();
request_ = NULL;
return false;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698