-
Notifications
You must be signed in to change notification settings - Fork 1
/
ALooper.cpp
290 lines (234 loc) · 7.41 KB
/
ALooper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "ALooper.h"
#include "ALooperRoster.h"
#include "AMessage.h"
#include "Logger.h"
#include "Thread.h"
namespace android {
ALooperRoster gLooperRoster;
struct ALooper::LooperThread : public Thread {
LooperThread(ALooper* looper, bool canCallJava)
: Thread(canCallJava),
mLooper(looper),
mThreadId(getThreadId()) {
}
virtual status_t readyToRun() {
mThreadId = getThreadId();
return Thread::readyToRun();
}
virtual bool threadLoop() {
bool ret= mLooper->loop();
return ret;
}
bool isCurrentThread() const {
return mThreadId == getThreadId();
}
virtual ~LooperThread() {}
private:
ALooper* mLooper;
thread_id_t mThreadId;
DISALLOW_EVIL_CONSTRUCTORS(LooperThread);
};
// static
int64_t ALooper::GetNowUs() {
auto now = std::chrono::high_resolution_clock::now();
auto duration = now.time_since_epoch();
return std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
}
ALooper::ALooper()
: mRunningLocally(false) {
LOGV("constructed {}", fmt::ptr(this));
// clean up stale AHandlers. Doing it here instead of in the destructor avoids
// the side effect of objects being deleted from the unregister function recursively.
gLooperRoster.unregisterStaleHandlers();
}
ALooper::~ALooper() {
LOGV("destructed {}", fmt::ptr(this));
stop();
if (mEventQueue.size() > 0) {
LOGV("destructed with droped {} message", mEventQueue.size());
}
// stale AHandlers are now cleaned up in the constructor of the next ALooper to come along
}
void ALooper::setName(const char* name) {
mName = name;
}
ALooper::handler_id ALooper::registerHandler(const std::shared_ptr<AHandler>& handler) {
return gLooperRoster.registerHandler(shared_from_this(), handler);
}
void ALooper::unregisterHandler(handler_id handlerID) {
gLooperRoster.unregisterHandler(handlerID);
}
status_t ALooper::start(
bool runOnCallingThread, bool canCallJava, int32_t priority) {
if (runOnCallingThread) {
{
MutexAutoLock autoLock(mLock);
if (mThread != NULL || mRunningLocally) {
return INVALID_OPERATION;
}
mRunningLocally = true;
}
do {
} while (loop());
return OK;
}
MutexAutoLock autoLock(mLock);
if (mThread != NULL || mRunningLocally) {
return INVALID_OPERATION;
}
mThread = std::make_shared<LooperThread>(this, canCallJava);
status_t err = mThread->run(
mName.empty() ? "ALooper" : mName.c_str(), priority);
if (err != OK) {
mThread.reset();
}
return err;
}
status_t ALooper::stop() {
std::shared_ptr<LooperThread> thread;
bool runningLocally;
{
MutexAutoLock autoLock(mLock);
thread = mThread;
runningLocally = mRunningLocally;
mThread.reset();
mRunningLocally = false;
}
if (thread == NULL && !runningLocally) {
return INVALID_OPERATION;
}
if (thread != NULL) {
thread->requestExit();
}
mQueueChangedCondition.notify_one();
{
MutexAutoLock autoLock(mRepliesLock);
mRepliesCondition.notify_all();
}
if (!runningLocally && !thread->isCurrentThread()) {
// If not running locally and this thread _is_ the looper thread,
// the loop() function will return and never be called again.
thread->requestExitAndWait();
}
return OK;
}
void ALooper::post(const std::shared_ptr<AMessage>& msg, int64_t delayUs) {
MutexAutoLock autoLock(mLock);
int64_t whenUs;
if (delayUs > 0) {
int64_t nowUs = GetNowUs();
whenUs = (delayUs > INT64_MAX - nowUs ? INT64_MAX : nowUs + delayUs);
}
else {
whenUs = GetNowUs();
}
std::vector<Event>::iterator it = mEventQueue.begin();
size_t index = 0;
while (it != mEventQueue.end() && (*it).mWhenUs <= whenUs) {
++it;
index++;
}
Event event;
event.mWhenUs = whenUs;
event.mMessage = msg;
mEventQueue.insert(it, event);
if (index == 0) {
mQueueChangedCondition.notify_one();
}
}
void ALooper::remove(const std::shared_ptr<AHandler>& handler)
{
if (handler == nullptr)
{
return;
}
MutexAutoLock autoLock(mLock);
auto iter = mEventQueue.begin();
while (iter != mEventQueue.end()) {
if (iter->mMessage->mHandler.lock() == handler) {
iter = mEventQueue.erase(iter);
} else {
iter++;
}
}
}
void ALooper::remove(const std::shared_ptr<AHandler>& handler, int what)
{
if (handler == nullptr)
{
return;
}
MutexAutoLock autoLock(mLock);
auto iter = mEventQueue.begin();
while (iter != mEventQueue.end()) {
if (iter->mMessage->mHandler.lock() == handler && iter->mMessage->what() == what) {
iter = mEventQueue.erase(iter);
}
else {
iter++;
}
}
}
bool ALooper::loop() {
Event event;
{
MutexAutoLock autoLock(mLock);
if (mThread == NULL && !mRunningLocally) {
return false;
}
if (mEventQueue.empty()) {
mQueueChangedCondition.wait(autoLock);
return true;
}
int64_t whenUs = (*mEventQueue.begin()).mWhenUs;
int64_t nowUs = GetNowUs();
if (whenUs > nowUs) {
int64_t delayUs = whenUs - nowUs;
if (delayUs > INT64_MAX / 1000) {
delayUs = INT64_MAX / 1000;
}
mQueueChangedCondition.wait_for(autoLock, std::chrono::nanoseconds(delayUs * 1000));
return true;
}
event = *mEventQueue.begin();
mEventQueue.erase(mEventQueue.begin());
}
event.mMessage->deliver();
// NOTE: It's important to note that at this point our "ALooper" object
// may no longer exist (its final reference may have gone away while
// delivering the message). We have made sure, however, that loop()
// won't be called again.
return true;
}
// to be called by AMessage::postAndAwaitResponse only
std::shared_ptr<AReplyToken> ALooper::createReplyToken() {
return std::make_shared<AReplyToken>(shared_from_this());
}
// to be called by AMessage::postAndAwaitResponse only
status_t ALooper::awaitResponse(const std::shared_ptr<AReplyToken>& replyToken, std::shared_ptr<AMessage>* response) {
// return status in case we want to handle an interrupted wait
MutexAutoLock autoLock(mRepliesLock);
if (replyToken == nullptr) {
LOGE("null replyToken");
abort();
}
while (!replyToken->retrieveReply(response)) {
{
MutexAutoLock _l(mLock);
if (mThread == NULL) {
return -ENOENT;
}
}
mRepliesCondition.wait(autoLock);
}
return OK;
}
status_t ALooper::postReply(const std::shared_ptr<AReplyToken>& replyToken, const std::shared_ptr<AMessage>& reply) {
MutexAutoLock autoLock(mRepliesLock);
status_t err = replyToken->setReply(reply);
if (err == OK) {
mRepliesCondition.notify_all();
}
return err;
}
} // namespace android