Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | namespace perfetto { |
| 33 | namespace base { |
| 34 | |
| 35 | // Runs a task runner on a thread owned by an Android Looper (ALooper). |
| 36 | class AndroidTaskRunner : public TaskRunner { |
| 37 | public: |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 38 | AndroidTaskRunner(); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 39 | ~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 Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 43 | // 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 Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 46 | void Run(); |
| 47 | void Quit(); |
| 48 | |
| 49 | // Checks whether there are any pending immediate tasks to run. Note that |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 50 | // delayed tasks don't count even if they are due to run. Can only be called |
| 51 | // from the main thread. |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 52 | 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 Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 71 | explicit operator bool() const { return tv_sec || tv_nsec; } |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 72 | |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 73 | TimePoint AdvanceByMs(time_t ms) { |
| 74 | const time_t kSecondsPerNs = 1000000000; |
| 75 | PERFETTO_DCHECK(tv_nsec < kSecondsPerNs); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 76 | TimePoint result(*this); |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 77 | time_t seconds = ms / 1000; |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 78 | result.tv_sec += seconds; |
| 79 | ms -= seconds * 1000; |
| 80 | result.tv_nsec += ms * 1000 * 1000; |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 81 | if (result.tv_nsec >= kSecondsPerNs) { |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 82 | result.tv_sec++; |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 83 | result.tv_nsec -= kSecondsPerNs; |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 84 | } |
| 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 Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 99 | ALooper* const looper_; |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 100 | ScopedFile immediate_event_; |
| 101 | ScopedFile delayed_timer_; |
| 102 | |
| 103 | ThreadChecker thread_checker_; |
| 104 | |
| 105 | // --- Begin lock-protected members. |
| 106 | std::mutex lock_; |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 107 | // 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 Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 109 | 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_ |