ALooper.cpp revision 348a8eab84f4bba76c04ca83b2f5418467aa1a48
1/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "ALooper"
19#include <utils/Log.h>
20
21#include <sys/time.h>
22
23#include "ALooper.h"
24
25#include "AHandler.h"
26#include "ALooperRoster.h"
27#include "AMessage.h"
28
29namespace android {
30
31ALooperRoster gLooperRoster;
32
33struct ALooper::LooperThread : public Thread {
34    LooperThread(ALooper *looper, bool canCallJava)
35        : Thread(canCallJava),
36          mLooper(looper) {
37    }
38
39    virtual bool threadLoop() {
40        return mLooper->loop();
41    }
42
43protected:
44    virtual ~LooperThread() {}
45
46private:
47    ALooper *mLooper;
48
49    DISALLOW_EVIL_CONSTRUCTORS(LooperThread);
50};
51
52// static
53int64_t ALooper::GetNowUs() {
54    struct timeval tv;
55    gettimeofday(&tv, NULL);
56
57    return (int64_t)tv.tv_sec * 1000000ll + tv.tv_usec;
58}
59
60ALooper::ALooper()
61    : mRunningLocally(false) {
62}
63
64ALooper::~ALooper() {
65    stop();
66}
67
68ALooper::handler_id ALooper::registerHandler(const sp<AHandler> &handler) {
69    return gLooperRoster.registerHandler(this, handler);
70}
71
72void ALooper::unregisterHandler(handler_id handlerID) {
73    gLooperRoster.unregisterHandler(handlerID);
74}
75
76status_t ALooper::start(
77        bool runOnCallingThread, bool canCallJava, int32_t priority) {
78    if (runOnCallingThread) {
79        {
80            Mutex::Autolock autoLock(mLock);
81
82            if (mThread != NULL || mRunningLocally) {
83                return INVALID_OPERATION;
84            }
85
86            mRunningLocally = true;
87        }
88
89        do {
90        } while (loop());
91
92        return OK;
93    }
94
95    Mutex::Autolock autoLock(mLock);
96
97    if (mThread != NULL || mRunningLocally) {
98        return INVALID_OPERATION;
99    }
100
101    mThread = new LooperThread(this, canCallJava);
102
103    status_t err = mThread->run("ALooper", priority);
104    if (err != OK) {
105        mThread.clear();
106    }
107
108    return err;
109}
110
111status_t ALooper::stop() {
112    sp<LooperThread> thread;
113    bool runningLocally;
114
115    {
116        Mutex::Autolock autoLock(mLock);
117
118        thread = mThread;
119        runningLocally = mRunningLocally;
120        mThread.clear();
121        mRunningLocally = false;
122    }
123
124    if (thread == NULL && !runningLocally) {
125        return INVALID_OPERATION;
126    }
127
128    if (thread != NULL) {
129        thread->requestExit();
130    }
131
132    mQueueChangedCondition.signal();
133
134    if (!runningLocally) {
135        thread->requestExitAndWait();
136    }
137
138    return OK;
139}
140
141void ALooper::post(const sp<AMessage> &msg, int64_t delayUs) {
142    Mutex::Autolock autoLock(mLock);
143
144    int64_t whenUs;
145    if (delayUs > 0) {
146        whenUs = GetNowUs() + delayUs;
147    } else {
148        whenUs = GetNowUs();
149    }
150
151    List<Event>::iterator it = mEventQueue.begin();
152    while (it != mEventQueue.end() && (*it).mWhenUs <= whenUs) {
153        ++it;
154    }
155
156    Event event;
157    event.mWhenUs = whenUs;
158    event.mMessage = msg;
159
160    if (it == mEventQueue.begin()) {
161        mQueueChangedCondition.signal();
162    }
163
164    mEventQueue.insert(it, event);
165}
166
167bool ALooper::loop() {
168    Event event;
169
170    {
171        Mutex::Autolock autoLock(mLock);
172        if (mThread == NULL && !mRunningLocally) {
173            return false;
174        }
175        if (mEventQueue.empty()) {
176            mQueueChangedCondition.wait(mLock);
177            return true;
178        }
179        int64_t whenUs = (*mEventQueue.begin()).mWhenUs;
180        int64_t nowUs = GetNowUs();
181
182        if (whenUs > nowUs) {
183            int64_t delayUs = whenUs - nowUs;
184            mQueueChangedCondition.waitRelative(mLock, delayUs * 1000ll);
185
186            return true;
187        }
188
189        event = *mEventQueue.begin();
190        mEventQueue.erase(mEventQueue.begin());
191    }
192
193    gLooperRoster.deliverMessage(event.mMessage);
194
195    return true;
196}
197
198}  // namespace android
199