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