blob: 0c00aa1038b73b6a07686897e5aed6212566992a [file] [log] [blame]
Sami Kyostila73d41c82017-11-24 18:38:46 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
18#define PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
19
20#include "base/scoped_file.h"
21#include "base/task_runner.h"
22#include "base/thread_checker.h"
23
24#include <poll.h>
25#include <chrono>
26#include <map>
27#include <mutex>
28#include <queue>
29
30#include <android/looper.h>
31
32namespace perfetto {
33namespace base {
34
35// Runs a task runner on a thread owned by an Android Looper (ALooper).
36class AndroidTaskRunner : public TaskRunner {
37 public:
Sami Kyostila7ae34482017-11-28 13:17:07 +000038 AndroidTaskRunner();
Sami Kyostila73d41c82017-11-24 18:38:46 +000039 ~AndroidTaskRunner() override;
40
41 // The following methods are only used in cases where the caller wants to take
42 // ownership of the current thread (e.g., tests and standalone tools).
Sami Kyostila7ae34482017-11-28 13:17:07 +000043 // Normally the Android Framework runs the event loop on the thread. Run() can
44 // only be called from the main thread but Quit() can be called from any
45 // thread.
Sami Kyostila73d41c82017-11-24 18:38:46 +000046 void Run();
47 void Quit();
48
49 // Checks whether there are any pending immediate tasks to run. Note that
Sami Kyostila7ae34482017-11-28 13:17:07 +000050 // delayed tasks don't count even if they are due to run. Can only be called
51 // from the main thread.
Sami Kyostila73d41c82017-11-24 18:38:46 +000052 bool IsIdleForTesting();
53
54 // TaskRunner implementation:
55 void PostTask(std::function<void()>) override;
56 void PostDelayedTask(std::function<void()>, int delay_ms) override;
57 void AddFileDescriptorWatch(int fd, std::function<void()>) override;
58 void RemoveFileDescriptorWatch(int fd) override;
59
60 private:
61 class TimePoint : public timespec {
62 public:
63 TimePoint() { tv_sec = tv_nsec = 0; }
64
65 bool operator<(const TimePoint& other) const {
66 if (tv_sec == other.tv_sec)
67 return tv_nsec < other.tv_nsec;
68 return tv_sec < other.tv_sec;
69 }
70
Sami Kyostila7ae34482017-11-28 13:17:07 +000071 explicit operator bool() const { return tv_sec || tv_nsec; }
Sami Kyostila73d41c82017-11-24 18:38:46 +000072
Sami Kyostila7ae34482017-11-28 13:17:07 +000073 TimePoint AdvanceByMs(time_t ms) {
74 const time_t kSecondsPerNs = 1000000000;
75 PERFETTO_DCHECK(tv_nsec < kSecondsPerNs);
Sami Kyostila73d41c82017-11-24 18:38:46 +000076 TimePoint result(*this);
Sami Kyostila7ae34482017-11-28 13:17:07 +000077 time_t seconds = ms / 1000;
Sami Kyostila73d41c82017-11-24 18:38:46 +000078 result.tv_sec += seconds;
79 ms -= seconds * 1000;
80 result.tv_nsec += ms * 1000 * 1000;
Sami Kyostila7ae34482017-11-28 13:17:07 +000081 if (result.tv_nsec >= kSecondsPerNs) {
Sami Kyostila73d41c82017-11-24 18:38:46 +000082 result.tv_sec++;
Sami Kyostila7ae34482017-11-28 13:17:07 +000083 result.tv_nsec -= kSecondsPerNs;
Sami Kyostila73d41c82017-11-24 18:38:46 +000084 }
85 return result;
86 }
87 };
88 TimePoint GetTime() const;
89
90 bool OnFileDescriptorEvent(int signalled_fd, int events);
91 void RunImmediateTask();
92 void RunDelayedTask();
93
94 void GetNextDelayedTaskRunTimeLocked(struct itimerspec* runtime);
95
96 void ScheduleImmediateWakeUp();
97 void ScheduleDelayedWakeUp(const TimePoint& time);
98
Sami Kyostila7ae34482017-11-28 13:17:07 +000099 ALooper* const looper_;
Sami Kyostila73d41c82017-11-24 18:38:46 +0000100 ScopedFile immediate_event_;
101 ScopedFile delayed_timer_;
102
103 ThreadChecker thread_checker_;
104
105 // --- Begin lock-protected members.
106 std::mutex lock_;
Sami Kyostila7ae34482017-11-28 13:17:07 +0000107 // Note: std::deque allocates blocks of 4k in some implementations. Consider
108 // another data structure if we end up having many task runner instances.
Sami Kyostila73d41c82017-11-24 18:38:46 +0000109 std::deque<std::function<void()>> immediate_tasks_;
110 std::multimap<TimePoint, std::function<void()>> delayed_tasks_;
111 std::map<int, std::function<void()>> watch_tasks_;
112 bool quit_ = false;
113 // --- End lock-protected members.
114};
115
116} // namespace base
117} // namespace perfetto
118
119#endif // PERFETTO_BASE_ANDROID_TASK_RUNNER_H_