OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_TEST_IO_THREAD_H_ | 5 #ifndef BASE_TEST_TEST_IO_THREAD_H_ |
6 #define BASE_TEST_TEST_IO_THREAD_H_ | 6 #define BASE_TEST_TEST_IO_THREAD_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 // Create and run an IO thread with a MessageLoop, and | 18 // Create and run an IO thread with a MessageLoop, and |
19 // making the MessageLoop accessible from its client. | 19 // making the MessageLoop accessible from its client. |
20 // It also provides some ideomatic API like PostTaskAndWait(). | 20 // It also provides some ideomatic API like PostTaskAndWait(). |
| 21 // |
| 22 // This API is not thread-safe: |
| 23 // - Start()/Stop() should only be called from the main (creation) thread. |
| 24 // - PostTask()/message_loop()/task_runner() are also safe to call from the |
| 25 // underlying thread itself (to post tasks from other threads: get the |
| 26 // task_runner() from the main thread first, it is then safe to pass _it_ |
| 27 // around). |
21 class TestIOThread { | 28 class TestIOThread { |
22 public: | 29 public: |
23 enum Mode { kAutoStart, kManualStart }; | 30 enum Mode { kAutoStart, kManualStart }; |
24 explicit TestIOThread(Mode mode); | 31 explicit TestIOThread(Mode mode); |
25 // Stops the I/O thread if necessary. | 32 // Stops the I/O thread if necessary. |
26 ~TestIOThread(); | 33 ~TestIOThread(); |
27 | 34 |
28 // |Start()|/|Stop()| should only be called from the main (creation) thread. | 35 // After Stop(), Start() may be called again to start a new I/O thread. |
29 // After |Stop()|, |Start()| may be called again to start a new I/O thread. | 36 // Stop() may be called even when the I/O thread is not started. |
30 // |Stop()| may be called even when the I/O thread is not started. | |
31 void Start(); | 37 void Start(); |
32 void Stop(); | 38 void Stop(); |
33 | 39 |
34 // Post |task| to the IO thread. | 40 // Post |task| to the IO thread. |
35 void PostTask(const tracked_objects::Location& from_here, | 41 void PostTask(const tracked_objects::Location& from_here, |
36 const base::Closure& task); | 42 const base::Closure& task); |
37 // Posts |task| to the IO-thread with an WaitableEvent associated blocks on | |
38 // it until the posted |task| is executed, then returns. | |
39 void PostTaskAndWait(const tracked_objects::Location& from_here, | |
40 const base::Closure& task); | |
41 | 43 |
42 base::MessageLoopForIO* message_loop() { | 44 base::MessageLoopForIO* message_loop() { |
43 return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop()); | 45 return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop()); |
44 } | 46 } |
45 | 47 |
46 scoped_refptr<SingleThreadTaskRunner> task_runner() { | 48 scoped_refptr<SingleThreadTaskRunner> task_runner() { |
47 return message_loop()->task_runner(); | 49 return message_loop()->task_runner(); |
48 } | 50 } |
49 | 51 |
50 private: | 52 private: |
51 base::Thread io_thread_; | 53 base::Thread io_thread_; |
52 bool io_thread_started_; | 54 bool io_thread_started_; |
53 | 55 |
54 DISALLOW_COPY_AND_ASSIGN(TestIOThread); | 56 DISALLOW_COPY_AND_ASSIGN(TestIOThread); |
55 }; | 57 }; |
56 | 58 |
57 } // namespace base | 59 } // namespace base |
58 | 60 |
59 #endif // BASE_TEST_TEST_IO_THREAD_H_ | 61 #endif // BASE_TEST_TEST_IO_THREAD_H_ |
OLD | NEW |