| 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;
|
|
|