OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.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 "base/worker_pool.h" | 10 #include "base/worker_pool.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 class ExampleEmployer::ExampleWorker | 40 class ExampleEmployer::ExampleWorker |
41 : public base::RefCountedThreadSafe<ExampleWorker> { | 41 : public base::RefCountedThreadSafe<ExampleWorker> { |
42 public: | 42 public: |
43 ExampleWorker(ExampleEmployer* employer, CompletionCallback* callback) | 43 ExampleWorker(ExampleEmployer* employer, CompletionCallback* callback) |
44 : employer_(employer), callback_(callback), | 44 : employer_(employer), callback_(callback), |
45 origin_loop_(MessageLoop::current()) { | 45 origin_loop_(MessageLoop::current()) { |
46 } | 46 } |
47 void DoWork(); | 47 void DoWork(); |
48 void DoCallback(); | 48 void DoCallback(); |
49 private: | 49 private: |
| 50 friend class base::RefCountedThreadSafe<ExampleWorker>; |
| 51 |
| 52 ~ExampleWorker() {} |
| 53 |
50 // Only used on the origin thread (where DoSomething was called). | 54 // Only used on the origin thread (where DoSomething was called). |
51 ExampleEmployer* employer_; | 55 ExampleEmployer* employer_; |
52 CompletionCallback* callback_; | 56 CompletionCallback* callback_; |
53 // Used to post ourselves onto the origin thread. | 57 // Used to post ourselves onto the origin thread. |
54 Lock origin_loop_lock_; | 58 Lock origin_loop_lock_; |
55 MessageLoop* origin_loop_; | 59 MessageLoop* origin_loop_; |
56 }; | 60 }; |
57 | 61 |
58 void ExampleEmployer::ExampleWorker::DoWork() { | 62 void ExampleEmployer::ExampleWorker::DoWork() { |
59 // Running on the worker thread | 63 // Running on the worker thread |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 TEST_F(TestCompletionCallbackTest, Simple) { | 115 TEST_F(TestCompletionCallbackTest, Simple) { |
112 ExampleEmployer boss; | 116 ExampleEmployer boss; |
113 TestCompletionCallback callback; | 117 TestCompletionCallback callback; |
114 bool queued = boss.DoSomething(&callback); | 118 bool queued = boss.DoSomething(&callback); |
115 EXPECT_EQ(queued, true); | 119 EXPECT_EQ(queued, true); |
116 int result = callback.WaitForResult(); | 120 int result = callback.WaitForResult(); |
117 EXPECT_EQ(result, kMagicResult); | 121 EXPECT_EQ(result, kMagicResult); |
118 } | 122 } |
119 | 123 |
120 // TODO: test deleting ExampleEmployer while work outstanding | 124 // TODO: test deleting ExampleEmployer while work outstanding |
OLD | NEW |