1 // Copyright 2014 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <list> // for list<>::iterator, list 18 #include <memory> // for unique_ptr 19 #include <mutex> // for mutex 20 #include <string_view> 21 #include <unordered_map> // for unordered_map 22 #include <unordered_set> // for unordered_set 23 24 #include "aemu/base/async/Looper.h" // for Looper::ClockType, Looper 25 26 namespace android { 27 namespace base { 28 class SocketWaiter; 29 class Stream; 30 31 // Default looper implementation based on select(). To make sure all timers and 32 // FD watches execute, run its runWithDeadlineMs() explicitly. 33 class DefaultLooper : public Looper { 34 public: 35 DefaultLooper(); 36 37 ~DefaultLooper() override; 38 name()39 std::string_view name() const override { return "Generic"; } 40 41 Duration nowMs(ClockType clockType = ClockType::kHost) override; 42 43 DurationNs nowNs(ClockType clockType = ClockType::kHost) override; 44 45 void forceQuit() override; 46 47 // 48 // F D W A T C H E S 49 // 50 class FdWatch : public Looper::FdWatch { 51 public: 52 FdWatch(DefaultLooper* looper, int fd, Callback callback, void* opaque); 53 54 DefaultLooper* defaultLooper() const; 55 56 ~FdWatch() override; 57 58 void addEvents(unsigned events) override; 59 60 void removeEvents(unsigned events) override; 61 62 unsigned poll() const override; 63 64 // Return true iff this FdWatch is pending execution. 65 bool isPending() const; 66 67 // Add this FdWatch to a pending queue. 68 void setPending(unsigned events); 69 70 // Remove this FdWatch from a pending queue. 71 void clearPending(); 72 73 // Fire the watch, i.e. invoke the callback with the right 74 // parameters. 75 void fire(); 76 77 private: 78 unsigned mWantedEvents; 79 unsigned mLastEvents; 80 bool mPending; 81 }; 82 83 void addFdWatch(FdWatch* watch); 84 85 void delFdWatch(FdWatch* watch); 86 87 void addPendingFdWatch(FdWatch* watch); 88 89 void delPendingFdWatch(FdWatch* watch); 90 91 void updateFdWatch(int fd, unsigned wantedEvents); 92 93 Looper::FdWatch* createFdWatch(int fd, 94 Looper::FdWatch::Callback callback, 95 void* opaque) override; 96 97 // 98 // T I M E R S 99 // 100 101 class Timer : public Looper::Timer { 102 public: 103 Timer(DefaultLooper* looper, 104 Callback callback, 105 void* opaque, 106 ClockType clock); 107 108 DefaultLooper* defaultLooper() const; 109 110 ~Timer() override; 111 112 Duration deadline() const; 113 114 void startRelative(Duration deadlineMs) override; 115 116 void startAbsolute(Duration deadlineMs) override; 117 118 void stop() override; 119 120 bool isActive() const override; 121 122 void setPending(); 123 124 void clearPending(); 125 126 void fire(); 127 128 void save(Stream* stream) const override; 129 130 void load(Stream* stream) override; 131 132 private: 133 Duration mDeadline; 134 bool mPending; 135 }; 136 137 void addTimer(Timer* timer); 138 139 void delTimer(Timer* timer); 140 141 void enableTimer(Timer* timer); 142 143 void disableTimer(Timer* timer); 144 145 void addPendingTimer(Timer* timer); 146 147 void delPendingTimer(Timer* timer); 148 149 Looper::Timer* createTimer(Looper::Timer::Callback callback, 150 void* opaque, 151 ClockType clock) override; 152 153 // 154 // Tasks 155 // 156 class Task : public Looper::Task { 157 public: 158 Task(Looper* looper, Looper::Task::Callback&& callback, 159 bool selfDeleting = false); 160 161 ~Task(); 162 163 void schedule() override; 164 void cancel() override; 165 void run(); 166 167 private: 168 const bool mSelfDeleting; 169 }; 170 171 void addTask(Task* task); 172 void delTask(Task* task); 173 174 TaskPtr createTask(TaskCallback&& callback) override; 175 void scheduleCallback(TaskCallback&& callback) override; 176 177 // 178 // M A I N L O O P 179 // 180 int runWithDeadlineMs(Duration deadlineMs) override; 181 182 typedef std::list<Timer*> TimerList; 183 typedef std::unordered_map<Timer*, TimerList::iterator> TimerSet; 184 185 typedef std::list<FdWatch*> FdWatchList; 186 typedef std::unordered_map<FdWatch*, FdWatchList::iterator> FdWatchSet; 187 188 protected: 189 bool runOneIterationWithDeadlineMs(Duration deadlineMs); 190 191 std::unique_ptr<SocketWaiter> mWaiter; 192 FdWatchSet mFdWatches; // Set of all fd watches. 193 FdWatchList mPendingFdWatches; // Queue of pending fd watches. 194 195 TimerSet mTimers; // Set of all timers. 196 TimerList mActiveTimers; // Sorted list of active timers. 197 TimerList mPendingTimers; // Sorted list of pending timers. 198 199 using TaskSet = std::unordered_set<Task*>; 200 TaskSet mScheduledTasks; 201 std::mutex mScheduledTasksAccess; 202 203 bool mForcedExit = false; 204 }; 205 206 } // namespace base 207 } // namespace android 208