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 INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_ 18 #define INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_ 19 20 #include "perfetto/base/event.h" 21 #include "perfetto/base/scoped_file.h" 22 #include "perfetto/base/task_runner.h" 23 #include "perfetto/base/thread_checker.h" 24 #include "perfetto/base/time.h" 25 26 #include <poll.h> 27 #include <chrono> 28 #include <map> 29 #include <mutex> 30 #include <queue> 31 32 #include <android/looper.h> 33 34 namespace perfetto { 35 namespace base { 36 37 // Runs a task runner on a thread owned by an Android Looper (ALooper). 38 class AndroidTaskRunner : public TaskRunner { 39 public: 40 AndroidTaskRunner(); 41 ~AndroidTaskRunner() override; 42 43 // The following methods are only used in cases where the caller wants to take 44 // ownership of the current thread (e.g., tests and standalone tools). 45 // Normally the Android Framework runs the event loop on the thread. Run() can 46 // only be called from the main thread but Quit() can be called from any 47 // thread. 48 void Run(); 49 void Quit(); 50 51 // Checks whether there are any pending immediate tasks to run. Note that 52 // delayed tasks don't count even if they are due to run. Can only be called 53 // from the main thread. 54 bool IsIdleForTesting(); 55 56 // TaskRunner implementation: 57 void PostTask(std::function<void()>) override; 58 void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override; 59 void AddFileDescriptorWatch(int fd, std::function<void()>) override; 60 void RemoveFileDescriptorWatch(int fd) override; 61 bool RunsTasksOnCurrentThread() const override; 62 63 private: 64 bool OnFileDescriptorEvent(int signalled_fd, int events); 65 void RunImmediateTask(); 66 void RunDelayedTask(); 67 68 void GetNextDelayedTaskRunTimeLocked(struct itimerspec* runtime); 69 70 void ScheduleImmediateWakeUp(); 71 void ScheduleDelayedWakeUp(TimeMillis time); 72 73 ALooper* const looper_; 74 Event immediate_event_; 75 ScopedFile delayed_timer_; 76 77 ThreadChecker thread_checker_; 78 79 // --- Begin lock-protected members. 80 std::mutex lock_; 81 // Note: std::deque allocates blocks of 4k in some implementations. Consider 82 // another data structure if we end up having many task runner instances. 83 std::deque<std::function<void()>> immediate_tasks_; 84 std::multimap<TimeMillis, std::function<void()>> delayed_tasks_; 85 std::map<int, std::function<void()>> watch_tasks_; 86 bool quit_ = false; 87 // --- End lock-protected members. 88 }; 89 90 } // namespace base 91 } // namespace perfetto 92 93 #endif // INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_ 94