| 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 #include "mojo/edk/test/test_io_thread.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/synchronization/waitable_event.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace test { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 void PostTaskAndWaitHelper(base::WaitableEvent* event, | |
| 18 const base::Closure& task) { | |
| 19 task.Run(); | |
| 20 event->Signal(); | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 TestIOThread::TestIOThread(StartMode start_mode) | |
| 26 : io_thread_("test_io_thread"), io_thread_started_(false) { | |
| 27 switch (start_mode) { | |
| 28 case StartMode::AUTO: | |
| 29 Start(); | |
| 30 return; | |
| 31 case StartMode::MANUAL: | |
| 32 return; | |
| 33 } | |
| 34 CHECK(false) << "Invalid mode"; | |
| 35 } | |
| 36 | |
| 37 TestIOThread::~TestIOThread() { | |
| 38 Stop(); | |
| 39 } | |
| 40 | |
| 41 void TestIOThread::Start() { | |
| 42 CHECK(!io_thread_started_); | |
| 43 io_thread_started_ = true; | |
| 44 CHECK(io_thread_.StartWithOptions( | |
| 45 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | |
| 46 } | |
| 47 | |
| 48 void TestIOThread::Stop() { | |
| 49 // Note: It's okay to call |Stop()| even if the thread isn't running. | |
| 50 io_thread_.Stop(); | |
| 51 io_thread_started_ = false; | |
| 52 } | |
| 53 | |
| 54 void TestIOThread::PostTask(const base::Closure& task) { | |
| 55 task_runner()->PostTask(tracked_objects::Location(), task); | |
| 56 } | |
| 57 | |
| 58 void TestIOThread::PostTaskAndWait(const base::Closure& task) { | |
| 59 base::WaitableEvent event(false, false); | |
| 60 task_runner()->PostTask(tracked_objects::Location(), | |
| 61 base::Bind(&PostTaskAndWaitHelper, &event, task)); | |
| 62 event.Wait(); | |
| 63 } | |
| 64 | |
| 65 } // namespace test | |
| 66 } // namespace mojo | |
| OLD | NEW |