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 #include "mojo/edk/system/test/test_io_thread.h" | 5 #include "mojo/edk/system/test/test_io_thread.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/logging.h" |
| 10 #include "mojo/edk/platform/io_thread.h" |
| 11 #include "mojo/edk/platform/thread.h" |
9 #include "mojo/edk/util/waitable_event.h" | 12 #include "mojo/edk/util/waitable_event.h" |
10 | 13 |
| 14 using mojo::platform::CreateAndStartIOThread; |
11 using mojo::util::AutoResetWaitableEvent; | 15 using mojo::util::AutoResetWaitableEvent; |
12 using mojo::util::MakeRefCounted; | 16 using mojo::util::MakeRefCounted; |
13 | 17 |
14 namespace mojo { | 18 namespace mojo { |
15 namespace system { | 19 namespace system { |
16 namespace test { | 20 namespace test { |
17 | 21 |
18 TestIOThread::TestIOThread(StartMode start_mode) | 22 TestIOThread::TestIOThread(StartMode start_mode) |
19 : io_thread_("test_io_thread"), io_thread_started_(false) { | 23 : io_platform_handle_watcher_(nullptr) { |
20 switch (start_mode) { | 24 switch (start_mode) { |
21 case StartMode::AUTO: | 25 case StartMode::AUTO: |
22 Start(); | 26 Start(); |
23 return; | 27 return; |
24 case StartMode::MANUAL: | 28 case StartMode::MANUAL: |
25 return; | 29 return; |
26 } | 30 } |
27 CHECK(false) << "Invalid mode"; | 31 CHECK(false) << "Invalid mode"; |
28 } | 32 } |
29 | 33 |
30 TestIOThread::~TestIOThread() { | 34 TestIOThread::~TestIOThread() { |
31 Stop(); | 35 Stop(); |
32 } | 36 } |
33 | 37 |
34 void TestIOThread::Start() { | 38 void TestIOThread::Start() { |
35 CHECK(!io_thread_started_); | 39 CHECK(!io_thread_); |
36 io_thread_started_ = true; | 40 io_thread_ = |
37 CHECK(io_thread_.StartWithOptions( | 41 CreateAndStartIOThread(&io_task_runner_, &io_platform_handle_watcher_); |
38 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | |
39 io_task_runner_ = MakeRefCounted<base_edk::PlatformTaskRunnerImpl>( | |
40 message_loop()->task_runner()); | |
41 } | 42 } |
42 | 43 |
43 void TestIOThread::Stop() { | 44 void TestIOThread::Stop() { |
44 // Note: It's okay to call |Stop()| even if the thread isn't running. | 45 if (!io_thread_) |
45 io_thread_.Stop(); | 46 return; // Nothing to do. |
46 io_thread_started_ = false; | 47 |
| 48 io_thread_->Stop(); |
| 49 io_thread_.reset(); |
| 50 io_task_runner_ = nullptr; |
| 51 io_platform_handle_watcher_ = nullptr; |
47 } | 52 } |
48 | 53 |
49 bool TestIOThread::IsCurrentAndRunning() const { | 54 bool TestIOThread::IsCurrentAndRunning() const { |
50 return base::MessageLoop::current() == io_thread_.message_loop() && | 55 return io_task_runner_->RunsTasksOnCurrentThread(); |
51 io_thread_.message_loop()->is_running(); | |
52 } | 56 } |
53 | 57 |
54 void TestIOThread::PostTask(std::function<void()>&& task) { | 58 void TestIOThread::PostTask(std::function<void()>&& task) { |
55 io_task_runner_->PostTask(std::move(task)); | 59 io_task_runner_->PostTask(std::move(task)); |
56 } | 60 } |
57 | 61 |
58 void TestIOThread::PostTaskAndWait(std::function<void()>&& task) { | 62 void TestIOThread::PostTaskAndWait(std::function<void()>&& task) { |
59 AutoResetWaitableEvent event; | 63 AutoResetWaitableEvent event; |
60 io_task_runner_->PostTask([&task, &event]() { | 64 io_task_runner_->PostTask([&task, &event]() { |
61 task(); | 65 task(); |
62 event.Signal(); | 66 event.Signal(); |
63 }); | 67 }); |
64 event.Wait(); | 68 event.Wait(); |
65 } | 69 } |
66 | 70 |
67 } // namespace test | 71 } // namespace test |
68 } // namespace system | 72 } // namespace system |
69 } // namespace mojo | 73 } // namespace mojo |
OLD | NEW |