blob: 9ebe865574579b19d1daa48091f21ceb264bbbfe [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
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -07005#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoef3955a2015-06-09 10:14:01 -07006
Alex Deymo106edd12015-06-18 20:15:11 -07007#include <base/location.h>
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -07008#include <brillo/bind_lambda.h>
Alex Deymoef3955a2015-06-09 10:14:01 -07009
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070010namespace brillo {
Alex Deymoef3955a2015-06-09 10:14:01 -070011
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,
Luis Hector Chavezc7b01772016-07-18 16:02:44 -070019 base::Bind([](bool* timeout_called) { *timeout_called = true; },
20 base::Unretained(&timeout_called)),
Alex Deymo106edd12015-06-18 20:15:11 -070021 timeout);
Alex Deymoef3955a2015-06-09 10:14:01 -070022 while (!timeout_called && (terminate.is_null() || !terminate.Run()))
23 loop->RunOnce(true);
24
25 if (!timeout_called)
26 loop->CancelTask(task_id);
27}
28
29int MessageLoopRunMaxIterations(MessageLoop* loop, int iterations) {
30 int result;
31 for (result = 0; result < iterations && loop->RunOnce(false); result++) {}
32 return result;
33}
34
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070035} // namespace brillo