blob: fd84b16c6c072e0c7d96b2b04df650705b653c0b [file] [log] [blame]
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -07001// 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 Luud892e472015-10-29 09:16:40 -07005#ifndef MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_
6#define MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -07007
Viet-Trung Luu2d07e272015-12-02 12:47:51 -08008#include <functional>
Viet-Trung Luu9b8ded62015-12-10 16:24:22 -08009#include <memory>
Viet-Trung Luu2d07e272015-12-02 12:47:51 -080010
Viet-Trung Luu6bcf7112015-11-23 12:46:55 -080011#include "mojo/edk/platform/task_runner.h"
Viet-Trung Luu1da183c2015-11-18 15:41:35 -080012#include "mojo/edk/util/ref_ptr.h"
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070013#include "mojo/public/cpp/system/macros.h"
14
15namespace mojo {
Viet-Trung Luu9b8ded62015-12-10 16:24:22 -080016
17namespace platform {
18class PlatformHandleWatcher;
19class Thread;
20}
21
Viet-Trung Luud892e472015-10-29 09:16:40 -070022namespace system {
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070023namespace test {
24
25// Class to help create/run threads with I/O |MessageLoop|s in tests.
Viet-Trung Luu49824cb2015-11-11 12:47:10 -080026class TestIOThread final {
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070027 public:
Viet-Trung Luu50b0d832015-09-23 16:38:00 -070028 enum class StartMode { AUTO, MANUAL };
29 explicit TestIOThread(StartMode start_mode);
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070030 // 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 Luuabe8f822015-11-24 14:18:17 -080039 // 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 Luu9a32daa2015-11-06 15:24:41 -080043 // Posts |task| to the I/O thread.
Viet-Trung Luu2d07e272015-12-02 12:47:51 -080044 void PostTask(std::function<void()>&& task);
Viet-Trung Luu9a32daa2015-11-06 15:24:41 -080045 // Posts |task| to the I/O thread, blocking the calling thread until the
46 // posted task is executed (note the deadlock risk!).
Viet-Trung Luu2d07e272015-12-02 12:47:51 -080047 void PostTaskAndWait(std::function<void()>&& task);
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070048
Viet-Trung Luuabe8f822015-11-24 14:18:17 -080049 const util::RefPtr<platform::TaskRunner>& task_runner() const {
Viet-Trung Luu1da183c2015-11-18 15:41:35 -080050 return io_task_runner_;
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070051 }
52
Viet-Trung Luu9b8ded62015-12-10 16:24:22 -080053 platform::PlatformHandleWatcher* platform_handle_watcher() const {
54 return io_platform_handle_watcher_;
55 }
56
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070057 private:
Viet-Trung Luu9b8ded62015-12-10 16:24:22 -080058 std::unique_ptr<platform::Thread> io_thread_;
Viet-Trung Luu6bcf7112015-11-23 12:46:55 -080059 util::RefPtr<platform::TaskRunner> io_task_runner_;
Viet-Trung Luu9b8ded62015-12-10 16:24:22 -080060 platform::PlatformHandleWatcher* io_platform_handle_watcher_;
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070061
62 MOJO_DISALLOW_COPY_AND_ASSIGN(TestIOThread);
63};
64
65} // namespace test
Viet-Trung Luud892e472015-10-29 09:16:40 -070066} // namespace system
Viet-Trung Luuc19ac4b2015-09-14 16:31:33 -070067} // namespace mojo
68
Viet-Trung Luud892e472015-10-29 09:16:40 -070069#endif // MOJO_EDK_SYSTEM_TEST_TEST_IO_THREAD_H_