OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ | 5 #ifndef BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ |
6 #define BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ | 6 #define BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/run_loop.h" | |
16 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
17 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
18 | 17 |
19 namespace base { | 18 namespace base { |
20 | 19 |
21 class MessageLoop; | 20 class MessageLoop; |
22 | 21 |
23 // Wrapper around SequencedWorkerPool for testing that blocks destruction | 22 // Wrapper around SequencedWorkerPool for testing that blocks destruction |
24 // until the pool is actually destroyed. This is so that a | 23 // until the pool is actually destroyed. This is so that a |
25 // SequencedWorkerPool from one test doesn't outlive its test and cause | 24 // SequencedWorkerPool from one test doesn't outlive its test and cause |
26 // strange races with other tests that touch global stuff (like histograms and | 25 // strange races with other tests that touch global stuff (like histograms and |
27 // logging). However, this requires that nothing else on this thread holds a | 26 // logging). However, this requires that nothing else on this thread holds a |
28 // ref to the pool when the SequencedWorkerPoolOwner is destroyed. | 27 // ref to the pool when the SequencedWorkerPoolOwner is destroyed. |
29 // | |
30 // This class calls Shutdown on the owned SequencedWorkerPool in the destructor. | |
31 // Tests may themselves call Shutdown earlier to test shutdown behavior. | |
32 class SequencedWorkerPoolOwner : public SequencedWorkerPool::TestingObserver { | 28 class SequencedWorkerPoolOwner : public SequencedWorkerPool::TestingObserver { |
33 public: | 29 public: |
34 SequencedWorkerPoolOwner(size_t max_threads, | 30 SequencedWorkerPoolOwner(size_t max_threads, |
35 const std::string& thread_name_prefix); | 31 const std::string& thread_name_prefix); |
36 | 32 |
37 ~SequencedWorkerPoolOwner() override; | 33 ~SequencedWorkerPoolOwner() override; |
38 | 34 |
39 // Don't change the returned pool's testing observer. | 35 // Don't change the returned pool's testing observer. |
40 const scoped_refptr<SequencedWorkerPool>& pool(); | 36 const scoped_refptr<SequencedWorkerPool>& pool(); |
41 | 37 |
42 // The given callback will be called on WillWaitForShutdown(). | 38 // The given callback will be called on WillWaitForShutdown(). |
43 void SetWillWaitForShutdownCallback(const Closure& callback); | 39 void SetWillWaitForShutdownCallback(const Closure& callback); |
44 | 40 |
45 int has_work_call_count() const; | 41 int has_work_call_count() const; |
46 | 42 |
47 private: | 43 private: |
48 // SequencedWorkerPool::TestingObserver implementation. | 44 // SequencedWorkerPool::TestingObserver implementation. |
49 void OnHasWork() override; | 45 void OnHasWork() override; |
50 void WillWaitForShutdown() override; | 46 void WillWaitForShutdown() override; |
51 void OnDestruct() override; | 47 void OnDestruct() override; |
52 | 48 |
53 // Used to run the current thread's message loop until the | |
54 // SequencedWorkerPool's destruction has been verified. | |
55 base::RunLoop exit_loop_; | |
56 MessageLoop* const constructor_message_loop_; | 49 MessageLoop* const constructor_message_loop_; |
57 | |
58 scoped_refptr<SequencedWorkerPool> pool_; | 50 scoped_refptr<SequencedWorkerPool> pool_; |
59 Closure will_wait_for_shutdown_callback_; | 51 Closure will_wait_for_shutdown_callback_; |
60 | 52 |
61 mutable Lock has_work_lock_; | 53 mutable Lock has_work_lock_; |
62 int has_work_call_count_; | 54 int has_work_call_count_; |
63 | 55 |
64 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner); | 56 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner); |
65 }; | 57 }; |
66 | 58 |
67 } // namespace base | 59 } // namespace base |
68 | 60 |
69 #endif // BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ | 61 #endif // BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ |
OLD | NEW |