1 // Copyright 2017 The Fuchsia 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 <zircon/syscalls.h>
6 
7 #include <stdio.h>
8 
9 #include <lib/async-loop/cpp/loop.h>
10 #include <lib/async/cpp/task.h>
11 #include <lib/async/cpp/time.h>
12 #include <trace-provider/provider.h>
13 #include <trace/event.h>
14 
main(int argc,char ** argv)15 int main(int argc, char** argv) {
16     async::Loop loop(&kAsyncLoopConfigNoAttachToThread);
17     trace::TraceProvider provider(loop.dispatcher());
18 
19     puts("Doing work for 30 seconds...");
20 
21     zx::time start_time = async::Now(loop.dispatcher());
22     zx::time quit_time = start_time + zx::sec(30);
23 
24     int iteration = 0;
25     async::TaskClosure task([&loop, &task, &iteration, quit_time] {
26         TRACE_DURATION("example", "Doing Work!", "iteration", ++iteration);
27 
28         // Simulate some kind of workload.
29         puts("Doing work!");
30         zx::nanosleep(zx::deadline_after(zx::msec(500)));
31 
32         // Stop if quitting.
33         zx::time now = async::Now(loop.dispatcher());
34         if (now > quit_time) {
35             loop.Quit();
36             return;
37         }
38 
39         // Schedule more work in a little bit.
40         task.PostForTime(loop.dispatcher(), now + zx::msec(200));
41     });
42     task.PostForTime(loop.dispatcher(), start_time);
43 
44     loop.Run();
45 
46     puts("Finished.");
47     return 0;
48 }
49