Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium OS 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 <chromeos/message_loops/message_loop_utils.h> |
| 6 | |
| 7 | #include <chromeos/bind_lambda.h> |
| 8 | |
| 9 | namespace chromeos { |
| 10 | |
| 11 | void MessageLoopRunUntil( |
| 12 | MessageLoop* loop, |
| 13 | base::TimeDelta timeout, |
| 14 | base::Callback<bool()> terminate) { |
| 15 | bool timeout_called = false; |
| 16 | MessageLoop::TaskId task_id = loop->PostDelayedTask( |
| 17 | base::Bind([&timeout_called]() { timeout_called = true; }), |
| 18 | timeout); |
| 19 | while (!timeout_called && (terminate.is_null() || !terminate.Run())) |
| 20 | loop->RunOnce(true); |
| 21 | |
| 22 | if (!timeout_called) |
| 23 | loop->CancelTask(task_id); |
| 24 | } |
| 25 | |
| 26 | int MessageLoopRunMaxIterations(MessageLoop* loop, int iterations) { |
| 27 | int result; |
| 28 | for (result = 0; result < iterations && loop->RunOnce(false); result++) {} |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | } // namespace chromeos |