blob: cb2eb2269b47fd381dc57fccfb4584a3878d2854 [file] [log] [blame]
Alex Deymoef3955a2015-06-09 10:14:01 -07001// 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
Alex Deymo106edd12015-06-18 20:15:11 -07007#include <base/location.h>
Alex Deymoef3955a2015-06-09 10:14:01 -07008#include <chromeos/bind_lambda.h>
9
10namespace chromeos {
11
12void 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 Deymo106edd12015-06-18 20:15:11 -070018 FROM_HERE,
19 base::Bind([&timeout_called]() { timeout_called = true; }),
20 timeout);
Alex Deymoef3955a2015-06-09 10:14:01 -070021 while (!timeout_called && (terminate.is_null() || !terminate.Run()))
22 loop->RunOnce(true);
23
24 if (!timeout_called)
25 loop->CancelTask(task_id);
26}
27
28int MessageLoopRunMaxIterations(MessageLoop* loop, int iterations) {
29 int result;
30 for (result = 0; result < iterations && loop->RunOnce(false); result++) {}
31 return result;
32}
33
34} // namespace chromeos