| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 worker threads that issue completion callbacks |
| 6 | 6 |
| 7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
| 8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
| 9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 void DoCallback(); | 48 void DoCallback(); |
| 49 private: | 49 private: |
| 50 friend class base::RefCountedThreadSafe<ExampleWorker>; | 50 friend class base::RefCountedThreadSafe<ExampleWorker>; |
| 51 | 51 |
| 52 ~ExampleWorker() {} | 52 ~ExampleWorker() {} |
| 53 | 53 |
| 54 // Only used on the origin thread (where DoSomething was called). | 54 // Only used on the origin thread (where DoSomething was called). |
| 55 ExampleEmployer* employer_; | 55 ExampleEmployer* employer_; |
| 56 CompletionCallback* callback_; | 56 CompletionCallback* callback_; |
| 57 // Used to post ourselves onto the origin thread. | 57 // Used to post ourselves onto the origin thread. |
| 58 Lock origin_loop_lock_; | 58 base::Lock origin_loop_lock_; |
| 59 MessageLoop* origin_loop_; | 59 MessageLoop* origin_loop_; |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 void ExampleEmployer::ExampleWorker::DoWork() { | 62 void ExampleEmployer::ExampleWorker::DoWork() { |
| 63 // Running on the worker thread | 63 // Running on the worker thread |
| 64 // In a real worker thread, some work would be done here. | 64 // In a real worker thread, some work would be done here. |
| 65 // Pretend it is, and send the completion callback. | 65 // Pretend it is, and send the completion callback. |
| 66 Task* reply = NewRunnableMethod(this, &ExampleWorker::DoCallback); | 66 Task* reply = NewRunnableMethod(this, &ExampleWorker::DoCallback); |
| 67 | 67 |
| 68 // The origin loop could go away while we are trying to post to it, so we | 68 // The origin loop could go away while we are trying to post to it, so we |
| 69 // need to call its PostTask method inside a lock. See ~ExampleEmployer. | 69 // need to call its PostTask method inside a lock. See ~ExampleEmployer. |
| 70 { | 70 { |
| 71 AutoLock locked(origin_loop_lock_); | 71 base::AutoLock locked(origin_loop_lock_); |
| 72 if (origin_loop_) { | 72 if (origin_loop_) { |
| 73 origin_loop_->PostTask(FROM_HERE, reply); | 73 origin_loop_->PostTask(FROM_HERE, reply); |
| 74 reply = NULL; | 74 reply = NULL; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Does nothing if it got posted. | 78 // Does nothing if it got posted. |
| 79 delete reply; | 79 delete reply; |
| 80 } | 80 } |
| 81 | 81 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 TEST_F(TestCompletionCallbackTest, Simple) { | 115 TEST_F(TestCompletionCallbackTest, Simple) { |
| 116 ExampleEmployer boss; | 116 ExampleEmployer boss; |
| 117 TestCompletionCallback callback; | 117 TestCompletionCallback callback; |
| 118 bool queued = boss.DoSomething(&callback); | 118 bool queued = boss.DoSomething(&callback); |
| 119 EXPECT_EQ(queued, true); | 119 EXPECT_EQ(queued, true); |
| 120 int result = callback.WaitForResult(); | 120 int result = callback.WaitForResult(); |
| 121 EXPECT_EQ(result, kMagicResult); | 121 EXPECT_EQ(result, kMagicResult); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // TODO: test deleting ExampleEmployer while work outstanding | 124 // TODO: test deleting ExampleEmployer while work outstanding |
| OLD | NEW |