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 <brillo/message_loops/message_loop_utils.h> 6 7 #include <base/bind.h> 8 #include <base/location.h> 9 10 namespace brillo { 11 MessageLoopRunUntil(MessageLoop * loop,base::TimeDelta timeout,base::RepeatingCallback<bool ()> terminate)12void MessageLoopRunUntil(MessageLoop* loop, 13 base::TimeDelta timeout, 14 base::RepeatingCallback<bool()> terminate) { 15 bool timeout_called = false; 16 MessageLoop::TaskId task_id = loop->PostDelayedTask( 17 FROM_HERE, 18 base::BindOnce([](bool* timeout_called) { *timeout_called = true; }, 19 &timeout_called), 20 timeout); 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 MessageLoopRunMaxIterations(MessageLoop * loop,int iterations)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 brillo 35