| 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 "base/test/test_io_thread.h" | 5 #include "base/test/test_io_thread.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/logging.h" |
| 8 #include "base/callback.h" | |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 void PostTaskAndWaitHelper(base::WaitableEvent* event, | |
| 14 const base::Closure& task) { | |
| 15 task.Run(); | |
| 16 event->Signal(); | |
| 17 } | |
| 18 | |
| 19 } // namespace | |
| 20 | 8 |
| 21 namespace base { | 9 namespace base { |
| 22 | 10 |
| 23 TestIOThread::TestIOThread(Mode mode) | 11 TestIOThread::TestIOThread(Mode mode) |
| 24 : io_thread_("test_io_thread"), io_thread_started_(false) { | 12 : io_thread_("test_io_thread"), io_thread_started_(false) { |
| 25 switch (mode) { | 13 switch (mode) { |
| 26 case kAutoStart: | 14 case kAutoStart: |
| 27 Start(); | 15 Start(); |
| 28 return; | 16 return; |
| 29 case kManualStart: | 17 case kManualStart: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 47 // Note: It's okay to call |Stop()| even if the thread isn't running. | 35 // Note: It's okay to call |Stop()| even if the thread isn't running. |
| 48 io_thread_.Stop(); | 36 io_thread_.Stop(); |
| 49 io_thread_started_ = false; | 37 io_thread_started_ = false; |
| 50 } | 38 } |
| 51 | 39 |
| 52 void TestIOThread::PostTask(const tracked_objects::Location& from_here, | 40 void TestIOThread::PostTask(const tracked_objects::Location& from_here, |
| 53 const base::Closure& task) { | 41 const base::Closure& task) { |
| 54 task_runner()->PostTask(from_here, task); | 42 task_runner()->PostTask(from_here, task); |
| 55 } | 43 } |
| 56 | 44 |
| 57 void TestIOThread::PostTaskAndWait(const tracked_objects::Location& from_here, | |
| 58 const base::Closure& task) { | |
| 59 base::WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 60 WaitableEvent::InitialState::NOT_SIGNALED); | |
| 61 task_runner()->PostTask(from_here, | |
| 62 base::Bind(&PostTaskAndWaitHelper, &event, task)); | |
| 63 event.Wait(); | |
| 64 } | |
| 65 | |
| 66 } // namespace base | 45 } // namespace base |
| OLD | NEW |