Home | History | Annotate | Download | only in message_loops
      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/location.h>
      8 #include <brillo/bind_lambda.h>
      9 
     10 namespace brillo {
     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(
     18       FROM_HERE,
     19       base::Bind([](bool* timeout_called) { *timeout_called = true; },
     20                  base::Unretained(&timeout_called)),
     21       timeout);
     22   while (!timeout_called && (terminate.is_null() || !terminate.Run()))
     23     loop->RunOnce(true);
     24 
     25   if (!timeout_called)
     26     loop->CancelTask(task_id);
     27 }
     28 
     29 int MessageLoopRunMaxIterations(MessageLoop* loop, int iterations) {
     30   int result;
     31   for (result = 0; result < iterations && loop->RunOnce(false); result++) {}
     32   return result;
     33 }
     34 
     35 }  // namespace brillo
     36