ALooperRoster.cpp revision 75c672fc376ef9b3ceff61a96513242b0e5ebd60
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 "ALooperRoster"
19#include <utils/Log.h>
20
21#include "ALooperRoster.h"
22
23#include "ADebug.h"
24#include "AHandler.h"
25#include "AMessage.h"
26
27namespace android {
28
29ALooperRoster::ALooperRoster()
30    : mNextHandlerID(1),
31      mNextReplyID(1) {
32}
33
34ALooper::handler_id ALooperRoster::registerHandler(
35        const sp<ALooper> looper, const sp<AHandler> &handler) {
36    Mutex::Autolock autoLock(mLock);
37
38    if (handler->id() != 0) {
39        CHECK(!"A handler must only be registered once.");
40        return INVALID_OPERATION;
41    }
42
43    HandlerInfo info;
44    info.mLooper = looper;
45    info.mHandler = handler;
46    ALooper::handler_id handlerID = mNextHandlerID++;
47    mHandlers.add(handlerID, info);
48
49    handler->setID(handlerID);
50
51    return handlerID;
52}
53
54void ALooperRoster::unregisterHandler(ALooper::handler_id handlerID) {
55    Mutex::Autolock autoLock(mLock);
56
57    ssize_t index = mHandlers.indexOfKey(handlerID);
58
59    if (index < 0) {
60        return;
61    }
62
63    const HandlerInfo &info = mHandlers.valueAt(index);
64
65    sp<AHandler> handler = info.mHandler.promote();
66
67    if (handler != NULL) {
68        handler->setID(0);
69    }
70
71    mHandlers.removeItemsAt(index);
72}
73
74void ALooperRoster::unregisterStaleHandlers() {
75
76    Vector<sp<ALooper> > activeLoopers;
77    {
78        Mutex::Autolock autoLock(mLock);
79
80        for (size_t i = mHandlers.size(); i-- > 0;) {
81            const HandlerInfo &info = mHandlers.valueAt(i);
82
83            sp<ALooper> looper = info.mLooper.promote();
84            if (looper == NULL) {
85                ALOGV("Unregistering stale handler %d", mHandlers.keyAt(i));
86                mHandlers.removeItemsAt(i);
87            } else {
88                // At this point 'looper' might be the only sp<> keeping
89                // the object alive. To prevent it from going out of scope
90                // and having ~ALooper call this method again recursively
91                // and then deadlocking because of the Autolock above, add
92                // it to a Vector which will go out of scope after the lock
93                // has been released.
94                activeLoopers.add(looper);
95            }
96        }
97    }
98}
99
100status_t ALooperRoster::postMessage(
101        const sp<AMessage> &msg, int64_t delayUs) {
102    Mutex::Autolock autoLock(mLock);
103    return postMessage_l(msg, delayUs);
104}
105
106status_t ALooperRoster::postMessage_l(
107        const sp<AMessage> &msg, int64_t delayUs) {
108    ssize_t index = mHandlers.indexOfKey(msg->target());
109
110    if (index < 0) {
111        ALOGW("failed to post message '%s'. Target handler not registered.",
112              msg->debugString().c_str());
113        return -ENOENT;
114    }
115
116    const HandlerInfo &info = mHandlers.valueAt(index);
117
118    sp<ALooper> looper = info.mLooper.promote();
119
120    if (looper == NULL) {
121        ALOGW("failed to post message. "
122             "Target handler %d still registered, but object gone.",
123             msg->target());
124
125        mHandlers.removeItemsAt(index);
126        return -ENOENT;
127    }
128
129    looper->post(msg, delayUs);
130
131    return OK;
132}
133
134void ALooperRoster::deliverMessage(const sp<AMessage> &msg) {
135    sp<AHandler> handler;
136
137    {
138        Mutex::Autolock autoLock(mLock);
139
140        ssize_t index = mHandlers.indexOfKey(msg->target());
141
142        if (index < 0) {
143            ALOGW("failed to deliver message. Target handler not registered.");
144            return;
145        }
146
147        const HandlerInfo &info = mHandlers.valueAt(index);
148        handler = info.mHandler.promote();
149
150        if (handler == NULL) {
151            ALOGW("failed to deliver message. "
152                 "Target handler %d registered, but object gone.",
153                 msg->target());
154
155            mHandlers.removeItemsAt(index);
156            return;
157        }
158    }
159
160    handler->onMessageReceived(msg);
161}
162
163sp<ALooper> ALooperRoster::findLooper(ALooper::handler_id handlerID) {
164    Mutex::Autolock autoLock(mLock);
165
166    ssize_t index = mHandlers.indexOfKey(handlerID);
167
168    if (index < 0) {
169        return NULL;
170    }
171
172    sp<ALooper> looper = mHandlers.valueAt(index).mLooper.promote();
173
174    if (looper == NULL) {
175        mHandlers.removeItemsAt(index);
176        return NULL;
177    }
178
179    return looper;
180}
181
182status_t ALooperRoster::postAndAwaitResponse(
183        const sp<AMessage> &msg, sp<AMessage> *response) {
184    Mutex::Autolock autoLock(mLock);
185
186    uint32_t replyID = mNextReplyID++;
187
188    msg->setInt32("replyID", replyID);
189
190    status_t err = postMessage_l(msg, 0 /* delayUs */);
191
192    if (err != OK) {
193        response->clear();
194        return err;
195    }
196
197    ssize_t index;
198    while ((index = mReplies.indexOfKey(replyID)) < 0) {
199        mRepliesCondition.wait(mLock);
200    }
201
202    *response = mReplies.valueAt(index);
203    mReplies.removeItemsAt(index);
204
205    return OK;
206}
207
208void ALooperRoster::postReply(uint32_t replyID, const sp<AMessage> &reply) {
209    Mutex::Autolock autoLock(mLock);
210
211    CHECK(mReplies.indexOfKey(replyID) < 0);
212    mReplies.add(replyID, reply);
213    mRepliesCondition.broadcast();
214}
215
216}  // namespace android
217