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 worker threads that issue completion callbacks |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 class ExampleEmployer::ExampleWorker | 41 class ExampleEmployer::ExampleWorker |
42 : public base::RefCountedThreadSafe<ExampleWorker> { | 42 : public base::RefCountedThreadSafe<ExampleWorker> { |
43 public: | 43 public: |
44 ExampleWorker(ExampleEmployer* employer, | 44 ExampleWorker(ExampleEmployer* employer, |
45 const net::CompletionCallback& callback) | 45 const net::CompletionCallback& callback) |
46 : employer_(employer), | 46 : employer_(employer), |
47 callback_(callback), | 47 callback_(callback), |
48 origin_loop_(base::MessageLoop::current()) {} | 48 origin_loop_(base::MessageLoop::current()) {} |
49 void DoWork(); | 49 void DoWork(); |
50 void DoCallback(); | 50 void DoCallback(); |
| 51 |
51 private: | 52 private: |
52 friend class base::RefCountedThreadSafe<ExampleWorker>; | 53 friend class base::RefCountedThreadSafe<ExampleWorker>; |
53 | 54 |
54 ~ExampleWorker() {} | 55 ~ExampleWorker() {} |
55 | 56 |
56 // Only used on the origin thread (where DoSomething was called). | 57 // Only used on the origin thread (where DoSomething was called). |
57 ExampleEmployer* employer_; | 58 ExampleEmployer* employer_; |
58 net::CompletionCallback callback_; | 59 net::CompletionCallback callback_; |
59 // Used to post ourselves onto the origin thread. | 60 // Used to post ourselves onto the origin thread. |
60 base::Lock origin_loop_lock_; | 61 base::Lock origin_loop_lock_; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 TEST_F(TestCompletionCallbackTest, Simple) { | 115 TEST_F(TestCompletionCallbackTest, Simple) { |
115 ExampleEmployer boss; | 116 ExampleEmployer boss; |
116 net::TestCompletionCallback callback; | 117 net::TestCompletionCallback callback; |
117 bool queued = boss.DoSomething(callback.callback()); | 118 bool queued = boss.DoSomething(callback.callback()); |
118 EXPECT_EQ(queued, true); | 119 EXPECT_EQ(queued, true); |
119 int result = callback.WaitForResult(); | 120 int result = callback.WaitForResult(); |
120 EXPECT_EQ(result, kMagicResult); | 121 EXPECT_EQ(result, kMagicResult); |
121 } | 122 } |
122 | 123 |
123 // TODO: test deleting ExampleEmployer while work outstanding | 124 // TODO: test deleting ExampleEmployer while work outstanding |
OLD | NEW |