| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_EDK_TEST_TEST_IO_THREAD_H_ | |
| 6 #define MOJO_EDK_TEST_TEST_IO_THREAD_H_ | |
| 7 | |
| 8 #include "base/callback_forward.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/task_runner.h" | |
| 11 #include "base/threading/thread.h" | |
| 12 #include "mojo/public/cpp/system/macros.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace test { | |
| 16 | |
| 17 // Class to help create/run threads with I/O |MessageLoop|s in tests. | |
| 18 class TestIOThread { | |
| 19 public: | |
| 20 enum class StartMode { AUTO, MANUAL }; | |
| 21 explicit TestIOThread(StartMode start_mode); | |
| 22 // Stops the I/O thread if necessary. | |
| 23 ~TestIOThread(); | |
| 24 | |
| 25 // |Start()|/|Stop()| should only be called from the main (creation) thread. | |
| 26 // After |Stop()|, |Start()| may be called again to start a new I/O thread. | |
| 27 // |Stop()| may be called even when the I/O thread is not started. | |
| 28 void Start(); | |
| 29 void Stop(); | |
| 30 | |
| 31 // Post |task| to the IO thread. | |
| 32 void PostTask(const base::Closure& task); | |
| 33 // Posts |task| to the IO-thread with an WaitableEvent associated blocks on | |
| 34 // it until the posted |task| is executed, then returns. | |
| 35 void PostTaskAndWait(const base::Closure& task); | |
| 36 | |
| 37 base::MessageLoopForIO* message_loop() { | |
| 38 return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop()); | |
| 39 } | |
| 40 | |
| 41 scoped_refptr<base::SingleThreadTaskRunner> task_runner() { | |
| 42 return message_loop()->task_runner(); | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 base::Thread io_thread_; | |
| 47 bool io_thread_started_; | |
| 48 | |
| 49 MOJO_DISALLOW_COPY_AND_ASSIGN(TestIOThread); | |
| 50 }; | |
| 51 | |
| 52 } // namespace test | |
| 53 } // namespace mojo | |
| 54 | |
| 55 #endif // MOJO_EDK_TEST_TEST_IO_THREAD_H_ | |
| OLD | NEW |