| 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 MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ | 5 #ifndef MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ |
| 6 #define MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ | 6 #define MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ |
| 7 | 7 |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <memory> |
| 9 | 10 |
| 10 #include "base/threading/thread.h" | |
| 11 #include "mojo/edk/base_edk/platform_task_runner_impl.h" | |
| 12 #include "mojo/edk/platform/task_runner.h" | 11 #include "mojo/edk/platform/task_runner.h" |
| 13 #include "mojo/edk/util/ref_ptr.h" | 12 #include "mojo/edk/util/ref_ptr.h" |
| 14 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
| 15 | 14 |
| 16 namespace mojo { | 15 namespace mojo { |
| 16 |
| 17 namespace platform { |
| 18 class PlatformHandleWatcher; |
| 19 class Thread; |
| 20 } |
| 21 |
| 17 namespace system { | 22 namespace system { |
| 18 namespace test { | 23 namespace test { |
| 19 | 24 |
| 20 // Class to help create/run threads with I/O |MessageLoop|s in tests. | 25 // Class to help create/run threads with I/O |MessageLoop|s in tests. |
| 21 class TestIOThread final { | 26 class TestIOThread final { |
| 22 public: | 27 public: |
| 23 enum class StartMode { AUTO, MANUAL }; | 28 enum class StartMode { AUTO, MANUAL }; |
| 24 explicit TestIOThread(StartMode start_mode); | 29 explicit TestIOThread(StartMode start_mode); |
| 25 // Stops the I/O thread if necessary. | 30 // Stops the I/O thread if necessary. |
| 26 ~TestIOThread(); | 31 ~TestIOThread(); |
| 27 | 32 |
| 28 // |Start()|/|Stop()| should only be called from the main (creation) thread. | 33 // |Start()|/|Stop()| should only be called from the main (creation) thread. |
| 29 // After |Stop()|, |Start()| may be called again to start a new I/O thread. | 34 // After |Stop()|, |Start()| may be called again to start a new I/O thread. |
| 30 // |Stop()| may be called even when the I/O thread is not started. | 35 // |Stop()| may be called even when the I/O thread is not started. |
| 31 void Start(); | 36 void Start(); |
| 32 void Stop(); | 37 void Stop(); |
| 33 | 38 |
| 34 // Returns true if called on the I/O thread with the message loop running. | 39 // Returns true if called on the I/O thread with the message loop running. |
| 35 // (This may be called on any thread.) | 40 // (This may be called on any thread.) |
| 36 bool IsCurrentAndRunning() const; | 41 bool IsCurrentAndRunning() const; |
| 37 | 42 |
| 38 // Posts |task| to the I/O thread. | 43 // Posts |task| to the I/O thread. |
| 39 void PostTask(std::function<void()>&& task); | 44 void PostTask(std::function<void()>&& task); |
| 40 // Posts |task| to the I/O thread, blocking the calling thread until the | 45 // Posts |task| to the I/O thread, blocking the calling thread until the |
| 41 // posted task is executed (note the deadlock risk!). | 46 // posted task is executed (note the deadlock risk!). |
| 42 void PostTaskAndWait(std::function<void()>&& task); | 47 void PostTaskAndWait(std::function<void()>&& task); |
| 43 | 48 |
| 44 base::MessageLoopForIO* message_loop() { | |
| 45 return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop()); | |
| 46 } | |
| 47 | |
| 48 const util::RefPtr<platform::TaskRunner>& task_runner() const { | 49 const util::RefPtr<platform::TaskRunner>& task_runner() const { |
| 49 return io_task_runner_; | 50 return io_task_runner_; |
| 50 } | 51 } |
| 51 | 52 |
| 53 platform::PlatformHandleWatcher* platform_handle_watcher() const { |
| 54 return io_platform_handle_watcher_; |
| 55 } |
| 56 |
| 52 private: | 57 private: |
| 53 base::Thread io_thread_; | 58 std::unique_ptr<platform::Thread> io_thread_; |
| 54 bool io_thread_started_; | |
| 55 util::RefPtr<platform::TaskRunner> io_task_runner_; | 59 util::RefPtr<platform::TaskRunner> io_task_runner_; |
| 60 platform::PlatformHandleWatcher* io_platform_handle_watcher_; |
| 56 | 61 |
| 57 MOJO_DISALLOW_COPY_AND_ASSIGN(TestIOThread); | 62 MOJO_DISALLOW_COPY_AND_ASSIGN(TestIOThread); |
| 58 }; | 63 }; |
| 59 | 64 |
| 60 } // namespace test | 65 } // namespace test |
| 61 } // namespace system | 66 } // namespace system |
| 62 } // namespace mojo | 67 } // namespace mojo |
| 63 | 68 |
| 64 #endif // MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ | 69 #endif // MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_ |
| OLD | NEW |