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