| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Illustrates how to use worker threads that issue completion callbacks | 5 // Illustrates how to use net::TestCompletionCallback. |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/worker_pool.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 15 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 18 | 17 |
| 19 namespace net { | 18 namespace net { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 const int kMagicResult = 8888; | 22 const int kMagicResult = 8888; |
| 24 | 23 |
| 25 void CallClosureAfterCheckingResult(const base::Closure& closure, | 24 void CallClosureAfterCheckingResult(const base::Closure& closure, |
| 26 bool* did_check_result, | 25 bool* did_check_result, |
| 27 int result) { | 26 int result) { |
| 28 DCHECK_EQ(result, kMagicResult); | 27 DCHECK_EQ(result, kMagicResult); |
| 29 *did_check_result = true; | 28 *did_check_result = true; |
| 30 closure.Run(); | 29 closure.Run(); |
| 31 } | 30 } |
| 32 | 31 |
| 33 // ExampleEmployer is a toy version of HostResolver | 32 // ExampleEmployer is a toy version of HostResolver |
| 34 // TODO: restore damage done in extracting example from real code | 33 // TODO: restore damage done in extracting example from real code |
| 35 // (e.g. bring back real destructor, bring back comments) | 34 // (e.g. bring back real destructor, bring back comments) |
| 36 class ExampleEmployer { | 35 class ExampleEmployer { |
| 37 public: | 36 public: |
| 38 ExampleEmployer(); | 37 ExampleEmployer(); |
| 39 ~ExampleEmployer(); | 38 ~ExampleEmployer(); |
| 40 | 39 |
| 41 // Do some imaginary work on a worker thread; | 40 // Posts to the current thread a task which itself posts |callback| to the |
| 42 // when done, worker posts callback on the original thread. | 41 // current thread. Returns true on success |
| 43 // Returns true on success | |
| 44 bool DoSomething(const CompletionCallback& callback); | 42 bool DoSomething(const CompletionCallback& callback); |
| 45 | 43 |
| 46 private: | 44 private: |
| 47 class ExampleWorker; | 45 class ExampleWorker; |
| 48 friend class ExampleWorker; | 46 friend class ExampleWorker; |
| 49 scoped_refptr<ExampleWorker> request_; | 47 scoped_refptr<ExampleWorker> request_; |
| 50 DISALLOW_COPY_AND_ASSIGN(ExampleEmployer); | 48 DISALLOW_COPY_AND_ASSIGN(ExampleEmployer); |
| 51 }; | 49 }; |
| 52 | 50 |
| 53 // Helper class; this is how ExampleEmployer puts work on a different thread | 51 // Helper class; this is how ExampleEmployer schedules work. |
| 54 class ExampleEmployer::ExampleWorker | 52 class ExampleEmployer::ExampleWorker |
| 55 : public base::RefCountedThreadSafe<ExampleWorker> { | 53 : public base::RefCountedThreadSafe<ExampleWorker> { |
| 56 public: | 54 public: |
| 57 ExampleWorker(ExampleEmployer* employer, const CompletionCallback& callback) | 55 ExampleWorker(ExampleEmployer* employer, const CompletionCallback& callback) |
| 58 : employer_(employer), | 56 : employer_(employer), callback_(callback) {} |
| 59 callback_(callback), | |
| 60 origin_loop_(base::MessageLoop::current()) {} | |
| 61 void DoWork(); | 57 void DoWork(); |
| 62 void DoCallback(); | 58 void DoCallback(); |
| 63 private: | 59 private: |
| 64 friend class base::RefCountedThreadSafe<ExampleWorker>; | 60 friend class base::RefCountedThreadSafe<ExampleWorker>; |
| 65 | 61 |
| 66 ~ExampleWorker() {} | 62 ~ExampleWorker() {} |
| 67 | 63 |
| 68 // Only used on the origin thread (where DoSomething was called). | 64 // Only used on the origin thread (where DoSomething was called). |
| 69 ExampleEmployer* employer_; | 65 ExampleEmployer* employer_; |
| 70 CompletionCallback callback_; | 66 CompletionCallback callback_; |
| 71 // Used to post ourselves onto the origin thread. | 67 // Used to post ourselves onto the origin thread. |
| 72 base::Lock origin_loop_lock_; | 68 const scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_ = |
| 73 base::MessageLoop* origin_loop_; | 69 base::ThreadTaskRunnerHandle::Get(); |
| 74 }; | 70 }; |
| 75 | 71 |
| 76 void ExampleEmployer::ExampleWorker::DoWork() { | 72 void ExampleEmployer::ExampleWorker::DoWork() { |
| 77 // Running on the worker thread | |
| 78 // In a real worker thread, some work would be done here. | 73 // In a real worker thread, some work would be done here. |
| 79 // Pretend it is, and send the completion callback. | 74 // Pretend it is, and send the completion callback. |
| 80 | 75 origin_task_runner_->PostTask(FROM_HERE, |
| 81 // The origin loop could go away while we are trying to post to it, so we | 76 base::Bind(&ExampleWorker::DoCallback, this)); |
| 82 // need to call its PostTask method inside a lock. See ~ExampleEmployer. | |
| 83 { | |
| 84 base::AutoLock locked(origin_loop_lock_); | |
| 85 if (origin_loop_) | |
| 86 origin_loop_->task_runner()->PostTask( | |
| 87 FROM_HERE, base::Bind(&ExampleWorker::DoCallback, this)); | |
| 88 } | |
| 89 } | 77 } |
| 90 | 78 |
| 91 void ExampleEmployer::ExampleWorker::DoCallback() { | 79 void ExampleEmployer::ExampleWorker::DoCallback() { |
| 92 // Running on the origin thread. | 80 // Running on the origin thread. |
| 93 | 81 |
| 94 // Drop the employer_'s reference to us. Do this before running the | 82 // Drop the employer_'s reference to us. Do this before running the |
| 95 // callback since the callback might result in the employer being | 83 // callback since the callback might result in the employer being |
| 96 // destroyed. | 84 // destroyed. |
| 97 employer_->request_ = NULL; | 85 employer_->request_ = NULL; |
| 98 | 86 |
| 99 callback_.Run(kMagicResult); | 87 callback_.Run(kMagicResult); |
| 100 } | 88 } |
| 101 | 89 |
| 102 ExampleEmployer::ExampleEmployer() { | 90 ExampleEmployer::ExampleEmployer() { |
| 103 } | 91 } |
| 104 | 92 |
| 105 ExampleEmployer::~ExampleEmployer() { | 93 ExampleEmployer::~ExampleEmployer() { |
| 106 } | 94 } |
| 107 | 95 |
| 108 bool ExampleEmployer::DoSomething(const CompletionCallback& callback) { | 96 bool ExampleEmployer::DoSomething(const CompletionCallback& callback) { |
| 109 DCHECK(!request_.get()) << "already in use"; | 97 DCHECK(!request_.get()) << "already in use"; |
| 110 | 98 |
| 111 request_ = new ExampleWorker(this, callback); | 99 request_ = new ExampleWorker(this, callback); |
| 112 | 100 |
| 113 // Dispatch to worker thread... | 101 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 114 if (!base::WorkerPool::PostTask( | 102 FROM_HERE, base::Bind(&ExampleWorker::DoWork, request_))) { |
| 115 FROM_HERE, base::Bind(&ExampleWorker::DoWork, request_), true)) { | |
| 116 NOTREACHED(); | 103 NOTREACHED(); |
| 117 request_ = NULL; | 104 request_ = NULL; |
| 118 return false; | 105 return false; |
| 119 } | 106 } |
| 120 | 107 |
| 121 return true; | 108 return true; |
| 122 } | 109 } |
| 123 | 110 |
| 124 } // namespace | 111 } // namespace |
| 125 | 112 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 145 EXPECT_TRUE(queued); | 132 EXPECT_TRUE(queued); |
| 146 | 133 |
| 147 EXPECT_FALSE(did_check_result); | 134 EXPECT_FALSE(did_check_result); |
| 148 closure.WaitForResult(); | 135 closure.WaitForResult(); |
| 149 EXPECT_TRUE(did_check_result); | 136 EXPECT_TRUE(did_check_result); |
| 150 } | 137 } |
| 151 | 138 |
| 152 // TODO: test deleting ExampleEmployer while work outstanding | 139 // TODO: test deleting ExampleEmployer while work outstanding |
| 153 | 140 |
| 154 } // namespace net | 141 } // namespace net |
| OLD | NEW |